Salome HOME
905117ae394ef5908c7a39abbed283f7f8f2221b
[modules/shaper.git] / src / SketchPlugin / Test / TestMultiRotation.py
1 """
2     TestMultiRotation.py
3     Unit test of SketchPlugin_MultiRotation class
4         
5     SketchPlugin_MultiRotation
6         static const std::string MY_CONSTRAINT_ROTATION_ID("SketchMultiRotation");
7         data()->addAttribute(ANGLE_TYPE(), ModelAPI_AttributeString::typeId());
8         data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
9         data()->addAttribute(ANGLE_FULL_ID(), ModelAPI_AttributeDouble::typeId());
10         data()->addAttribute(NUMBER_OF_OBJECTS_ID(), ModelAPI_AttributeInteger::typeId());
11         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
12         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
13         data()->addAttribute(ROTATION_LIST_ID(), ModelAPI_AttributeRefList::typeId());
14
15 """
16 from GeomDataAPI import *
17 from ModelAPI import *
18 import math
19 from salome.shaper import model
20
21 #=========================================================================
22 # Auxiliary functions
23 #=========================================================================
24 def createSketch(theSketch):
25     # Initialize sketch by arc
26     allFeatures = []
27     # Create arc
28     aSketchArc = theSketch.addFeature("SketchArc")
29     aCenter     = geomDataAPI_Point2D(aSketchArc.attribute("center_point"))
30     aStartPoint = geomDataAPI_Point2D(aSketchArc.attribute("start_point"))
31     aEndPoint   = geomDataAPI_Point2D(aSketchArc.attribute("end_point"))
32     aCenter.setValue(5., 5.)
33     aStartPoint.setValue(10., 5.)
34     aEndPoint.setValue(5., 10.)
35     allFeatures.append(aSketchArc)
36     theSketch.execute()
37     return allFeatures
38     
39 def createLine(theSketch):
40     aSketchLine = theSketch.addFeature("SketchLine")
41     aStartPoint = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint"))
42     aEndPoint   = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint"))
43     aStartPoint.setValue(7., 5.)
44     aEndPoint.setValue(1., 3.)
45     theSketch.execute()
46     return aSketchLine
47     
48 def checkRotation(theObjects, theNbObjects, theCenterX, theCenterY, theAngle):
49     # Verify distances of the objects and the number of copies
50     aFeatures = []
51     aSize = theObjects.size()
52     for i in range (0, aSize):
53         feat = ModelAPI_Feature.feature(theObjects.object(i))
54         assert(feat is not None)
55         aFeatures.append(feat)
56         
57     cosA = math.cos(theAngle * math.pi / 180.)
58         
59     anInd = 0 
60     for feat, next in zip(aFeatures[:-1], aFeatures[1:]):
61         anInd = anInd + 1
62         if (anInd > theNbObjects-1):
63             anInd = 0
64             continue
65         assert(feat.getKind() == next.getKind())
66         
67         anAttributes = []
68         if (feat.getKind() == "SketchLine"):
69             anAttributes.append('StartPoint')
70             anAttributes.append('EndPoint')
71         elif (feat.getKind() == "SketchArc"):
72             anAttributes.append('center_point')
73             anAttributes.append('start_point')
74             anAttributes.append('end_point')
75             
76         for attr in anAttributes:
77             aPoint1 = geomDataAPI_Point2D(feat.attribute(attr))
78             aPoint2 = geomDataAPI_Point2D(next.attribute(attr))
79             aDirX1 = aPoint1.x() - theCenterX
80             aDirY1 = aPoint1.y() - theCenterY
81             aLen1 = math.hypot(aDirX1, aDirY1)
82             aDirX2 = aPoint2.x() - theCenterX
83             aDirY2 = aPoint2.y() - theCenterY
84             aLen2 = math.hypot(aDirX2, aDirY2)
85             aLocCosA = (aDirX1 * aDirX2 + aDirY1 * aDirY2) / aLen1 / aLen2
86             assert(math.fabs(aLocCosA - cosA) < 1.e-10)
87     # Check the number of copies is as planed
88     assert(anInd == theNbObjects-1)
89
90
91 #=========================================================================
92 # Initialization of the test
93 #=========================================================================
94
95 __updated__ = "2015-09-18"
96
97 aSession = ModelAPI_Session.get()
98 aDocument = aSession.moduleDocument()
99 #=========================================================================
100 # Creation of a sketch
101 #=========================================================================
102 aSession.startOperation()
103 aSketchCommonFeature = aDocument.addFeature("Sketch")
104 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
105 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
106 origin.setValue(0, 0, 0)
107 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
108 dirx.setValue(1, 0, 0)
109 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
110 norm.setValue(0, 0, 1)
111 aSession.finishOperation()
112 #=========================================================================
113 # Initialize sketch
114 #=========================================================================
115 aSession.startOperation()
116 aFeaturesList = createSketch(aSketchFeature)
117 aSession.finishOperation()
118 assert(model.dof(aSketchFeature) == 5)
119 #=========================================================================
120 # Global variables
121 #=========================================================================
122 CENTER_X = 0.
123 CENTER_Y = 0.
124 ANGLE = 30.
125 #=========================================================================
126 # Create rotation point
127 #=========================================================================
128 aSession.startOperation()
129 aRotationPoint = aSketchFeature.addFeature("SketchPoint")
130 aRotationPointPoint = geomDataAPI_Point2D(aRotationPoint.attribute("PointCoordinates"))
131 aRotationPointPoint.setValue(CENTER_X, CENTER_Y)
132 aSession.finishOperation()
133 assert(model.dof(aSketchFeature) == 7)
134 #=========================================================================
135 # Create the Rotation constraint
136 #=========================================================================
137 aSession.startOperation()
138 aMultiRotation = aSketchFeature.addFeature("SketchMultiRotation")
139 aRotList = aMultiRotation.reflist("MultiRotationList")
140 for aFeature in aFeaturesList:
141     aResult = modelAPI_ResultConstruction(aFeature.lastResult())
142     assert(aResult is not None)
143     aRotList.append(aResult)
144
145 aValueType = aMultiRotation.string("AngleType")
146 aValueType.setValue("SingleValue")
147
148 aCenter = aMultiRotation.refattr("MultiRotationCenter")
149 aCenter.setAttr(aRotationPointPoint)
150
151 anAngle = aMultiRotation.real("MultiRotationAngle")
152 anAngle.setValue(ANGLE)
153
154 anAngle = aMultiRotation.string("AngleType")
155 anAngle.setValue("SingleAngle")
156
157 aNbCopies = aMultiRotation.integer("MultiRotationObjects")
158 aNbCopies.setValue(2)
159 aMultiRotation.execute()
160 aSession.finishOperation()
161 assert(model.dof(aSketchFeature) == 7)
162 #=========================================================================
163 # Verify the objects are moved for the specified distance
164 #=========================================================================
165 aRotated = aMultiRotation.reflist("ConstraintEntityB")
166 checkRotation(aRotated, aNbCopies.value(), CENTER_X, CENTER_Y, ANGLE)
167 #=========================================================================
168 # Change number of copies and verify Rotation
169 #=========================================================================
170 aSession.startOperation()
171 aNbCopies.setValue(3)
172 aSession.finishOperation()
173 aRotated = aMultiRotation.reflist("ConstraintEntityB")
174 checkRotation(aRotated, aNbCopies.value(), CENTER_X, CENTER_Y, ANGLE)
175 assert(model.dof(aSketchFeature) == 7)
176
177 #=========================================================================
178 # Create new feature and add it into the Rotation
179 #=========================================================================
180 aSession.startOperation()
181 aLine = createLine(aSketchFeature)
182 aSession.finishOperation()
183 aSession.startOperation()
184 aResult = modelAPI_ResultConstruction(aLine.lastResult())
185 assert(aResult is not None)
186 aRotList.append(aResult)
187 aSession.finishOperation()
188 checkRotation(aRotated, aNbCopies.value(), CENTER_X, CENTER_Y, ANGLE)
189 assert(model.dof(aSketchFeature) == 11)
190 #=========================================================================
191 # Move line and check the copies are moved too
192 #=========================================================================
193 aSession.startOperation()
194 aStartPoint = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
195 aStartPoint.setValue(12., 5.)
196 aSession.finishOperation()
197 checkRotation(aRotated, aNbCopies.value(), CENTER_X, CENTER_Y, ANGLE)
198 assert(model.dof(aSketchFeature) == 11)
199 #=========================================================================
200 # Change number of copies and verify Rotation
201 #=========================================================================
202 aSession.startOperation()
203 aNbCopies.setValue(2)
204 aSession.finishOperation()
205 checkRotation(aRotated, aNbCopies.value(), CENTER_X, CENTER_Y, ANGLE)
206 assert(model.dof(aSketchFeature) == 11)
207
208 #=========================================================================
209 # Remove a feature from the Rotation
210 #=========================================================================
211 aSession.startOperation()
212 aRemoveIt = aRotList.object(0)
213 aRotList.remove(aRemoveIt)
214 aSession.finishOperation()
215 checkRotation(aRotated, aNbCopies.value(), CENTER_X, CENTER_Y, ANGLE)
216 assert(model.dof(aSketchFeature) == 11)
217
218 #=========================================================================
219 # Clear the list of rotated features
220 #=========================================================================
221 aSession.startOperation()
222 aRotList.clear()
223 checkRotation(aRotated, 1, CENTER_X, CENTER_Y, ANGLE)
224 # Add line once again
225 aRotList.append(aResult)
226 aSession.finishOperation()
227 checkRotation(aRotated, aNbCopies.value(), CENTER_X, CENTER_Y, ANGLE)
228 assert(model.dof(aSketchFeature) == 11)
229 #=========================================================================
230 # End of test
231 #=========================================================================
232
233 assert(model.checkPythonDump())