1- import os .path
21import uuid
32
43from tempfile import TemporaryDirectory
54from shutil import make_archive
65from typing import Optional
76from typing_extensions import Literal
7+ from pathlib import Path
88
99from vtkmodules .vtkIOExport import vtkJSONSceneExporter , vtkVRMLExporter
1010from vtkmodules .vtkRenderingCore import vtkRenderWindow
@@ -50,7 +50,7 @@ class ExportModes:
5050
5151def exportAssembly (
5252 assy : AssemblyProtocol ,
53- path : str ,
53+ path : Path ,
5454 mode : STEPExportModeLiterals = "default" ,
5555 ** kwargs ,
5656) -> bool :
@@ -102,14 +102,14 @@ def exportAssembly(
102102 Interface_Static .SetIVal_s ("write.stepcaf.subshapes.name" , 1 )
103103 writer .Transfer (doc , STEPControl_StepModelType .STEPControl_AsIs )
104104
105- status = writer .Write (path )
105+ status = writer .Write (str ( path ) )
106106
107107 return status == IFSelect_ReturnStatus .IFSelect_RetDone
108108
109109
110110def exportStepMeta (
111111 assy : AssemblyProtocol ,
112- path : str ,
112+ path : Path ,
113113 write_pcurves : bool = True ,
114114 precision_mode : int = 0 ,
115115) -> bool :
@@ -161,10 +161,12 @@ def _process_child(child: AssemblyProtocol, assy_label: TDF_Label):
161161 # Collect all of the shapes in the child object
162162 if child .obj :
163163 child_items = (
164- child .obj
165- if isinstance (child .obj , Shape )
166- else Compound .makeCompound (
167- s for s in child .obj .vals () if isinstance (s , Shape )
164+ (
165+ child .obj
166+ if isinstance (child .obj , Shape )
167+ else Compound .makeCompound (
168+ s for s in child .obj .vals () if isinstance (s , Shape )
169+ )
168170 ),
169171 child .name ,
170172 child .loc ,
@@ -268,19 +270,20 @@ def _process_assembly(
268270 Interface_Static .SetIVal_s ("write.precision.mode" , precision_mode )
269271 writer .Transfer (doc , STEPControl_StepModelType .STEPControl_AsIs )
270272
271- status = writer .Write (path )
273+ status = writer .Write (str ( path ) )
272274
273275 return status == IFSelect_ReturnStatus .IFSelect_RetDone
274276
275277
276- def exportCAF (assy : AssemblyProtocol , path : str , binary : bool = False ) -> bool :
278+ def exportCAF (assy : AssemblyProtocol , path : Path , binary : bool = False ) -> bool :
277279 """
278280 Export an assembly to an XCAF xml or xbf file (internal OCCT formats).
279281 """
280282
281- folder , fname = os .path .split (path )
282- name , ext = os .path .splitext (fname )
283- ext = ext [1 :] if ext [0 ] == "." else ext
283+ folder = path .parent
284+ fname = path .name
285+ name = path .stem
286+ ext = path .suffix .lstrip ("." )
284287
285288 _ , doc = toCAF (assy , binary = binary )
286289 app = XCAFApp_Application .GetApplication_s ()
@@ -308,10 +311,10 @@ def exportCAF(assy: AssemblyProtocol, path: str, binary: bool = False) -> bool:
308311 format_name , format_desc , TCollection_AsciiString (ext ), ret , store ,
309312 )
310313
311- doc .SetRequestedFolder (TCollection_ExtendedString (folder ))
314+ doc .SetRequestedFolder (TCollection_ExtendedString (str ( folder ) ))
312315 doc .SetRequestedName (TCollection_ExtendedString (name ))
313316
314- status = app .SaveAs (doc , TCollection_ExtendedString (path ))
317+ status = app .SaveAs (doc , TCollection_ExtendedString (str ( path ) ))
315318
316319 app .Close (doc )
317320
@@ -335,7 +338,7 @@ def _vtkRenderWindow(
335338 return renderWindow
336339
337340
338- def exportVTKJS (assy : AssemblyProtocol , path : str ):
341+ def exportVTKJS (assy : AssemblyProtocol , path : Path ):
339342 """
340343 Export an assembly to a zipped vtkjs. NB: .zip extensions is added to path.
341344 """
@@ -348,12 +351,12 @@ def exportVTKJS(assy: AssemblyProtocol, path: str):
348351 exporter .SetFileName (tmpdir )
349352 exporter .SetRenderWindow (renderWindow )
350353 exporter .Write ()
351- make_archive (path , "zip" , tmpdir )
354+ make_archive (str ( path ) , "zip" , tmpdir )
352355
353356
354357def exportVRML (
355358 assy : AssemblyProtocol ,
356- path : str ,
359+ path : Path ,
357360 tolerance : float = 1e-3 ,
358361 angularTolerance : float = 0.1 ,
359362):
@@ -362,14 +365,14 @@ def exportVRML(
362365 """
363366
364367 exporter = vtkVRMLExporter ()
365- exporter .SetFileName (path )
368+ exporter .SetFileName (str ( path ) )
366369 exporter .SetRenderWindow (_vtkRenderWindow (assy , tolerance , angularTolerance ))
367370 exporter .Write ()
368371
369372
370373def exportGLTF (
371374 assy : AssemblyProtocol ,
372- path : str ,
375+ path : Path ,
373376 binary : Optional [bool ] = None ,
374377 tolerance : float = 1e-3 ,
375378 angularTolerance : float = 0.1 ,
@@ -382,10 +385,10 @@ def exportGLTF(
382385 if binary is None :
383386 # Handle the binary option for GLTF export based on file extension
384387 binary = True
385- path_parts = path .split ("." )
388+ sfx = path .suffix . lstrip ("." ). lower ( )
386389
387390 # Binary will be the default if the user specified a non-standard file extension
388- if len ( path_parts ) > 0 and path_parts [ - 1 ] == "gltf" :
391+ if sfx == "gltf" :
389392 binary = False
390393
391394 # map from CadQuery's right-handed +Z up coordinate system to glTF's right-handed +Y up coordinate system
@@ -395,7 +398,7 @@ def exportGLTF(
395398
396399 _ , doc = toCAF (assy , True , True , tolerance , angularTolerance )
397400
398- writer = RWGltf_CafWriter (TCollection_AsciiString (path ), binary )
401+ writer = RWGltf_CafWriter (TCollection_AsciiString (str ( path ) ), binary )
399402 result = writer .Perform (
400403 doc , TColStd_IndexedDataMapOfStringString (), Message_ProgressRange ()
401404 )
0 commit comments