Skip to content

Support for materials #1815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ out1.3mf
out2.3mf
out3.3mf
orig.dxf
box.brep
sketch.dxf
material_test*
6 changes: 5 additions & 1 deletion cadquery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
)
from .occ_impl import exporters
from .occ_impl import importers
from .materials import Color, Material, SimpleMaterial, PbrMaterial

# these items are the common implementation

Expand All @@ -37,7 +38,7 @@
)
from .sketch import Sketch
from .cq import CQ, Workplane
from .assembly import Assembly, Color, Constraint
from .assembly import Assembly, Constraint
from . import selectors
from . import plugins

Expand All @@ -47,6 +48,9 @@
"Workplane",
"Assembly",
"Color",
"Material",
"SimpleMaterial",
"PbrMaterial",
"Constraint",
"plugins",
"selectors",
Expand Down
39 changes: 31 additions & 8 deletions cadquery/assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from .cq import Workplane
from .occ_impl.shapes import Shape, Compound
from .occ_impl.geom import Location
from .occ_impl.assembly import Color
from .occ_impl.assembly import Color, Material, AssemblyElement
from .occ_impl.solver import (
ConstraintKind,
ConstraintSolver,
Expand Down Expand Up @@ -85,6 +85,7 @@ class Assembly(object):
loc: Location
name: str
color: Optional[Color]
material: Optional[Material]
metadata: Dict[str, Any]

obj: AssemblyObjects
Expand All @@ -107,6 +108,7 @@ def __init__(
loc: Optional[Location] = None,
name: Optional[str] = None,
color: Optional[Color] = None,
material: Optional[Material] = None,
metadata: Optional[Dict[str, Any]] = None,
):
"""
Expand All @@ -116,6 +118,7 @@ def __init__(
:param loc: location of the root object (default: None, interpreted as identity transformation)
:param name: unique name of the root object (default: None, resulting in an UUID being generated)
:param color: color of the added object (default: None)
:param material: material of the added object (default: None)
:param metadata: a store for user-defined metadata (default: None)
:return: An Assembly object.
Expand All @@ -135,6 +138,7 @@ def __init__(
self.loc = loc if loc else Location()
self.name = name if name else str(uuid())
self.color = color if color else None
self.material = material if material else None
self.metadata = metadata if metadata else {}
self.parent = None

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

rv = self.__class__(self.obj, self.loc, self.name, self.color, self.metadata)
rv = self.__class__(
self.obj, self.loc, self.name, self.color, self.material, self.metadata
)

for ch in self.children:
ch_copy = ch._copy()
Expand All @@ -172,6 +178,7 @@ def add(
loc: Optional[Location] = None,
name: Optional[str] = None,
color: Optional[Color] = None,
material: Optional[Material] = None,
) -> "Assembly":
"""
Add a subassembly to the current assembly.
Expand All @@ -183,6 +190,8 @@ def add(
the subassembly being used)
:param color: color of the added object (default: None, resulting in the color stored in the
subassembly being used)
:param material: material of the added object (default: None, resulting in the material stored in the
subassembly being used)
"""
...

Expand All @@ -193,6 +202,7 @@ def add(
loc: Optional[Location] = None,
name: Optional[str] = None,
color: Optional[Color] = None,
material: Optional[Material] = None,
metadata: Optional[Dict[str, Any]] = None,
) -> "Assembly":
"""
Expand All @@ -204,6 +214,7 @@ def add(
:param name: unique name of the root object (default: None, resulting in an UUID being
generated)
:param color: color of the added object (default: None)
:param material: material of the added object (default: None)
:param metadata: a store for user-defined metadata (default: None)
"""
...
Expand All @@ -225,6 +236,9 @@ def add(self, arg, **kwargs):
subassy.loc = kwargs["loc"] if kwargs.get("loc") else arg.loc
subassy.name = kwargs["name"] if kwargs.get("name") else arg.name
subassy.color = kwargs["color"] if kwargs.get("color") else arg.color
subassy.material = (
kwargs["material"] if kwargs.get("material") else arg.material
)
subassy.metadata = (
kwargs["metadata"] if kwargs.get("metadata") else arg.metadata
)
Expand Down Expand Up @@ -658,22 +672,31 @@ def __iter__(
loc: Optional[Location] = None,
name: Optional[str] = None,
color: Optional[Color] = None,
) -> Iterator[Tuple[Shape, str, Location, Optional[Color]]]:
material: Optional[Material] = None,
) -> Iterator[AssemblyElement]:
"""
Assembly iterator yielding shapes, names, locations and colors.
Assembly iterator yielding shapes, names, locations, colors and materials.
"""

name = f"{name}/{self.name}" if name else self.name
loc = loc * self.loc if loc else self.loc
color = self.color if self.color else color
material = self.material if self.material else material

if self.obj:
yield self.obj if isinstance(self.obj, Shape) else Compound.makeCompound(
s for s in self.obj.vals() if isinstance(s, Shape)
), name, loc, color
shape = (
self.obj
if isinstance(self.obj, Shape)
else Compound.makeCompound(
s for s in self.obj.vals() if isinstance(s, Shape)
)
)
yield AssemblyElement(
shape=shape, name=name, location=loc, color=color, material=material
)

for ch in self.children:
yield from ch.__iter__(loc, name, color)
yield from ch.__iter__(loc, name, color, material)

def toCompound(self) -> Compound:
"""
Expand Down
6 changes: 3 additions & 3 deletions cadquery/cq_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from json import dumps

from cadquery import exporters, Assembly, Compound, Color, Sketch
from cadquery import exporters, Assembly, Compound, Sketch
from cadquery import cqgi
from cadquery.occ_impl.assembly import toJSON
from cadquery.occ_impl.jupyter_tools import DEFAULT_COLOR
Expand Down Expand Up @@ -299,9 +299,9 @@ def run(self):
if isinstance(shape, Assembly):
assy = shape
elif isinstance(shape, Sketch):
assy = Assembly(shape._faces, color=Color(*DEFAULT_COLOR))
assy = Assembly(shape._faces, color=DEFAULT_COLOR)
else:
assy = Assembly(shape, color=Color(*DEFAULT_COLOR))
assy = Assembly(shape, color=DEFAULT_COLOR)
else:
raise result.exception

Expand Down
Loading