Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / SketchPlugin / Test / TestMultiTranslation.py
1 ## Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 ##
3 ## This library is free software; you can redistribute it and/or
4 ## modify it under the terms of the GNU Lesser General Public
5 ## License as published by the Free Software Foundation; either
6 ## version 2.1 of the License, or (at your option) any later version.
7 ##
8 ## This library is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 ## Lesser General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU Lesser General Public
14 ## License along with this library; if not, write to the Free Software
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 ##
17 ## See http:##www.salome-platform.org/ or
18 ## email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 ##
20
21 """
22     TestMultiTranslation.py
23     Unit test of SketchPlugin_MultiTranslation class
24
25     SketchPlugin_MultiTranslation
26         static const std::string MY_CONSTRAINT_TRANSLATION_ID("SketchMultiTranslation");
27         data()->addAttribute(VALUE_TYPE(), ModelAPI_AttributeString::typeId());
28         data()->addAttribute(START_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
29         data()->addAttribute(END_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
30         data()->addAttribute(NUMBER_OF_OBJECTS_ID(), ModelAPI_AttributeInteger::typeId());
31         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
32         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
33         data()->addAttribute(TRANSLATION_LIST_ID(), ModelAPI_AttributeRefList::typeId());
34
35 """
36 from GeomDataAPI import *
37 from ModelAPI import *
38 from salome.shaper import model
39
40 #=========================================================================
41 # Auxiliary functions
42 #=========================================================================
43 def createSketch(theSketch):
44     # Initialize sketch by arc
45     allFeatures = []
46     # Create arc
47     aSketchArc = theSketch.addFeature("SketchArc")
48     aCenter     = geomDataAPI_Point2D(aSketchArc.attribute("center_point"))
49     aStartPoint = geomDataAPI_Point2D(aSketchArc.attribute("start_point"))
50     aEndPoint   = geomDataAPI_Point2D(aSketchArc.attribute("end_point"))
51     aCenter.setValue(5., 5.)
52     aStartPoint.setValue(10., 5.)
53     aEndPoint.setValue(5., 10.)
54     allFeatures.append(aSketchArc)
55     theSketch.execute()
56     return allFeatures
57
58 def createLine(theSketch):
59     aSketchLine = theSketch.addFeature("SketchLine")
60     aStartPoint = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint"))
61     aEndPoint   = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint"))
62     aStartPoint.setValue(7., 5.)
63     aEndPoint.setValue(1., 3.)
64     theSketch.execute()
65     return aSketchLine
66
67 def checkTranslation(theObjects, theNbObjects, theDeltaX, theDeltaY):
68     # Verify distances of the objects and the number of copies
69     aFeatures = []
70     aSize = theObjects.size()
71     for i in range (0, aSize):
72         feat = ModelAPI_Feature.feature(theObjects.object(i))
73         assert(feat is not None)
74         aFeatures.append(feat)
75
76     anInd = 0
77     for feat, next in zip(aFeatures[:-1], aFeatures[1:]):
78         anInd = anInd + 1
79         if (anInd > theNbObjects-1):
80             anInd = 0
81             continue
82         assert(feat.getKind() == next.getKind())
83
84         anAttributes = []
85         if (feat.getKind() == "SketchLine"):
86             anAttributes.append('StartPoint')
87             anAttributes.append('EndPoint')
88         elif (feat.getKind() == "SketchArc"):
89             anAttributes.append('center_point')
90             anAttributes.append('start_point')
91             anAttributes.append('end_point')
92
93         for attr in anAttributes:
94              aPoint1 = geomDataAPI_Point2D(feat.attribute(attr))
95              aPoint2 = geomDataAPI_Point2D(next.attribute(attr))
96              aDiffX = aPoint2.x() - aPoint1.x() - theDeltaX
97              aDiffY = aPoint2.y() - aPoint1.y() - theDeltaY
98              assert(aDiffX**2 + aDiffY**2 < 1.e-15)
99     # Check the number of copies is as planed
100     assert(anInd == theNbObjects-1)
101
102
103 #=========================================================================
104 # Initialization of the test
105 #=========================================================================
106
107 __updated__ = "2015-09-18"
108
109 aSession = ModelAPI_Session.get()
110 aDocument = aSession.moduleDocument()
111 #=========================================================================
112 # Creation of a sketch
113 #=========================================================================
114 aSession.startOperation()
115 aSketchCommonFeature = aDocument.addFeature("Sketch")
116 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
117 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
118 origin.setValue(0, 0, 0)
119 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
120 dirx.setValue(1, 0, 0)
121 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
122 norm.setValue(0, 0, 1)
123 aSession.finishOperation()
124 #=========================================================================
125 # Initialize sketch
126 #=========================================================================
127 aSession.startOperation()
128 aFeaturesList = createSketch(aSketchFeature)
129 aSession.finishOperation()
130 assert(model.dof(aSketchFeature) == 5)
131 #=========================================================================
132 # Global variables
133 #=========================================================================
134 START_X = 0.
135 START_Y = 15.
136 DIR_X = 20.
137 DIR_Y = 10.
138 DELTA_X = -5.
139 DELTA_Y = 3.
140 #=========================================================================
141 # Create translation line
142 #=========================================================================
143 aSession.startOperation()
144 aTransLine = aSketchFeature.addFeature("SketchLine")
145 aTransLineStartPoint = geomDataAPI_Point2D(aTransLine.attribute("StartPoint"))
146 aTransLineEndPoint = geomDataAPI_Point2D(aTransLine.attribute("EndPoint"))
147 aTransLineStartPoint.setValue(START_X, START_Y)
148 aTransLineEndPoint.setValue(START_X + DELTA_X, START_Y + DELTA_Y)
149 aSession.finishOperation()
150 assert(model.dof(aSketchFeature) == 9)
151 #=========================================================================
152 # Create the Translation constraint
153 #=========================================================================
154 aSession.startOperation()
155 aMultiTranslation = aSketchFeature.addFeature("SketchMultiTranslation")
156 aTransList = aMultiTranslation.reflist("MultiTranslationList")
157 for aFeature in aFeaturesList:
158     aResult = modelAPI_ResultConstruction(aFeature.lastResult())
159     assert(aResult is not None)
160     aTransList.append(aResult)
161
162 aValueType = aMultiTranslation.string("ValueType")
163 aValueType.setValue("SingleValue")
164 aStartPoint = aMultiTranslation.refattr("MultiTranslationStartPoint")
165 aEndPoint = aMultiTranslation.refattr("MultiTranslationEndPoint")
166 aStartPoint.setAttr(aTransLineStartPoint)
167 aEndPoint.setAttr(aTransLineEndPoint)
168 aNbCopies = aMultiTranslation.integer("MultiTranslationObjects")
169 aNbCopies.setValue(2)
170 aMultiTranslation.execute()
171 aSession.finishOperation()
172 assert(model.dof(aSketchFeature) == 9)
173 #=========================================================================
174 # Verify the objects are moved for the specified distance
175 #=========================================================================
176 aTranslated = aMultiTranslation.reflist("ConstraintEntityB")
177 checkTranslation(aTranslated, aNbCopies.value(), DELTA_X, DELTA_Y)
178 #=========================================================================
179 # Change number of copies and verify translation
180 #=========================================================================
181 aSession.startOperation()
182 aNbCopies.setValue(3)
183 aSession.finishOperation()
184 aTranslated = aMultiTranslation.reflist("ConstraintEntityB")
185 checkTranslation(aTranslated, aNbCopies.value(), DELTA_X, DELTA_Y)
186 assert(model.dof(aSketchFeature) == 9)
187
188 #=========================================================================
189 # Create new feature and add it into the Rotation
190 #=========================================================================
191 aSession.startOperation()
192 aLine = createLine(aSketchFeature)
193 aSession.finishOperation()
194 aSession.startOperation()
195 aResult = modelAPI_ResultConstruction(aLine.lastResult())
196 assert(aResult is not None)
197 aTransList.append(aResult)
198 aSession.finishOperation()
199 checkTranslation(aTranslated, aNbCopies.value(), DELTA_X, DELTA_Y)
200 assert(model.dof(aSketchFeature) == 13)
201 #=========================================================================
202 # Move line and check the copies are moved too
203 #=========================================================================
204 aSession.startOperation()
205 aStartPoint = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
206 aStartPoint.setValue(12., 5.)
207 aSession.finishOperation()
208 checkTranslation(aTranslated, aNbCopies.value(), DELTA_X, DELTA_Y)
209 assert(model.dof(aSketchFeature) == 13)
210 #=========================================================================
211 # Change number of copies and verify Rotation
212 #=========================================================================
213 aSession.startOperation()
214 aNbCopies.setValue(2)
215 aSession.finishOperation()
216 checkTranslation(aTranslated, aNbCopies.value(), DELTA_X, DELTA_Y)
217 assert(model.dof(aSketchFeature) == 13)
218
219 #=========================================================================
220 # Remove a feature from the Rotation
221 #=========================================================================
222 aSession.startOperation()
223 aRemoveIt = aTransList.object(0)
224 aTransList.remove(aRemoveIt)
225 aSession.finishOperation()
226 checkTranslation(aTranslated, aNbCopies.value(), DELTA_X, DELTA_Y)
227 assert(model.dof(aSketchFeature) == 13)
228
229 #=========================================================================
230 # Clear the list of rotated features
231 #=========================================================================
232 aSession.startOperation()
233 aTransList.clear()
234 checkTranslation(aTranslated, 1, DELTA_X, DELTA_Y)
235 # Add line once again
236 aTransList.append(aResult)
237 aSession.finishOperation()
238 checkTranslation(aTranslated, aNbCopies.value(), DELTA_X, DELTA_Y)
239 assert(model.dof(aSketchFeature) == 13)
240 #=========================================================================
241 # End of test
242 #=========================================================================
243
244 assert(model.checkPythonDump())