Skip to content

Commit 0c78697

Browse files
Make assy subshape mappings bidirectional
1 parent b4edf2f commit 0c78697

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

cadquery/assembly.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from .occ_impl.importers.assembly import importStep as _importStep
3939

4040
from .selectors import _expression_grammar as _selector_grammar
41-
from .utils import deprecate
41+
from .utils import deprecate, BiDict
4242

4343
# type definitions
4444
AssemblyObjects = Union[Shape, Workplane, None]
@@ -97,9 +97,9 @@ class Assembly(object):
9797
constraints: List[Constraint]
9898

9999
# Allows metadata to be stored for exports
100-
_subshape_names: dict[Shape, str]
101-
_subshape_colors: dict[Shape, Color]
102-
_subshape_layers: dict[Shape, str]
100+
_subshape_names: BiDict[Shape, str]
101+
_subshape_colors: BiDict[Shape, Color]
102+
_subshape_layers: BiDict[Shape, str]
103103

104104
_solve_result: Optional[Dict[str, Any]]
105105

@@ -146,9 +146,9 @@ def __init__(
146146

147147
self._solve_result = None
148148

149-
self._subshape_names = {}
150-
self._subshape_colors = {}
151-
self._subshape_layers = {}
149+
self._subshape_names = BiDict()
150+
self._subshape_colors = BiDict()
151+
self._subshape_layers = BiDict()
152152

153153
def _copy(self) -> "Assembly":
154154
"""
@@ -157,9 +157,9 @@ def _copy(self) -> "Assembly":
157157

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

160-
rv._subshape_colors = dict(self._subshape_colors)
161-
rv._subshape_names = dict(self._subshape_names)
162-
rv._subshape_layers = dict(self._subshape_layers)
160+
rv._subshape_colors = BiDict(self._subshape_colors)
161+
rv._subshape_names = BiDict(self._subshape_names)
162+
rv._subshape_layers = BiDict(self._subshape_layers)
163163

164164
for ch in self.children:
165165
ch_copy = ch._copy()
@@ -768,9 +768,19 @@ def addSubshape(
768768
def __getitem__(self, name: str) -> "Assembly":
769769
"""
770770
[] based access to children.
771+
771772
"""
772773

773-
return self.objects[name]
774+
if name in self.objects:
775+
return self.objects[name]
776+
elif name[0] in self.objects:
777+
rv = self.objects[name[0]]
778+
779+
if name[1] in rv._subshape_names.inv:
780+
rv = self._subshape_names.inv[name[1]]
781+
return rv[0] if len(rv) == 1 else compound(rv)
782+
783+
raise KeyError
774784

775785
def _ipython_key_completions_(self) -> List[str]:
776786
"""
@@ -790,15 +800,22 @@ def __getattr__(self, name: str) -> "Assembly":
790800

791801
if name in self.objects:
792802
return self.objects[name]
803+
elif name in self._subshape_names.inv:
804+
rv = self._subshape_names.inv[name]
805+
return rv[0] if len(rv) == 1 else compound(rv)
793806

794-
raise AttributeError
807+
raise AttributeError(f"{name} is not an attribute of {self}")
795808

796809
def __dir__(self):
797810
"""
798811
Modified __dir__ for autocompletion.
799812
"""
800813

801-
return list(self.__dict__) + list(ch.name for ch in self.children)
814+
return (
815+
list(self.__dict__)
816+
+ list(ch.name for ch in self.children)
817+
+ list(self._subshape_names.inverse.keys())
818+
)
802819

803820
def __getstate__(self):
804821
"""

0 commit comments

Comments
 (0)