Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve auto constraints #425

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 26 additions & 0 deletions operators/utilities.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from contextlib import contextmanager

import bpy
from bpy.types import Context, Operator
Expand All @@ -7,6 +8,9 @@
from .. import global_data
from ..declarations import GizmoGroups, WorkSpaceTools
from ..converters import update_convertor_geometry
from ..model.group_constraints import SlvsConstraints
from ..model.sketch import SlvsSketch
from ..solver import solve_system
from ..utilities.preferences import use_experimental, get_prefs
from ..utilities.data_handling import entities_3d

Expand Down Expand Up @@ -201,3 +205,25 @@ def select_target_ob(context, sketch):
if target_ob.name in context.view_layer.objects:
target_ob.select_set(True)
context.view_layer.objects.active = target_ob


@contextmanager
def safe_constraints(context: Context, sketch: SlvsSketch = None, constraints: SlvsConstraints = None):
sketch = sketch or context.scene.sketcher.active_sketch
constraints = constraints or context.scene.sketcher.constraints

solve = solve_system(context, sketch)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably avoid the initial solve check and just get the value from sketch.solver_state

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I wasn't aware of that, I'll use it instead of calling the function then yeah.

count = len(list(constraints.all))

try:
yield constraints

finally:
# Don't bother if the sketch can't be solved.
if not solve:
return

# Remove the newest constraint until the sketch solves.
new = list(constraints.all)[count:]
while not solve_system(context, sketch):
constraints.remove(new.pop())
Loading