Aligning multiple instances to different walls in linked model(by ray bounce)

Hi i am working on a dynamo script with OOTB nodes. My goal is to select multiple instances in active model and align each of them to their closest walls. The closest wall is in linked model.

My step is:

  1. Picking input instances in active model, and “wall” in linked model
  2. Finding the 4 nearest walls (by linked element in ray bounce, bimorph node) with direction x,y,-x,-y
  3. Looking for the wall with shortest distance from my input instances
  4. Getting closest wall surface and its isoline
  5. Getting Rotation of isoline
  6. Finding the closest point from the original instance and the reference face, setting the new location, adjusting z coordinate

So far the script works well with 1 instance, however when i want to multiple instances, it doesn’t work and the instance all align to the same wall.

Any idea how to fix ?


Aligning Selected items to Wall - WIP r.5.dyn (115.5 KB)

With 1 instance:

With >1 instance:

I got to the solution!!
so i will share my version here in case who may concern.
Aligning Selected items to Wall - 6.dyn (131.1 KB)

(Dynamo Revit version 2.0.4+Bimorph package)