Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from math import pi, atan, sqrt
from pyrevit import HOST_APP, revit, forms, script
from pyrevit import UI, DB
from pyrevit.framework import System
from Autodesk.Revit.Exceptions import InvalidOperationException

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

WINDOW_POSITION = "measure_window_pos"


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

# Handle window close event
self.Closed += self.window_closed

try:
pos = script.load_data(WINDOW_POSITION, this_project=False)
all_bounds = [s.WorkingArea for s in System.Windows.Forms.Screen.AllScreens]
x, y = pos["Left"], pos["Top"]
visible = any(
(b.Left <= x <= b.Right and b.Top <= y <= b.Bottom) for b in all_bounds
)
if not visible:
raise Exception
self.WindowStartupLocation = System.Windows.WindowStartupLocation.Manual
self.Left = pos.get("Left", 200)
self.Top = pos.get("Top", 150)
except Exception:
self.WindowStartupLocation = (
System.Windows.WindowStartupLocation.CenterScreen
)

script.restore_window_position(self)
self.Show()

# Automatically start the first measurement
Expand All @@ -332,9 +312,7 @@ def __init__(self, xaml_file_name):
def window_closed(self, sender, args):
"""Handle window close event - copy history to clipboard, cleanup DC3D server and visual aids."""
global dc3d_server
new_pos = {"Left": self.Left, "Top": self.Top}
script.store_data(WINDOW_POSITION, new_pos, this_project=False)

script.save_window_position(self)
# Copy measurement history to clipboard before cleanup
try:
if measurement_history:
Expand Down
68 changes: 66 additions & 2 deletions pyrevitlib/pyrevit/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
script.exit()
```
"""
#pylint: disable=consider-using-f-string
# pylint: disable=consider-using-f-string

import sys
import os
Expand Down Expand Up @@ -38,7 +38,7 @@
# suppress any warning generated by native or third-party modules
warnings.filterwarnings("ignore")

#pylint: disable=W0703,C0302,C0103,W0614
# pylint: disable=W0703,C0302,C0103,W0614
mlogger = logger.get_logger(__name__)


Expand All @@ -48,6 +48,7 @@
ICON_MEDIUM = 24
ICON_LARGE = 32


def get_info():
"""Return info on current pyRevit command.

Expand Down Expand Up @@ -799,3 +800,66 @@ def data_exists(slot_name, this_project=True):
file_ext=DATAFEXT,
add_cmd_name=False)
return os.path.exists(data_file)


def restore_window_position(window, command_name=EXEC_PARAMS.command_name):
"""
Restore window position from saved data.

Args:
window (System.Windows.Window): WPF window instance
command_name (str): Unique identifier for this window

Returns:
bool: True if position was restored, False if centered to screen
"""
storage_key = "last_window_position_" + command_name

try:
pos = load_data(storage_key, this_project=False)
if not pos:
raise Exception("No saved position")

left, top, width, height = (
pos.get("Left", 0),
pos.get("Top", 0),
pos.get("Width", window.Width),
pos.get("Height", window.Height),
)

if coreutils.is_box_visible_on_screens(left, top, width, height):
window.WindowStartupLocation = (
framework.Windows.WindowStartupLocation.Manual
)
window.Left = left
window.Top = top
window.Width = width
window.Height = height
return True
else:
raise Exception("Position not visible on any screen")

except Exception as ex:
mlogger.debug("Could not restore window position: %s", ex)
window.WindowStartupLocation = (
framework.Windows.WindowStartupLocation.CenterScreen
)
return False


def save_window_position(window, command_name=EXEC_PARAMS.command_name):
"""
Save window position to persistent storage.

Args:
window (System.Windows.Window): WPF window instance
command_name (str): Unique identifier for this window
"""
storage_key = "last_window_position_" + command_name
position_data = {
"Left": window.Left,
"Top": window.Top,
"Width": window.ActualWidth,
"Height": window.ActualHeight
}
store_data(storage_key, position_data, this_project=False)