Skip to content

Commit 5be1bb2

Browse files
committed
store values for nudging, box states; support switching documents by updating globals
1 parent 240e9e1 commit 5be1bb2

File tree

1 file changed

+85
-31
lines changed

1 file changed

+85
-31
lines changed

extensions/pyRevitTools.extension/pyRevit.tab/Modify.panel/3D.pulldown/Section Box Navigator.pushbutton/script.py

Lines changed: 85 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,53 @@
3636
# Initialize Variables
3737
# --------------------
3838

39-
uidoc = revit.uidoc
40-
doc = revit.doc
41-
active_view = revit.active_view
39+
uidoc = None
40+
doc = None
41+
active_view = None
4242

4343
logger = script.get_logger()
4444
output = script.get_output()
4545
output.close_others()
4646

47+
my_config = script.get_config()
48+
4749
sb_form = None
4850

49-
length_format_options = doc.GetUnits().GetFormatOptions(DB.SpecTypeId.Length)
50-
length_unit = length_format_options.GetUnitTypeId()
51-
length_unit_label = DB.LabelUtils.GetLabelForUnit(length_unit)
52-
length_unit_symbol = length_format_options.GetSymbolTypeId()
51+
length_unit = None
52+
length_unit_label = None
5353
length_unit_symbol_label = None
54-
if not length_unit_symbol.Empty():
55-
length_unit_symbol_label = DB.LabelUtils.GetLabelForSymbol(length_unit_symbol)
5654

57-
DEFAULT_NUDGE_VALUE_MM = 500.0
58-
default_nudge_value = DB.UnitUtils.Convert(
59-
DEFAULT_NUDGE_VALUE_MM, DB.UnitTypeId.Millimeters, length_unit
60-
)
55+
config_level_nudge_value = my_config.get_option("level_nudge_value", 1.64042)
56+
config_grid_nudge_value = my_config.get_option("grid_nudge_value", 1.64042)
57+
config_expand_nudge_value = my_config.get_option("expand_nudge_value", 1.64042)
58+
6159
TOLERANCE = 1e-5
6260
DATAFILENAME = "SectionBox"
6361
TEMP_DATAFILE = script.get_instance_data_file("SectionBoxTemp")
6462
WINDOW_POSITION = "sbnavigator_window_pos"
6563

64+
65+
def initialize_globals():
66+
global uidoc, doc, active_view
67+
global length_unit, length_unit_label, length_unit_symbol_label
68+
global level_nudge_value, grid_nudge_value, expand_nudge_value
69+
70+
uidoc = revit.uidoc
71+
doc = revit.doc
72+
active_view = revit.active_view
73+
74+
length_format_options = doc.GetUnits().GetFormatOptions(DB.SpecTypeId.Length)
75+
length_unit = length_format_options.GetUnitTypeId()
76+
length_unit_label = DB.LabelUtils.GetLabelForUnit(length_unit)
77+
length_unit_symbol = length_format_options.GetSymbolTypeId()
78+
length_unit_symbol_label = None
79+
if not length_unit_symbol.Empty():
80+
length_unit_symbol_label = DB.LabelUtils.GetLabelForSymbol(length_unit_symbol)
81+
82+
level_nudge_value = DB.UnitUtils.Convert(config_level_nudge_value, DB.UnitTypeId.Feet, length_unit)
83+
grid_nudge_value = DB.UnitUtils.Convert(config_grid_nudge_value, DB.UnitTypeId.Feet, length_unit)
84+
expand_nudge_value = DB.UnitUtils.Convert(config_expand_nudge_value, DB.UnitTypeId.Feet, length_unit)
85+
6686
# --------------------
6787
# Helper Functions
6888
# --------------------
@@ -135,14 +155,16 @@ def format_length_value(value):
135155

136156

137157
# --------------------
138-
# View Changed Monitor
158+
# Event Monitor
139159
# --------------------
140160

141161

142162
@events.handle("doc-changed")
143163
@events.handle("view-activated")
144164
def on_view_or_doc_changed(sender, args):
145165
try:
166+
if revit.doc != doc:
167+
initialize_globals()
146168
if not sb_form or not sb_form.chkAutoupdate.IsChecked:
147169
return
148170
sb_form.Dispatcher.Invoke(System.Action(sb_form.update_info))
@@ -162,7 +184,16 @@ class SectionBoxNavigatorForm(forms.WPFWindow):
162184
def __init__(self, xaml_file_name):
163185
forms.WPFWindow.__init__(self, xaml_file_name, handle_esc=False)
164186

187+
self.chkIncludeLinks.IsChecked = my_config.get_option("chkLinks_state")
188+
self.chkPreview.IsChecked = my_config.get_option("chkPreview_state")
189+
self.chkAutoupdate.IsChecked = my_config.get_option("chkAutoupdate_state")
190+
self.rbLevel.IsChecked = my_config.get_option("rbLevel_state", True)
191+
self.rbLevelNudge.IsChecked = not self.rbLevel.IsChecked
192+
self.rbGrid.IsChecked = my_config.get_option("rbGrid_state", True)
193+
self.rbGridNudge.IsChecked = not self.rbGrid.IsChecked
194+
165195
self.current_view = doc.ActiveView
196+
self.current_length_unit = length_unit
166197
self.all_levels = get_all_levels(doc, self.chkIncludeLinks.IsChecked)
167198
self.all_grids = get_all_grids(doc, self.chkIncludeLinks.IsChecked)
168199
self.preview_server = None
@@ -179,18 +210,7 @@ def __init__(self, xaml_file_name):
179210

180211
self.pending_action = None
181212

182-
if not length_unit_symbol_label:
183-
self.project_unit_text.Visibility = forms.WPF_VISIBLE
184-
self.project_unit_text.Text = (
185-
self.get_locale_string("LengthLabelAdjust") + "\n" + length_unit_label
186-
)
187-
self.txtLevelNudgeAmount.Text = str(round(default_nudge_value, 3))
188-
self.txtLevelNudgeUnit.Text = length_unit_symbol_label or ""
189-
self.txtExpandAmount.Text = str(round(default_nudge_value, 3))
190-
self.txtExpandUnit.Text = length_unit_symbol_label or ""
191-
self.txtGridNudgeAmount.Text = str(round(default_nudge_value, 3))
192-
self.txtGridNudgeUnit.Text = length_unit_symbol_label or ""
193-
213+
self.update_fields_with_unit_dependencies()
194214
self.update_info()
195215
self.update_grid_status()
196216
self.update_expand_actions_status()
@@ -200,6 +220,22 @@ def __init__(self, xaml_file_name):
200220
script.restore_window_position(self)
201221
self.Show()
202222

223+
def update_fields_with_unit_dependencies(self):
224+
if not length_unit_symbol_label:
225+
self.project_unit_text.Visibility = forms.WPF_VISIBLE
226+
self.project_unit_text.Text = (
227+
self.get_locale_string("LengthLabelAdjust") + "\n" + length_unit_label
228+
)
229+
else:
230+
self.project_unit_text.Visibility = forms.WPF_COLLAPSED
231+
232+
self.txtLevelNudgeAmount.Text = str(round(level_nudge_value, 3))
233+
self.txtLevelNudgeUnit.Text = length_unit_symbol_label or ""
234+
self.txtExpandAmount.Text = str(round(expand_nudge_value, 3))
235+
self.txtExpandUnit.Text = length_unit_symbol_label or ""
236+
self.txtGridNudgeAmount.Text = str(round(grid_nudge_value, 3))
237+
self.txtGridNudgeUnit.Text = length_unit_symbol_label or ""
238+
203239
def update_dropdown_visibility(self):
204240
"""Show/hide dropdown arrows based on Level mode."""
205241
visibility = forms.WPF_VISIBLE if self.rbLevel.IsChecked else forms.WPF_COLLAPSED
@@ -302,6 +338,10 @@ def update_info(self):
302338
last_view = self.current_view.Id
303339
self.current_view = doc.ActiveView
304340

341+
if self.current_length_unit != length_unit:
342+
self.current_length_unit = length_unit
343+
self.update_fields_with_unit_dependencies()
344+
305345
if is_2d_view(self.current_view):
306346
self.btnAlignToView.Content = self.get_locale_string("AlignWith3DView")
307347
if last_view != self.current_view.Id:
@@ -1326,12 +1366,14 @@ def _handle_grid_move(self, tag):
13261366
)
13271367
return
13281368

1329-
def _get_validated_nudge_amount(self, text_control, column=None, unit=length_unit):
1369+
def _get_validated_nudge_amount(self, text_control, column=None, unit=None):
13301370
"""Extract and validate nudge amount from text control.
13311371
13321372
Returns:
13331373
float: Converted distance in internal units, or None if invalid
13341374
"""
1375+
if unit is None:
1376+
unit = length_unit
13351377
try:
13361378
distance_text = text_control.Text.strip()
13371379
if column and not distance_text:
@@ -1731,10 +1773,21 @@ def form_closed(self, sender, args):
17311773
logger.warning("Error removing DC3D server: {}".format(ex))
17321774

17331775
# Refresh view
1734-
try:
1735-
uidoc.RefreshActiveView()
1736-
except Exception as ex:
1737-
logger.warning("Error refreshing view: {}".format(ex))
1776+
uidoc.RefreshActiveView()
1777+
1778+
# Save nudge values and radio buttons
1779+
level_nudge_value = self._get_validated_nudge_amount(self.txtLevelNudgeAmount)
1780+
grid_nudge_value = self._get_validated_nudge_amount(self.txtGridNudgeAmount)
1781+
expand_nudge_value = self._get_validated_nudge_amount(self.txtExpandAmount)
1782+
my_config.set_option("level_nudge_value", level_nudge_value)
1783+
my_config.set_option("grid_nudge_value", grid_nudge_value)
1784+
my_config.set_option("expand_nudge_value", expand_nudge_value)
1785+
my_config.set_option("rbLevel_state", self.rbLevel.IsChecked)
1786+
my_config.set_option("rbGrid_state", self.rbGrid.IsChecked)
1787+
my_config.set_option("chkLinks_state", self.chkIncludeLinks.IsChecked)
1788+
my_config.set_option("chkPreview_state", self.chkPreview.IsChecked)
1789+
my_config.set_option("chkAutoupdate_state", self.chkAutoupdate.IsChecked)
1790+
script.save_config()
17381791

17391792
except Exception:
17401793
logger.exception("Error during cleanup.")
@@ -1746,6 +1799,7 @@ def form_closed(self, sender, args):
17461799

17471800
if __name__ == "__main__":
17481801
try:
1802+
initialize_globals()
17491803
# Check if section box is active
17501804
if not active_view.IsSectionBoxActive:
17511805
try:

0 commit comments

Comments
 (0)