Skip to content

Commit

Permalink
allow viewer.unit = "..."
Browse files Browse the repository at this point in the history
  • Loading branch information
Licini committed Jul 11, 2024
1 parent ae5f9cb commit 28c9cd4
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions src/compas_viewer/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def __init__(self, config: Optional[Config] = None, **kwargs):
self._unit = "m"

self.config = config or Config()
self.unit = self.config.unit
self.timer = QTimer()
self.mouse = Mouse()

Expand All @@ -40,6 +39,7 @@ def __init__(self, config: Optional[Config] = None, **kwargs):
# renderer should be part of UI
self.renderer = Renderer(self)
self.ui = UI(self)
self.unit = self.config.unit

@property
def scene(self) -> ViewerScene:
Expand All @@ -60,26 +60,24 @@ def unit(self) -> str:

@unit.setter
def unit(self, unit: str):
if self.running:
raise NotImplementedError("Changing the unit after the viewer is running is not yet supported.")
if unit != self._unit:
if self.running:
raise NotImplementedError("Changing the unit after the viewer is running is not yet supported.")
previous_scale = self.config.camera.scale
if unit == "m":
self.config.renderer.gridsize = (10.0, 10, 10.0, 10)
self.renderer.camera.scale = 1.0
elif unit == "cm":
self.config.renderer.gridsize = (1000.0, 10, 1000.0, 10)
self.renderer.camera.scale = 100.0
elif unit == "mm":
self.config.renderer.gridsize = (10000.0, 10, 10000.0, 10)
self.renderer.camera.scale = 1000.0
else:
if unit == "m":
self.config.renderer.gridsize = (10.0, 10, 10.0, 10)
self.config.camera.scale = 1.0
self.config.camera.position = [-10.0, -10.0, 10.0]
elif unit == "cm":
self.config.renderer.gridsize = (1000.0, 10, 1000.0, 10)
self.config.camera.scale = 100.0
self.config.camera.position = [-1000.0, -1000.0, 1000.0]
elif unit == "mm":
self.config.renderer.gridsize = (10000.0, 10, 10000.0, 10)
self.config.camera.scale = 1000.0
self.config.camera.position = [-10000.0, -10000.0, 10000.0]
else:
raise ValueError(f"Invalid unit: {unit}. Valid units are 'm', 'cm', 'mm'.")

self._unit = unit
raise ValueError(f"Invalid unit: {unit}. Valid units are 'm', 'cm', 'mm'.")
self.renderer.camera.distance *= self.renderer.camera.scale / previous_scale

self._unit = unit

def show(self):
self.running = True
Expand Down

0 comments on commit 28c9cd4

Please sign in to comment.