Last active
November 5, 2025 11:46
-
-
Save eirannejad/4e275166bf2015f4f844bb1d087ca4d6 to your computer and use it in GitHub Desktop.
RevitPythonShell init
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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() |
add the following lines starting from line11
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Architecture import *
from Autodesk.Revit.DB.Analysis import *
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
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
i ask the chatgpt, and the i pastet the code"# -- coding: utf-8 --
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *
doc = revit.ActiveUIDocument.Document
uidoc = revit.ActiveUIDocument" on the top, and i solve the problem
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

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