Salome HOME
Unit tests:
[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 defined by 3 points
32     circle = aSketchFeature.addFeature("SketchCircle")
33     circle.string("CircleType").setValue("ThreePoints")
34     geomDataAPI_Point2D(circle.attribute("FirstPoint")).setValue(-40., 50.)
35     geomDataAPI_Point2D(circle.attribute("SecondPoint")).setValue(-50., 60.)
36     geomDataAPI_Point2D(circle.attribute("ThirdPoint")).setValue(-60., 50.)
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
52 #=========================================================================
53 # Start of test
54 #=========================================================================
55 aSession = ModelAPI_Session.get()
56 aDocument = aSession.moduleDocument()
57 #=========================================================================
58 # Creation external sketch
59 #=========================================================================
60 aSession.startOperation()
61 externalSketch(aDocument)
62 aSession.finishOperation()
63 #=========================================================================
64 # Creation of a sketch
65 #=========================================================================
66 aSession.startOperation()
67 aSketchCommonFeature = aDocument.addFeature("Sketch")
68 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
69 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
70 origin.setValue(0, 0, 0)
71 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
72 dirx.setValue(1, 0, 0)
73 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
74 norm.setValue(0, 0, 1)
75 aSession.finishOperation()
76 #=========================================================================
77 # Creation of an arc and a circle
78 #=========================================================================
79 aSession.startOperation()
80 aSketchArc = aSketchFeature.addFeature("SketchArc")
81 anArcCentr = geomDataAPI_Point2D(aSketchArc.attribute("ArcCenter"))
82 anArcCentr.setValue(10., 10.)
83 anArcStartPoint = geomDataAPI_Point2D(aSketchArc.attribute("ArcStartPoint"))
84 anArcStartPoint.setValue(0., 50.)
85 anArcEndPoint = geomDataAPI_Point2D(aSketchArc.attribute("ArcEndPoint"))
86 anArcEndPoint.setValue(50., 0.)
87 aSession.finishOperation()
88 # Circle
89 aSession.startOperation()
90 aSketchCircle = aSketchFeature.addFeature("SketchCircle")
91 anCircleCentr = geomDataAPI_Point2D(aSketchCircle.attribute("CircleCenter"))
92 aCircleRadius = aSketchCircle.real("CircleRadius")
93 anCircleCentr.setValue(-25., -25.)
94 aCircleRadius.setValue(25.)
95 aSession.finishOperation()
96 #=========================================================================
97 # A constraint to make equal radii of arc and circle
98 #=========================================================================
99 aSession.startOperation()
100 aConstraintEqRad = aSketchFeature.addFeature("SketchConstraintEqual")
101 aRefObjectA = aConstraintEqRad.refattr("ConstraintEntityA")
102 aRefObjectB = aConstraintEqRad.refattr("ConstraintEntityB")
103 aResultA = modelAPI_ResultConstruction(aSketchArc.lastResult())
104 aResultB = modelAPI_ResultConstruction(aSketchCircle.lastResult())
105 assert (aResultA is not None)
106 assert (aResultB is not None)
107 aRefObjectA.setObject(aResultA)
108 aRefObjectB.setObject(aResultB)
109 aConstraintEqRad.execute()
110 aSession.finishOperation()
111 aCircRadius = aCircleRadius.value();
112 anArcVecX = anArcStartPoint.x() - anArcCentr.x()
113 anArcVecY = anArcStartPoint.y() - anArcCentr.y()
114 anArcRadius = math.sqrt(anArcVecX**2 + anArcVecY**2)
115 assert (math.fabs(aCircRadius - anArcRadius) <= 1.e-10)
116 #=========================================================================
117 # A constraint to make equal radii of arc and external circle
118 #=========================================================================
119 aSession.startOperation()
120 anExtCircRes = modelAPI_Result(aDocument.objectByName("Construction", "SketchCircle_1"))
121 assert(anExtCircRes)
122 anExtCircShape = anExtCircRes.shape()
123 assert(anExtCircShape)
124 anExtCircle = aSketchFeature.addFeature("SketchCircle")
125 anExtCircleCenter = geomDataAPI_Point2D(anExtCircle.attribute("CircleCenter"))
126 anExtCircleRadius = anExtCircle.real("CircleRadius")
127 anExtCircleCenter.setValue(-50., 50.)
128 anExtCircleRadius.setValue(10.)
129 anExtCircle.selection("External").setValue(anExtCircRes, anExtCircShape)
130 aSession.finishOperation()
131 aSession.startOperation()
132 aConstraintEqRad2 = aSketchFeature.addFeature("SketchConstraintEqual")
133 aRefObjectA = aConstraintEqRad2.refattr("ConstraintEntityA")
134 aRefObjectB = aConstraintEqRad2.refattr("ConstraintEntityB")
135 aRefObjectA.setObject(aSketchCircle.lastResult())
136 aRefObjectB.setObject(anExtCircle.lastResult())
137 aSession.finishOperation()
138 assert (math.fabs(anExtCircleRadius.value() - aCircleRadius.value()) < 1.e-10)
139 anArcVecX = anArcStartPoint.x() - anArcCentr.x()
140 anArcVecY = anArcStartPoint.y() - anArcCentr.y()
141 anArcRadius = math.sqrt(anArcVecX**2 + anArcVecY**2)
142 assert (math.fabs(anExtCircleRadius.value() - anArcRadius) <= 1.e-10)
143
144 #=========================================================================
145 # Creation of two different lines
146 #=========================================================================
147 # First Line
148 aSession.startOperation()
149 aSketchLine1 = aSketchFeature.addFeature("SketchLine")
150 aLine1StartPoint = geomDataAPI_Point2D(aSketchLine1.attribute("StartPoint"))
151 aLine1EndPoint = geomDataAPI_Point2D(aSketchLine1.attribute("EndPoint"))
152 aLine1StartPoint.setValue(0., 15.)
153 aLine1EndPoint.setValue(20., 25.)
154 aSession.finishOperation()
155 # Second Line
156 aSession.startOperation()
157 aSketchLine2 = aSketchFeature.addFeature("SketchLine")
158 aLine2StartPoint = geomDataAPI_Point2D(aSketchLine2.attribute("StartPoint"))
159 aLine2EndPoint = geomDataAPI_Point2D(aSketchLine2.attribute("EndPoint"))
160 aLine2StartPoint.setValue(0., 0.)
161 aLine2EndPoint.setValue(-1., 10.)
162 aSession.finishOperation()
163 #=========================================================================
164 # A constraint to make equal lengths of lines
165 #=========================================================================
166 aSession.startOperation()
167 aConstraintEqLen = aSketchFeature.addFeature("SketchConstraintEqual")
168 aRefObjectA = aConstraintEqLen.refattr("ConstraintEntityA")
169 aRefObjectB = aConstraintEqLen.refattr("ConstraintEntityB")
170 aResultA = modelAPI_ResultConstruction(aSketchLine1.firstResult())
171 aResultB = modelAPI_ResultConstruction(aSketchLine2.firstResult())
172 assert (aResultA is not None)
173 assert (aResultB is not None)
174 aRefObjectA.setObject(aResultA)
175 aRefObjectB.setObject(aResultB)
176 aConstraintEqLen.execute()
177 aSession.finishOperation()
178 aLine1Len = lineLength(aSketchLine1)
179 aLine2Len = lineLength(aSketchLine2)
180 assert (math.fabs(aLine1Len - aLine2Len) < 1.e-10)
181 #=========================================================================
182 # A constraint to make equal length of line with external line
183 #=========================================================================
184 aSession.startOperation()
185 anExtLineRes = modelAPI_Result(aDocument.objectByName("Construction", "SketchLine_1"))
186 assert(anExtLineRes)
187 anExtLineShape = anExtLineRes.shape()
188 assert(anExtLineShape)
189 anExtLine = aSketchFeature.addFeature("SketchLine")
190 anExtLineStart = geomDataAPI_Point2D(anExtLine.attribute("StartPoint"))
191 anExtLineEnd = geomDataAPI_Point2D(anExtLine.attribute("EndPoint"))
192 anExtLineStart.setValue(-40., 35.)
193 anExtLineEnd.setValue(-60., 25.)
194 anExtLine.selection("External").setValue(anExtLineRes, anExtLineShape)
195 anExtLineLen = lineLength(anExtLine)
196 aSession.finishOperation()
197 aSession.startOperation()
198 aConstraintEqLen2 = aSketchFeature.addFeature("SketchConstraintEqual")
199 aRefObjectA = aConstraintEqLen2.refattr("ConstraintEntityA")
200 aRefObjectB = aConstraintEqLen2.refattr("ConstraintEntityB")
201 aRefObjectA.setObject(anExtLine.lastResult())
202 aRefObjectB.setObject(aSketchLine2.lastResult())
203 aSession.finishOperation()
204 aLine1Len = lineLength(aSketchLine1)
205 aLine2Len = lineLength(aSketchLine2)
206 assert (math.fabs(aLine1Len - anExtLineLen) < 1.e-10)
207 assert (math.fabs(aLine2Len - anExtLineLen) < 1.e-10)
208 #=========================================================================
209 # End of test
210 #=========================================================================