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