22# pylint: skip-file
33# by Roman Golev
44
5-
6-
7- #Load Revit API
8- import clr
9- clr .AddReference ("RevitAPI" )
10- import Autodesk
11- from Autodesk .Revit .DB import *
12- from Autodesk .Revit .UI import *
13-
145import sys
6+ import Autodesk .Revit .DB as DB
7+ from Autodesk .Revit .DB import BuiltInParameter , XYZ , Transform , BoundingBoxXYZ , \
8+ ViewSection , ViewFamilyType , FilteredElementCollector , \
9+ ViewFamily
1510
16- import pyrevit
1711from pyrevit import forms
1812from core .selectionhelpers import CustomISelectionFilterByIdInclude , ID_WALLS
1913from Autodesk .Revit .UI .Selection import ObjectType
2014
21- doc = __revit__ .ActiveUIDocument .Document
22- uidoc = __revit__ .ActiveUIDocument
23- uiapp = __revit__
24- app = uiapp .Application
25- t = Autodesk .Revit .DB .Transaction (doc )
26- tg = Autodesk .Revit .DB .TransactionGroup (doc )
27-
28-
15+ doc = __revit__ .ActiveUIDocument .Document # type: ignore
16+ uidoc = __revit__ .ActiveUIDocument # type: ignore
17+ transaction = DB .Transaction (doc )
18+ transaction_group = DB .TransactionGroup (doc )
2919
3020# Get unput: selected by user elements
3121def get_selection ():
@@ -40,8 +30,8 @@ def get_selection():
4030 return selection
4131
4232
43- def create_section_by_wall (el , doc , viewFamilyTypeId , t , flip ):
44- offset = units ( 100 )
33+ def create_section_by_wall (el , doc , viewFamilyTypeId , transaction , flip ):
34+ offset = 100 / 304.8 # 100mm offset as Double
4535 wall = doc .GetElement (el )
4636 # Determine section box
4737 lc = wall .Location
@@ -61,11 +51,8 @@ def create_section_by_wall(el, doc, viewFamilyTypeId, t, flip):
6151 w = v .GetLength ()
6252 h = maxZ - minZ
6353 d = wall .WallType .Width
64- # wallBaseOffset = units(float(wall.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET).AsString()))
65- # wallUnconnectedHeight = units(float(wall.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM).AsString()))
66- wallBaseOffset = builtInParam (wall , BuiltInParameter .WALL_BASE_OFFSET )
67- wallUnconnectedHeight = builtInParam (wall , BuiltInParameter .WALL_USER_HEIGHT_PARAM )
68-
54+ wallBaseOffset = wall .get_Parameter (BuiltInParameter .WALL_BASE_OFFSET ).AsDouble ()
55+ wallUnconnectedHeight = wall .get_Parameter (BuiltInParameter .WALL_USER_HEIGHT_PARAM ).AsDouble ()
6956
7057 # XYZ(min/max section line length, min/max height of the section box, min/max far clip)
7158 min = XYZ (- 0.5 * w - offset , wallBaseOffset - offset , - offset - 0.5 * d )
@@ -95,35 +82,25 @@ def create_section_by_wall(el, doc, viewFamilyTypeId, t, flip):
9582 sectionBox .Min = min # scope box bottom
9683 sectionBox .Max = max # scope box top
9784
98- t .Start ('Create Section' )
9985 # Create wall section view
100- newSection = ViewSection .CreateSection (doc , viewFamilyTypeId , sectionBox )
101- t .Commit ()
102- return newSection
103-
104-
105- def units (mmToFeets ):
86+ transaction .Start ('Create Section' )
10687 try :
107- # R19, R20, R21
108- dut = DisplayUnitType . DUT_MILLIMETERS
109- return UnitUtils . ConvertToInternalUnits ( mmToFeets , dut )
110- except :
111- # R22, R22, R23 and later
112- return mmToFeets / 304.8
113-
88+ newSection = ViewSection . CreateSection ( doc , viewFamilyTypeId , sectionBox )
89+ transaction . Commit ()
90+ return newSection
91+ except Exception as e :
92+ transaction . RollBack ()
93+ transaction . Dispose ()
94+ print ( e )
11495
11596
116- def builtInParam (wall , wallParam ):
117- return units (float (wall .get_Parameter (wallParam ).AsValueString ()))
118-
11997def main ():
12098 # Get selection
121- # sel = uidoc.Selection.GetElementIds()
12299 sel = get_selection ()
123100
124101 # Check selected elements
125102 if not sel :
126- forms .alert ("Please, select wall" ,"Section For Wall" )
103+ forms .alert ("Please, select wall" , "Section For Wall" )
127104 sys .exit ()
128105 for element in sel :
129106 if doc .GetElement (element ).Category .Id .ToString () != "-2000011" :
@@ -157,13 +134,12 @@ def main():
157134
158135 #Make section(s)
159136 if len (sel ) == 1 :
160- create_section_by_wall (sel [0 ], doc , viewFamilyTypeId , t , sw ["Flip section" ])
137+ create_section_by_wall (sel [0 ], doc , viewFamilyTypeId , transaction , sw ["Flip section" ])
161138 elif len (sel ) > 1 :
162- tg .Start ("Create multiple sections" )
139+ transaction_group .Start ("Create multiple sections" )
163140 for el in sel :
164- create_section_by_wall (el , doc , viewFamilyTypeId , t , sw ["Flip section" ])
165- tg .Assimilate ()
166-
141+ create_section_by_wall (el , doc , viewFamilyTypeId , transaction , sw ["Flip section" ])
142+ transaction_group .Assimilate ()
167143
168144if __name__ == '__main__' :
169145 main ()
0 commit comments