Create section with Revit API, Python

Create section with Revit API, Python

PieterL_TM
Advocate Advocate
1,287 Views
4 Replies
Message 1 of 5

Create section with Revit API, Python

PieterL_TM
Advocate
Advocate

Hi everyone

 

I'm trying to create a section with python. The creating of the section is easy. But does anyone have a simple way of changing the oriëntation. By default the section looks down, I would like it to look forward, like a normal section (say e.g. XYZ(0,1,0). 

Does anyone know how to do this or where I can find a guide to this. I sadly only know python and no C#.

 

Greetings,
Pieter

0 Likes
1,288 Views
4 Replies
Replies (4)
Message 2 of 5

jeremy_tammik
Alumni
Alumni

I love Python. C# is also very nice. Most of my Revit API samples are in C#.

  

The Building Coder includes a whole topic group on articles on how to set up section views:

  

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 5

PieterL_TM
Advocate
Advocate

Hi @jeremy_tammik

 

First of all thanks for all the sample codes! I came up with the idea to use chatGPT to 'translate' your code to Python and then I changed it to fit my needs. My code now creates a section that looks at a window from the outside. 
I am by no means a good coder, but this code gets the job done 😅

 

def raamstaat_sections(window):
    # find window dimensions, center and host(wall)
    window_bb = window.get_BoundingBox(doc.ActiveView)
    window_center = (window_bb.Min + window_bb.Max)/2
    window_width = window.LookupParameter("Width").AsDouble()
    window_height = window.LookupParameter("Height").AsDouble()
    window_sill_height = window.LookupParameter("Sill Height").AsDouble()
    host = window.Host
    
    # vectors for view direction
    wall_ext = host.Orientation
    wall_direction = XYZ(wall_ext.Y, -wall_ext.X, wall_ext.Z)
    up_direction = XYZ.BasisZ
    wall_int = XYZ(-wall_ext.X, -wall_ext.Y, wall_ext.Z)

    ### BOUNDING BOX FOR ELEVATION
    # Calculate the bounding box
    min_point_elev = XYZ((-window_width/2) - 500/304.8, (-window_height/2) - window_sill_height - 750/304.8, -1000/304.8)
    max_point_elev = XYZ((window_width/2) + 500/304.8, (window_height/2) + 1000/304.8, 0)

    # create 'rotated' bounding box
    rotation_transform_elev = Transform.Identity
    rotation_transform_elev.Origin = window_center
    rotation_transform_elev.BasisX = wall_direction
    rotation_transform_elev.BasisY = up_direction
    rotation_transform_elev.BasisZ = wall_int

    rotated_bounding_box_elev = BoundingBoxXYZ()
    rotated_bounding_box_elev.Transform = rotation_transform_elev
    rotated_bounding_box_elev.Min = min_point_elev
    rotated_bounding_box_elev.Max = max_point_elev
    
    ### CREATE SECTION
    # create sections, change name, apply view template
    t = Transaction(doc, "create section")
    t.Start()

    type = window.LookupParameter("S3A_Type").AsString()
    #elevation
    newViewElev = ViewSection.CreateSection(doc, SectionTypes[1].Id, rotated_bounding_box_elev)
    newViewElev.Name = type + " - aanzicht TEST"
    newViewElev.ViewTemplateId = templateS.Id

    t.Commit()

 

0 Likes
Message 4 of 5

jeremy_tammik
Alumni
Alumni

Great! Congratulations! Thank you for the appreciation and for the interesting point that ChatGPT was useful in translating the code. Glad to hear it works now, and thank you for sharing the result.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 5 of 5

jeremy_tammik
Alumni
Alumni