Salome HOME
e78dd6837a102712f047601b6cc5768a98e58e2c
[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 anExtCircle = aSketchFeature.addFeature("SketchCircle")
121 anExtCircleCenter = geomDataAPI_Point2D(anExtCircle.attribute("CircleCenter"))
122 anExtCircleRadius = anExtCircle.real("CircleRadius")
123 anExtCircleCenter.setValue(-50., 50.)
124 anExtCircleRadius.setValue(10.)
125 anExtCircle.selection("External").selectSubShape("EDGE", "Sketch_1/Edge-SketchCircle_1_2")
126 aSession.finishOperation()
127 aSession.startOperation()
128 aConstraintEqRad2 = aSketchFeature.addFeature("SketchConstraintEqual")
129 aRefObjectA = aConstraintEqRad2.refattr("ConstraintEntityA")
130 aRefObjectB = aConstraintEqRad2.refattr("ConstraintEntityB")
131 aRefObjectA.setObject(aSketchCircle.lastResult())
132 aRefObjectB.setObject(anExtCircle.lastResult())
133 aSession.finishOperation()
134 assert (math.fabs(anExtCircleRadius.value() - aCircleRadius.value()) < 1.e-10)
135 anArcVecX = anArcStartPoint.x() - anArcCentr.x()
136 anArcVecY = anArcStartPoint.y() - anArcCentr.y()
137 anArcRadius = math.sqrt(anArcVecX**2 + anArcVecY**2)
138 assert (math.fabs(anExtCircleRadius.value() - anArcRadius) <= 1.e-10)
139
140 #=========================================================================
141 # Creation of two different lines
142 #=========================================================================
143 # First Line
144 aSession.startOperation()
145 aSketchLine1 = aSketchFeature.addFeature("SketchLine")
146 aLine1StartPoint = geomDataAPI_Point2D(aSketchLine1.attribute("StartPoint"))
147 aLine1EndPoint = geomDataAPI_Point2D(aSketchLine1.attribute("EndPoint"))
148 aLine1StartPoint.setValue(0., 15.)
149 aLine1EndPoint.setValue(20., 25.)
150 aSession.finishOperation()
151 # Second Line
152 aSession.startOperation()
153 aSketchLine2 = aSketchFeature.addFeature("SketchLine")
154 aLine2StartPoint = geomDataAPI_Point2D(aSketchLine2.attribute("StartPoint"))
155 aLine2EndPoint = geomDataAPI_Point2D(aSketchLine2.attribute("EndPoint"))
156 aLine2StartPoint.setValue(0., 0.)
157 aLine2EndPoint.setValue(-1., 10.)
158 aSession.finishOperation()
159 #=========================================================================
160 # A constraint to make equal lengths of lines
161 #=========================================================================
162 aSession.startOperation()
163 aConstraintEqLen = aSketchFeature.addFeature("SketchConstraintEqual")
164 aRefObjectA = aConstraintEqLen.refattr("ConstraintEntityA")
165 aRefObjectB = aConstraintEqLen.refattr("ConstraintEntityB")
166 aResultA = modelAPI_ResultConstruction(aSketchLine1.firstResult())
167 aResultB = modelAPI_ResultConstruction(aSketchLine2.firstResult())
168 assert (aResultA is not None)
169 assert (aResultB is not None)
170 aRefObjectA.setObject(aResultA)
171 aRefObjectB.setObject(aResultB)
172 aConstraintEqLen.execute()
173 aSession.finishOperation()
174 aLine1Len = lineLength(aSketchLine1)
175 aLine2Len = lineLength(aSketchLine2)
176 assert (math.fabs(aLine1Len - aLine2Len) < 1.e-10)
177 #=========================================================================
178 # A constraint to make equal length of line with external line
179 #=========================================================================
180 aSession.startOperation()
181 anExtLine = aSketchFeature.addFeature("SketchLine")
182 anExtLineStart = geomDataAPI_Point2D(anExtLine.attribute("StartPoint"))
183 anExtLineEnd = geomDataAPI_Point2D(anExtLine.attribute("EndPoint"))
184 anExtLineStart.setValue(-40., 35.)
185 anExtLineEnd.setValue(-60., 25.)
186 anExtLine.selection("External").selectSubShape("EDGE", "Sketch_1/Edge-SketchLine_1")
187 anExtLineLen = lineLength(anExtLine)
188 aSession.finishOperation()
189 aSession.startOperation()
190 aConstraintEqLen2 = aSketchFeature.addFeature("SketchConstraintEqual")
191 aRefObjectA = aConstraintEqLen2.refattr("ConstraintEntityA")
192 aRefObjectB = aConstraintEqLen2.refattr("ConstraintEntityB")
193 aRefObjectA.setObject(anExtLine.lastResult())
194 aRefObjectB.setObject(aSketchLine2.lastResult())
195 aSession.finishOperation()
196 aLine1Len = lineLength(aSketchLine1)
197 aLine2Len = lineLength(aSketchLine2)
198 assert (math.fabs(aLine1Len - anExtLineLen) < 1.e-10)
199 assert (math.fabs(aLine2Len - anExtLineLen) < 1.e-10)
200 #=========================================================================
201 # End of test
202 #=========================================================================
203
204 from salome.shaper import model
205 assert(model.checkPythonDump())