Skip to content

Commit 03bb5b4

Browse files
authored
Merge pull request #2941 from Wurschdhaud/new-feature-window-pos
New feature window pos
2 parents 72a0a14 + 4747b8c commit 03bb5b4

File tree

2 files changed

+68
-26
lines changed
  • extensions/pyRevitTools.extension/pyRevit.tab/Modify.panel/3D.pulldown/Measure.pushbutton
  • pyrevitlib/pyrevit

2 files changed

+68
-26
lines changed

extensions/pyRevitTools.extension/pyRevit.tab/Modify.panel/3D.pulldown/Measure.pushbutton/script.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from math import pi, atan, sqrt
44
from pyrevit import HOST_APP, revit, forms, script
55
from pyrevit import UI, DB
6-
from pyrevit.framework import System
76
from Autodesk.Revit.Exceptions import InvalidOperationException
87

98
logger = script.get_logger()
@@ -42,8 +41,6 @@
4241
LINE_COLOR_DIAG = DB.ColorWithTransparency(200, 200, 0, 0) # Dark Yellow
4342
CUBE_COLOR = DB.ColorWithTransparency(255, 165, 0, 50) # Orange
4443

45-
WINDOW_POSITION = "measure_window_pos"
46-
4744

4845
def calculate_distances(point1, point2):
4946
"""Calculate dx, dy, dz, diagonal distance, and slope angle between two points.
@@ -306,24 +303,7 @@ def __init__(self, xaml_file_name):
306303

307304
# Handle window close event
308305
self.Closed += self.window_closed
309-
310-
try:
311-
pos = script.load_data(WINDOW_POSITION, this_project=False)
312-
all_bounds = [s.WorkingArea for s in System.Windows.Forms.Screen.AllScreens]
313-
x, y = pos["Left"], pos["Top"]
314-
visible = any(
315-
(b.Left <= x <= b.Right and b.Top <= y <= b.Bottom) for b in all_bounds
316-
)
317-
if not visible:
318-
raise Exception
319-
self.WindowStartupLocation = System.Windows.WindowStartupLocation.Manual
320-
self.Left = pos.get("Left", 200)
321-
self.Top = pos.get("Top", 150)
322-
except Exception:
323-
self.WindowStartupLocation = (
324-
System.Windows.WindowStartupLocation.CenterScreen
325-
)
326-
306+
script.restore_window_position(self)
327307
self.Show()
328308

329309
# Automatically start the first measurement
@@ -332,9 +312,7 @@ def __init__(self, xaml_file_name):
332312
def window_closed(self, sender, args):
333313
"""Handle window close event - copy history to clipboard, cleanup DC3D server and visual aids."""
334314
global dc3d_server
335-
new_pos = {"Left": self.Left, "Top": self.Top}
336-
script.store_data(WINDOW_POSITION, new_pos, this_project=False)
337-
315+
script.save_window_position(self)
338316
# Copy measurement history to clipboard before cleanup
339317
try:
340318
if measurement_history:

pyrevitlib/pyrevit/script.py

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
script.exit()
99
```
1010
"""
11-
#pylint: disable=consider-using-f-string
11+
# pylint: disable=consider-using-f-string
1212

1313
import sys
1414
import os
@@ -38,7 +38,7 @@
3838
# suppress any warning generated by native or third-party modules
3939
warnings.filterwarnings("ignore")
4040

41-
#pylint: disable=W0703,C0302,C0103,W0614
41+
# pylint: disable=W0703,C0302,C0103,W0614
4242
mlogger = logger.get_logger(__name__)
4343

4444

@@ -48,6 +48,7 @@
4848
ICON_MEDIUM = 24
4949
ICON_LARGE = 32
5050

51+
5152
def get_info():
5253
"""Return info on current pyRevit command.
5354
@@ -799,3 +800,66 @@ def data_exists(slot_name, this_project=True):
799800
file_ext=DATAFEXT,
800801
add_cmd_name=False)
801802
return os.path.exists(data_file)
803+
804+
805+
def restore_window_position(window, command_name=EXEC_PARAMS.command_name):
806+
"""
807+
Restore window position from saved data.
808+
809+
Args:
810+
window (System.Windows.Window): WPF window instance
811+
command_name (str): Unique identifier for this window
812+
813+
Returns:
814+
bool: True if position was restored, False if centered to screen
815+
"""
816+
storage_key = "last_window_position_" + command_name
817+
818+
try:
819+
pos = load_data(storage_key, this_project=False)
820+
if not pos:
821+
raise Exception("No saved position")
822+
823+
left, top, width, height = (
824+
pos.get("Left", 0),
825+
pos.get("Top", 0),
826+
pos.get("Width", window.Width),
827+
pos.get("Height", window.Height),
828+
)
829+
830+
if coreutils.is_box_visible_on_screens(left, top, width, height):
831+
window.WindowStartupLocation = (
832+
framework.Windows.WindowStartupLocation.Manual
833+
)
834+
window.Left = left
835+
window.Top = top
836+
window.Width = width
837+
window.Height = height
838+
return True
839+
else:
840+
raise Exception("Position not visible on any screen")
841+
842+
except Exception as ex:
843+
mlogger.debug("Could not restore window position: %s", ex)
844+
window.WindowStartupLocation = (
845+
framework.Windows.WindowStartupLocation.CenterScreen
846+
)
847+
return False
848+
849+
850+
def save_window_position(window, command_name=EXEC_PARAMS.command_name):
851+
"""
852+
Save window position to persistent storage.
853+
854+
Args:
855+
window (System.Windows.Window): WPF window instance
856+
command_name (str): Unique identifier for this window
857+
"""
858+
storage_key = "last_window_position_" + command_name
859+
position_data = {
860+
"Left": window.Left,
861+
"Top": window.Top,
862+
"Width": window.ActualWidth,
863+
"Height": window.ActualHeight
864+
}
865+
store_data(storage_key, position_data, this_project=False)

0 commit comments

Comments
 (0)