Salome HOME
updated copyright message
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintTangent.py
1 # Copyright (C) 2014-2023  CEA, EDF
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 """
21     TestConstraintTangent.py
22     Unit test of SketchPlugin_ConstraintTangent class
23
24     SketchPlugin_ConstraintTangent
25         static const std::string MY_CONSTRAINT_TANGENT_ID("SketchConstraintTangent");
26         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
27         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
28
29 """
30 from GeomDataAPI import *
31 from GeomAPI import *
32 from ModelAPI import *
33 import math
34 from salome.shaper import model
35
36 #=========================================================================
37 # Initialization of the test
38 #=========================================================================
39
40 __updated__ = "2015-03-17"
41
42 def checkArcLineTangency(theArc, theLine):
43     """
44     subroutine to check that the line is tangent to arc/circle
45     """
46     if (theArc.getKind() == "SketchCircle"):
47         aCenter = geomDataAPI_Point2D(theArc.attribute("circle_center"))
48         aRadius = theArc.real("circle_radius").value()
49     else:
50         aCenter = geomDataAPI_Point2D(theArc.attribute("center_point"))
51         aStartPnt = geomDataAPI_Point2D(theArc.attribute("start_point"))
52         aRadius = model.distancePointPoint(aStartPnt, aCenter)
53     aDist = model.distancePointLine(aCenter, theLine)
54     assert math.fabs(aDist - aRadius) < 2.e-5, "aDist = {0}, aRadius = {1}".format(aDist, aRadius)
55
56 def checkArcArcTangency(theArc1, theArc2):
57     """
58     subroutine to check that arcs/circles arc tangent
59     """
60     anArcs = [theArc1, theArc2]
61     aCenters = []
62     aRadii = []
63     for anArc in anArcs:
64         if (anArc.getKind() == "SketchCircle"):
65             aCenter = geomDataAPI_Point2D(anArc.attribute("circle_center"))
66             aRadius = anArc.real("circle_radius").value()
67         else:
68             aCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
69             aStartPnt = geomDataAPI_Point2D(anArc.attribute("start_point"))
70             aRadius = model.distancePointPoint(aStartPnt, aCenter)
71         aCenters.append(aCenter)
72         aRadii.append(aRadius)
73     aDist = model.distancePointPoint(aCenters[0], aCenters[1])
74     aRSum = aRadii[0] + aRadii[1]
75     aRDiff = math.fabs(aRadii[0] - aRadii[1])
76     assert math.fabs(aDist - aRSum) < 2.e-5 or math.fabs(aDist - aRDiff) < 2.e-5, "aDist = {0}, aRSum = {1}, aRDiff = {2}".format(aDist, aRSum, aRDiff)
77
78
79 aSession = ModelAPI_Session.get()
80 aDocument = aSession.moduleDocument()
81 #=========================================================================
82 # Creation of a sketch
83 #=========================================================================
84 aSession.startOperation()
85 aSketchCommonFeature = aDocument.addFeature("Sketch")
86 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
87 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
88 origin.setValue(0, 0, 0)
89 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
90 dirx.setValue(1, 0, 0)
91 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
92 norm.setValue(0, 0, 1)
93 aSession.finishOperation()
94 #=========================================================================
95 # TEST 1. Arc-line tangency
96 #=========================================================================
97 # Creation of an arc and two lines
98 #=========================================================================
99 # Arc
100 aSession.startOperation()
101 aSketchArc1 = aSketchFeature.addFeature("SketchArc")
102 anArcCentr = geomDataAPI_Point2D(aSketchArc1.attribute("center_point"))
103 anArcCentr.setValue(10., 10.)
104 anArcStartPoint = geomDataAPI_Point2D(aSketchArc1.attribute("start_point"))
105 anArcStartPoint.setValue(0., 50.)
106 anArcEndPoint = geomDataAPI_Point2D(aSketchArc1.attribute("end_point"))
107 anArcEndPoint.setValue(50., 0.)
108 aSession.finishOperation()
109 # Line 1
110 aSession.startOperation()
111 aSketchLine1 = aSketchFeature.addFeature("SketchLine")
112 aLine1StartPoint = geomDataAPI_Point2D(aSketchLine1.attribute("StartPoint"))
113 aLine1EndPoint = geomDataAPI_Point2D(aSketchLine1.attribute("EndPoint"))
114 aLine1StartPoint.setValue(0., 50.)
115 aLine1EndPoint.setValue(0., 100.)
116 aSession.finishOperation()
117 # Line 2
118 aSession.startOperation()
119 aSketchLine2 = aSketchFeature.addFeature("SketchLine")
120 aLine2StartPoint = geomDataAPI_Point2D(aSketchLine2.attribute("StartPoint"))
121 aLine2EndPoint = geomDataAPI_Point2D(aSketchLine2.attribute("EndPoint"))
122 aLine2StartPoint.setValue(50., 0.)
123 aLine2EndPoint.setValue(100., 0.)
124 aSession.finishOperation()
125 assert (model.dof(aSketchFeature) == 13)
126 #=========================================================================
127 # Link arc points and lines points by the coincidence constraint
128 #=========================================================================
129 aSession.startOperation()
130 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
131 reflistA = aConstraint.refattr("ConstraintEntityA")
132 reflistB = aConstraint.refattr("ConstraintEntityB")
133 reflistA.setAttr(anArcStartPoint)
134 reflistB.setAttr(aLine1StartPoint)
135 aConstraint.execute()
136 aSession.finishOperation()
137 aSession.startOperation()
138 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
139 reflistA = aConstraint.refattr("ConstraintEntityA")
140 reflistB = aConstraint.refattr("ConstraintEntityB")
141 reflistA.setAttr(anArcEndPoint)
142 reflistB.setAttr(aLine2StartPoint)
143 aConstraint.execute()
144 aSession.finishOperation()
145 assert (model.dof(aSketchFeature) == 9)
146 #=========================================================================
147 # Add tangency constraint and check correctness
148 #=========================================================================
149 aSession.startOperation()
150 aTangency = aSketchFeature.addFeature("SketchConstraintTangent")
151 aRefObjectA = aTangency.refattr("ConstraintEntityA")
152 aRefObjectB = aTangency.refattr("ConstraintEntityB")
153 anObjectA = modelAPI_ResultConstruction(aSketchArc1.lastResult())
154 anObjectB = modelAPI_ResultConstruction(aSketchLine1.lastResult())
155 assert (anObjectA is not None)
156 assert (anObjectB is not None)
157 aRefObjectA.setObject(anObjectA)
158 aRefObjectB.setObject(anObjectB)
159 aTangency.execute()
160 aSession.finishOperation()
161 checkArcLineTangency(aSketchArc1, aSketchLine1)
162 assert (model.dof(aSketchFeature) == 8)
163 #=========================================================================
164 # Add tangency constraint for arc and second line and check correctness
165 #=========================================================================
166 aSession.startOperation()
167 aTangency = aSketchFeature.addFeature("SketchConstraintTangent")
168 aRefObjectA = aTangency.refattr("ConstraintEntityA")
169 aRefObjectB = aTangency.refattr("ConstraintEntityB")
170 anObjectA = modelAPI_ResultConstruction(aSketchArc1.lastResult())
171 anObjectB = modelAPI_ResultConstruction(aSketchLine2.lastResult())
172 assert (anObjectA is not None)
173 assert (anObjectB is not None)
174 aRefObjectA.setObject(anObjectA)
175 aRefObjectB.setObject(anObjectB)
176 aTangency.execute()
177 aSession.finishOperation()
178 checkArcLineTangency(aSketchArc1, aSketchLine2)
179 assert (model.dof(aSketchFeature) == 7)
180
181 #=========================================================================
182 # TEST 2. Arc-arc tangency
183 #=========================================================================
184 # Creation of arcs
185 #=========================================================================
186 # Arc 1
187 aSession.startOperation()
188 aSketchArc1 = aSketchFeature.addFeature("SketchArc")
189 anArc1Centr = geomDataAPI_Point2D(aSketchArc1.attribute("center_point"))
190 anArc1Centr.setValue(10., 10.)
191 anArc1StartPoint = geomDataAPI_Point2D(aSketchArc1.attribute("start_point"))
192 anArc1StartPoint.setValue(50., 0.)
193 anArc1EndPoint = geomDataAPI_Point2D(aSketchArc1.attribute("end_point"))
194 anArc1EndPoint.setValue(0., 50.)
195 aSession.finishOperation()
196 # Arc 2
197 aSession.startOperation()
198 aSketchArc2 = aSketchFeature.addFeature("SketchMacroArc")
199 aSketchArc2.string("arc_type").setValue("by_three_points")
200 anArc2StartPoint = geomDataAPI_Point2D(aSketchArc2.attribute("start_point_2"))
201 anArc2StartPoint.setValue(0., 50.)
202 anArc2EndPoint = geomDataAPI_Point2D(aSketchArc2.attribute("end_point_2"))
203 anArc2EndPoint.setValue(-50., 0.)
204 anArc2PassedPoint = geomDataAPI_Point2D(aSketchArc2.attribute("passed_point"))
205 anArc2PassedPoint.setValue(-40., 40.)
206 aSession.finishOperation()
207 #assert (model.dof(aSketchFeature) == 17)
208 aSketchArc2 = model.lastSubFeature(aSketchFeature, "SketchArc")
209 anArc2Centr = geomDataAPI_Point2D(aSketchArc2.attribute("center_point"))
210 anArc2StartPoint = geomDataAPI_Point2D(aSketchArc2.attribute("start_point"))
211 #=========================================================================
212 # Link points of arcs by the coincidence constraint
213 #=========================================================================
214 aSession.startOperation()
215 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
216 reflistA = aConstraint.refattr("ConstraintEntityA")
217 reflistB = aConstraint.refattr("ConstraintEntityB")
218 reflistA.setAttr(anArc1EndPoint)
219 reflistB.setAttr(anArc2StartPoint)
220 aConstraint.execute()
221 aSession.finishOperation()
222 assert (model.dof(aSketchFeature) == 15)
223 #=========================================================================
224 # Add tangency constraint and check correctness
225 #=========================================================================
226 aSession.startOperation()
227 aTangency = aSketchFeature.addFeature("SketchConstraintTangent")
228 aRefObjectA = aTangency.refattr("ConstraintEntityA")
229 aRefObjectB = aTangency.refattr("ConstraintEntityB")
230 anObjectA = modelAPI_ResultConstruction(aSketchArc1.lastResult())
231 anObjectB = modelAPI_ResultConstruction(aSketchArc2.lastResult())
232 assert (anObjectA is not None)
233 assert (anObjectB is not None)
234 aRefObjectA.setObject(anObjectA)
235 aRefObjectB.setObject(anObjectB)
236 aTangency.execute()
237 aSession.finishOperation()
238 checkArcArcTangency(aSketchArc1, aSketchArc2)
239 assert (model.dof(aSketchFeature) == 14)
240
241 #=========================================================================
242 # TEST 3. Tangency between non-connected objects should work
243 #=========================================================================
244 # 3.1 tangency between arc2 and line2
245 aSession.startOperation()
246 aTangency = aSketchFeature.addFeature("SketchConstraintTangent")
247 aRefObjectA = aTangency.refattr("ConstraintEntityA")
248 aRefObjectB = aTangency.refattr("ConstraintEntityB")
249 anObjectA = modelAPI_ResultConstruction(aSketchArc2.lastResult())
250 anObjectB = modelAPI_ResultConstruction(aSketchLine2.lastResult())
251 assert (anObjectA is not None)
252 assert (anObjectB is not None)
253 aRefObjectA.setObject(anObjectA)
254 aRefObjectB.setObject(anObjectB)
255 aTangency.execute()
256 aSession.finishOperation()
257 checkArcLineTangency(aSketchArc2, aSketchLine2)
258 assert (model.dof(aSketchFeature) == 13)
259
260 aSession.startOperation()
261 aDocument.removeFeature(aTangency)
262 aSession.finishOperation()
263 assert (model.dof(aSketchFeature) == 14)
264
265 # 3.2  tangency between non-connected arcs
266 aSession.startOperation()
267 aSketchArc3 = aSketchFeature.addFeature("SketchArc")
268 anArc3Centr = geomDataAPI_Point2D(aSketchArc3.attribute("center_point"))
269 anArc3Centr.setValue(100., -10.)
270 anArc3StartPoint = geomDataAPI_Point2D(aSketchArc3.attribute("start_point"))
271 anArc3StartPoint.setValue(70., -10.)
272 anArc3EndPoint = geomDataAPI_Point2D(aSketchArc3.attribute("end_point"))
273 anArc3EndPoint.setValue(100., 20.)
274 aSession.finishOperation()
275 assert (model.dof(aSketchFeature) == 19)
276
277 aSession.startOperation()
278 aTangency = aSketchFeature.addFeature("SketchConstraintTangent")
279 aTangency.refattr("ConstraintEntityA").setObject(modelAPI_ResultConstruction(aSketchArc2.lastResult()))
280 aTangency.refattr("ConstraintEntityB").setObject(modelAPI_ResultConstruction(aSketchArc3.lastResult()))
281 aSession.finishOperation()
282 checkArcArcTangency(aSketchArc2, aSketchArc3)
283 assert (model.dof(aSketchFeature) == 18)
284
285 aSession.startOperation()
286 aDocument.removeFeature(aSketchArc3)
287 aDocument.removeFeature(aTangency)
288 aSession.finishOperation()
289 assert (model.dof(aSketchFeature) == 14)
290
291 # 3.3  tangency between arc and circle
292 aSession.startOperation()
293 aCircle1 = aSketchFeature.addFeature("SketchCircle")
294 aCircleCenter = geomDataAPI_Point2D(aCircle1.attribute("circle_center"))
295 aCircleRadius = aCircle1.real("circle_radius")
296 aCircleCenter.setValue(150., 100.)
297 aCircleRadius.setValue(50.)
298 aSession.finishOperation()
299 assert (model.dof(aSketchFeature) == 17)
300
301 aSession.startOperation()
302 aTangency1 = aSketchFeature.addFeature("SketchConstraintTangent")
303 aTangency1.refattr("ConstraintEntityA").setObject(modelAPI_ResultConstruction(aSketchArc2.lastResult()))
304 aTangency1.refattr("ConstraintEntityB").setObject(modelAPI_ResultConstruction(aCircle1.lastResult()))
305 aSession.finishOperation()
306 checkArcArcTangency(aSketchArc2, aCircle1)
307 assert (model.dof(aSketchFeature) == 16)
308
309 # 3.4  tangency between two circles
310 aSession.startOperation()
311 aCircle2 = aSketchFeature.addFeature("SketchCircle")
312 aCircleCenter = geomDataAPI_Point2D(aCircle2.attribute("circle_center"))
313 aCircleRadius = aCircle2.real("circle_radius")
314 aCircleCenter.setValue(120., 70.)
315 aCircleRadius.setValue(20.)
316 aSession.finishOperation()
317 assert (model.dof(aSketchFeature) == 19)
318
319 aSession.startOperation()
320 aTangency2 = aSketchFeature.addFeature("SketchConstraintTangent")
321 aTangency2.refattr("ConstraintEntityA").setObject(modelAPI_ResultConstruction(aCircle1.lastResult()))
322 aTangency2.refattr("ConstraintEntityB").setObject(modelAPI_ResultConstruction(aCircle2.lastResult()))
323 aSession.finishOperation()
324 checkArcArcTangency(aCircle1, aCircle2)
325 assert (model.dof(aSketchFeature) == 18)
326
327 #=========================================================================
328 # TEST 4. Creating of tangency arc by the option of the SketchArc feature
329 #=========================================================================
330 aSession.startOperation()
331 aSketchArc3 = aSketchFeature.addFeature("SketchMacroArc")
332 aSketchArc3.string("arc_type").setValue("by_tangent_edge")
333 anArc3Start = aSketchArc3.refattr("tangent_point")
334 anArc3Start.setAttr(anArc1StartPoint)
335 anArc3EndPoint = geomDataAPI_Point2D(aSketchArc3.attribute("end_point_3"))
336 anArc3EndPoint.setValue(anArc1StartPoint.x()-5, anArc1StartPoint.y()-30)
337 aSession.finishOperation()
338 aSketchArc3 = model.lastSubFeature(aSketchFeature, "SketchArc")
339 checkArcArcTangency(aSketchArc1, aSketchArc3)
340 # freeze radius of tangent arc
341 aSession.startOperation()
342 aConstraintRadius = aSketchFeature.addFeature("SketchConstraintRadius")
343 aConstraintRadius.refattr("ConstraintEntityA").setObject(modelAPI_ResultConstruction(aSketchArc3.lastResult()))
344 aConstraintRadius.real("ConstraintValue").setValue(30.)
345 aSession.finishOperation()
346 checkArcArcTangency(aSketchArc1, aSketchArc3)
347 # do not check DoF here because it is unstable for tangent arcs,
348 # remove tangent arc to avoid instability while dumping
349 aSession.startOperation()
350 ModelAPI.removeFeaturesAndReferences(FeatureSet([aSketchArc3]))
351 aSession.finishOperation()
352 assert (model.dof(aSketchFeature) == 18)
353
354 #=========================================================================
355 # TEST 5. Creating of tangency between line and circle
356 #=========================================================================
357 aSession.startOperation()
358 aLine = aSketchFeature.addFeature("SketchLine")
359 aLineStart = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
360 aLineEnd = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
361 aLineStart.setValue(100., 100.)
362 aLineEnd.setValue(200., 200.)
363 aCircle = aSketchFeature.addFeature("SketchCircle")
364 aCircleCenter = geomDataAPI_Point2D(aCircle.attribute("circle_center"))
365 aCircleRadius = aCircle.real("circle_radius")
366 aCircleCenter.setValue(150., 100.)
367 aCircleRadius.setValue(20.)
368 aSession.finishOperation()
369 assert (model.dof(aSketchFeature) == 25)
370
371 aSession.startOperation()
372 aTangency = aSketchFeature.addFeature("SketchConstraintTangent")
373 aRefObjectA = aTangency.refattr("ConstraintEntityA")
374 aRefObjectB = aTangency.refattr("ConstraintEntityB")
375 anObjectA = modelAPI_ResultConstruction(aLine.lastResult())
376 anObjectB = modelAPI_ResultConstruction(aCircle.lastResult())
377 assert (anObjectA is not None)
378 assert (anObjectB is not None)
379 aRefObjectA.setObject(anObjectA)
380 aRefObjectB.setObject(anObjectB)
381 aTangency.execute()
382 aSession.finishOperation()
383
384 checkArcLineTangency(aCircle, aLine)
385 assert (model.dof(aSketchFeature) == 24)
386 #=========================================================================
387 # End of test
388 #=========================================================================
389
390 assert(model.checkPythonDump())