forked from hlorus/CAD_Sketcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_constraint_init.py
304 lines (236 loc) · 9.33 KB
/
test_constraint_init.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import math
from unittest import skip
from CAD_Sketcher.testing.utils import Sketch2dTestCase
class TestConstraintAdd(Sketch2dTestCase):
def test_horizontal(self):
context = self.context
entities = self.entities
constraints = self.constraints
sketch = self.sketch
p0 = entities.add_point_2d((0, 0), sketch)
p0.fixed = True
# Test constraint between two points
p1 = entities.add_point_2d((3, 1), sketch)
c1 = constraints.add_horizontal(p0, entity2=p1, sketch=sketch)
self.solve()
self.assertAlmostEqual(p1.co.y, 0.0)
# Test constraint with one line
p2 = entities.add_point_2d((-3, -1), sketch)
line = entities.add_line_2d(p0, p2, sketch)
c2 = constraints.add_horizontal(line, sketch=sketch)
self.solve()
self.assertAlmostEqual(p2.co.y, 0.0)
@skip
def test_vertical(self):
context = self.context
entities = self.entities
constraints = self.constraints
sketch = self.sketch
p0 = entities.add_point_2d((0, 0), sketch)
p0.fixed = True
# Test constraint between two points
p1 = entities.add_point_2d((3, 1), sketch)
c1 = constraints.add_vertical(p0, entity2=p1, sketch=sketch)
self.solve()
self.assertAlmostEqual(p1.co.x, 0.0)
# Test constraint with one line
p2 = entities.add_point_2d((-3, -1), sketch)
line = entities.add_line_2d(p0, p2, sketch)
c2 = constraints.add_vertical(line, sketch=sketch)
self.solve()
self.assertAlmostEqual(p2.co.x, 0.0)
class TestConstraintInit(Sketch2dTestCase):
def test_distance(self):
context = self.context
entities = self.entities
constraints = self.constraints
sketch = self.sketch
p0 = entities.add_point_2d((0, 0), sketch)
p0.fixed = True
# Constrain single Line
p1 = entities.add_point_2d((-2, 0), sketch)
line = entities.add_line_2d(p0, p1, sketch)
c1 = constraints.add_distance(line, None, sketch=sketch, init=True)
self.solve()
self.assertAlmostEqual(line.length, 2.0)
self.assertAlmostEqual(c1.value, 2.0)
# Constrain 2 points
p2 = entities.add_point_2d((0.0, 2.0), sketch)
c2 = constraints.add_distance(p0, p2, sketch=sketch, init=True)
self.solve()
self.assertAlmostEqual(p2.co.y, 2.0)
self.assertAlmostEqual(c2.value, 2.0)
# Constrain line-point
# c3 = constraints.add_distance(p2, line, sketch=sketch, init=True)
# self.solve()
# self.assertAlmostEqual(c3.value, 2.0)
def test_distance_flip(self):
context = self.context
entities = self.entities
constraints = self.constraints
sketch = self.sketch
p0 = entities.add_point_2d((0, 0), sketch)
p0.fixed = True
# Line
p1 = entities.add_point_2d((1, -1), sketch)
p1.fixed = True
p2 = entities.add_point_2d((1, 1), sketch)
line = entities.add_line_2d(p1, p2, sketch)
c1 = constraints.add_distance(p0, line, sketch=sketch, init=True)
self.solve()
self.assertTrue(c1.flip)
self.assertAlmostEqual(p2.co.x, 1.0)
# Flip distance
c1.flip = False
self.assertAlmostEqual(p2.co.y, -1.0)
# Line2 (Opposite direction)
p3 = entities.add_point_2d((-1, -1), sketch)
p3.fixed = True
p4 = entities.add_point_2d((-1, 1), sketch)
line2 = entities.add_line_2d(p3, p4, sketch)
c2 = constraints.add_distance(p0, line2, sketch=sketch, init=True)
self.solve()
self.assertFalse(c2.flip)
self.assertAlmostEqual(p4.co.x, -1.0)
# Flip distance2
c2.flip = True
self.assertAlmostEqual(p4.co.y, -1.0)
def test_distance_aligned(self):
context = self.context
entities = self.entities
constraints = self.constraints
sketch = self.sketch
p0 = entities.add_point_2d((0, 0), sketch)
p0.fixed = True
p1 = entities.add_point_2d((1, 2), sketch)
c1 = constraints.add_distance(
p0, p1, sketch=sketch, init=True, align="VERTICAL"
)
c2 = constraints.add_distance(
p0, p1, sketch=sketch, init=True, align="HORIZONTAL"
)
self.solve()
self.assertAlmostEqual(c1.value, 2.0)
self.assertAlmostEqual(c2.value, 1.0)
# Change alignment
length = (p1.co - p0.co).length
c1.align = "NONE"
self.assertAlmostEqual(c1.value, length)
def test_diameter(self):
context = self.context
entities = self.entities
constraints = self.constraints
sketch = self.sketch
p0 = entities.add_point_2d((0, 0), sketch)
p0.fixed = True
nm = sketch.wp.nm
# Constrain circle diameter
circle1 = entities.add_circle(nm, p0, 3.0, sketch)
c1 = constraints.add_diameter(circle1, sketch=sketch, init=True, value=4.0)
self.solve()
self.assertAlmostEqual(circle1.radius, 2.0)
self.assertAlmostEqual(c1.value, 4.0)
# Constrain circle radius
circle2 = entities.add_circle(nm, p0, 3.0, sketch)
c2 = constraints.add_diameter(circle2, sketch=sketch, init=True, setting=True)
self.solve()
self.assertAlmostEqual(circle2.radius, 3.0)
self.assertAlmostEqual(c2.value, 3.0)
# Submit value and setting
circle3 = entities.add_circle(nm, p0, 1.0, sketch)
c3 = constraints.add_diameter(
circle3, sketch=sketch, init=True, value=0.8, setting=True
)
self.solve()
self.assertAlmostEqual(circle3.radius, 0.8)
self.assertAlmostEqual(c3.value, 0.8)
# Toggle radius/diameter
c1.setting = True
self.assertAlmostEqual(circle1.radius, 2.0)
self.assertAlmostEqual(c1.value, 2.0)
c2.setting = False
self.assertAlmostEqual(circle2.radius, 3.0)
self.assertAlmostEqual(c2.value, 6.0)
def test_diameter_arc(self):
context = self.context
entities = self.entities
constraints = self.constraints
sketch = self.sketch
p0 = entities.add_point_2d((0, 0), sketch)
p0.fixed = True
nm = sketch.wp.nm
# Constrain arc
p1 = entities.add_point_2d((3.0, 0.0), sketch)
p2 = entities.add_point_2d((0.0, 3.0), sketch)
arc1 = entities.add_arc(nm, p0, p1, p2, sketch)
c1 = constraints.add_diameter(arc1, sketch=sketch, init=True)
self.solve()
self.assertAlmostEqual(arc1.radius, 3.0)
self.assertAlmostEqual(c1.value, 6.0)
c1.setting = True
self.assertAlmostEqual(arc1.radius, 3.0)
self.assertAlmostEqual(c1.value, 3.0)
def test_angle(self):
context = self.context
entities = self.entities
constraints = self.constraints
sketch = self.sketch
p0 = entities.add_point_2d((0, 0), sketch)
p0.fixed = True
# Line1
p1 = entities.add_point_2d((1, 1), sketch)
p1.fixed = True
line1 = entities.add_line_2d(p0, p1, sketch)
# Line2
p2 = entities.add_point_2d((0, 1), sketch)
line2 = entities.add_line_2d(p0, p2, sketch)
c = constraints.add_angle(line1, line2, sketch=sketch, init=True)
self.solve()
self.assertAlmostEqual(p2.co.x, 0.0)
self.assertGreater(p2.co.y, 0.0)
self.assertAlmostEqual(c.value, math.radians(45))
# NOTE: Which value (45 / (180-45)) is constrained depends on the directions of the lines
c.setting = not c.setting
self.solve()
self.assertAlmostEqual(p2.co.x, 0.0)
self.assertGreater(p2.co.y, 0.0)
self.assertAlmostEqual(c.value, math.radians(180 - 45))
def test_distance_ref(self):
context = self.context
entities = self.entities
constraints = self.constraints
sketch = self.sketch
p0 = entities.add_point_2d((0, 0), sketch)
p0.fixed = True
p1 = entities.add_point_2d((1, 1), sketch)
c1 = constraints.add_distance(p0, p1, sketch=sketch)
c1.is_reference = True
p1.co = (2, 0)
self.assertAlmostEqual(c1.value, 2)
def test_diameter_ref(self):
context = self.context
entities = self.entities
constraints = self.constraints
sketch = self.sketch
p0 = entities.add_point_2d((0, 0), sketch)
p0.fixed = True
nm = sketch.wp.nm
circle = entities.add_circle(nm, p0, 3, sketch)
c1 = constraints.add_diameter(circle, sketch=sketch)
c1.is_reference = True
circle.radius = 2.5
self.assertAlmostEqual(c1.value, 5.0)
def test_ratio(self):
p0 = self.entities.add_point_2d((0, 0), self.sketch)
p0.fixed = True
p1 = self.entities.add_point_2d((3, 0), self.sketch)
line1 = self.entities.add_line_2d(p0, p1, self.sketch)
p2 = self.entities.add_point_2d((0, 1), self.sketch)
line2 = self.entities.add_line_2d(p0, p2, self.sketch)
c = self.constraints.add_ratio(line1, line2, self.sketch, init=True)
self.solve()
self.assertAlmostEqual(c.value, 3.0)
p2.fixed = True
c.value = 4.0
self.solve()
self.assertAlmostEqual(line1.length, 4.0)