How to write a code in CPython?

Hello,

si i created a collector ind dynamo 2.17. Revit 2024…

I am supprised that IronPython2 runs ! and also IronPython3! Whats the difference ?

How can i translate this code to CPython ? is there any documantion in the ned or 2024 packages to learn (f.e. on Github)

accessing Revit API seems to be not working :frowning:

import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure 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 DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

collector = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Walls)
all_walls = collector.WhereElementIsNotElementType().ToElements()

walls = [i for i in all_walls if i.Name.Contains("STB")]

OUT = walls

https://ironpython.net/documentation/

KR

Andreas

Hello, while trying to solve my problem of displaying the python code (I don’t see it appearing anymore),
I saw that there was an update to 2024.1 dated July 11th
for information

Cordially
christian.stan

1 Like

@christian.stan
grafik


thats may versions now

:wink: i do it now

KR

Andreas

I have to stay in 2023 for the moment (as I was doing a udapte of 2023, and I saw that your code did not work) to avoid the cafarnaum between our different establishments

Cordially
christian.stan

Using 2024.0 the code ran fine for me when it errored out on with 'str' object has no attribute 'contains', as base python doesn’t have a contains method for strings, so you need to use if "STB" in i.Name instead for the filter of the list comprehension.

@christian.stan @jacob.small ,

i installed Revit 2024.1 and Dynamo 2.18.


i have to correct it runs
i changed this
grafik
collector works!
grafik

KR

Andreas

1 Like

@jacob.small

i still have an error

walls = [i for i in all_walls if "STB" in i.Name]

is there still a syntex error

What error?

@jacob.small

i tried also this

so the error says no “Name” for “wall”

grafik

Looks like an issue with your imports… I can’t reproduce though.

Confirm you’re working with a wall by taking one wall out of the list and getting the class using something like OUT = all_walls[0].__class__.

@jacob.small ,

that was just a test with the single one…

thats my code, i have 9 walls and 3 of them contain name “STB”

import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

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

clr.AddReference('System')
from System.Collections.Generic import *

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

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

# Access the Revit application and document
app = DocumentManager.Instance.CurrentUIApplication
doc = DocumentManager.Instance.CurrentDBDocument

collector = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Walls)
all_walls = collector.WhereElementIsNotElementType().ToElements()

walls = [i for i in all_walls if "STB" in i.Name]


OUT = walls

And this errors out as before?

If so, what happens if y ou take one wall out of the list and get the class (as noted before) and directory using dir(all_walls[0])?

@jacob.small

grafik

OUT = all_walls

works perfectly

grafik

KR

Andreas

try this

walls = [i for i in all_walls if i.Name.count('STB')>0]

cordially
christian.stan

1 Like

@christian.stan

still the same

grafik

which library template do you use ?

hmmm :frowning:

KR

Andreas

same librairies (but version 2023)

import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure 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 DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

collector = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Walls)
all_walls = collector.WhereElementIsNotElementType().ToElements()

walls = [i for i in all_walls if i.Name.count('Relevé')>0]

OUT = walls,all_walls[0].__class__
1 Like

Going to start a zoom in ~2 minutes to do this live and record it so we can see the full end to end process.

1 Like

Recording is here for anyone who’s interested:
https://autodesk.zoom.us/rec/share/009BUV3-6ACnGyuXM2lyCw7Vh_e4v8gMfJALw8LEGaM7NljpN-v89NWvpo7Vrw_s.RocAzZcugA3IiCu-
Passcode: Fgh4e$@d

Should be good for 30 days or so.

Sadly it didn’t help @Draxl_Andreas as there appears to be a deeper Python blocker impacting even imports within the CPython environment.

1 Like

Well, you need to update your code with below updated code.

import clr
import sys

# Add the path to RevitAPI.dll and RevitAPIUI.dll if they are not in the system path
sys.path.append(r'C:\Program Files\Autodesk\Revit XXXX\AddIns\RevitAPI.dll')
sys.path.append(r'C:\Program Files\Autodesk\Revit XXXX\AddIns\RevitAPIUI.dll')

clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')

from Autodesk.Revit.DB import BuiltInCategory, FilteredElementCollector
from Autodesk.Revit.DB import Wall, Element

# ... import other necessary namespaces

doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument

collector = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Walls)
all_walls = collector.WhereElementIsNotElementType().ToElements()

walls = [i for i in all_walls if i.Name.Contains("STB")]

OUT = walls

Thanks

1 Like

@negigulshan903 ,

thanks for sharing, but it is not working… “something” is blocking REVITAPI access, we will find out, what the root cause is…

KR

Andreas