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