Skip to content

Commit

Permalink
Use safe_constraints for add_line_2d
Browse files Browse the repository at this point in the history
This operator was the simplest to add it to, and serves as an example of how to use the context manager.
  • Loading branch information
bonjorno7 committed Nov 19, 2023
1 parent 9dffe59 commit aa1f283
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions operators/add_line_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from ..solver import solve_system
from .base_2d import Operator2d
from .constants import types_point_2d
from .utilities import ignore_hover
from .utilities import ignore_hover, safe_constraints

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -54,16 +54,16 @@ def main(self, context: Context):
self.target.construction = True

# auto vertical/horizontal constraint
constraints = context.scene.sketcher.constraints
vec_dir = self.target.direction_vec()
if vec_dir.length:
angle = vec_dir.angle(Vector((1, 0)))

threshold = 0.1
if angle < threshold or angle > HALF_TURN - threshold:
constraints.add_horizontal(self.target, sketch=self.sketch)
elif (QUARTER_TURN - threshold) < angle < (QUARTER_TURN + threshold):
constraints.add_vertical(self.target, sketch=self.sketch)

with safe_constraints(context, sketch=self.sketch) as constraints:
if angle < threshold or angle > HALF_TURN - threshold:
constraints.add_horizontal(self.target, sketch=self.sketch)
elif (QUARTER_TURN - threshold) < angle < (QUARTER_TURN + threshold):
constraints.add_vertical(self.target, sketch=self.sketch)

ignore_hover(self.target)
return True
Expand Down

0 comments on commit aa1f283

Please sign in to comment.