Select objects by python code

Is it possible to retrieve selected objects from screen by python code? if yes, may i know how

Look into the Tool.GetRevitSelection node in Steamnodes package.

You can also try writing code on your own. This is where you should start:
http://www.revitapidocs.com/2018.1/4af3bdc7-e69c-8aa6-406d-865d7f036927.htm

i wrote the code as below. its not working perfectly. don’t know where is the mistake.

import clr
clr.AddReference(‘RevitAPI’)
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Mechanical import *
#from Autodesk.Revit.UI.Selection import *
clr.AddReference(‘RevitAPIUI’)
from Autodesk.Revit.UI import *

clr.AddReference(‘System’)
from System.Collections.Generic import List

clr.AddReference(‘RevitNodes’)
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

clr.AddReference(‘RevitServices’)
import RevitServices
from RevitServices.Persistence import *
#from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import *
#from RevitServices.Transactions import TransactionManager
#The inputs to this node will be stored as a list in the IN variables.

doc1 = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc = uiapp.ActiveUIDocument
doc = uidoc.Document
sel = uidoc.Selection
selid = sel.GetElementIds()
OUT = selid

Try this:

import clr

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

OUT = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument.Selection.GetElementIds()
2 Likes

this is same as the which i posted. both are working at one time. second time it is not working !

It’s perfectly normal. You’re not changing the inputs/wire/etc., so fore Dynamo perspective, there is no need to reevaluate this script.

Try connecting a “pseudo-input” to the script and change its value everytime you want to re-run the script (e.g. a true/false button, that you’d switch every time).

Add second input Refresh bool true/false.

now its working.

that means we can’t use Dynamo as a plugin, right?

Glad it worked!

We can use. It depends how you build your script.

Please mark the post as solved.

It means that scripts that use selection are better run from Dynamo Player rather than from opened Dynamo.