Help with Python Script

Hi All,

I’ve used chatgpt to write a python script that creates a new elevation for every vertical surface of a room. I get various error messages when I try to run it. Does anybody have any idea why this might happen?

The script is below

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

Get the current Revit document

doc = DocumentManager.Instance.CurrentDBDocument

Get all walls in the document

walls = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType().ToElements()

Get all rooms in the document

rooms = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Rooms).WhereElementIsNotElementType().ToElements()

Loop through each room

for room in rooms:
# Get the closed shell of the room
shell = SpatialElementGeometryCalculator(doc).CalculateSpatialElementGeometry(room).GetGeometry()

# Loop through each face of the closed shell
for face in shell.Faces:
    normal = face.ComputeNormal(UV.Zero)
    if normal.Z == 0:
        # This face is not vertical, skip it
        continue
    
    # Get the location point of the face
    point = face.Origin
    
    # Get the name of the wall that corresponds to this face
    wall = None
    for w in walls:
        if w.Location.PointOnFace().IsAlmostEqualTo(point):
            wall = w
            break
    if not wall:
        # This face is not a wall, skip it
        continue
    
    # Get the mark parameter value of the wall
    mark = wall.get_Parameter(BuiltInParameter.ALL_MODEL_MARK).AsString()
    
    # Create a new elevation view
    view = ViewSection.CreateElevation(doc, wall.GetTypeId(), wall.Id, point)
    
    # Set the view's orientation to face the wall
    view.SetOrientation(new XYZ(-normal.Y, normal.X, 0), new XYZ(normal.X, normal.Y, normal.Z))
    
    # Set the view's name to the wall's mark value
    view.Name = mark

Don’t even bother with ChatGPT for things that need the Revit API. You’re just going to end up beating your head against a wall unless you already know enough about Python/Dynamo/Revit API to identify the issues and fix them (at which point you probably could have just written it from scratch anyway).

Dynamo is a great entry into programming without needing to know how to code. You can do a heap of powerful stuff without ever needing to jump into Python. ChatGPT is a long way from ‘do it for me’ with regards to anything related to Dynamo or Revit. Use it for some more common Python tasks that could be integrated into a node based graph for now, and maybe one day ChatGPT will actually work okay for more specific things.

2 Likes

To reiterate what @Hamish stated, you can have a read into my exploration here if you’d like:

The TLDR: I spent 30 minutes attempting to get an answer, before I tried one Google search to get working code.

1 Like

I wouldn’t get the shell of the room. The lines of the boundary segments will work a little better. You just need a level and a direction for placing the elevation. Of course, if it a rectangular’ish room, you can just place the elevation at the centroid of the room and then you only need to pull the bounding box of the room for the extents. Really don’t need the geometry of the walls to make elevations. Unless you’re trying to do some 2d non-modeled drafting view of the wall. Which of course would be silly.

The code you have isn’t even close to a solution for your stated goal. (And there are numerous other plugins that already do this very well.)

ChatGPT isn’t going to write your code. So, learn to code or stick to Dynamo nodes.

I can’t like this enough.

1 Like