Skip to content

Commit 02a087d

Browse files
Fix new assembly subshape referencing functionality
1 parent 0c78697 commit 02a087d

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
lines changed

cadquery/assembly.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
PATH_DELIM = "/"
4848

4949
# entity selector grammar definition
50+
51+
5052
def _define_grammar():
5153

5254
from pyparsing import (
@@ -765,10 +767,10 @@ def addSubshape(
765767

766768
return self
767769

768-
def __getitem__(self, name: str) -> "Assembly":
770+
def __getitem__(self, name: str) -> Union["Assembly", Shape]:
769771
"""
770772
[] based access to children.
771-
773+
772774
"""
773775

774776
if name in self.objects:
@@ -777,7 +779,7 @@ def __getitem__(self, name: str) -> "Assembly":
777779
rv = self.objects[name[0]]
778780

779781
if name[1] in rv._subshape_names.inv:
780-
rv = self._subshape_names.inv[name[1]]
782+
rv = compound(self._subshape_names.inv[name[1]])
781783
return rv[0] if len(rv) == 1 else compound(rv)
782784

783785
raise KeyError
@@ -793,7 +795,7 @@ def __contains__(self, name: str) -> bool:
793795

794796
return name in self.objects
795797

796-
def __getattr__(self, name: str) -> "Assembly":
798+
def __getattr__(self, name: str) -> Union["Assembly", Shape]:
797799
"""
798800
. based access to children.
799801
"""

cadquery/occ_impl/assembly.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
from .shapes import Shape, Solid, Compound
5353
from .exporters.vtk import toString
5454
from ..cq import Workplane
55+
from ..utils import BiDict
5556

5657
# type definitions
5758
AssemblyObjects = Union[Shape, Workplane, None]
@@ -210,15 +211,15 @@ def children(self) -> Iterable["AssemblyProtocol"]:
210211
...
211212

212213
@property
213-
def _subshape_names(self) -> Dict[Shape, str]:
214+
def _subshape_names(self) -> BiDict[Shape, str]:
214215
...
215216

216217
@property
217-
def _subshape_colors(self) -> Dict[Shape, Color]:
218+
def _subshape_colors(self) -> BiDict[Shape, Color]:
218219
...
219220

220221
@property
221-
def _subshape_layers(self) -> Dict[Shape, str]:
222+
def _subshape_layers(self) -> BiDict[Shape, str]:
222223
...
223224

224225
@overload
@@ -276,7 +277,7 @@ def __iter__(
276277
) -> Iterator[Tuple[Shape, str, Location, Optional[Color]]]:
277278
...
278279

279-
def __getitem__(self, name: str) -> Self:
280+
def __getitem__(self, name: str) -> Self | Shape:
280281
...
281282

282283
def __contains__(self, name: str) -> bool:

cadquery/occ_impl/importers/assembly.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from OCP.XCAFDoc import XCAFDoc_ColorSurf, XCAFDoc_DocumentTool, XCAFDoc_GraphNode
1111
from OCP.Interface import Interface_Static
1212

13+
from typing import cast
14+
1315
from ..assembly import AssemblyProtocol, Color
1416
from ..geom import Location
1517
from ..shapes import Shape
@@ -94,7 +96,7 @@ def _process_label(lbl: TDF_Label, parent: AssemblyProtocol):
9496
parent.add(tmp)
9597

9698
# change the current assy to handle subshape data
97-
current = parent[ref_name]
99+
current = cast(AssemblyProtocol, parent[ref_name])
98100

99101
# iterate over subshape and handle names, layers and colors
100102
subshape_labels = TDF_LabelSequence()
@@ -206,7 +208,7 @@ def _process_label(lbl: TDF_Label, parent: AssemblyProtocol):
206208
# extras on successive round-trips. exportStepMeta does not add the extra top-level
207209
# node and so does not exhibit this behavior.
208210
if assy.name in imported_assy:
209-
imported_assy = imported_assy[assy.name]
211+
imported_assy = cast(AssemblyProtocol, imported_assy[assy.name])
210212

211213
# Copy all of the children over to the main assembly object
212214
for child in imported_assy.children:

cadquery/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class BiDict(UserDict[K, V]):
9595
Bi-directional dictionary.
9696
"""
9797

98-
_inv = dict[V, list[K]]
98+
_inv: dict[V, list[K]]
9999

100100
def __init__(self, *args, **kwargs):
101101

0 commit comments

Comments
 (0)