Rotating (free angle) cropviews with Python

Hi there !

I never used python before, but it seems that my problem has no other solution so… I begin with this program. End, of course, it doesn’t works.

import clr
from Autodesk.DesignScript.Geometry import *

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

import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

cropBox = UnwrapElement (IN[0])
angle =UnwrapElement ( IN [1])
center = UnwrapElement (IN[2])

axiss =[]

id = UnwrapElement (IN[4])

doc = DocumentManager.Instance.CurrentDBDocument
	for j in angle:
		for k in center:
			axiss.append(Line.CreateBound(XYZ(k.X, k.Y, k.Z), XYZ(0, 0, 1)))
				for i in axiss:
					for l in id:
						ElementTransformUtils.RotateElement(doc, l, i, j)

#Affectez la sortie à la variable OUT.
OUT = 0

Here is the message
Do you have any idea what is wrong?

Thanks !

Hi @mickael.g,

Given the error, you must have indented a line that shouldn’t have been indented. Python doesn’t understand code as C++/C#/DesignScript/Java do. A line should be indented only if it is in a if statement or in a loop.

Please give us a screenshot of your Python code indented so we can see where is the mistake.

the message tells you, that you have a problem with your python code (an unexpected indent).
as you posted your code as text without format (instead of using the code option or a picture) no help is possible

ok, sorry. Here it is :

You indeed have extra indents :

  • lines 28,29 and 30 : 1 extra
  • lines 31,32 and 33 : 2 extras

Your code should look like this if i’m not mistaken. Hope it helps. Don’t forget to affect an output to OUT, otherwise your script will always return 0.

For more documentation on Python and its identation system, take a look at this : (in English)

Or this : (in French)

Move the code beginning with the first for loop 1 tab to the left

Thanks, well, that works but… I had another error and well, I don’t know what I can do… since I already made a loop… :frowning:


One of the input you are giving to RotateElement is not of the correct type. Sadly, I do not know how RotateElement work, so I can’t help you more than that. Try to check every variable you are feeding the function : maybe one of your variables is a List instead of being of another type (ElementId).

For the rotateElement, you give 4 argument : the document, the id of the element to rotate, the axis and the angle. It work if I have no list, so I decided to make the loops to get, for each argument, one element of the list. My inputs are list and I do a loop for each element of these lists…

Well, I had in the last input, a list of list, so maybe it is the probleme, but I have the feeling that it makes an infinite loop cause… It is running for half an hour now, for only 14 elements in each loop…

First, make sure that all of the inputs are correct, before even running the script. Please show us a screen of the inputs (angle, center and id).

Second, I don’t think it is what you intended, but if angle, center and id are lists of 14 elements, you are actually calling RotateElement about 250.000 times. If you just want to rotate 14 elements, just get the element you need in a single for loop, and then in that same loop, just get its id, its angle and its axis.

You could try something of that style (beware, i don’t know what objects you are actually manipulating, so you should double check this).

doc = DocumentManager.Instance.CurrentDBDocument
for number in range(len(angle)):
    j = angle[number]
    k = center[number]
    axiss = Line.CreateBound(XYZ(k.X, k.Y, k.Z), XYZ (0,0,1))
    l = id[number]
    ElementTransformUtils.RotateElement(doc, l, axiss, j)

here is a pic of the input (hope it helps)

in this picture, I tried your code and you can see the message. I don’t know why again… :confused:

Sorry, i got myself a bit confused. The code in the previous post should be correct by know. In case it stille does not work, here is a .txt file containing the script.

script.txt (850 Bytes)

Thanks a lot, that works.

Now, well, I have another probleme… The cropbox is rotating, BUT note in the right way. Here is the problem, it rotate with the good angle but it seems to translate too… Just as if the origin of the rotation is false. So I tried to test it and well, the center is the good one… Any ideas about that?