Skip to content

Commit

Permalink
Checks for duplicate constraints.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
TimStrauven committed Apr 17, 2024
1 parent d018d09 commit 06f1d40
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 67 deletions.
17 changes: 10 additions & 7 deletions operators/add_angle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)


Expand Down Expand Up @@ -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)

Expand Down
15 changes: 9 additions & 6 deletions operators/add_diameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)


Expand All @@ -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)

Expand Down
29 changes: 22 additions & 7 deletions operators/add_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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):
Expand Down
112 changes: 65 additions & 47 deletions operators/add_geometric_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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)


Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down
38 changes: 38 additions & 0 deletions operators/base_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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

0 comments on commit 06f1d40

Please sign in to comment.