1010import compas
1111from compas .geometry import Brep
1212from compas .scene import Scene
13+ from compas .tolerance import Tolerance
1314
1415from .settings import Settings
1516
17+ # NOTES
18+ # -----
19+ # a session should not be loaded explicitly
20+ # to load a session
21+ # just unset all stored data
22+ # this will trigger lazy re-loading of the data from the (new) system files and data dir
23+
1624
1725class LazyLoadSessionError (Exception ):
1826 pass
@@ -51,6 +59,7 @@ class LazyLoadSession:
5159 _data : dict [str , Any ]
5260 _settings : Settings
5361 _scene : Scene
62+ _tolerance : Tolerance
5463
5564 # this can be overwritten in a child class
5665 # to influence the default settings and scene objects
@@ -64,6 +73,7 @@ def __new__(
6473 basedir : Optional [Union [str , pathlib .Path ]] = None ,
6574 scene : Optional [Scene ] = None ,
6675 settings : Optional [Settings ] = None ,
76+ tolerance : Optional [Tolerance ] = None ,
6777 depth : Optional [int ] = None ,
6878 delete_existing : bool = False ,
6979 ):
@@ -89,6 +99,7 @@ def __new__(
8999 instance ._depth = depth or cls ._depth
90100 instance ._history = []
91101 instance ._data = {}
102+ instance ._tolerance = tolerance or instance .load_tolerance () or Tolerance ()
92103 instance ._settings = settings or instance .settingsclass ()
93104 instance ._scene = scene or instance .sceneclass ()
94105
@@ -107,6 +118,7 @@ def __str__(self) -> str:
107118 return "\n " .join (
108119 [
109120 f"Data: { self .data } " ,
121+ f"Tolerance: { self .tolerance } " ,
110122 f"Settings: { self .settings } " ,
111123 f"Scene: { None } " ,
112124 f"History: { self .history } " ,
@@ -153,6 +165,10 @@ def scenefile(self) -> pathlib.Path:
153165 def settingsfile (self ) -> pathlib .Path :
154166 return self .sessiondir / "__settings.json"
155167
168+ @property
169+ def tolerancefile (self ) -> pathlib .Path :
170+ return self .sessiondir / "__tolerance.json"
171+
156172 @property
157173 def current (self ):
158174 return self ._current
@@ -203,6 +219,15 @@ def data(self, value):
203219 raise ValueError
204220 self ._data = value
205221
222+ @property
223+ def tolerance (self ) -> Tolerance :
224+ return self ._tolerance
225+
226+ @tolerance .setter
227+ def tolerance (self , tolerance : Tolerance ) -> None :
228+ self ._tolerance .update_from_dict (tolerance .__data__ )
229+ compas .json_dump (self ._tolerance , self .tolerancefile )
230+
206231 # =============================================================================
207232 # Dict behaviour
208233 # =============================================================================
@@ -252,6 +277,16 @@ def create_dirs(self) -> None:
252277 self .recordsdir .mkdir (exist_ok = True )
253278 self .datadir .mkdir (exist_ok = True )
254279
280+ # =============================================================================
281+ # Tolerance
282+ # =============================================================================
283+
284+ def load_tolerance (self ) -> Optional [Tolerance ]:
285+ if self .tolerancefile .exists ():
286+ tolerance = compas .json_load (self .tolerancefile )
287+ if isinstance (tolerance , Tolerance ):
288+ return tolerance
289+
255290 # =============================================================================
256291 # Data
257292 # =============================================================================
@@ -358,18 +393,19 @@ def dump(self, sessiondir: Optional[Union[str, pathlib.Path]] = None) -> None:
358393
359394 history = {"depth" : self .depth , "current" : self .current , "records" : self .history }
360395
361- compas .json_dump (history , sessiondir / "__history.json" )
362- compas .json_dump (self .scene , sessiondir / "__scene.json" )
363- compas .json_dump (self .settings .model_dump (), sessiondir / "__settings.json" )
396+ compas .json_dump (history , self .historyfile )
397+ compas .json_dump (self .scene , self .scenefile )
398+ compas .json_dump (self .settings .model_dump (), self .settingsfile )
399+ compas .json_dump (self .tolerance , self .tolerancefile )
364400
365401 for key in self .data :
366402 value = self .data [key ]
367403
368404 if isinstance (value , Brep ):
369- filepath = sessiondir / "__data" / f"{ key } .stp"
405+ filepath = self . datadir / f"{ key } .stp"
370406 value .to_step (filepath )
371407 else :
372- filepath = sessiondir / "__data" / f"{ key } .json"
408+ filepath = self . datadir / f"{ key } .json"
373409 compas .json_dump (value , filepath )
374410
375411 def load (self , sessiondir : Optional [Union [str , pathlib .Path ]] = None , clear_history : bool = True ) -> None :
0 commit comments