Python error "Expected Line, got line"

Hello guys,

I am trying to rotate an element with Python. I get in the end a weird error. It says “expected line, got line” why is it like that?

Thnaks!

import clr

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

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

clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *
doc= DocumentManager.Instance.CurrentDBDocument

clr.AddReference("System")
from System.Collections.Generic import *
clr.AddReference("ProtoGeometry")
from Autodesk.DesignScript.Geometry import *
elements = UnwrapElement(IN[0])
angle= IN[1]
line_axis=IN[2]

rot_axis=line_axis
	
TransactionManager.Instance.EnsureInTransaction(doc)
ElementTransformUtils.RotateElement(doc, elements.Id, rot_axis, angle)
TransactionManager.Instance.TransactionDone()

Try unwrapping your input IN[2]. You are using a DesignScript object with a Revit API method.

Edit: This actually doesn’t work as the line being used does not exist in Revit.

Edit 2: Here is a modified/simplified version of your script that should work:

import clr
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import XYZ, Line, ElementTransformUtils

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

doc = DocumentManager.Instance.CurrentDBDocument
element = UnwrapElement(IN[0])
angle = IN[1]
p0 = IN[2]
p1 = IN[3]

p0xyz = XYZ(p0.X, p0.Y, p0.Z)
p1xyz = XYZ(p1.X, p1.Y, p1.Z)

rot_axis = Line.CreateBound(p0xyz, p1xyz)
	
TransactionManager.Instance.EnsureInTransaction(doc)
ElementTransformUtils.RotateElement(doc, element.Id, rot_axis, angle)
TransactionManager.Instance.TransactionTaskDone()
1 Like

For geometry, you have to convert from Dynamo geometry to Revit geometry in order to use it with API commands. One method is what @cgartland showed where you manually recreate the geometry using RevitAPI methods. Another is to use Dynamo’s geometry conversion library. Check out this page for more info on wrapping/unwrapping (that @cgartland mentioned) and geometry conversion (under GeometryObjects): https://github.com/DynamoDS/Dynamo/wiki/Python-0.6.3-to-0.7.x-Migration

First, you have to import the geometry conversion library using these lines at the top of the Python script (you already have import clr and the Revit nodes, so no need to duplicate):

import clr

clr.AddReference("RevitNodes")
import Revit

# Import ToProtoType, ToRevitType geometry conversion extension methods
clr.ImportExtensions(Revit.GeometryConversion)

After that, use the command .ToRevitType() to convert to a Revit geometry, like so:

rot_axis = line_axis.ToRevitType()

and it should work.

3 Likes

Ah, .ToRevitType() what I was thinking of instead of UnwrapElement.

1 Like

Thank you guys for informative replies. It worked but it rotated around X or Y axis. I would like to rotate that crop box in order to get the correct area. I have many sheets, which I must edit.
With using RevitAPI I can rotate it in Sheet View. I mean 2D Rotate. With that script it rotates 3D.
How can we deal with that?

If your axis is vertical, it should rotate it only within the XY plane.

1 Like

Judging from the two points you used in the first picture to make the line, it looks like you are using an X-axis as the axis of rotation, meaning you are rotating it in the YZ plane. Try using a vertical axis like a Z-axis as the rotation.

1 Like

That is right Kenny! The line is horizontal…
I changed the script but I got an error
Warnung:IronPythonEvaluator.EvaluateIronPythonScript fehlgeschlagen.
Traceback (most recent call last):
File “”, line 16, in
AttributeError: ‘Line’ object has no attribute ‘ToRevitType’

import clr
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import XYZ, Line, ElementTransformUtils

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

doc = DocumentManager.Instance.CurrentDBDocument
element = UnwrapElement(IN[0])
angle = IN[1]
line_axis = IN[2]


rot_axis = line_axis.ToRevitType()
	
TransactionManager.Instance.EnsureInTransaction(doc)
ElementTransformUtils.RotateElement(doc, element.Id, rot_axis, angle)
TransactionManager.Instance.TransactionTaskDone()

Can you show the Dynamo graph? Where is the line coming from?

Edit: Oh, I think it is because you are missing the imports for the conversion tools. See the post above for what you need to put in.

1 Like

In addition to @kennyb6’s last message which should solve the ToRevitType() error, don’t forget that in computing (and maths generally) angles are given and measured in radians. If you are inputting your angle as degrees then you will also need to convert to radians by importing the math module and using math.radians(angle).

4 Likes

I think that is the problem. I will test it and let you know. Thank you for your help :slight_smile:

Oh, thank you very much for that detail. I was wondering about that! Great!

1 Like