Get updated/actual element after change of parameter within a loop

Hello everyone,
I try to create a workflow for an automated solving of clashes by moving elements or parts of it.
In my example I have a mobile crane and i can move the “arm” up and down by changing the angle.

With python I created a “While” loop until a boolean variable, indicating whether a clash exists or not, is “False”.
In this loop I change the angle by +1° and set the new value with the Transaction methods. Then, I check again, if a clash occurs.

The angle changes every time, but even if the crane’s arm is higher than the wall, unfortunately, the boolean variable does not change.
My theory is that with x1 = doc.GetElement(ElementId(1248947)).ToDSType(True) the code select every time the original element and ignore the “new” one with the changed angle.

Can anyone tell me how to tell the script/method, that the selected element has changed parameters and has to be recalculated with different circumstances?

This is the result when I just check if a clash exist

Here, the clash does not exist anymore, but the boolean variable “ClashExists” is still true…

import sys
sys.path.append(....\Dynamo Revit\2.3\packages\bimorphNodes\bin')

import clr

clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference('BimorphNodes')
from Revit import Element as Bimorph

clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB import Element as RevitElement

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('System')
from System.Collections.Generic import *

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

#Creating Lists with specific Type for usage BimorphNode
elementsListA = List[Revit.Elements.Element]()
elementsListB = List[Revit.Elements.Element]()
x1 = doc.GetElement(ElementId(1248947)).ToDSType(True)
y2 = doc.GetElement(ElementId(1254250)).ToDSType(True)
elementsListA.Add(x1)
elementsListB.Add(y2)
ListClashExist = []
counter = 1

#Check the clash
stillClash = Bimorph.IntersectsElement(elementsListA,elementsListB)
inter_Element = stillClash['Element[][]'][0][0]
if inter_Element == []:
	ClashExists = False
else: 
	ClashExists = True

#Remove the aboce selected element, having an empty list for the while loop
elementsListA.Remove(x1)

for i in UnwrapElement(x1).Parameters:
	if i.Definition.Name == 'Auslegerwinkel':
		actualValue_Rad = i.AsDouble()

p = UnwrapElement(x1).LookupParameter('Auslegerwinkel')

while (ClashExists == True):
	actualValue_Rad = actualValue_Rad + 3.14159265/180
	
	#Trasnsaction for Revit
	TransactionManager.Instance.EnsureInTransaction(doc)
	p.Set(actualValue_Rad)
	TransactionManager.Instance.TransactionTaskDone()
	#Select updated element with changed parameter value
	x1new = doc.GetElement(ElementId(1248947)).ToDSType(True)
	
	#add the updated element
	elementsListA.Add(x1new)
	
	#Check clash again, but with new element
	stillClash = Bimorph.IntersectsElement(elementsListA,elementsListB)
	inter_Element = stillClash['Element[][]'][0][0]
	if inter_Element == []:
		ClashExists = False
	
	#Track the solution of the Intersect method. It should be the wall until the arm is high enough
	ListClashExist.append(inter_Element)
	
	# clean the list for the next itaration
	elementsListA.Remove(x1new)
	
	#avoid an infinite loop
	counter = counter + 1
	if counter > 20: break

OUT = ClashExists, stillClash, ListClashExist

I think you need to move the get elements into your while function otherwise you are using the original element information each run. If you get the element each time a new loop is run then it should grab the new information.

Hi Steven, thank you for your response.
I changed the order and put mostly everything in the while loop, but it does not solve the problem…
Now it does not even change the parameter everytime (just once), although it seems to go everytime through the loop (see the Watch Node with results from the tracking variables)

EDIT:
I found here ( The Building Coder: About the Author (typepad.com)) a topic about “regenerating” a modell, so I add doc.regenerate() in the while loop and it seems to work.
But there is still the problem that the parameter “ClashExists” never changes to False. So the criteria in lines 59-62 is not working as expected.

With inter_Element = stillClash['Element[][]'][0][0] (line 59) I get an error telling me, that the index is out of range (Hope it is the right translation).

The Output of “IntersectsElements” is a Dictionary with lists (see BimophNodes) so I have now to find a criteria that is looking for an empty Dictionary…

I may be wrong here, but I believe the python transaction still occurs within the Dynamo transaction. So even though you’re commiting the change in python, nothing is updating within Revit until Dynamo completes its transaction.

Your break conditional also only allows for two iterations. Is it possible that you’re not missing the clash within those first two iterations?

Ahh, okay I think I know what your mean… That may be the reason, why the crane’s arm changes his position directly to the last value, but for me this is enough. I do not need to see every change directly in the model.
For me the important thing is that the python script should just stop in the right moment, when stillClash['Element[][]'] is empty. Than the last found value would be transmitted to the model.

I changed the value of the break condition just to have all results visible in the watch node, so that everybody can see the whole ouput. And I know, that at value 31 there is no clash anymore, so depending on my original value from the model I change the amount of iterations to a minimum.

But now I recognize that the image above is not the best one, i post a better one here:

From iteration 3 to 4, there is no clash, that is why there is an empty list in the first list of the Output…