forked from hlorus/CAD_Sketcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
262 lines (206 loc) · 8.03 KB
/
solver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import logging
from .functions import bpyEnum
from .global_data import solver_state_items
logger = logging.getLogger(__name__)
class Solver:
group_fixed = 1
group_3d = 2
start_sketch_groups = 3
# iterate over constraints of active group and lazily init required entities
def __init__(self, context, sketch, all=False):
self.context = context
self.entities = []
self.constraints = {}
self.tweak_entity = None
self.tweak_pos = None
self.tweak_constraint = None
self.report = False
self.all = all
self.failed_sketches = []
group = self._get_group(sketch) if sketch else self.group_3d
logger.info(
"--- Start solving ---\nAll:{}, Sketch:{}, g:{}".format(
all, sketch, group
)
)
from py_slvs import slvs
self.solvesys = slvs.System()
self.FREE_IN_3D = slvs.SLVS_FREE_IN_3D
self.sketch = sketch
self.ok = True
self.result = None
def get_workplane(self):
if self.sketch:
return self.sketch.wp.py_data
return self.FREE_IN_3D
def _store_constraint_indices(self, c, indices):
for i in indices:
self.constraints[i] = c
def _get_group(self, sketch):
if not sketch:
return self.group_3d
type, index = self.context.scene.sketcher.entities._breakdown_index(
sketch.slvs_index
)
return self.start_sketch_groups + index
def _init_slvs_data(self):
context = self.context
# Initialize Entities
for e in context.scene.sketcher.entities.all:
self.entities.append(e)
if e.fixed:
group = self.group_fixed
elif hasattr(e, "sketch"):
group = self._get_group(e.sketch)
else:
group = self.group_3d
if self.tweak_entity and e == self.tweak_entity:
wp = self.get_workplane()
if hasattr(e, "tweak"):
e.tweak(self.solvesys, self.tweak_pos, group)
else:
if not self.sketch:
params = [
self.solvesys.addParamV(val, group)
for val in self.tweak_pos
]
p = self.solvesys.addPoint3d(*params, group=group)
else:
wrkpln = self.sketch.wp
u, v, _ = wrkpln.matrix_basis.inverted() @ self.tweak_pos
params = [self.solvesys.addParamV(val, group) for val in (u, v)]
p = self.solvesys.addPoint2d(
wrkpln.py_data, *params, group=group
)
e.create_slvs_data(self.solvesys, group=group)
from .class_defines import make_coincident
self.tweak_constraint = make_coincident(
self.solvesys, p, e, wp, group
)
self.solvesys.addWhereDragged(p, wrkpln=wp, group=group)
continue
e.create_slvs_data(self.solvesys, group=group)
def _get_msg():
msg = "Initialize entities:"
for e in context.scene.sketcher.entities.all:
msg += "\n - {}".format(e)
return msg
logger.debug(_get_msg())
# Initialize Constraints
for c in context.scene.sketcher.constraints.all:
if hasattr(c, "sketch") and c.sketch:
group = self._get_group(c.sketch)
else:
group = self.group_3d
if self.report:
c.failed = False
# Store a index-constraint mapping
from collections.abc import Iterable
indices = c.create_slvs_data(self.solvesys, group=group)
self._store_constraint_indices(
c, indices if isinstance(indices, Iterable) else (indices,)
)
def _get_msg():
msg = "Initialize constraints:"
for c in context.scene.sketcher.constraints.all:
msg += "\n - {}".format(c)
return msg
logger.debug(_get_msg())
def tweak(self, entity, pos):
logger.debug("tweak: {} to: {}".format(entity, pos))
self.tweak_entity = entity
# NOTE: there should be a difference between 2d coords or 3d location...
self.tweak_pos = pos
def is_active(self, e):
if e.fixed:
return False
return e.is_active(self.sketch)
# NOTE: When solving not everything might be relevant...
# An approach could be to find all constraints of a sketch and all neccesary entities
# and only inizialize them
# def dummy():
# wp = None
# if context.scene.sketcher.active_workplane_i == -1:
# group = self.group_3d
# else:
# wp = context.scene.sketcher.active_workplane
# # i = context.scene.sketcher.entities.get_local_index(wp.slvs_index)
# # group = i + 2
# group = group_wp
#
# constraints = self.get_constraints(context, wp)
#
# entities = []
# for c in constraints:
# # ensure entities are initialized
# for e in c.entities(): # should be recursive!
# if e not in entities:
# entities.append(e)
#
# c.create_slvs_data(solvesys)
# def get_constraints(self, context, wp):
# constraints = []
# for c in context.scene.sketcher.constraints.all:
# if wp and not hasattr(c, "wp"):
# continue
# if hasattr(c, "wp") and c.wp != wp:
# continue # c.is_active(group)
# constraints.append(c)
# return constraints
def solve(self, report=True):
self.report = report
self._init_slvs_data()
if self.all:
sse = self.context.scene.sketcher.entities
sketches = [None, *sse.sketches]
else:
sketches = [self.sketch,]
for sketch in sketches:
g = self._get_group(sketch)
retval = self.solvesys.solve(
group=g,
reportFailed=report,
findFreeParams=False,
)
# NOTE: For some reason solve() might return undocumented values,
# Clamp result value to 4
if retval > 3:
logger.debug("Solver returned undocumented value: {}".format(retval))
retval = 4
self.result = bpyEnum(solver_state_items, index=retval)
if report and sketch:
sketch.solver_state = self.result.index
sketch.dof = self.solvesys.Dof
if retval != 0:
self.ok = False
# Store sketch failures
self.failed_sketches.append(sketch)
logger.info(self.result.description)
fails = self.solvesys.Failed
if report and fails:
for i in fails:
if i == self.tweak_constraint:
continue
constr = self.constraints[i]
constr.failed = True
def _get_msg():
msg = "Failed constraints:"
for i in fails:
constr = self.constraints[i]
msg += "\n - {}".format(constr)
return msg
logger.debug(_get_msg())
msg = ""
for e in self.entities:
# Skip entities that belong to a failed sketch
if hasattr(e, "sketch") and e.sketch in self.failed_sketches:
continue
# TODO: skip entities that aren't in active group
msg += "\n - " + str(e)
e.update_from_slvs(self.solvesys)
if msg:
logger.debug("Update entities from solver:" + msg)
return self.ok
def solve_system(context, sketch=None):
solver = Solver(context, sketch)
return solver.solve()