88"""
99
1010import math
11+ import uuid
1112from typing import Any
1213
1314from sketch_canonical .adapter import (
@@ -1389,12 +1390,17 @@ def _point_to_ref(self, point, element_id: str) -> PointRef:
13891390
13901391 return PointRef (element_id , PointType .CENTER )
13911392
1393+ def _generate_constraint_id (self ) -> str :
1394+ """Generate a unique constraint ID."""
1395+ return f"C_{ uuid .uuid4 ().hex [:8 ]} "
1396+
13921397 def _convert_horizontal (self , constraint ) -> SketchConstraint | None :
13931398 """Convert a horizontal constraint."""
13941399 entity = constraint .line
13951400 entity_id = self ._get_id_for_entity (entity )
13961401 if entity_id :
13971402 return SketchConstraint (
1403+ id = self ._generate_constraint_id (),
13981404 constraint_type = ConstraintType .HORIZONTAL ,
13991405 references = [entity_id ]
14001406 )
@@ -1406,6 +1412,7 @@ def _convert_vertical(self, constraint) -> SketchConstraint | None:
14061412 entity_id = self ._get_id_for_entity (entity )
14071413 if entity_id :
14081414 return SketchConstraint (
1415+ id = self ._generate_constraint_id (),
14091416 constraint_type = ConstraintType .VERTICAL ,
14101417 references = [entity_id ]
14111418 )
@@ -1419,6 +1426,7 @@ def _convert_parallel(self, constraint) -> SketchConstraint | None:
14191426 id2 = self ._get_id_for_entity (line2 )
14201427 if id1 and id2 :
14211428 return SketchConstraint (
1429+ id = self ._generate_constraint_id (),
14221430 constraint_type = ConstraintType .PARALLEL ,
14231431 references = [id1 , id2 ]
14241432 )
@@ -1432,6 +1440,7 @@ def _convert_perpendicular(self, constraint) -> SketchConstraint | None:
14321440 id2 = self ._get_id_for_entity (line2 )
14331441 if id1 and id2 :
14341442 return SketchConstraint (
1443+ id = self ._generate_constraint_id (),
14351444 constraint_type = ConstraintType .PERPENDICULAR ,
14361445 references = [id1 , id2 ]
14371446 )
@@ -1445,6 +1454,7 @@ def _convert_tangent(self, constraint) -> SketchConstraint | None:
14451454 id2 = self ._get_id_for_entity (curve2 )
14461455 if id1 and id2 :
14471456 return SketchConstraint (
1457+ id = self ._generate_constraint_id (),
14481458 constraint_type = ConstraintType .TANGENT ,
14491459 references = [id1 , id2 ]
14501460 )
@@ -1458,6 +1468,7 @@ def _convert_equal(self, constraint) -> SketchConstraint | None:
14581468 id2 = self ._get_id_for_entity (curve2 )
14591469 if id1 and id2 :
14601470 return SketchConstraint (
1471+ id = self ._generate_constraint_id (),
14611472 constraint_type = ConstraintType .EQUAL ,
14621473 references = [id1 , id2 ]
14631474 )
@@ -1471,6 +1482,7 @@ def _convert_concentric(self, constraint) -> SketchConstraint | None:
14711482 id2 = self ._get_id_for_entity (entity2 )
14721483 if id1 and id2 :
14731484 return SketchConstraint (
1485+ id = self ._generate_constraint_id (),
14741486 constraint_type = ConstraintType .CONCENTRIC ,
14751487 references = [id1 , id2 ]
14761488 )
@@ -1484,6 +1496,7 @@ def _convert_collinear(self, constraint) -> SketchConstraint | None:
14841496 id2 = self ._get_id_for_entity (line2 )
14851497 if id1 and id2 :
14861498 return SketchConstraint (
1499+ id = self ._generate_constraint_id (),
14871500 constraint_type = ConstraintType .COLLINEAR ,
14881501 references = [id1 , id2 ]
14891502 )
@@ -1495,6 +1508,7 @@ def _convert_fixed(self, constraint) -> SketchConstraint | None:
14951508 entity_id = self ._get_id_for_entity (entity )
14961509 if entity_id :
14971510 return SketchConstraint (
1511+ id = self ._generate_constraint_id (),
14981512 constraint_type = ConstraintType .FIXED ,
14991513 references = [entity_id ]
15001514 )
@@ -1510,6 +1524,7 @@ def _convert_symmetric(self, constraint) -> SketchConstraint | None:
15101524 line_id = self ._get_id_for_entity (line )
15111525 if id1 and id2 and line_id :
15121526 return SketchConstraint (
1527+ id = self ._generate_constraint_id (),
15131528 constraint_type = ConstraintType .SYMMETRIC ,
15141529 references = [id1 , id2 , line_id ]
15151530 )
@@ -1524,6 +1539,7 @@ def _convert_midpoint(self, constraint) -> SketchConstraint | None:
15241539 if point_id and line_id :
15251540 ref = self ._point_to_ref (point , point_id )
15261541 return SketchConstraint (
1542+ id = self ._generate_constraint_id (),
15271543 constraint_type = ConstraintType .MIDPOINT ,
15281544 references = [ref , line_id ]
15291545 )
@@ -1556,6 +1572,8 @@ def _convert_dimensional_constraint(self, dim) -> SketchConstraint | None:
15561572 return self ._convert_diameter_dimension (dim , value_mm )
15571573 elif "SketchAngularDimension" in obj_type :
15581574 return self ._convert_angular_dimension (dim )
1575+ elif "SketchOffsetDimension" in obj_type :
1576+ return self ._convert_offset_dimension (dim , value_mm )
15591577 else :
15601578 return None
15611579 except Exception :
@@ -1580,18 +1598,21 @@ def _convert_linear_dimension(self, dim, value: float) -> SketchConstraint | Non
15801598 # Check orientation for X/Y constraints
15811599 if orientation == self ._adsk_fusion .DimensionOrientations .HorizontalDimensionOrientation :
15821600 return SketchConstraint (
1601+ id = self ._generate_constraint_id (),
15831602 constraint_type = ConstraintType .DISTANCE_X ,
15841603 references = [ref1 , ref2 ],
15851604 value = value
15861605 )
15871606 elif orientation == self ._adsk_fusion .DimensionOrientations .VerticalDimensionOrientation :
15881607 return SketchConstraint (
1608+ id = self ._generate_constraint_id (),
15891609 constraint_type = ConstraintType .DISTANCE_Y ,
15901610 references = [ref1 , ref2 ],
15911611 value = value
15921612 )
15931613 else :
15941614 return SketchConstraint (
1615+ id = self ._generate_constraint_id (),
15951616 constraint_type = ConstraintType .DISTANCE ,
15961617 references = [ref1 , ref2 ],
15971618 value = value
@@ -1601,6 +1622,7 @@ def _convert_linear_dimension(self, dim, value: float) -> SketchConstraint | Non
16011622 entity_id = self ._get_id_for_entity (entity1 )
16021623 if entity_id :
16031624 return SketchConstraint (
1625+ id = self ._generate_constraint_id (),
16041626 constraint_type = ConstraintType .LENGTH ,
16051627 references = [entity_id ],
16061628 value = value
@@ -1614,6 +1636,7 @@ def _convert_radial_dimension(self, dim, value: float) -> SketchConstraint | Non
16141636 entity_id = self ._get_id_for_entity (entity )
16151637 if entity_id :
16161638 return SketchConstraint (
1639+ id = self ._generate_constraint_id (),
16171640 constraint_type = ConstraintType .RADIUS ,
16181641 references = [entity_id ],
16191642 value = value
@@ -1626,6 +1649,7 @@ def _convert_diameter_dimension(self, dim, value: float) -> SketchConstraint | N
16261649 entity_id = self ._get_id_for_entity (entity )
16271650 if entity_id :
16281651 return SketchConstraint (
1652+ id = self ._generate_constraint_id (),
16291653 constraint_type = ConstraintType .DIAMETER ,
16301654 references = [entity_id ],
16311655 value = value
@@ -1645,8 +1669,46 @@ def _convert_angular_dimension(self, dim) -> SketchConstraint | None:
16451669
16461670 if id1 and id2 :
16471671 return SketchConstraint (
1672+ id = self ._generate_constraint_id (),
16481673 constraint_type = ConstraintType .ANGLE ,
16491674 references = [id1 , id2 ],
16501675 value = value_deg
16511676 )
16521677 return None
1678+
1679+ def _convert_offset_dimension (self , dim , value : float ) -> SketchConstraint | None :
1680+ """Convert an offset dimension constraint (distance from origin to line)."""
1681+ # SketchOffsetDimension uses .line property, not .entity
1682+ entity = dim .line
1683+ entity_id = self ._get_id_for_entity (entity )
1684+
1685+ if entity_id :
1686+ # Offset dimensions are typically from origin to a line
1687+ # Use the isHorizontal property to determine constraint type
1688+ try :
1689+ is_horizontal = dim .isHorizontal
1690+ if is_horizontal :
1691+ # Horizontal offset means vertical distance (Y constraint)
1692+ return SketchConstraint (
1693+ id = self ._generate_constraint_id (),
1694+ constraint_type = ConstraintType .DISTANCE_Y ,
1695+ references = [entity_id ],
1696+ value = value
1697+ )
1698+ else :
1699+ # Vertical offset means horizontal distance (X constraint)
1700+ return SketchConstraint (
1701+ id = self ._generate_constraint_id (),
1702+ constraint_type = ConstraintType .DISTANCE_X ,
1703+ references = [entity_id ],
1704+ value = value
1705+ )
1706+ except AttributeError :
1707+ # If isHorizontal not available, use generic distance
1708+ return SketchConstraint (
1709+ id = self ._generate_constraint_id (),
1710+ constraint_type = ConstraintType .DISTANCE ,
1711+ references = [entity_id ],
1712+ value = value
1713+ )
1714+ return None
0 commit comments