-
Notifications
You must be signed in to change notification settings - Fork 14
/
cadquery_helpers.py
60 lines (46 loc) · 2.13 KB
/
cadquery_helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from os import makedirs, path
from typing import Optional
import cadquery as cq
from OCP.Message import Message, Message_Gravity # type: ignore
class StepConstants:
THT_LEAD_SOLDER_LENGTH = 3.0 # Lead length on the PCB solder side
class StepColor:
IC_BODY = cq.Color('gray16')
IC_PIN1_DOT = cq.Color('gray55')
LEAD_SMT = cq.Color('gainsboro')
LEAD_THT = cq.Color('gainsboro')
class StepAssembly:
"""
A STEP assembly.
"""
def __init__(self, name: str):
self.assembly = cq.Assembly(name=name)
# Less verbose output
for printer in Message.DefaultMessenger_s().Printers():
printer.SetTraceLevel(Message_Gravity.Message_Fail)
def add_body(self, body: cq.Workplane, name: str, color: cq.Color,
location: Optional[cq.Location] = None) -> None:
"""
Add a body to the assembly.
Important: If the same body is added multiple times to the assembly
with different transformations, please use the `location` parameter
instead of transforming each body! This leads to much more efficient
STEP minification.
"""
self.assembly.add(body, name=name, color=color, loc=location)
def save(self, out_path: str, fused: bool) -> None:
"""
Write the STEP file to the specified path.
Important: For simple bodies with (almost) no repetition (like
resistors, capacitors, ...), pass `fused=True` to get a simple,
non-hierarchical STEP file. However, for models with repetition (like
an IC with several pins), pass `fused=False` since this leads to much
more efficient STEP minification (saves several 100MB in total!).
"""
dir_path = path.dirname(out_path)
if path.exists(dir_path) and not path.isdir(dir_path):
raise RuntimeError(f'Path "{dir_path}" exists but is not a directory')
if not path.exists(dir_path):
makedirs(dir_path)
mode = 'fused' if fused else 'default' # type: cq.occ_impl.exporters.assembly.STEPExportModeLiterals
self.assembly.save(out_path, 'STEP', mode=mode, write_pcurves=False)