]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/Test/TestConstraintEqual.py
Salome HOME
Code cleanup in SketchPlugin and SketchSolver.
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintEqual.py
1 """
2     TestConstraintEqual.py
3     Unit test of SketchPlugin_ConstraintEqual class
4         
5     SketchPlugin_ConstraintEqual
6         static const std::string MY_CONSTRAINT_EQAUL_ID("SketchConstraintEqual");
7         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
8         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
9
10 """
11 from GeomDataAPI import *
12 from ModelAPI import *
13 import math
14 #=========================================================================
15 # Initialization of the test
16 #=========================================================================
17
18 __updated__ = "2015-03-16"
19
20 #=========================================================================
21 # Auxiliary functions
22 #=========================================================================
23 def externalSketch(theDoc):
24     aSketchFeature = featureToCompositeFeature(theDoc.addFeature("Sketch"))
25     origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
26     origin.setValue(0, 0, 0)
27     dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
28     dirx.setValue(1, 0, 0)
29     norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
30     norm.setValue(0, 0, 1)
31     # add circle
32     circle = aSketchFeature.addFeature("SketchCircle")
33     circleCenter = geomDataAPI_Point2D(circle.attribute("CircleCenter"))
34     circleRadius = circle.real("CircleRadius")
35     circleCenter.setValue(-50., 50.)
36     circleRadius.setValue(10.)
37     # add line
38     line = aSketchFeature.addFeature("SketchLine")
39     lineStart = geomDataAPI_Point2D(line.attribute("StartPoint"))
40     lineEnd = geomDataAPI_Point2D(line.attribute("EndPoint"))
41     lineStart.setValue(-40., 35.)
42     lineEnd.setValue(-60., 25.)
43
44 def lineLength(theLine):
45     aLineStart = geomDataAPI_Point2D(theLine.attribute("StartPoint"))
46     aLineEnd = geomDataAPI_Point2D(theLine.attribute("EndPoint"))
47     aVecX = aLineStart.x() - aLineEnd.x()
48     aVecY = aLineStart.y() - aLineEnd.y()
49     return math.hypot(aVecX, aVecY)
50
51 def arcLength(theArc):
52     aCenter = geomDataAPI_Point2D(theArc.attribute("ArcCenter"))
53     aStart  = geomDataAPI_Point2D(theArc.attribute("ArcStartPoint"))
54     aEnd    = geomDataAPI_Point2D(theArc.attribute("ArcEndPoint"))
55     # use the law of cosines to calculate angular length of arc
56     aRadius = math.hypot(aStart.x() - aCenter.x(), aStart.y() - aCenter.y())
57     aDist2 = (aEnd.x()-aStart.x())**2 + (aEnd.y()-aStart.y())**2
58     anAngle = math.acos(1. - aDist2 / (2. * aRadius**2))
59     return aRadius * anAngle
60
61
62 #=========================================================================
63 # Start of test
64 #=========================================================================
65 aSession = ModelAPI_Session.get()
66 aDocument = aSession.moduleDocument()
67 #=========================================================================
68 # Creation external sketch
69 #=========================================================================
70 aSession.startOperation()
71 externalSketch(aDocument)
72 aSession.finishOperation()
73 #=========================================================================
74 # Creation of a sketch
75 #=========================================================================
76 aSession.startOperation()
77 aSketchCommonFeature = aDocument.addFeature("Sketch")
78 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
79 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
80 origin.setValue(0, 0, 0)
81 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
82 dirx.setValue(1, 0, 0)
83 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
84 norm.setValue(0, 0, 1)
85 aSession.finishOperation()
86 #=========================================================================
87 # Creation of an arc and a circle
88 #=========================================================================
89 aSession.startOperation()
90 aSketchArc = aSketchFeature.addFeature("SketchArc")
91 anArcCentr = geomDataAPI_Point2D(aSketchArc.attribute("ArcCenter"))
92 anArcCentr.setValue(10., 10.)
93 anArcStartPoint = geomDataAPI_Point2D(aSketchArc.attribute("ArcStartPoint"))
94 anArcStartPoint.setValue(0., 50.)
95 anArcEndPoint = geomDataAPI_Point2D(aSketchArc.attribute("ArcEndPoint"))
96 anArcEndPoint.setValue(50., 0.)
97 aSession.finishOperation()
98 # Circle
99 aSession.startOperation()
100 aSketchCircle = aSketchFeature.addFeature("SketchCircle")
101 anCircleCentr = geomDataAPI_Point2D(aSketchCircle.attribute("CircleCenter"))
102 aCircleRadius = aSketchCircle.real("CircleRadius")
103 anCircleCentr.setValue(-25., -25.)
104 aCircleRadius.setValue(25.)
105 aSession.finishOperation()
106 #=========================================================================
107 # A constraint to make equal radii of arc and circle
108 #=========================================================================
109 aSession.startOperation()
110 aConstraintEqRad = aSketchFeature.addFeature("SketchConstraintEqual")
111 aRefObjectA = aConstraintEqRad.refattr("ConstraintEntityA")
112 aRefObjectB = aConstraintEqRad.refattr("ConstraintEntityB")
113 aResultA = modelAPI_ResultConstruction(aSketchArc.lastResult())
114 aResultB = modelAPI_ResultConstruction(aSketchCircle.lastResult())
115 assert (aResultA is not None)
116 assert (aResultB is not None)
117 aRefObjectA.setObject(aResultA)
118 aRefObjectB.setObject(aResultB)
119 aConstraintEqRad.execute()
120 aSession.finishOperation()
121 aCircRadius = aCircleRadius.value();
122 anArcVecX = anArcStartPoint.x() - anArcCentr.x()
123 anArcVecY = anArcStartPoint.y() - anArcCentr.y()
124 anArcRadius = math.sqrt(anArcVecX**2 + anArcVecY**2)
125 assert (math.fabs(aCircRadius - anArcRadius) <= 1.e-10)
126 #=========================================================================
127 # A constraint to make equal radii of arc and external circle
128 #=========================================================================
129 aSession.startOperation()
130 anExtCircRes = modelAPI_Result(aDocument.objectByName("Construction", "SketchCircle_1"))
131 assert(anExtCircRes)
132 anExtCircShape = anExtCircRes.shape()
133 assert(anExtCircShape)
134 anExtCircle = aSketchFeature.addFeature("SketchCircle")
135 anExtCircleCenter = geomDataAPI_Point2D(anExtCircle.attribute("CircleCenter"))
136 anExtCircleRadius = anExtCircle.real("CircleRadius")
137 anExtCircleCenter.setValue(-50., 50.)
138 anExtCircleRadius.setValue(10.)
139 anExtCircle.selection("External").setValue(anExtCircRes, anExtCircShape)
140 aSession.finishOperation()
141 aSession.startOperation()
142 aConstraintEqRad2 = aSketchFeature.addFeature("SketchConstraintEqual")
143 aRefObjectA = aConstraintEqRad2.refattr("ConstraintEntityA")
144 aRefObjectB = aConstraintEqRad2.refattr("ConstraintEntityB")
145 aRefObjectA.setObject(aSketchCircle.lastResult())
146 aRefObjectB.setObject(anExtCircle.lastResult())
147 aSession.finishOperation()
148 assert (math.fabs(anExtCircleRadius.value() - aCircleRadius.value()) < 1.e-10)
149 anArcVecX = anArcStartPoint.x() - anArcCentr.x()
150 anArcVecY = anArcStartPoint.y() - anArcCentr.y()
151 anArcRadius = math.sqrt(anArcVecX**2 + anArcVecY**2)
152 assert (math.fabs(anExtCircleRadius.value() - anArcRadius) <= 1.e-10)
153
154 #=========================================================================
155 # Creation of two different lines
156 #=========================================================================
157 # First Line
158 aSession.startOperation()
159 aSketchLine1 = aSketchFeature.addFeature("SketchLine")
160 aLine1StartPoint = geomDataAPI_Point2D(aSketchLine1.attribute("StartPoint"))
161 aLine1EndPoint = geomDataAPI_Point2D(aSketchLine1.attribute("EndPoint"))
162 aLine1StartPoint.setValue(0., 15.)
163 aLine1EndPoint.setValue(20., 25.)
164 aSession.finishOperation()
165 # Second Line
166 aSession.startOperation()
167 aSketchLine2 = aSketchFeature.addFeature("SketchLine")
168 aLine2StartPoint = geomDataAPI_Point2D(aSketchLine2.attribute("StartPoint"))
169 aLine2EndPoint = geomDataAPI_Point2D(aSketchLine2.attribute("EndPoint"))
170 aLine2StartPoint.setValue(0., 0.)
171 aLine2EndPoint.setValue(-1., 10.)
172 aSession.finishOperation()
173 #=========================================================================
174 # A constraint to make equal lengths of lines
175 #=========================================================================
176 aSession.startOperation()
177 aConstraintEqLen = aSketchFeature.addFeature("SketchConstraintEqual")
178 aRefObjectA = aConstraintEqLen.refattr("ConstraintEntityA")
179 aRefObjectB = aConstraintEqLen.refattr("ConstraintEntityB")
180 aResultA = modelAPI_ResultConstruction(aSketchLine1.firstResult())
181 aResultB = modelAPI_ResultConstruction(aSketchLine2.firstResult())
182 assert (aResultA is not None)
183 assert (aResultB is not None)
184 aRefObjectA.setObject(aResultA)
185 aRefObjectB.setObject(aResultB)
186 aConstraintEqLen.execute()
187 aSession.finishOperation()
188 aLine1Len = lineLength(aSketchLine1)
189 aLine2Len = lineLength(aSketchLine2)
190 assert (math.fabs(aLine1Len - aLine2Len) < 1.e-10)
191 #=========================================================================
192 # A constraint to make equal length of line with external line
193 #=========================================================================
194 aSession.startOperation()
195 anExtLineRes = modelAPI_Result(aDocument.objectByName("Construction", "SketchLine_1"))
196 assert(anExtLineRes)
197 anExtLineShape = anExtLineRes.shape()
198 assert(anExtLineShape)
199 anExtLine = aSketchFeature.addFeature("SketchLine")
200 anExtLineStart = geomDataAPI_Point2D(anExtLine.attribute("StartPoint"))
201 anExtLineEnd = geomDataAPI_Point2D(anExtLine.attribute("EndPoint"))
202 anExtLineStart.setValue(-40., 35.)
203 anExtLineEnd.setValue(-60., 25.)
204 anExtLine.selection("External").setValue(anExtLineRes, anExtLineShape)
205 anExtLineLen = lineLength(anExtLine)
206 aSession.finishOperation()
207 aSession.startOperation()
208 aConstraintEqLen2 = aSketchFeature.addFeature("SketchConstraintEqual")
209 aRefObjectA = aConstraintEqLen2.refattr("ConstraintEntityA")
210 aRefObjectB = aConstraintEqLen2.refattr("ConstraintEntityB")
211 aRefObjectA.setObject(anExtLine.lastResult())
212 aRefObjectB.setObject(aSketchLine2.lastResult())
213 aSession.finishOperation()
214 aLine1Len = lineLength(aSketchLine1)
215 aLine2Len = lineLength(aSketchLine2)
216 assert (math.fabs(aLine1Len - anExtLineLen) < 1.e-10)
217 assert (math.fabs(aLine2Len - anExtLineLen) < 1.e-10)
218
219 #=========================================================================
220 # A constraint to make equal lengths of line and arc
221 #=========================================================================
222 # Third Line
223 aSession.startOperation()
224 aSketchLine3 = aSketchFeature.addFeature("SketchLine")
225 aLine3StartPoint = geomDataAPI_Point2D(aSketchLine3.attribute("StartPoint"))
226 aLine3EndPoint = geomDataAPI_Point2D(aSketchLine3.attribute("EndPoint"))
227 aLine3StartPoint.setValue(20., 15.)
228 aLine3EndPoint.setValue(20., 25.)
229 aSession.finishOperation()
230 aSession.startOperation()
231 anEqArcLineLen = aSketchFeature.addFeature("SketchConstraintEqual")
232 aRefObjectA = anEqArcLineLen.refattr("ConstraintEntityA")
233 aRefObjectB = anEqArcLineLen.refattr("ConstraintEntityB")
234 aRefObjectA.setObject(aSketchArc.lastResult())
235 aRefObjectB.setObject(aSketchLine3.lastResult())
236 aSession.finishOperation()
237 aLine3Len = lineLength(aSketchLine3)
238 anArcLen = arcLength(aSketchArc)
239 assert (math.fabs(aLine3Len - anArcLen) < 1.e-7)
240 #=========================================================================
241 # End of test
242 #=========================================================================