Coverage for trimesh/exchange/cascade.py: 86%

21 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-06-24 04:40 +0000

1import os 

2import tempfile 

3 

4from ..exceptions import ExceptionWrapper 

5from ..typed import BinaryIO, Number 

6 

7# used as an intermediate format 

8from .gltf import load_glb 

9 

10 

11def load_step( 

12 file_obj: BinaryIO, 

13 file_type, 

14 tol_linear: Number | None = None, 

15 tol_angular: Number | None = None, 

16 tol_relative: bool | None = False, 

17 merge_primitives: bool = True, 

18 **kwargs, 

19) -> dict: 

20 """ 

21 Use `cascadio` a packaged version of OpenCASCADE 

22 to load a STEP file using GLB as an intermediate. 

23 

24 Parameters 

25 ----------- 

26 file_obj 

27 STEP file to load. 

28 **kwargs 

29 Passed to `cascadio.step_to_glb` 

30 

31 Returns 

32 ---------- 

33 kwargs 

34 Keyword arguments for a Scene. 

35 """ 

36 # TODO : update upstream `cascadio` to accept bytes objects 

37 # so that we don't need to write a temporary file to disc! 

38 with tempfile.TemporaryDirectory() as F: 

39 # temporarily copy the STEP 

40 stepfile = os.path.join(F, "data.step") 

41 with open(stepfile, "wb") as f: 

42 f.write(file_obj.read()) 

43 

44 # where to save the converted GLB 

45 glbfile = os.path.join(F, "converted.glb") 

46 

47 # the arguments for cascadio are not optional so 

48 # filter out any `None` value arguments here 

49 cascadio_kwargs = { 

50 "merge_primitives": bool(merge_primitives), 

51 "tol_linear": tol_linear, 

52 "tol_angular": tol_angular, 

53 "tol_relative": tol_relative, 

54 } 

55 # run the conversion 

56 cascadio.step_to_glb( 

57 stepfile, 

58 glbfile, 

59 **{k: v for k, v in cascadio_kwargs.items() if v is not None}, 

60 ) 

61 

62 with open(glbfile, "rb") as f: 

63 # return the parsed intermediate file 

64 return load_glb(file_obj=f, merge_primitives=merge_primitives, **kwargs) 

65 

66 

67try: 

68 # wheels for most platforms: `pip install cascadio` 

69 import cascadio 

70 

71 _cascade_loaders = {"stp": load_step, "step": load_step} 

72except BaseException as E: 

73 wrapper = ExceptionWrapper(E) 

74 _cascade_loaders = {"stp": wrapper, "step": wrapper}