Skip to content

Commit cb0d9d3

Browse files
Fix coincident constraint handling in SolidWorks.
1 parent 926b9e4 commit cb0d9d3

1 file changed

Lines changed: 65 additions & 6 deletions

File tree

sketch_adapter_solidworks/adapter.py

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,17 +1139,76 @@ def _add_equal(self, model: Any, refs: list) -> bool:
11391139
return True
11401140

11411141
def _add_concentric(self, model: Any, refs: list) -> bool:
1142-
"""Add a concentric constraint."""
1142+
"""Add a concentric constraint by moving the second entity's center.
1143+
1144+
Note: SolidWorks API's sgCONCENTRIC and sgCOINCIDENT on center points
1145+
don't reliably move geometry. Instead, we delete and recreate the second
1146+
circle/arc at the first entity's center position.
1147+
"""
11431148
if len(refs) < 2:
11441149
raise ConstraintError("Concentric requires 2 references")
11451150

1151+
entity1_id = refs[0]
1152+
entity2_id = refs[1]
1153+
1154+
# Get the center of the first entity
1155+
center1_x, center1_y = None, None
1156+
for geom in self._segment_geometry_list:
1157+
if geom.get('element_id') == entity1_id:
1158+
if geom['type'] in ('circle', 'arc'):
1159+
center1_x, center1_y = geom['center']
1160+
break
1161+
1162+
if center1_x is None:
1163+
raise ConstraintError(f"Could not find center for first entity: {entity1_id}")
1164+
1165+
# Get the second entity's geometry info
1166+
geom2 = None
1167+
for geom in self._segment_geometry_list:
1168+
if geom.get('element_id') == entity2_id:
1169+
geom2 = geom
1170+
break
1171+
1172+
if geom2 is None or geom2['type'] not in ('circle', 'arc'):
1173+
raise ConstraintError(f"Second entity must be a circle or arc: {entity2_id}")
1174+
1175+
# Get the second entity for deletion
1176+
entity2 = self._id_to_entity.get(entity2_id)
1177+
if entity2 is None:
1178+
raise ConstraintError(f"Could not find second entity: {entity2_id}")
1179+
1180+
# Delete the second entity
11461181
model.ClearSelection2(True)
1147-
if not self._select_entity(refs[0], False):
1148-
raise ConstraintError("Could not select first entity")
1149-
if not self._select_entity(refs[1], True):
1150-
raise ConstraintError("Could not select second entity")
1182+
entity2.Select(False)
1183+
model.EditDelete()
1184+
1185+
# Recreate the second entity at the first entity's center
1186+
if geom2['type'] == 'circle':
1187+
radius = geom2['radius']
1188+
new_entity = self._sketch_manager.CreateCircle(
1189+
center1_x * MM_TO_M, center1_y * MM_TO_M, 0,
1190+
(center1_x + radius) * MM_TO_M, center1_y * MM_TO_M, 0
1191+
)
1192+
else: # arc
1193+
# For arcs, we need start/end angles and radius
1194+
radius = geom2['radius']
1195+
start_angle = geom2.get('start_angle', 0)
1196+
end_angle = geom2.get('end_angle', math.pi)
1197+
new_entity = self._sketch_manager.CreateArc(
1198+
center1_x * MM_TO_M, center1_y * MM_TO_M, 0,
1199+
(center1_x + radius * math.cos(start_angle)) * MM_TO_M,
1200+
(center1_y + radius * math.sin(start_angle)) * MM_TO_M, 0,
1201+
(center1_x + radius * math.cos(end_angle)) * MM_TO_M,
1202+
(center1_y + radius * math.sin(end_angle)) * MM_TO_M, 0,
1203+
1 # Direction
1204+
)
1205+
1206+
# Update mappings
1207+
if new_entity is not None:
1208+
self._id_to_entity[entity2_id] = new_entity
1209+
# Update stored geometry
1210+
geom2['center'] = (center1_x, center1_y)
11511211

1152-
model.SketchAddConstraints("sgCONCENTRIC")
11531212
return True
11541213

11551214
def _add_collinear(self, model: Any, refs: list) -> bool:

0 commit comments

Comments
 (0)