Namespace issues - Python

Namespace issues - Python

Kevin.Bell
Advisor Advisor
2,726 Views
2 Replies
Message 1 of 3

Namespace issues - Python

Kevin.Bell
Advisor
Advisor

Hi,

 

I'm using Dynamo to write a Python script to move some objects around.

 

I'm trying to get a user selection of objects using:

 

obstorotate = Autodesk.Revit.UI.Selection.PickElementsByRectangle()

But I get the error:

 

Attribute Error: attribute 'PickElementsByRectangle' of 'namespace#' object is read-only.

 

I believe this is because I'm not importing the class, but still get the error when I add this at the beginning:

 

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

I'd appriciate some pointers on what I'm missing.

 

Thanks.

0 Likes
Accepted solutions (1)
2,727 Views
2 Replies
Replies (2)
Message 2 of 3

sobon.konrad
Advocate
Advocate
Accepted solution

Kevin,

 

You are getting this error because you are not calling this method on an Active UI Document. Selection is a method that can be only called on the UI document. Try this instead: 

 

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

# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

# Import Element wrapper extension methods
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)

if IN[0]:
	OUT = uidoc.Selection.PickElementsByRectangle()
else:
	OUT = None

As you can see I first reference the Revit UI library. I also reference in Dynamo's Revit Services library that wraps some of the Document access methods for us. I then define `uidoc` so I can call selection from it. Give that a try. I also put the OUT variable inside an IF statement to allow for re-selection if needed: 

 

Capture.PNG

 

Message 3 of 3

Kevin.Bell
Advisor
Advisor

Perfect, thanks for your help Konrad.

0 Likes