From 06f1d402b0647671b4dd62d43f705e67199d5c2c Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 17 Apr 2024 21:18:42 +0200 Subject: [PATCH] Checks for duplicate constraints. On constraint creation checks are added to see if it exists on the selected entity or entitiies. The exists method was added to GenericConstraintOp for this. Line distances have been replaced for point to point distances. --- operators/add_angle.py | 17 ++-- operators/add_diameter.py | 15 ++-- operators/add_distance.py | 29 +++++-- operators/add_geometric_constraints.py | 112 ++++++++++++++----------- operators/base_constraint.py | 38 +++++++++ 5 files changed, 144 insertions(+), 67 deletions(-) diff --git a/operators/add_angle.py b/operators/add_angle.py index 13760182..6740a3b9 100644 --- a/operators/add_angle.py +++ b/operators/add_angle.py @@ -9,6 +9,8 @@ from ..stateful_operator.utilities.register import register_stateops_factory from .base_constraint import GenericConstraintOp +from ..model.angle import SlvsAngle + logger = logging.getLogger(__name__) @@ -45,13 +47,14 @@ class VIEW3D_OT_slvs_add_angle(Operator, GenericConstraintOp): property_keys = ("value", "setting") def main(self, context): - self.target = context.scene.sketcher.constraints.add_angle( - self.entity1, - self.entity2, - sketch=self.sketch, - init=not self.initialized, - **self.get_settings() - ) + if not self.exists(context, SlvsAngle): + self.target = context.scene.sketcher.constraints.add_angle( + self.entity1, + self.entity2, + sketch=self.sketch, + init=not self.initialized, + **self.get_settings() + ) return super().main(context) diff --git a/operators/add_diameter.py b/operators/add_diameter.py index 3a0b18d9..3393a386 100644 --- a/operators/add_diameter.py +++ b/operators/add_diameter.py @@ -7,6 +7,8 @@ from ..stateful_operator.utilities.register import register_stateops_factory from .base_constraint import GenericConstraintOp +from ..model.diameter import SlvsDiameter + logger = logging.getLogger(__name__) @@ -30,12 +32,13 @@ class VIEW3D_OT_slvs_add_diameter(Operator, GenericConstraintOp): property_keys = ("value", "setting") def main(self, context): - self.target = context.scene.sketcher.constraints.add_diameter( - self.entity1, - sketch=self.sketch, - init=not self.initialized, - **self.get_settings(), - ) + if not self.exists(context, SlvsDiameter): + self.target = context.scene.sketcher.constraints.add_diameter( + self.entity1, + sketch=self.sketch, + init=not self.initialized, + **self.get_settings(), + ) return super().main(context) diff --git a/operators/add_distance.py b/operators/add_distance.py index 6aa7df99..74394c5c 100644 --- a/operators/add_distance.py +++ b/operators/add_distance.py @@ -8,6 +8,9 @@ from ..declarations import Operators from ..stateful_operator.utilities.register import register_stateops_factory +from ..model.distance import SlvsDistance +from ..model.line_2d import SlvsLine2D +from ..model.point_2d import SlvsPoint2D logger = logging.getLogger(__name__) @@ -33,13 +36,25 @@ class VIEW3D_OT_slvs_add_distance(Operator, GenericConstraintOp): property_keys = ("value", "align", "flip") def main(self, context): - self.target = context.scene.sketcher.constraints.add_distance( - self.entity1, - self.entity2, - sketch=self.sketch, - init=not self.initialized, - **self.get_settings(), - ) + if type(self.entity1) == SlvsLine2D and self.entity2 == None: + dependencies = self.entity1.dependencies() + if type(dependencies[0]) == SlvsPoint2D and type(dependencies[1]) == SlvsPoint2D: + # for loop changes the values of self.entity1 and self.entity2 from a line to its endpoints + for i in range(0,2): + self.state_index = i # set index to access i state_data + self.state_data["hovered"] = -1 + self.state_data["type"] = type(dependencies[i]) + self.state_data["is_existing_entity"] = True + self.state_data["entity_index"] = dependencies[i].slvs_index + + if not self.exists(context, SlvsDistance): + self.target = context.scene.sketcher.constraints.add_distance( + self.entity1, + self.entity2, + sketch=self.sketch, + init=not self.initialized, + **self.get_settings(), + ) return super().main(context) def fini(self, context: Context, succeede: bool): diff --git a/operators/add_geometric_constraints.py b/operators/add_geometric_constraints.py index 3306878c..d3487e21 100644 --- a/operators/add_geometric_constraints.py +++ b/operators/add_geometric_constraints.py @@ -12,6 +12,15 @@ from ..utilities.view import refresh from ..solver import solve_system +from ..model.coincident import SlvsCoincident +from ..model.equal import SlvsEqual +from ..model.vertical import SlvsVertical +from ..model.horizontal import SlvsHorizontal +from ..model.parallel import SlvsParallel +from ..model.perpendicular import SlvsPerpendicular +from ..model.tangent import SlvsTangent +from ..model.midpoint import SlvsMidpoint +from ..model.ratio import SlvsRatio logger = logging.getLogger(__name__) @@ -52,11 +61,12 @@ def main(self, context: Context): if self.handle_merge(context): return True - self.target = context.scene.sketcher.constraints.add_coincident( - self.entity1, - self.entity2, - sketch=self.sketch, - ) + if not self.exists(context, SlvsCoincident): + self.target = context.scene.sketcher.constraints.add_coincident( + self.entity1, + self.entity2, + sketch=self.sketch, + ) return super().main(context) @@ -70,11 +80,12 @@ class VIEW3D_OT_slvs_add_equal(Operator, GenericConstraintOp): type = "EQUAL" def main(self, context): - self.target = context.scene.sketcher.constraints.add_equal( - self.entity1, - self.entity2, - sketch=self.sketch, - ) + if not self.exists(context, SlvsEqual): + self.target = context.scene.sketcher.constraints.add_equal( + self.entity1, + self.entity2, + sketch=self.sketch, + ) return super().main(context) @@ -89,11 +100,12 @@ class VIEW3D_OT_slvs_add_vertical(Operator, GenericConstraintOp): type = "VERTICAL" def main(self, context): - self.target = context.scene.sketcher.constraints.add_vertical( - self.entity1, - entity2=self.entity2, - sketch=self.sketch, - ) + if not self.exists(context, SlvsVertical): + self.target = context.scene.sketcher.constraints.add_vertical( + self.entity1, + entity2=self.entity2, + sketch=self.sketch, + ) return super().main(context) @@ -108,11 +120,12 @@ class VIEW3D_OT_slvs_add_horizontal(Operator, GenericConstraintOp): type = "HORIZONTAL" def main(self, context): - self.target = context.scene.sketcher.constraints.add_horizontal( - self.entity1, - entity2=self.entity2, - sketch=self.sketch, - ) + if not self.exists(context, SlvsHorizontal): + self.target = context.scene.sketcher.constraints.add_horizontal( + self.entity1, + entity2=self.entity2, + sketch=self.sketch, + ) return super().main(context) @@ -127,11 +140,12 @@ class VIEW3D_OT_slvs_add_parallel(Operator, GenericConstraintOp): type = "PARALLEL" def main(self, context): - self.target = context.scene.sketcher.constraints.add_parallel( - self.entity1, - self.entity2, - sketch=self.sketch, - ) + if not self.exists(context, SlvsParallel): + self.target = context.scene.sketcher.constraints.add_parallel( + self.entity1, + self.entity2, + sketch=self.sketch, + ) return super().main(context) @@ -146,11 +160,12 @@ class VIEW3D_OT_slvs_add_perpendicular(Operator, GenericConstraintOp): type = "PERPENDICULAR" def main(self, context): - self.target = context.scene.sketcher.constraints.add_perpendicular( - self.entity1, - self.entity2, - sketch=self.sketch, - ) + if not self.exists(context, SlvsPerpendicular): + self.target = context.scene.sketcher.constraints.add_perpendicular( + self.entity1, + self.entity2, + sketch=self.sketch, + ) return super().main(context) @@ -165,11 +180,12 @@ class VIEW3D_OT_slvs_add_tangent(Operator, GenericConstraintOp): type = "TANGENT" def main(self, context): - self.target = context.scene.sketcher.constraints.add_tangent( - self.entity1, - self.entity2, - sketch=self.sketch, - ) + if not self.exists(context, SlvsTangent): + self.target = context.scene.sketcher.constraints.add_tangent( + self.entity1, + self.entity2, + sketch=self.sketch, + ) return super().main(context) @@ -184,11 +200,12 @@ class VIEW3D_OT_slvs_add_midpoint(Operator, GenericConstraintOp): type = "MIDPOINT" def main(self, context): - self.target = context.scene.sketcher.constraints.add_midpoint( - self.entity1, - self.entity2, - sketch=self.sketch, - ) + if not self.exists(context, SlvsMidpoint): + self.target = context.scene.sketcher.constraints.add_midpoint( + self.entity1, + self.entity2, + sketch=self.sketch, + ) return super().main(context) @@ -211,13 +228,14 @@ class VIEW3D_OT_slvs_add_ratio(Operator, GenericConstraintOp): property_keys = ("value",) def main(self, context): - self.target = context.scene.sketcher.constraints.add_ratio( - self.entity1, - self.entity2, - sketch=self.sketch, - init=not self.initialized, - **self.get_settings(), - ) + if not self.exists(context, SlvsRatio): + self.target = context.scene.sketcher.constraints.add_ratio( + self.entity1, + self.entity2, + sketch=self.sketch, + init=not self.initialized, + **self.get_settings(), + ) return super().main(context) diff --git a/operators/base_constraint.py b/operators/base_constraint.py index 49ee73bf..df9ba29d 100644 --- a/operators/base_constraint.py +++ b/operators/base_constraint.py @@ -14,6 +14,12 @@ from ..utilities.view import refresh from ..solver import solve_system +from ..model.distance import SlvsDistance +from ..model.types import SlvsPoint2D +from ..model.types import SlvsLine2D +from ..model.types import SlvsPoint3D +from ..model.types import SlvsLine3D + logger = logging.getLogger(__name__) @@ -124,3 +130,35 @@ def draw(self, context: Context): for key in self.property_keys: layout.prop(self, key) + + + def exists(self, context, constraint_type=None) -> bool: + if hasattr(self, "entity2"): + new_dependencies = [i for i in [self.entity1, self.entity2, self.sketch] if i is not None] + + if ((SlvsPoint3D == type(self.entity1) or SlvsPoint3D == type(self.entity2)) or + (SlvsLine3D == type(self.entity1) or SlvsLine3D == type(self.entity2))): + max_distance_dof = 3 + elif ((type(self.entity1) == SlvsLine2D and self.entity2 == None) or + type(self.entity1) == SlvsPoint2D and type(self.entity2) == SlvsPoint2D): + max_distance_dof = 2 + else: + max_distance_dof = 1 + else: + new_dependencies = [i for i in [self.entity1, self.sketch] if i is not None] + max_distance_dof = 1 + + + distance_dof = 0 + for c in context.scene.sketcher.constraints.all: + if type(c) == constraint_type: + if set(c.dependencies()) == set(new_dependencies): + if constraint_type == SlvsDistance: + distance_dof +=1 + else: + return True + + if distance_dof < max_distance_dof: + return False + else: + return True \ No newline at end of file