Skip to content

Commit e8f77f5

Browse files
Added materials
1 parent f754469 commit e8f77f5

File tree

7 files changed

+594
-141
lines changed

7 files changed

+594
-141
lines changed

cadquery/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
)
2222
from .occ_impl import exporters
2323
from .occ_impl import importers
24+
from .materials import Color, Material, CommonMaterial, PbrMaterial
2425

2526
# these items are the common implementation
2627

@@ -37,7 +38,7 @@
3738
)
3839
from .sketch import Sketch
3940
from .cq import CQ, Workplane
40-
from .assembly import Assembly, Color, Constraint
41+
from .assembly import Assembly, Constraint
4142
from . import selectors
4243
from . import plugins
4344

@@ -47,6 +48,9 @@
4748
"Workplane",
4849
"Assembly",
4950
"Color",
51+
"Material",
52+
"CommonMaterial",
53+
"PbrMaterial",
5054
"Constraint",
5155
"plugins",
5256
"selectors",

cadquery/assembly.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from .cq import Workplane
1919
from .occ_impl.shapes import Shape, Compound
2020
from .occ_impl.geom import Location
21-
from .occ_impl.assembly import Color
21+
from .occ_impl.assembly import Color, Material, AssemblyElement
2222
from .occ_impl.solver import (
2323
ConstraintKind,
2424
ConstraintSolver,
@@ -85,6 +85,7 @@ class Assembly(object):
8585
loc: Location
8686
name: str
8787
color: Optional[Color]
88+
material: Optional[Material]
8889
metadata: Dict[str, Any]
8990

9091
obj: AssemblyObjects
@@ -107,6 +108,7 @@ def __init__(
107108
loc: Optional[Location] = None,
108109
name: Optional[str] = None,
109110
color: Optional[Color] = None,
111+
material: Optional[Material] = None,
110112
metadata: Optional[Dict[str, Any]] = None,
111113
):
112114
"""
@@ -116,6 +118,7 @@ def __init__(
116118
:param loc: location of the root object (default: None, interpreted as identity transformation)
117119
:param name: unique name of the root object (default: None, resulting in an UUID being generated)
118120
:param color: color of the added object (default: None)
121+
:param material: material of the added object (default: None)
119122
:param metadata: a store for user-defined metadata (default: None)
120123
:return: An Assembly object.
121124
@@ -135,6 +138,7 @@ def __init__(
135138
self.loc = loc if loc else Location()
136139
self.name = name if name else str(uuid())
137140
self.color = color if color else None
141+
self.material = material if material else None
138142
self.metadata = metadata if metadata else {}
139143
self.parent = None
140144

@@ -153,7 +157,9 @@ def _copy(self) -> "Assembly":
153157
Make a deep copy of an assembly
154158
"""
155159

156-
rv = self.__class__(self.obj, self.loc, self.name, self.color, self.metadata)
160+
rv = self.__class__(
161+
self.obj, self.loc, self.name, self.color, self.material, self.metadata
162+
)
157163

158164
for ch in self.children:
159165
ch_copy = ch._copy()
@@ -172,6 +178,7 @@ def add(
172178
loc: Optional[Location] = None,
173179
name: Optional[str] = None,
174180
color: Optional[Color] = None,
181+
material: Optional[Material] = None,
175182
) -> "Assembly":
176183
"""
177184
Add a subassembly to the current assembly.
@@ -183,6 +190,8 @@ def add(
183190
the subassembly being used)
184191
:param color: color of the added object (default: None, resulting in the color stored in the
185192
subassembly being used)
193+
:param material: material of the added object (default: None, resulting in the material stored in the
194+
subassembly being used)
186195
"""
187196
...
188197

@@ -193,6 +202,7 @@ def add(
193202
loc: Optional[Location] = None,
194203
name: Optional[str] = None,
195204
color: Optional[Color] = None,
205+
material: Optional[Material] = None,
196206
metadata: Optional[Dict[str, Any]] = None,
197207
) -> "Assembly":
198208
"""
@@ -204,6 +214,7 @@ def add(
204214
:param name: unique name of the root object (default: None, resulting in an UUID being
205215
generated)
206216
:param color: color of the added object (default: None)
217+
:param material: material of the added object (default: None)
207218
:param metadata: a store for user-defined metadata (default: None)
208219
"""
209220
...
@@ -225,6 +236,9 @@ def add(self, arg, **kwargs):
225236
subassy.loc = kwargs["loc"] if kwargs.get("loc") else arg.loc
226237
subassy.name = kwargs["name"] if kwargs.get("name") else arg.name
227238
subassy.color = kwargs["color"] if kwargs.get("color") else arg.color
239+
subassy.material = (
240+
kwargs["material"] if kwargs.get("material") else arg.material
241+
)
228242
subassy.metadata = (
229243
kwargs["metadata"] if kwargs.get("metadata") else arg.metadata
230244
)
@@ -658,22 +672,31 @@ def __iter__(
658672
loc: Optional[Location] = None,
659673
name: Optional[str] = None,
660674
color: Optional[Color] = None,
661-
) -> Iterator[Tuple[Shape, str, Location, Optional[Color]]]:
675+
material: Optional[Material] = None,
676+
) -> Iterator[AssemblyElement]:
662677
"""
663-
Assembly iterator yielding shapes, names, locations and colors.
678+
Assembly iterator yielding shapes, names, locations, colors and materials.
664679
"""
665680

666681
name = f"{name}/{self.name}" if name else self.name
667682
loc = loc * self.loc if loc else self.loc
668683
color = self.color if self.color else color
684+
material = self.material if self.material else material
669685

670686
if self.obj:
671-
yield self.obj if isinstance(self.obj, Shape) else Compound.makeCompound(
672-
s for s in self.obj.vals() if isinstance(s, Shape)
673-
), name, loc, color
687+
shape = (
688+
self.obj
689+
if isinstance(self.obj, Shape)
690+
else Compound.makeCompound(
691+
s for s in self.obj.vals() if isinstance(s, Shape)
692+
)
693+
)
694+
yield AssemblyElement(
695+
shape=shape, name=name, location=loc, color=color, material=material
696+
)
674697

675698
for ch in self.children:
676-
yield from ch.__iter__(loc, name, color)
699+
yield from ch.__iter__(loc, name, color, material)
677700

678701
def toCompound(self) -> Compound:
679702
"""

0 commit comments

Comments
 (0)