Salome HOME
Receive DoF from the solver. Update test cases to check DoF.
[modules/shaper.git] / src / SketchPlugin / Test / TestMultiTranslation.py
1 """
2     TestMultiTranslation.py
3     Unit test of SketchPlugin_MultiTranslation class
4         
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());
14
15 """
16 from GeomDataAPI import *
17 from ModelAPI import *
18 from salome.shaper import model
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 checkTranslation(theObjects, theNbObjects, theDeltaX, theDeltaY):
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     anInd = 0 
57     for feat, next in zip(aFeatures[:-1], aFeatures[1:]):
58         anInd = anInd + 1
59         if (anInd > theNbObjects-1):
60             anInd = 0
61             continue
62         assert(feat.getKind() == next.getKind())
63         
64         anAttributes = []
65         if (feat.getKind() == "SketchLine"):
66             anAttributes.append('StartPoint')
67             anAttributes.append('EndPoint')
68         elif (feat.getKind() == "SketchArc"):
69             anAttributes.append('ArcCenter')
70             anAttributes.append('ArcStartPoint')
71             anAttributes.append('ArcEndPoint')
72             
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)
81
82
83 #=========================================================================
84 # Initialization of the test
85 #=========================================================================
86
87 __updated__ = "2015-09-18"
88
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 #=========================================================================
105 # Initialize sketch
106 #=========================================================================
107 aSession.startOperation()
108 aFeaturesList = createSketch(aSketchFeature)
109 aSession.finishOperation()
110 assert(model.dof(aSketchFeature) == 5)
111 #=========================================================================
112 # Global variables
113 #=========================================================================
114 START_X = 0.
115 START_Y = 15.
116 DIR_X = 20.
117 DIR_Y = 10.
118 DELTA_X = -5.
119 DELTA_Y = 3.
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)
141
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)
167
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)
198
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)
208
209 #=========================================================================
210 # Clear the list of rotated features
211 #=========================================================================
212 aSession.startOperation()
213 aTransList.clear()
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 #=========================================================================
221 # End of test
222 #=========================================================================
223
224 assert(model.checkPythonDump())