Add connector forwarding API
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
## Class Definition
|
||||
|
||||
```python
|
||||
class Connector(connector_id: str, geometry_ref: GeometryRef, name: Optional[str] = None)
|
||||
class Connector(connector_id: str, geometry_ref: Optional[GeometryRef] = None, name: Optional[str] = None, anchor: Optional[ConnectorAnchor] = None)
|
||||
```
|
||||
|
||||
*Source: product.py*
|
||||
@@ -14,7 +14,6 @@ class Connector(connector_id: str, geometry_ref: GeometryRef, name: Optional[str
|
||||
|
||||
## Description
|
||||
|
||||
Semantic datum frame anchored to a geometry sub-element (Face/Edge/Vertex).
|
||||
Semantic datum frame anchored by geometry, placement, or forwarding.
|
||||
|
||||
The connector wraps a QL-selected sub-shape. The Placement is derived
|
||||
from the geometry at solve/translate time, not stored directly.
|
||||
Geometry connectors derive placement from a selected BREP sub-shape. Placement connectors store an explicit local datum frame. Forwarded connectors expose a component connector as an assembly-level public interface.
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
# ConnectorAnchor
|
||||
|
||||
## Class Definition
|
||||
|
||||
```python
|
||||
class ConnectorAnchor(anchor_kind: str, geometry_ref: Optional[GeometryRef] = None, placement: Optional[Placement] = None, source_component_id: Optional[str] = None, source_connector_id: Optional[str] = None, offset: Optional[Placement] = None)
|
||||
```
|
||||
|
||||
*Source: product.py*
|
||||
|
||||
## Import Surface
|
||||
|
||||
- top-level: `from simplecadapi import ConnectorAnchor`
|
||||
|
||||
## Description
|
||||
|
||||
Serializable source for a connector datum frame.
|
||||
|
||||
Supported `anchor_kind` values are `geometry`, `placement`, and `forwarded`.
|
||||
@@ -34,6 +34,7 @@ This index includes generated docs for the public SimpleCAD API surface, includi
|
||||
- [make_line_redge](make_line_redge.md) *(from operations.py)* `top-level`
|
||||
- [make_material_rmaterial](make_material_rmaterial.md) *(from operations.py)* `top-level`
|
||||
- [make_part_rpart](make_part_rpart.md) *(from operations.py)* `top-level`
|
||||
- [make_placement_connector_rconnector](make_placement_connector_rconnector.md) *(from operations.py)* `top-level`
|
||||
- [make_placement_rplacement](make_placement_rplacement.md) *(from operations.py)* `top-level`
|
||||
- [make_point_rvertex](make_point_rvertex.md) *(from operations.py)* `top-level`
|
||||
- [make_polyline_rwire](make_polyline_rwire.md) *(from operations.py)* `top-level`
|
||||
@@ -143,6 +144,7 @@ This index includes generated docs for the public SimpleCAD API surface, includi
|
||||
- [Assembly](Assembly.md) *(from product.py)* `top-level`
|
||||
- [Component](Component.md) *(from product.py)* `top-level`
|
||||
- [Connector](Connector.md) *(from product.py)* `top-level`
|
||||
- [ConnectorAnchor](ConnectorAnchor.md) *(from product.py)* `top-level`
|
||||
- [ConnectorRef](ConnectorRef.md) *(from product.py)* `top-level`
|
||||
- [Constraint](Constraint.md) *(from product.py)* `top-level`
|
||||
- [ConstraintReport](ConstraintReport.md) *(from product.py)* `top-level`
|
||||
@@ -192,6 +194,7 @@ This index includes generated docs for the public SimpleCAD API surface, includi
|
||||
- [constrain_symmetric_rsketch](constrain_symmetric_rsketch.md) *(from operations.py)* `top-level`
|
||||
- [constrain_tangent_rsketch](constrain_tangent_rsketch.md) *(from operations.py)* `top-level`
|
||||
- [constrain_vertical_rsketch](constrain_vertical_rsketch.md) *(from operations.py)* `top-level`
|
||||
- [forward_connector_rassembly](forward_connector_rassembly.md) *(from operations.py)* `top-level`
|
||||
- [geo](geo.md) *(from ql.py)* `submodule:ql`
|
||||
- [get_sketch_entity_rsketchref](get_sketch_entity_rsketchref.md) *(from operations.py)* `top-level`
|
||||
- [get_sketch_point_rsketchref](get_sketch_point_rsketchref.md) *(from operations.py)* `top-level`
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
# forward_connector_rassembly
|
||||
|
||||
## API Definition
|
||||
|
||||
```python
|
||||
def forward_connector_rassembly(assembly: Assembly, connector_id: str, source_component_id: str, source_connector_id: str, name: Optional[str] = None, offset: Optional[Placement] = None) -> Assembly
|
||||
```
|
||||
|
||||
*Source: operations.py*
|
||||
|
||||
## Import Surface
|
||||
|
||||
- top-level: `from simplecadapi import forward_connector_rassembly`
|
||||
|
||||
## Description
|
||||
|
||||
Expose an internal component connector as an assembly-level connector.
|
||||
|
||||
The forwarded connector resolves to `source_component.placement * source_connector.placement`, followed by the optional `offset` placement. Parent assemblies can then constrain to the subassembly's public connector without knowing its private component structure.
|
||||
@@ -0,0 +1,19 @@
|
||||
# make_placement_connector_rconnector
|
||||
|
||||
## API Definition
|
||||
|
||||
```python
|
||||
def make_placement_connector_rconnector(connector_id: str, placement: Placement, name: Optional[str] = None) -> Connector
|
||||
```
|
||||
|
||||
*Source: operations.py*
|
||||
|
||||
## Import Surface
|
||||
|
||||
- top-level: `from simplecadapi import make_placement_connector_rconnector`
|
||||
|
||||
## Description
|
||||
|
||||
Create a connector anchored to an explicit local placement frame.
|
||||
|
||||
Use this when a datum should be defined by a stable coordinate frame instead of a selected BREP face, edge, or vertex.
|
||||
@@ -59,8 +59,10 @@ from .operations import (
|
||||
make_face_connector_rconnector,
|
||||
make_edge_connector_rconnector,
|
||||
make_vertex_connector_rconnector,
|
||||
make_placement_connector_rconnector,
|
||||
add_connector_rpart,
|
||||
add_connector_rassembly,
|
||||
forward_connector_rassembly,
|
||||
make_connector_ref_rconnectorref,
|
||||
make_scalar_limit_rscalarlimit,
|
||||
ground_component_rassembly,
|
||||
@@ -171,6 +173,7 @@ from .product import (
|
||||
Assembly,
|
||||
Component,
|
||||
Connector,
|
||||
ConnectorAnchor,
|
||||
ConnectorRef,
|
||||
Constraint,
|
||||
ConstraintReport,
|
||||
@@ -302,8 +305,10 @@ __all__ = [
|
||||
"make_face_connector_rconnector",
|
||||
"make_edge_connector_rconnector",
|
||||
"make_vertex_connector_rconnector",
|
||||
"make_placement_connector_rconnector",
|
||||
"add_connector_rpart",
|
||||
"add_connector_rassembly",
|
||||
"forward_connector_rassembly",
|
||||
"make_connector_ref_rconnectorref",
|
||||
"make_scalar_limit_rscalarlimit",
|
||||
"ground_component_rassembly",
|
||||
@@ -426,6 +431,7 @@ __all__ = [
|
||||
"Assembly",
|
||||
"Component",
|
||||
"Connector",
|
||||
"ConnectorAnchor",
|
||||
"ConnectorRef",
|
||||
"Constraint",
|
||||
"ConstraintReport",
|
||||
|
||||
@@ -39,6 +39,7 @@ from .product import (
|
||||
Assembly,
|
||||
Component,
|
||||
Connector,
|
||||
ConnectorAnchor,
|
||||
ConnectorRef,
|
||||
Constraint,
|
||||
ConstraintReport,
|
||||
@@ -173,8 +174,10 @@ _OP_MAKE_COMPOUND_FROM_ASSEMBLY_RCOMPOUND = "make_compound_from_assembly_rcompou
|
||||
_OP_MAKE_FACE_CONNECTOR_RCONNECTOR = "make_face_connector_rconnector"
|
||||
_OP_MAKE_EDGE_CONNECTOR_RCONNECTOR = "make_edge_connector_rconnector"
|
||||
_OP_MAKE_VERTEX_CONNECTOR_RCONNECTOR = "make_vertex_connector_rconnector"
|
||||
_OP_MAKE_PLACEMENT_CONNECTOR_RCONNECTOR = "make_placement_connector_rconnector"
|
||||
_OP_MAKE_ADD_CONNECTOR_RPART = "make_add_connector_rpart"
|
||||
_OP_MAKE_ADD_CONNECTOR_RASSEMBLY = "make_add_connector_rassembly"
|
||||
_OP_MAKE_FORWARD_CONNECTOR_RASSEMBLY = "make_forward_connector_rassembly"
|
||||
_OP_MAKE_CONNECTOR_REF_RCONNECTORREF = "make_connector_ref_rconnectorref"
|
||||
_OP_MAKE_SCALAR_LIMIT_RSCALARLIMIT = "make_scalar_limit_rscalarlimit"
|
||||
_OP_MAKE_GROUND_COMPONENT_RASSEMBLY = "make_ground_component_rassembly"
|
||||
@@ -5082,11 +5085,7 @@ def _make_geometry_backed_connector(
|
||||
connector = Connector(connector_id, geometry_ref, name=name)
|
||||
record_operation_if_active(
|
||||
op,
|
||||
{
|
||||
"connector_id": connector.connector_id,
|
||||
"geometry_ref": geometry_ref.to_dict(),
|
||||
"name": connector.name,
|
||||
},
|
||||
_connector_params(connector),
|
||||
outputs=connector,
|
||||
input_shapes=[shape],
|
||||
context=_current_context_metadata(),
|
||||
@@ -5208,6 +5207,46 @@ def make_vertex_connector_rconnector(
|
||||
)
|
||||
|
||||
|
||||
def make_placement_connector_rconnector(
|
||||
connector_id: str,
|
||||
placement: Placement,
|
||||
name: Optional[str] = None,
|
||||
) -> Connector:
|
||||
"""Create a connector anchored to an explicit local placement frame."""
|
||||
|
||||
try:
|
||||
if not isinstance(placement, Placement):
|
||||
raise TypeError("placement must be a Placement")
|
||||
anchor = ConnectorAnchor("placement", placement=placement)
|
||||
connector = Connector(connector_id, None, name=name, anchor=anchor)
|
||||
record_operation_if_active(
|
||||
_OP_MAKE_PLACEMENT_CONNECTOR_RCONNECTOR,
|
||||
{
|
||||
"connector_id": connector.connector_id,
|
||||
"placement": placement.to_dict(),
|
||||
"name": connector.name,
|
||||
},
|
||||
outputs=connector,
|
||||
input_shapes=[placement],
|
||||
context=_current_context_metadata(),
|
||||
)
|
||||
return connector
|
||||
except Exception as e:
|
||||
_wrap_public_api_error(
|
||||
operation="make_placement_connector_rconnector",
|
||||
what_happened="Failed to create the placement connector.",
|
||||
possible_causes=[
|
||||
"The connector_id is empty or malformed.",
|
||||
"The placement input is not a Placement.",
|
||||
],
|
||||
how_to_fix=[
|
||||
"Create placements with make_placement_rplacement.",
|
||||
"Use a stable connector_id such as 'bearing_axis'.",
|
||||
],
|
||||
error=e,
|
||||
)
|
||||
|
||||
|
||||
def add_connector_rpart(part: Part, connector: Connector) -> Part:
|
||||
"""Attach a connector datum frame to a Part definition."""
|
||||
|
||||
@@ -5278,6 +5317,66 @@ def add_connector_rassembly(assembly: Assembly, connector: Connector) -> Assembl
|
||||
)
|
||||
|
||||
|
||||
def forward_connector_rassembly(
|
||||
assembly: Assembly,
|
||||
connector_id: str,
|
||||
source_component_id: str,
|
||||
source_connector_id: str,
|
||||
name: Optional[str] = None,
|
||||
offset: Optional[Placement] = None,
|
||||
) -> Assembly:
|
||||
"""Expose an internal component connector as an assembly-level connector."""
|
||||
|
||||
try:
|
||||
if not isinstance(assembly, Assembly):
|
||||
raise TypeError("assembly must be an Assembly")
|
||||
if offset is not None and not isinstance(offset, Placement):
|
||||
raise TypeError("offset must be a Placement")
|
||||
anchor = ConnectorAnchor(
|
||||
"forwarded",
|
||||
source_component_id=source_component_id,
|
||||
source_connector_id=source_connector_id,
|
||||
offset=offset,
|
||||
)
|
||||
connector = Connector(connector_id, None, name=name, anchor=anchor)
|
||||
result = assembly.with_connector(connector)
|
||||
record_operation_if_active(
|
||||
_OP_MAKE_FORWARD_CONNECTOR_RASSEMBLY,
|
||||
{
|
||||
"assembly_id": assembly.assembly_id,
|
||||
"connector_id": connector.connector_id,
|
||||
"source_component_id": anchor.source_component_id,
|
||||
"source_connector_id": anchor.source_connector_id,
|
||||
"name": connector.name,
|
||||
"offset": offset.to_dict() if offset is not None else None,
|
||||
},
|
||||
outputs=result,
|
||||
input_shapes=[assembly] + ([offset] if offset is not None else []),
|
||||
semantic_delta=_semantic_modified(
|
||||
"Assembly", assembly.assembly_id, {"connector": connector.to_dict()}
|
||||
),
|
||||
context=_current_context_metadata(),
|
||||
)
|
||||
return result
|
||||
except Exception as e:
|
||||
_wrap_public_api_error(
|
||||
operation="forward_connector_rassembly",
|
||||
what_happened="Failed to forward the assembly connector.",
|
||||
possible_causes=[
|
||||
"The assembly input is not an Assembly.",
|
||||
"The connector_id is duplicated in the Assembly.",
|
||||
"The source component or source connector does not exist.",
|
||||
"The optional offset is not a Placement.",
|
||||
],
|
||||
how_to_fix=[
|
||||
"Add the source component before forwarding its connector.",
|
||||
"Use a connector_id unique within the Assembly.",
|
||||
"Create offsets with make_placement_rplacement when needed.",
|
||||
],
|
||||
error=e,
|
||||
)
|
||||
|
||||
|
||||
def make_connector_ref_rconnectorref(
|
||||
component_id: str, connector_id: str
|
||||
) -> ConnectorRef:
|
||||
|
||||
+213
-24
@@ -266,16 +266,85 @@ class GeometryRef(SemanticValueMixin):
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Connector(SemanticValueMixin):
|
||||
"""Semantic datum frame anchored to a geometry sub-element (Face/Edge/Vertex).
|
||||
class ConnectorAnchor(SemanticValueMixin):
|
||||
"""Serializable source for a connector datum frame."""
|
||||
|
||||
The connector wraps a QL-selected sub-shape. The Placement is derived
|
||||
from the geometry at solve/translate time, not stored directly.
|
||||
"""
|
||||
anchor_kind: str
|
||||
geometry_ref: Optional[GeometryRef] = None
|
||||
placement: Optional[Placement] = None
|
||||
source_component_id: Optional[str] = None
|
||||
source_connector_id: Optional[str] = None
|
||||
offset: Optional[Placement] = None
|
||||
_metadata: Dict[str, Any] = field(default_factory=dict, compare=False, repr=False)
|
||||
_runtime: Dict[str, Any] = field(default_factory=dict, compare=False, repr=False)
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
kind = str(self.anchor_kind).strip().lower()
|
||||
if kind not in {"geometry", "placement", "forwarded"}:
|
||||
raise ValueError("anchor_kind must be geometry, placement, or forwarded")
|
||||
object.__setattr__(self, "anchor_kind", kind)
|
||||
if kind == "geometry":
|
||||
if not isinstance(self.geometry_ref, GeometryRef):
|
||||
raise TypeError("geometry anchors require geometry_ref")
|
||||
if self.placement is not None:
|
||||
raise ValueError("geometry anchors do not accept placement")
|
||||
if self.source_component_id is not None or self.source_connector_id is not None:
|
||||
raise ValueError("geometry anchors do not accept forwarded source ids")
|
||||
if self.offset is not None:
|
||||
raise ValueError("geometry anchors do not accept offset")
|
||||
return
|
||||
if kind == "placement":
|
||||
if not isinstance(self.placement, Placement):
|
||||
raise TypeError("placement anchors require placement")
|
||||
if self.geometry_ref is not None:
|
||||
raise ValueError("placement anchors do not accept geometry_ref")
|
||||
if self.source_component_id is not None or self.source_connector_id is not None:
|
||||
raise ValueError("placement anchors do not accept forwarded source ids")
|
||||
if self.offset is not None:
|
||||
raise ValueError("placement anchors do not accept offset")
|
||||
return
|
||||
if self.geometry_ref is not None or self.placement is not None:
|
||||
raise ValueError("forwarded anchors do not accept geometry_ref or placement")
|
||||
object.__setattr__(
|
||||
self,
|
||||
"source_component_id",
|
||||
_validate_identifier(
|
||||
self.source_component_id or "",
|
||||
field_name="source_component_id",
|
||||
),
|
||||
)
|
||||
object.__setattr__(
|
||||
self,
|
||||
"source_connector_id",
|
||||
_validate_identifier(
|
||||
self.source_connector_id or "",
|
||||
field_name="source_connector_id",
|
||||
),
|
||||
)
|
||||
if self.offset is not None and not isinstance(self.offset, Placement):
|
||||
raise TypeError("offset must be a Placement")
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
payload: Dict[str, Any] = {"anchor_kind": self.anchor_kind}
|
||||
if self.anchor_kind == "geometry":
|
||||
payload["geometry_ref"] = cast(GeometryRef, self.geometry_ref).to_dict()
|
||||
elif self.anchor_kind == "placement":
|
||||
payload["placement"] = cast(Placement, self.placement).to_dict()
|
||||
else:
|
||||
payload["source_component_id"] = self.source_component_id
|
||||
payload["source_connector_id"] = self.source_connector_id
|
||||
payload["offset"] = self.offset.to_dict() if self.offset is not None else None
|
||||
return payload
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Connector(SemanticValueMixin):
|
||||
"""Semantic datum frame anchored by geometry, placement, or forwarding."""
|
||||
|
||||
connector_id: str
|
||||
geometry_ref: GeometryRef
|
||||
geometry_ref: Optional[GeometryRef] = None
|
||||
name: Optional[str] = None
|
||||
anchor: Optional[ConnectorAnchor] = None
|
||||
_metadata: Dict[str, Any] = field(default_factory=dict, compare=False, repr=False)
|
||||
_runtime: Dict[str, Any] = field(default_factory=dict, compare=False, repr=False)
|
||||
|
||||
@@ -285,25 +354,43 @@ class Connector(SemanticValueMixin):
|
||||
"connector_id",
|
||||
_validate_identifier(self.connector_id, field_name="connector_id"),
|
||||
)
|
||||
if not isinstance(self.geometry_ref, GeometryRef):
|
||||
raise TypeError("geometry_ref must be a GeometryRef")
|
||||
anchor = self.anchor
|
||||
if anchor is None:
|
||||
if not isinstance(self.geometry_ref, GeometryRef):
|
||||
raise TypeError("geometry_ref must be a GeometryRef when anchor is omitted")
|
||||
anchor = ConnectorAnchor("geometry", geometry_ref=self.geometry_ref)
|
||||
object.__setattr__(self, "anchor", anchor)
|
||||
elif not isinstance(anchor, ConnectorAnchor):
|
||||
raise TypeError("anchor must be a ConnectorAnchor")
|
||||
if anchor.anchor_kind == "geometry":
|
||||
object.__setattr__(self, "geometry_ref", anchor.geometry_ref)
|
||||
elif self.geometry_ref is not None:
|
||||
raise ValueError("geometry_ref is only valid for geometry connectors")
|
||||
if self.name is not None:
|
||||
name = str(self.name).strip()
|
||||
if not name:
|
||||
raise ValueError("name must not be empty when provided")
|
||||
object.__setattr__(self, "name", name)
|
||||
|
||||
@property
|
||||
def anchor_kind(self) -> str:
|
||||
return cast(ConnectorAnchor, self.anchor).anchor_kind
|
||||
|
||||
@property
|
||||
def placement(self) -> Placement:
|
||||
"""Lazily-computed Placement derived from the geometry_ref."""
|
||||
return _placement_from_geometry_ref(self.geometry_ref)
|
||||
"""Lazily-computed local Placement derived from the connector anchor."""
|
||||
owner = self._get_runtime("owner_assembly")
|
||||
return resolve_connector_placement(self, owner_assembly=owner)
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
return {
|
||||
payload: Dict[str, Any] = {
|
||||
"connector_id": self.connector_id,
|
||||
"name": self.name,
|
||||
"geometry_ref": self.geometry_ref.to_dict(),
|
||||
"anchor": cast(ConnectorAnchor, self.anchor).to_dict(),
|
||||
}
|
||||
if self.geometry_ref is not None:
|
||||
payload["geometry_ref"] = self.geometry_ref.to_dict()
|
||||
return payload
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -579,6 +666,7 @@ class Part(SemanticValueMixin):
|
||||
if self.material is not None and not isinstance(self.material, Material):
|
||||
raise TypeError("material must be a Material")
|
||||
connectors = _validate_connectors(self.connectors)
|
||||
_validate_part_connector_anchors(connectors)
|
||||
object.__setattr__(self, "connectors", connectors)
|
||||
|
||||
def with_material(self, material: Material) -> "Part":
|
||||
@@ -690,11 +778,20 @@ class Assembly(SemanticValueMixin):
|
||||
if not name:
|
||||
raise ValueError("name must not be empty when provided")
|
||||
object.__setattr__(self, "name", name)
|
||||
ids = [component.component_id for component in self.components]
|
||||
components = tuple(self.components or ())
|
||||
for component in components:
|
||||
if not isinstance(component, Component):
|
||||
raise TypeError("components must contain Component values")
|
||||
object.__setattr__(self, "components", components)
|
||||
ids = [component.component_id for component in components]
|
||||
duplicates = sorted({component_id for component_id in ids if ids.count(component_id) > 1})
|
||||
if duplicates:
|
||||
raise ValueError("duplicate component_id in assembly: " + ", ".join(duplicates))
|
||||
object.__setattr__(self, "connectors", _validate_connectors(self.connectors))
|
||||
connectors = _validate_connectors(self.connectors)
|
||||
object.__setattr__(self, "connectors", connectors)
|
||||
for connector in connectors:
|
||||
connector._set_runtime("owner_assembly", self)
|
||||
_validate_assembly_connector_anchors(self)
|
||||
constraints = _validate_constraints(self.constraints)
|
||||
object.__setattr__(self, "constraints", constraints)
|
||||
for constraint in constraints:
|
||||
@@ -777,6 +874,7 @@ class Assembly(SemanticValueMixin):
|
||||
target = _validate_identifier(connector_id, field_name="connector_id")
|
||||
for connector in self.connectors:
|
||||
if connector.connector_id == target:
|
||||
connector._set_runtime("owner_assembly", self)
|
||||
return connector
|
||||
raise KeyError(f"assembly has no connector_id '{target}'")
|
||||
|
||||
@@ -1143,6 +1241,35 @@ def _validate_connectors(connectors: Iterable[Connector]) -> Tuple[Connector, ..
|
||||
return result
|
||||
|
||||
|
||||
def _validate_part_connector_anchors(connectors: Iterable[Connector]) -> None:
|
||||
for connector in connectors:
|
||||
if connector.anchor_kind == "forwarded":
|
||||
raise ValueError("forwarded connectors can only be added to assemblies")
|
||||
|
||||
|
||||
def _validate_assembly_connector_anchors(assembly: Assembly) -> None:
|
||||
for connector in assembly.connectors:
|
||||
if connector.anchor_kind != "forwarded":
|
||||
continue
|
||||
anchor = cast(ConnectorAnchor, connector.anchor)
|
||||
try:
|
||||
source_component = assembly.get_component(cast(str, anchor.source_component_id))
|
||||
except KeyError as exc:
|
||||
raise ValueError(
|
||||
f"forwarded connector '{connector.connector_id}' references missing "
|
||||
f"component '{anchor.source_component_id}'"
|
||||
) from exc
|
||||
try:
|
||||
source_component.item.get_connector(cast(str, anchor.source_connector_id))
|
||||
except KeyError as exc:
|
||||
raise ValueError(
|
||||
f"forwarded connector '{connector.connector_id}' references missing "
|
||||
f"connector '{anchor.source_connector_id}' on component "
|
||||
f"'{anchor.source_component_id}'"
|
||||
) from exc
|
||||
resolve_connector_placement(connector, owner_assembly=assembly)
|
||||
|
||||
|
||||
def _validate_constraints(constraints: Iterable[Constraint]) -> Tuple[Constraint, ...]:
|
||||
result = tuple(constraints or ())
|
||||
for constraint in result:
|
||||
@@ -1168,10 +1295,70 @@ def _resolve_connector(assembly: Assembly, connector_ref: ConnectorRef) -> Conne
|
||||
return item.get_connector(connector_ref.connector_id)
|
||||
|
||||
|
||||
def _connector_world_frame(assembly: Assembly, connector_ref: ConnectorRef) -> Placement:
|
||||
def resolve_connector_placement(
|
||||
connector: Connector,
|
||||
owner_assembly: Optional[Assembly] = None,
|
||||
_seen: Optional[set] = None,
|
||||
) -> Placement:
|
||||
if not isinstance(connector, Connector):
|
||||
raise TypeError("connector must be a Connector")
|
||||
anchor = cast(ConnectorAnchor, connector.anchor)
|
||||
if anchor.anchor_kind == "geometry":
|
||||
return _placement_from_geometry_ref(cast(GeometryRef, anchor.geometry_ref))
|
||||
if anchor.anchor_kind == "placement":
|
||||
return cast(Placement, anchor.placement)
|
||||
if owner_assembly is None:
|
||||
raise ValueError(
|
||||
f"forwarded connector '{connector.connector_id}' requires an owner assembly"
|
||||
)
|
||||
seen = set(_seen or set())
|
||||
key = (id(owner_assembly), connector.connector_id)
|
||||
if key in seen:
|
||||
raise ValueError(f"forwarded connector cycle detected at '{connector.connector_id}'")
|
||||
seen.add(key)
|
||||
source_component_id = cast(str, anchor.source_component_id)
|
||||
source_connector_id = cast(str, anchor.source_connector_id)
|
||||
try:
|
||||
source_component = owner_assembly.get_component(source_component_id)
|
||||
except KeyError as exc:
|
||||
raise ValueError(
|
||||
f"forwarded connector '{connector.connector_id}' references missing "
|
||||
f"component '{source_component_id}'"
|
||||
) from exc
|
||||
try:
|
||||
source_connector = source_component.item.get_connector(source_connector_id)
|
||||
except KeyError as exc:
|
||||
raise ValueError(
|
||||
f"forwarded connector '{connector.connector_id}' references missing "
|
||||
f"connector '{source_connector_id}' on component '{source_component_id}'"
|
||||
) from exc
|
||||
source_owner = source_component.item if isinstance(source_component.item, Assembly) else None
|
||||
source_frame = source_component.placement.compose(
|
||||
resolve_connector_placement(
|
||||
source_connector,
|
||||
owner_assembly=source_owner,
|
||||
_seen=seen,
|
||||
)
|
||||
)
|
||||
if anchor.offset is not None:
|
||||
source_frame = source_frame.compose(anchor.offset)
|
||||
return source_frame
|
||||
|
||||
|
||||
def _connector_local_frame_for_ref(
|
||||
assembly: Assembly, connector_ref: ConnectorRef
|
||||
) -> Placement:
|
||||
component = assembly.get_component(connector_ref.component_id)
|
||||
connector = _resolve_connector(assembly, connector_ref)
|
||||
return component.placement.compose(connector.placement)
|
||||
owner = component.item if isinstance(component.item, Assembly) else None
|
||||
return resolve_connector_placement(connector, owner_assembly=owner)
|
||||
|
||||
|
||||
def _connector_world_frame(assembly: Assembly, connector_ref: ConnectorRef) -> Placement:
|
||||
component = assembly.get_component(connector_ref.component_id)
|
||||
return component.placement.compose(
|
||||
_connector_local_frame_for_ref(assembly, connector_ref)
|
||||
)
|
||||
|
||||
|
||||
def _constraint_current_scalar(assembly: Assembly, constraint: Constraint) -> float:
|
||||
@@ -1262,11 +1449,11 @@ def _loop_constraint_residual_at_scalar(
|
||||
motion = _motion_from_scalar(constraint, candidate_scalar)
|
||||
if known_side == "b":
|
||||
motion = inverse_placement(motion)
|
||||
known_connector = _resolve_connector(assembly, known_ref)
|
||||
unknown_connector = _resolve_connector(assembly, unknown_ref)
|
||||
known_frame = known_placement.compose(known_connector.placement)
|
||||
known_connector_frame = _connector_local_frame_for_ref(assembly, known_ref)
|
||||
unknown_connector_frame = _connector_local_frame_for_ref(assembly, unknown_ref)
|
||||
known_frame = known_placement.compose(known_connector_frame)
|
||||
target_unknown_frame = known_frame.compose(motion)
|
||||
target_placement = target_unknown_frame.compose(inverse_placement(unknown_connector.placement))
|
||||
target_placement = target_unknown_frame.compose(inverse_placement(unknown_connector_frame))
|
||||
|
||||
test = assembly.with_component_placement(
|
||||
unknown_ref.component_id, target_placement
|
||||
@@ -1313,11 +1500,11 @@ def _solve_other_component(
|
||||
else:
|
||||
known_ref = constraint.connector_b
|
||||
unknown_ref = constraint.connector_a
|
||||
known_connector = _resolve_connector(assembly, known_ref)
|
||||
unknown_connector = _resolve_connector(assembly, unknown_ref)
|
||||
known_frame = known_placement.compose(known_connector.placement)
|
||||
known_connector_frame = _connector_local_frame_for_ref(assembly, known_ref)
|
||||
unknown_connector_frame = _connector_local_frame_for_ref(assembly, unknown_ref)
|
||||
known_frame = known_placement.compose(known_connector_frame)
|
||||
target_unknown_frame = known_frame.compose(motion)
|
||||
return target_unknown_frame.compose(inverse_placement(unknown_connector.placement))
|
||||
return target_unknown_frame.compose(inverse_placement(unknown_connector_frame))
|
||||
|
||||
|
||||
def _support_constraint_for_ref(
|
||||
@@ -1652,6 +1839,7 @@ __all__ = [
|
||||
"Assembly",
|
||||
"Component",
|
||||
"Connector",
|
||||
"ConnectorAnchor",
|
||||
"ConnectorRef",
|
||||
"Constraint",
|
||||
"ConstraintReport",
|
||||
@@ -1667,6 +1855,7 @@ __all__ = [
|
||||
"inverse_placement",
|
||||
"measure_constraint_residual",
|
||||
"relative_placement",
|
||||
"resolve_connector_placement",
|
||||
"rotate_z_placement",
|
||||
"solve_assembly_constraints",
|
||||
"translate_z_placement",
|
||||
|
||||
@@ -124,8 +124,10 @@ PUBLIC_API_COVERAGE: Dict[str, Dict[str, str]] = {
|
||||
"make_face_connector_rconnector": {"status": "replayable", "op": "make_face_connector_rconnector"},
|
||||
"make_edge_connector_rconnector": {"status": "replayable", "op": "make_edge_connector_rconnector"},
|
||||
"make_vertex_connector_rconnector": {"status": "replayable", "op": "make_vertex_connector_rconnector"},
|
||||
"make_placement_connector_rconnector": {"status": "replayable", "op": "make_placement_connector_rconnector"},
|
||||
"add_connector_rpart": {"status": "replayable", "op": "make_add_connector_rpart"},
|
||||
"add_connector_rassembly": {"status": "replayable", "op": "make_add_connector_rassembly"},
|
||||
"forward_connector_rassembly": {"status": "replayable", "op": "make_forward_connector_rassembly"},
|
||||
"make_connector_ref_rconnectorref": {"status": "replayable", "op": "make_connector_ref_rconnectorref"},
|
||||
"make_scalar_limit_rscalarlimit": {"status": "replayable", "op": "make_scalar_limit_rscalarlimit"},
|
||||
"ground_component_rassembly": {"status": "replayable", "op": "make_ground_component_rassembly"},
|
||||
@@ -294,8 +296,10 @@ CANONICAL_CORE_OP_SET: Tuple[str, ...] = (
|
||||
"make_face_connector_rconnector",
|
||||
"make_edge_connector_rconnector",
|
||||
"make_vertex_connector_rconnector",
|
||||
"make_placement_connector_rconnector",
|
||||
"make_add_connector_rpart",
|
||||
"make_add_connector_rassembly",
|
||||
"make_forward_connector_rassembly",
|
||||
"make_connector_ref_rconnectorref",
|
||||
"make_scalar_limit_rscalarlimit",
|
||||
"make_ground_component_rassembly",
|
||||
@@ -1811,6 +1815,28 @@ def _execute_graph(
|
||||
_store_outputs(node, connector)
|
||||
continue
|
||||
|
||||
if op_name == "make_placement_connector_rconnector":
|
||||
ctx.require_params(node.node_id, op_name, params, ("connector_id",))
|
||||
placement_outputs = _input_outputs(ctx, outputs, node, 0)
|
||||
placement = None
|
||||
if placement_outputs:
|
||||
placement = cast(Placement, placement_outputs[0])
|
||||
elif isinstance(params.get("placement"), dict):
|
||||
placement_data = cast(Dict[str, Any], params["placement"])
|
||||
placement = Placement(
|
||||
cast(Any, tuple(placement_data.get("origin", (0.0, 0.0, 0.0)))),
|
||||
x_axis=cast(Any, tuple(placement_data.get("x_axis", (1.0, 0.0, 0.0)))),
|
||||
y_axis=cast(Any, tuple(placement_data.get("y_axis", (0.0, 1.0, 0.0)))),
|
||||
)
|
||||
if placement is not None:
|
||||
result = ops.make_placement_connector_rconnector(
|
||||
str(params["connector_id"]),
|
||||
placement,
|
||||
name=cast(Optional[str], params.get("name")),
|
||||
)
|
||||
_store_outputs(node, result)
|
||||
continue
|
||||
|
||||
if op_name == "make_add_connector_rpart":
|
||||
part_outputs = _input_outputs(ctx, outputs, node, 0)
|
||||
connector_outputs = _input_outputs(ctx, outputs, node, 1)
|
||||
@@ -1833,6 +1859,35 @@ def _execute_graph(
|
||||
_store_outputs(node, result)
|
||||
continue
|
||||
|
||||
if op_name == "make_forward_connector_rassembly":
|
||||
ctx.require_params(
|
||||
node.node_id,
|
||||
op_name,
|
||||
params,
|
||||
("connector_id", "source_component_id", "source_connector_id"),
|
||||
)
|
||||
assembly_outputs = _input_outputs(ctx, outputs, node, 0)
|
||||
offset_outputs = _input_outputs(ctx, outputs, node, 1)
|
||||
offset = cast(Optional[Placement], offset_outputs[0]) if offset_outputs else None
|
||||
if offset is None and isinstance(params.get("offset"), dict):
|
||||
offset_data = cast(Dict[str, Any], params["offset"])
|
||||
offset = Placement(
|
||||
cast(Any, tuple(offset_data.get("origin", (0.0, 0.0, 0.0)))),
|
||||
x_axis=cast(Any, tuple(offset_data.get("x_axis", (1.0, 0.0, 0.0)))),
|
||||
y_axis=cast(Any, tuple(offset_data.get("y_axis", (0.0, 1.0, 0.0)))),
|
||||
)
|
||||
if assembly_outputs:
|
||||
result = ops.forward_connector_rassembly(
|
||||
cast(Assembly, assembly_outputs[0]),
|
||||
str(params["connector_id"]),
|
||||
str(params["source_component_id"]),
|
||||
str(params["source_connector_id"]),
|
||||
name=cast(Optional[str], params.get("name")),
|
||||
offset=offset,
|
||||
)
|
||||
_store_outputs(node, result)
|
||||
continue
|
||||
|
||||
if op_name == "make_connector_ref_rconnectorref":
|
||||
ctx.require_params(node.node_id, op_name, params, ("component_id", "connector_id"))
|
||||
result = ops.make_connector_ref_rconnectorref(
|
||||
|
||||
@@ -45,8 +45,10 @@ def test_product_and_constraint_public_apis_do_not_use_bare_star_parameters():
|
||||
scad.make_face_connector_rconnector,
|
||||
scad.make_edge_connector_rconnector,
|
||||
scad.make_vertex_connector_rconnector,
|
||||
scad.make_placement_connector_rconnector,
|
||||
scad.add_connector_rpart,
|
||||
scad.add_connector_rassembly,
|
||||
scad.forward_connector_rassembly,
|
||||
scad.make_connector_ref_rconnectorref,
|
||||
scad.make_scalar_limit_rscalarlimit,
|
||||
scad.ground_component_rassembly,
|
||||
@@ -158,6 +160,104 @@ def _part_with_face_connector(part_id):
|
||||
return scad.add_connector_rpart(part, connector)
|
||||
|
||||
|
||||
def _part_with_placement_connector(part_id, connector_origin=(0.0, 0.0, 0.0)):
|
||||
body = scad.make_box_rsolid(1.0, 1.0, 1.0)
|
||||
part = scad.make_part_rpart(part_id, body)
|
||||
placement = scad.make_placement_rplacement(origin=connector_origin)
|
||||
connector = scad.make_placement_connector_rconnector("axis", placement)
|
||||
return scad.add_connector_rpart(part, connector)
|
||||
|
||||
|
||||
def test_placement_connector_can_drive_fixed_constraints():
|
||||
part = _part_with_placement_connector("placement_constraint_part")
|
||||
assembly = scad.make_assembly_rassembly("placement_constraint_asm")
|
||||
assembly = scad.add_component_rassembly(
|
||||
assembly,
|
||||
part,
|
||||
component_id="base",
|
||||
placement=scad.make_placement_rplacement(origin=(1.0, 2.0, 3.0)),
|
||||
)
|
||||
assembly = scad.add_component_rassembly(
|
||||
assembly,
|
||||
part,
|
||||
component_id="follower",
|
||||
placement=scad.identity_placement_rplacement(),
|
||||
)
|
||||
assembly = scad.ground_component_rassembly(assembly, "base")
|
||||
assembly = scad.add_fixed_constraint_rassembly(
|
||||
assembly,
|
||||
"fixed",
|
||||
scad.make_connector_ref_rconnectorref("base", "axis"),
|
||||
scad.make_connector_ref_rconnectorref("follower", "axis"),
|
||||
)
|
||||
|
||||
solved = scad.solve_assembly_constraints_rassembly(assembly)
|
||||
|
||||
assert solved.get_component("follower").placement.origin == (1.0, 2.0, 3.0)
|
||||
assert scad.measure_constraint_residual_rconstraintresidual(solved, "fixed").within_tolerance
|
||||
|
||||
|
||||
def test_forwarded_connector_resolves_and_solves_at_parent_level():
|
||||
inner_part = _part_with_placement_connector("forwarded_inner_part", (2.0, 0.0, 0.0))
|
||||
base_part = _part_with_placement_connector("forwarded_base_part")
|
||||
child = scad.make_assembly_rassembly("forwarded_child")
|
||||
child = scad.add_component_rassembly(
|
||||
child,
|
||||
inner_part,
|
||||
component_id="inner",
|
||||
placement=scad.make_placement_rplacement(origin=(5.0, 0.0, 0.0)),
|
||||
)
|
||||
child = scad.forward_connector_rassembly(
|
||||
child,
|
||||
connector_id="public_axis",
|
||||
source_component_id="inner",
|
||||
source_connector_id="axis",
|
||||
)
|
||||
|
||||
assert child.connector_ids() == ("public_axis",)
|
||||
assert child.get_connector("public_axis").placement.origin == (7.0, 0.0, 0.0)
|
||||
|
||||
root = scad.make_assembly_rassembly("forwarded_root")
|
||||
root = scad.add_component_rassembly(
|
||||
root,
|
||||
base_part,
|
||||
component_id="base",
|
||||
placement=scad.make_placement_rplacement(origin=(10.0, 0.0, 0.0)),
|
||||
)
|
||||
root = scad.add_component_rassembly(
|
||||
root,
|
||||
child,
|
||||
component_id="child",
|
||||
placement=scad.identity_placement_rplacement(),
|
||||
)
|
||||
root = scad.ground_component_rassembly(root, "base")
|
||||
root = scad.add_fixed_constraint_rassembly(
|
||||
root,
|
||||
"bind_forwarded_axis",
|
||||
scad.make_connector_ref_rconnectorref("base", "axis"),
|
||||
scad.make_connector_ref_rconnectorref("child", "public_axis"),
|
||||
)
|
||||
solved = scad.solve_assembly_constraints_rassembly(root)
|
||||
|
||||
assert solved.get_component("child").placement.origin == (3.0, 0.0, 0.0)
|
||||
assert scad.measure_constraint_residual_rconstraintresidual(
|
||||
solved,
|
||||
"bind_forwarded_axis",
|
||||
).within_tolerance
|
||||
|
||||
|
||||
def test_forwarded_connector_validation_reports_missing_sources():
|
||||
assembly = scad.make_assembly_rassembly("bad_forwarded_connector_asm")
|
||||
|
||||
with pytest.raises(Exception, match="missing component"):
|
||||
scad.forward_connector_rassembly(
|
||||
assembly,
|
||||
connector_id="public_axis",
|
||||
source_component_id="inner",
|
||||
source_connector_id="axis",
|
||||
)
|
||||
|
||||
|
||||
def test_fixed_revolute_and_prismatic_constraints_solve_component_placements():
|
||||
part = _part_with_face_connector("constraint_part")
|
||||
|
||||
|
||||
@@ -184,8 +184,10 @@ class TestCoverageMatrix(unittest.TestCase):
|
||||
"make_face_connector_rconnector",
|
||||
"make_edge_connector_rconnector",
|
||||
"make_vertex_connector_rconnector",
|
||||
"make_placement_connector_rconnector",
|
||||
"add_connector_rpart",
|
||||
"add_connector_rassembly",
|
||||
"forward_connector_rassembly",
|
||||
"make_connector_ref_rconnectorref",
|
||||
"make_scalar_limit_rscalarlimit",
|
||||
"ground_component_rassembly",
|
||||
@@ -281,8 +283,10 @@ class TestCoverageMatrix(unittest.TestCase):
|
||||
"make_face_connector_rconnector",
|
||||
"make_edge_connector_rconnector",
|
||||
"make_vertex_connector_rconnector",
|
||||
"make_placement_connector_rconnector",
|
||||
"make_add_connector_rpart",
|
||||
"make_add_connector_rassembly",
|
||||
"make_forward_connector_rassembly",
|
||||
"make_connector_ref_rconnectorref",
|
||||
"make_scalar_limit_rscalarlimit",
|
||||
"make_ground_component_rassembly",
|
||||
|
||||
Reference in New Issue
Block a user