Skip to content

Commit 96b9894

Browse files
committed
General cleanup
- Black formatting - Removed unnecessary comments - Error Handling - pyrevit module usage instead of Autodesk's
1 parent 58ce923 commit 96b9894

File tree

1 file changed

+111
-87
lines changed
  • extensions/pyRevitTools.extension/pyRevit.tab/Analysis.panel/Tools.stack/Analyse.pulldown/Door Mirror State.pushbutton

1 file changed

+111
-87
lines changed
Lines changed: 111 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,33 @@
11
# -*- coding: utf-8 -*-
2-
"""Writes the door swing direction of doors
3-
to a shared parameter."""
4-
__title__ = "DoorMirrorState"
52

6-
#IMPORTS
7-
# System
83
import time, sys
9-
#pyRevit
10-
from pyrevit import script, DB
4+
from pyrevit import script, DB, revit, DOCS, forms
115
from pyrevit.framework import List
12-
#Autodesk.Revit.DB
13-
from Autodesk.Revit.DB import Transaction, BuiltInParameter
146

15-
doc = __revit__.ActiveUIDocument.Document
16-
uidoc = __revit__.ActiveUIDocument
17-
app = __revit__.Application
7+
doc = DOCS.doc
188
output = script.get_output()
199

20-
#GLOBAL PARAMETERS
21-
DOORDIR_STANDARD_PARAM = "DoorFamilyOpeningDirection_standard"
22-
DOORDIR_MIRRORED_PARAM = "DoorFamilyOpeningDirection_mirrored"
10+
DOORDIR_STANDARD_PARAM = "DoorFamilyOpeningDirection_standard"
11+
DOORDIR_MIRRORED_PARAM = "DoorFamilyOpeningDirection_mirrored"
2312
DOORDIR_WRITEBACK_PARAM = "Door Wing Opening Direction"
24-
DOORDIR_ERROR_VALUE = "-" # Value if the family does't have shared param. above
13+
DOORDIR_ERROR_VALUE = "-" # Value if the family does't have shared param. above
2514

26-
#MAIN
15+
# MAIN
2716
timer_start = time.time()
2817

2918
# GET ALL DOORS
30-
doors_collector = DB.FilteredElementCollector(doc).OfCategory(DB.BuiltInCategory.OST_Doors).WhereElementIsNotElementType().ToElements()
19+
doors_collector = (
20+
DB.FilteredElementCollector(doc)
21+
.OfCategory(DB.BuiltInCategory.OST_Doors)
22+
.WhereElementIsNotElementType()
23+
.ToElements()
24+
)
25+
3126
if not doors_collector:
32-
sys.exit("No doors were found in the project.")
27+
forms.alert("No doors found in the model.", title="Warning")
28+
script.exit()
29+
3330

34-
# FUNCTION TO CHECK IF PARAMETER EXISTS
3531
def parameter_exists(parameter_name):
3632
binding_map = doc.ParameterBindings
3733
iterator = binding_map.ForwardIterator()
@@ -41,86 +37,114 @@ def parameter_exists(parameter_name):
4137
return True
4238
return False
4339

44-
# CHECK IF TUERAUFG_WRITEBACK_PARAMETER EXISTS
40+
4541
if not parameter_exists(DOORDIR_WRITEBACK_PARAM):
46-
print("The parameter '{}' does not exist in the current document.".format(DOORDIR_WRITEBACK_PARAM))
47-
print("Please create the parameter as an instance parameter, option different values for groupes,")
42+
print(
43+
"The parameter '{}' does not exist in the current document.".format(
44+
DOORDIR_WRITEBACK_PARAM
45+
)
46+
)
47+
print(
48+
"Please create the parameter as an instance parameter, option different values for groupes,"
49+
)
4850
print("for the category doors in the project.")
4951

5052
sys.exit()
5153

52-
# CREATE CONTAINERS
5354
count_parameter = 0
5455
count_not_parameter = 0
5556
doors_without_parameter = []
56-
# CREATE A LIST FOR DOORS WITH CHANGED PARAMETERS
5757
data_doors_changed = []
5858

59-
t = Transaction(doc,__title__)
60-
t.Start()
61-
#door_changed =[]
62-
# LOOP THROUGH ALL DOORS
63-
for door in doors_collector:
64-
# GET VALUE
65-
try:
66-
if door.Mirrored:
67-
value = door.LookupParameter(DOORDIR_MIRRORED_PARAM).AsString()
68-
else:
69-
value = door.LookupParameter(DOORDIR_STANDARD_PARAM).AsString()
70-
count_parameter +=1
71-
72-
except:
73-
# IF VALUE IS UNAVAILABLE - USE DEFAULT ERROR VALUE
74-
value = DOORDIR_ERROR_VALUE
75-
count_not_parameter += 1
76-
77-
# LOG DOOR TYPE WITHOUT VALUE
78-
door_family = door.get_Parameter(BuiltInParameter.ELEM_FAMILY_PARAM).AsValueString()
79-
door_type = door.get_Parameter(BuiltInParameter.ELEM_TYPE_PARAM).AsValueString()
80-
door_name = "{family}-{type}".format(family=door_family, type=door_type)
81-
if door_name not in doors_without_parameter:
82-
doors_without_parameter.append(door_name)
83-
84-
# SET PARAMETER
85-
try:
86-
# CHECK IF THE WRITEBACK PARAMETER EXISTS
87-
door_out_param = door.LookupParameter(DOORDIR_WRITEBACK_PARAM)
88-
89-
# WRITE CHANGES IN PARAMETERS
90-
door_out_param_old = door_out_param.AsString()
91-
door_out_param_new = value
92-
if door_out_param_old != door_out_param_new:
93-
door_out_param_changed = "{} -> {}".format(door_out_param_old, door_out_param_new)
94-
door_name = door.Name
95-
door_changed_link = output.linkify(door.Id)
96-
data_doors_changed.append([door_name, door_out_param_changed, door_changed_link])
97-
# SET DOOR FLIP STATE TO THE WRITEBACK PARAMETER
98-
door_out_param.Set(str(value))
99-
except:
100-
print('Please make sure OUT instance parameter exists: {}'.format(DOORDIR_WRITEBACK_PARAM))
101-
sys.exit()
102-
t.Commit()
103-
104-
# FINAL PRINT
105-
output.print_md('### Number of doors found in the project: {} '.format(len(doors_collector)))
106-
output.print_md('of these with writeback parameters defined in door family: {}'.format(count_parameter))
107-
output.print_md('without writeback parameters defined in door family: {a}'.format(a=count_not_parameter,))
108-
output.print_md('parameters missing: '"'{b}'"', '"'{c}'"''.format(b=DOORDIR_STANDARD_PARAM, c=DOORDIR_MIRRORED_PARAM))
109-
output.print_md('you will find in the folder of the script a shared parameter file containing this parameters.')
110-
output.print_md('The default writeback value for doors without defined values will be : '" {d} "' '.format(d=DOORDIR_ERROR_VALUE))
111-
output.print_md('---')
112-
output.print_md('### Door families without writeback parameter defined in family:')
59+
with revit.Transaction(doc, "DoorMirrorState"):
60+
for door in doors_collector:
61+
try:
62+
if door.Mirrored:
63+
value = door.LookupParameter(DOORDIR_MIRRORED_PARAM).AsString()
64+
else:
65+
value = door.LookupParameter(DOORDIR_STANDARD_PARAM).AsString()
66+
count_parameter += 1
67+
68+
except AttributeError:
69+
value = DOORDIR_ERROR_VALUE
70+
count_not_parameter += 1
71+
72+
door_family = door.get_Parameter(
73+
DB.BuiltInParameter.ELEM_FAMILY_PARAM
74+
).AsValueString()
75+
door_type = door.get_Parameter(
76+
DB.BuiltInParameter.ELEM_TYPE_PARAM
77+
).AsValueString()
78+
door_name = "{family}-{type}".format(family=door_family, type=door_type)
79+
if door_name not in doors_without_parameter:
80+
doors_without_parameter.append(door_name)
81+
82+
try:
83+
door_out_param = door.LookupParameter(DOORDIR_WRITEBACK_PARAM)
84+
85+
door_out_param_old = door_out_param.AsString()
86+
door_out_param_new = value
87+
if door_out_param_old != door_out_param_new:
88+
door_out_param_changed = "{} -> {}".format(
89+
door_out_param_old, door_out_param_new
90+
)
91+
door_name = door.Name
92+
door_changed_link = output.linkify(door.Id)
93+
data_doors_changed.append(
94+
[door_name, door_out_param_changed, door_changed_link]
95+
)
96+
door_out_param.Set(str(value))
97+
except AttributeError:
98+
print(
99+
"Please make sure instance parameter exists: {}".format(
100+
DOORDIR_WRITEBACK_PARAM
101+
)
102+
)
103+
104+
output.print_md(
105+
"### Number of doors found in the project: {} ".format(len(doors_collector))
106+
)
107+
output.print_md(
108+
"of these with writeback parameters defined in door family: {}".format(
109+
count_parameter
110+
)
111+
)
112+
output.print_md(
113+
"without writeback parameters defined in door family: {a}".format(
114+
a=count_not_parameter,
115+
)
116+
)
117+
output.print_md(
118+
"parameters missing: "
119+
"'{b}'"
120+
", "
121+
"'{c}'"
122+
"".format(b=DOORDIR_STANDARD_PARAM, c=DOORDIR_MIRRORED_PARAM)
123+
)
124+
output.print_md(
125+
"you will find in the folder of the script a shared parameter file containing this parameters."
126+
)
127+
output.print_md(
128+
"The default writeback value for doors without defined values will be : "
129+
" {d} "
130+
" ".format(d=DOORDIR_ERROR_VALUE)
131+
)
132+
output.print_md("---")
133+
output.print_md("### Door families without writeback parameter defined in family:")
113134
for door_type in doors_without_parameter:
114135
print(door_type)
115-
output.print_md('---')
116-
output.print_md('### Changes to previous run of the script:')
136+
output.print_md("---")
137+
output.print_md("### Changes to previous run of the script:")
117138

118-
# Create a table for changed parameter values
119139
if data_doors_changed:
120-
output.print_table(table_data=data_doors_changed, title="Doors with changed parameters:", columns=["Door", "Changed Value (old->new)", "ElementId"])
140+
output.print_table(
141+
table_data=data_doors_changed,
142+
title="Doors with changed parameters:",
143+
columns=["Door", "Changed Value (old->new)", "ElementId"],
144+
)
121145
else:
122-
output.print_md('#### No doors with changed parameters were found.')
146+
output.print_md("#### No doors with changed parameters were found.")
123147

124-
# End
125-
output.print_md('---')
126-
output.print_md('#### Script has finished in {}s'.format(time.time() - timer_start))
148+
output.print_md("---")
149+
elapsed_time = time.time() - timer_start
150+
output.print_md("#### Script has finished in {:.2f}s".format(elapsed_time))

0 commit comments

Comments
 (0)