2 TestMultiTranslation.py
3 Unit test of SketchPlugin_MultiTranslation class
5 SketchPlugin_MultiTranslation
6 static const std::string MY_CONSTRAINT_TRANSLATION_ID("SketchMultiTranslation");
7 data()->addAttribute(VALUE_TYPE(), ModelAPI_AttributeString::typeId());
8 data()->addAttribute(START_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
9 data()->addAttribute(END_POINT_ID(), ModelAPI_AttributeRefAttr::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(TRANSLATION_LIST_ID(), ModelAPI_AttributeRefList::typeId());
16 from GeomDataAPI import *
17 from ModelAPI import *
18 from salome.shaper import model
20 #=========================================================================
22 #=========================================================================
23 def createSketch(theSketch):
24 # Initialize sketch by arc
27 aSketchArc = theSketch.addFeature("SketchArc")
28 aCenter = geomDataAPI_Point2D(aSketchArc.attribute("center_point"))
29 aStartPoint = geomDataAPI_Point2D(aSketchArc.attribute("start_point"))
30 aEndPoint = geomDataAPI_Point2D(aSketchArc.attribute("end_point"))
31 aCenter.setValue(5., 5.)
32 aStartPoint.setValue(10., 5.)
33 aEndPoint.setValue(5., 10.)
34 allFeatures.append(aSketchArc)
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.)
47 def checkTranslation(theObjects, theNbObjects, theDeltaX, theDeltaY):
48 # Verify distances of the objects and the number of copies
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)
57 for feat, next in zip(aFeatures[:-1], aFeatures[1:]):
59 if (anInd > theNbObjects-1):
62 assert(feat.getKind() == next.getKind())
65 if (feat.getKind() == "SketchLine"):
66 anAttributes.append('StartPoint')
67 anAttributes.append('EndPoint')
68 elif (feat.getKind() == "SketchArc"):
69 anAttributes.append('center_point')
70 anAttributes.append('start_point')
71 anAttributes.append('end_point')
73 for attr in anAttributes:
74 aPoint1 = geomDataAPI_Point2D(feat.attribute(attr))
75 aPoint2 = geomDataAPI_Point2D(next.attribute(attr))
76 aDiffX = aPoint2.x() - aPoint1.x() - theDeltaX
77 aDiffY = aPoint2.y() - aPoint1.y() - theDeltaY
78 assert(aDiffX**2 + aDiffY**2 < 1.e-15)
79 # Check the number of copies is as planed
80 assert(anInd == theNbObjects-1)
83 #=========================================================================
84 # Initialization of the test
85 #=========================================================================
87 __updated__ = "2015-09-18"
89 aSession = ModelAPI_Session.get()
90 aDocument = aSession.moduleDocument()
91 #=========================================================================
92 # Creation of a sketch
93 #=========================================================================
94 aSession.startOperation()
95 aSketchCommonFeature = aDocument.addFeature("Sketch")
96 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
97 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
98 origin.setValue(0, 0, 0)
99 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
100 dirx.setValue(1, 0, 0)
101 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
102 norm.setValue(0, 0, 1)
103 aSession.finishOperation()
104 #=========================================================================
106 #=========================================================================
107 aSession.startOperation()
108 aFeaturesList = createSketch(aSketchFeature)
109 aSession.finishOperation()
110 assert(model.dof(aSketchFeature) == 5)
111 #=========================================================================
113 #=========================================================================
120 #=========================================================================
121 # Create translation line
122 #=========================================================================
123 aSession.startOperation()
124 aTransLine = aSketchFeature.addFeature("SketchLine")
125 aTransLineStartPoint = geomDataAPI_Point2D(aTransLine.attribute("StartPoint"))
126 aTransLineEndPoint = geomDataAPI_Point2D(aTransLine.attribute("EndPoint"))
127 aTransLineStartPoint.setValue(START_X, START_Y)
128 aTransLineEndPoint.setValue(START_X + DELTA_X, START_Y + DELTA_Y)
129 aSession.finishOperation()
130 assert(model.dof(aSketchFeature) == 9)
131 #=========================================================================
132 # Create the Translation constraint
133 #=========================================================================
134 aSession.startOperation()
135 aMultiTranslation = aSketchFeature.addFeature("SketchMultiTranslation")
136 aTransList = aMultiTranslation.reflist("MultiTranslationList")
137 for aFeature in aFeaturesList:
138 aResult = modelAPI_ResultConstruction(aFeature.lastResult())
139 assert(aResult is not None)
140 aTransList.append(aResult)
142 aValueType = aMultiTranslation.string("ValueType")
143 aValueType.setValue("SingleValue")
144 aStartPoint = aMultiTranslation.refattr("MultiTranslationStartPoint")
145 aEndPoint = aMultiTranslation.refattr("MultiTranslationEndPoint")
146 aStartPoint.setAttr(aTransLineStartPoint)
147 aEndPoint.setAttr(aTransLineEndPoint)
148 aNbCopies = aMultiTranslation.integer("MultiTranslationObjects")
149 aNbCopies.setValue(2)
150 aMultiTranslation.execute()
151 aSession.finishOperation()
152 assert(model.dof(aSketchFeature) == 9)
153 #=========================================================================
154 # Verify the objects are moved for the specified distance
155 #=========================================================================
156 aTranslated = aMultiTranslation.reflist("ConstraintEntityB")
157 checkTranslation(aTranslated, aNbCopies.value(), DELTA_X, DELTA_Y)
158 #=========================================================================
159 # Change number of copies and verify translation
160 #=========================================================================
161 aSession.startOperation()
162 aNbCopies.setValue(3)
163 aSession.finishOperation()
164 aTranslated = aMultiTranslation.reflist("ConstraintEntityB")
165 checkTranslation(aTranslated, aNbCopies.value(), DELTA_X, DELTA_Y)
166 assert(model.dof(aSketchFeature) == 9)
168 #=========================================================================
169 # Create new feature and add it into the Rotation
170 #=========================================================================
171 aSession.startOperation()
172 aLine = createLine(aSketchFeature)
173 aSession.finishOperation()
174 aSession.startOperation()
175 aResult = modelAPI_ResultConstruction(aLine.lastResult())
176 assert(aResult is not None)
177 aTransList.append(aResult)
178 aSession.finishOperation()
179 checkTranslation(aTranslated, aNbCopies.value(), DELTA_X, DELTA_Y)
180 assert(model.dof(aSketchFeature) == 13)
181 #=========================================================================
182 # Move line and check the copies are moved too
183 #=========================================================================
184 aSession.startOperation()
185 aStartPoint = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
186 aStartPoint.setValue(12., 5.)
187 aSession.finishOperation()
188 checkTranslation(aTranslated, aNbCopies.value(), DELTA_X, DELTA_Y)
189 assert(model.dof(aSketchFeature) == 13)
190 #=========================================================================
191 # Change number of copies and verify Rotation
192 #=========================================================================
193 aSession.startOperation()
194 aNbCopies.setValue(2)
195 aSession.finishOperation()
196 checkTranslation(aTranslated, aNbCopies.value(), DELTA_X, DELTA_Y)
197 assert(model.dof(aSketchFeature) == 13)
199 #=========================================================================
200 # Remove a feature from the Rotation
201 #=========================================================================
202 aSession.startOperation()
203 aRemoveIt = aTransList.object(0)
204 aTransList.remove(aRemoveIt)
205 aSession.finishOperation()
206 checkTranslation(aTranslated, aNbCopies.value(), DELTA_X, DELTA_Y)
207 assert(model.dof(aSketchFeature) == 13)
209 #=========================================================================
210 # Clear the list of rotated features
211 #=========================================================================
212 aSession.startOperation()
214 checkTranslation(aTranslated, 1, DELTA_X, DELTA_Y)
215 # Add line once again
216 aTransList.append(aResult)
217 aSession.finishOperation()
218 checkTranslation(aTranslated, aNbCopies.value(), DELTA_X, DELTA_Y)
219 assert(model.dof(aSketchFeature) == 13)
220 #=========================================================================
222 #=========================================================================
224 assert(model.checkPythonDump())