3 Unit test of SketchPlugin_MultiRotation class
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());
16 from GeomDataAPI import *
17 from ModelAPI import *
19 from salome.shaper import model
21 #=========================================================================
23 #=========================================================================
24 def createSketch(theSketch):
25 # Initialize sketch by 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)
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.)
48 def checkRotation(theObjects, theNbObjects, theCenterX, theCenterY, theAngle):
49 # Verify distances of the objects and the number of copies
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)
57 cosA = math.cos(theAngle * math.pi / 180.)
60 for feat, next in zip(aFeatures[:-1], aFeatures[1:]):
62 if (anInd > theNbObjects-1):
65 assert(feat.getKind() == next.getKind())
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')
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)
91 #=========================================================================
92 # Initialization of the test
93 #=========================================================================
95 __updated__ = "2015-09-18"
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 #=========================================================================
114 #=========================================================================
115 aSession.startOperation()
116 aFeaturesList = createSketch(aSketchFeature)
117 aSession.finishOperation()
118 assert(model.dof(aSketchFeature) == 5)
119 #=========================================================================
121 #=========================================================================
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)
145 aValueType = aMultiRotation.string("AngleType")
146 aValueType.setValue("SingleValue")
148 aCenter = aMultiRotation.refattr("MultiRotationCenter")
149 aCenter.setAttr(aRotationPointPoint)
151 anAngle = aMultiRotation.real("MultiRotationAngle")
152 anAngle.setValue(ANGLE)
154 anAngle = aMultiRotation.string("AngleType")
155 anAngle.setValue("SingleAngle")
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)
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)
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)
218 #=========================================================================
219 # Clear the list of rotated features
220 #=========================================================================
221 aSession.startOperation()
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 #=========================================================================
231 #=========================================================================
233 assert(model.checkPythonDump())