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