Skip to content

Instantly share code, notes, and snippets.

@eirannejad
Last active January 12, 2024 19:21
Show Gist options
  • Save eirannejad/4e275166bf2015f4f844bb1d087ca4d6 to your computer and use it in GitHub Desktop.
Save eirannejad/4e275166bf2015f4f844bb1d087ca4d6 to your computer and use it in GitHub Desktop.
RevitPythonShell init
# these commands get executed in the current scope
# of each new shell (but not for canned commands)
#pylint: disable=all
import clr
clr.AddReferenceByPartialName('PresentationCore')
clr.AddReferenceByPartialName('AdWindows')
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName('System')
clr.AddReferenceByPartialName('System.Windows.Forms')
from Autodesk.Revit import DB
from Autodesk.Revit import UI
import Autodesk.Windows as aw
# creates variables for selected elements in global scope
# e1, e2, ...
max_elements = 5
gdict = globals()
uiapp = __revit__
uidoc = uiapp.ActiveUIDocument
if uidoc:
doc = uiapp.ActiveUIDocument.Document
selection = [doc.GetElement(x) for x in uidoc.Selection.GetElementIds()]
for idx, el in enumerate(selection):
if idx < max_elements:
gdict['e{}'.format(idx+1)] = el
else:
break
# alert function
def alert(msg):
TaskDialog.Show('RPS', msg)
# quit function
def quit():
__window__.Close()
@samuelpancu94
Copy link

Hi! I am also following your amazing video: https://www.youtube.com/watch?v=PfCn7OrY_-A&list=PLc_1PNcpnV5742XyF8z7xyL9OF8XJNYnv&index=3

But I got stuck at the first line: 'cl = FilteredElementCollector()'
As I keep getting the message 'Exception : IronPython.Runtime.UnboundNameException: name 'FilteredElementCollector' is not defined'

It works though, if I switch the startup script to init.py and simply add 'doc = revit.ActiveUIDocument.Document' as the first line of the code. It seems like there is something wrong with the code posted here. I would be glad if you could help

I had the same issue and it worked for me when I selected the elements in Revit and then opened the RevitPythonShell.

@yafimski
Copy link

@DanailMomchilov
I also solved the 'Exception : IronPython.Runtime.UnboundNameException: name 'FilteredElementCollector' is not defined' error by changing the import lines to:

from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *

@shivajreddy
Copy link

add the following lines starting from line11

from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Architecture import *
from Autodesk.Revit.DB.Analysis import *

@eirannejad
Copy link
Author

Don't do this

from Autodesk.Revit.DB import *

It's bad practice. Always use the namespace to point to the type you want e.g. DB.FilteredElementCollector

@MohamedNassar1
Copy link

i faced an issue, can you help me in this.

Snipaste_2023-02-22_21-03-15

@shivajreddy
Copy link

import clr
import sys
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
import System
from System import Array
from System.Collections.Generic import *
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager 
from RevitServices.Transactions import TransactionManager 

clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")

import Autodesk 
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *

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

#######OK NOW YOU CAN CODE########

Paste this on top of your script
Your code is using a variable called 'doc' but it was not defined before.
Go through this site https://dynamopythonprimer.gitbook.io/dynamo-python-primer/getting-started/boilerplate-setup-code
it will go over some basics on getting started

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment