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 all 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
4 changes: 2 additions & 2 deletions model/sketch.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ def is_visible(self, context):
def get_solver_state(self):
return bpyEnum(global_data.solver_state_items, identifier=self.solver_state)

def solve(self, context):
return solve_system(context, sketch=self)
def solve(self, context, report=True, update_entities=True):
return solve_system(context, sketch=self, report=report, update_entities=update_entities)

@classmethod
def is_sketch(cls):
Expand Down
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
34 changes: 34 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.base_constraint import GenericConstraint
from ..model.group_constraints import SlvsConstraints
from ..model.sketch import SlvsSketch
from ..utilities.preferences import use_experimental, get_prefs
from ..utilities.data_handling import entities_3d

Expand Down Expand Up @@ -201,3 +205,33 @@ 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,
remove_only_failed=False,
):
sketch = sketch or context.scene.sketcher.active_sketch
constraints = constraints or context.scene.sketcher.constraints

solvable = sketch.solver_state == "OKAY"
old: set[GenericConstraint] = set(constraints.all)

try:
yield constraints

finally:
if not solvable:
return

if not sketch.solve(context, update_entities=False):
new: set[GenericConstraint] = set(constraints.all) - old

for constraint in new:
if constraint.failed or not remove_only_failed:
constraints.remove(constraint)

sketch.solve(context, update_entities=False)
15 changes: 8 additions & 7 deletions solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def needs_update(self, e):
# TODO: skip entities that aren't in active group
return True

def solve(self, report=True):
def solve(self, report=True, update_entities=True):
self.report = report
self._init_slvs_data()

Expand Down Expand Up @@ -249,11 +249,12 @@ def _get_msg_failed():
logger.debug(_get_msg_failed())

# Update entities from solver
for e in self.entities:
if not self.needs_update(e):
continue
if update_entities:
for e in self.entities:
if not self.needs_update(e):
continue

e.update_from_slvs(self.solvesys)
e.update_from_slvs(self.solvesys)

def _get_msg_update():
msg = "Update entities from solver:"
Expand All @@ -269,6 +270,6 @@ def _get_msg_update():
return self.ok


def solve_system(context, sketch=None):
def solve_system(context, sketch=None, report=True, update_entities=True):
solver = Solver(context, sketch)
return solver.solve()
return solver.solve(report=report, update_entities=update_entities)
Loading