Salome HOME
Merge branch 'Pre_2.8.0_development'
[modules/shaper.git] / src / SketchPlugin / Test / TestMultiRotation.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     TestMultiRotation.py
23     Unit test of SketchPlugin_MultiRotation class
24
25     SketchPlugin_MultiRotation
26         static const std::string MY_CONSTRAINT_ROTATION_ID("SketchMultiRotation");
27         data()->addAttribute(ANGLE_TYPE(), ModelAPI_AttributeString::typeId());
28         data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
29         data()->addAttribute(ANGLE_FULL_ID(), ModelAPI_AttributeDouble::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(ROTATION_LIST_ID(), ModelAPI_AttributeRefList::typeId());
34
35 """
36 from GeomDataAPI import *
37 from ModelAPI import *
38 import math
39 from salome.shaper import model
40
41 #=========================================================================
42 # Auxiliary functions
43 #=========================================================================
44 def createSketch(theSketch):
45     # Initialize sketch by arc
46     allFeatures = []
47     # Create arc
48     aSketchArc = theSketch.addFeature("SketchArc")
49     aCenter     = geomDataAPI_Point2D(aSketchArc.attribute("center_point"))
50     aStartPoint = geomDataAPI_Point2D(aSketchArc.attribute("start_point"))
51     aEndPoint   = geomDataAPI_Point2D(aSketchArc.attribute("end_point"))
52     aCenter.setValue(5., 5.)
53     aStartPoint.setValue(10., 5.)
54     aEndPoint.setValue(5., 10.)
55     allFeatures.append(aSketchArc)
56     theSketch.execute()
57     return allFeatures
58
59 def createLine(theSketch):
60     aSketchLine = theSketch.addFeature("SketchLine")
61     aStartPoint = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint"))
62     aEndPoint   = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint"))
63     aStartPoint.setValue(7., 5.)
64     aEndPoint.setValue(1., 3.)
65     theSketch.execute()
66     return aSketchLine
67
68 def checkRotation(theObjects, theNbObjects, theCenterX, theCenterY, theAngle):
69     # Verify distances of the objects and the number of copies
70     aFeatures = []
71     aSize = theObjects.size()
72     for i in range (0, aSize):
73         feat = ModelAPI_Feature.feature(theObjects.object(i))
74         assert(feat is not None)
75         aFeatures.append(feat)
76
77     cosA = math.cos(theAngle * math.pi / 180.)
78
79     anInd = 0
80     for feat, next in zip(aFeatures[:-1], aFeatures[1:]):
81         anInd = anInd + 1
82         if (anInd > theNbObjects-1):
83             anInd = 0
84             continue
85         assert(feat.getKind() == next.getKind())
86
87         anAttributes = []
88         if (feat.getKind() == "SketchLine"):
89             anAttributes.append('StartPoint')
90             anAttributes.append('EndPoint')
91         elif (feat.getKind() == "SketchArc"):
92             anAttributes.append('center_point')
93             anAttributes.append('start_point')
94             anAttributes.append('end_point')
95
96         for attr in anAttributes:
97             aPoint1 = geomDataAPI_Point2D(feat.attribute(attr))
98             aPoint2 = geomDataAPI_Point2D(next.attribute(attr))
99             aDirX1 = aPoint1.x() - theCenterX
100             aDirY1 = aPoint1.y() - theCenterY
101             aLen1 = math.hypot(aDirX1, aDirY1)
102             aDirX2 = aPoint2.x() - theCenterX
103             aDirY2 = aPoint2.y() - theCenterY
104             aLen2 = math.hypot(aDirX2, aDirY2)
105             aLocCosA = (aDirX1 * aDirX2 + aDirY1 * aDirY2) / aLen1 / aLen2
106             assert(math.fabs(aLocCosA - cosA) < 1.e-10)
107     # Check the number of copies is as planed
108     assert(anInd == theNbObjects-1)
109
110
111 #=========================================================================
112 # Initialization of the test
113 #=========================================================================
114
115 __updated__ = "2015-09-18"
116
117 aSession = ModelAPI_Session.get()
118 aDocument = aSession.moduleDocument()
119 #=========================================================================
120 # Creation of a sketch
121 #=========================================================================
122 aSession.startOperation()
123 aSketchCommonFeature = aDocument.addFeature("Sketch")
124 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
125 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
126 origin.setValue(0, 0, 0)
127 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
128 dirx.setValue(1, 0, 0)
129 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
130 norm.setValue(0, 0, 1)
131 aSession.finishOperation()
132 #=========================================================================
133 # Initialize sketch
134 #=========================================================================
135 aSession.startOperation()
136 aFeaturesList = createSketch(aSketchFeature)
137 aSession.finishOperation()
138 assert(model.dof(aSketchFeature) == 5)
139 #=========================================================================
140 # Global variables
141 #=========================================================================
142 CENTER_X = 0.
143 CENTER_Y = 0.
144 ANGLE = 30.
145 #=========================================================================
146 # Create rotation point
147 #=========================================================================
148 aSession.startOperation()
149 aRotationPoint = aSketchFeature.addFeature("SketchPoint")
150 aRotationPointPoint = geomDataAPI_Point2D(aRotationPoint.attribute("PointCoordinates"))
151 aRotationPointPoint.setValue(CENTER_X, CENTER_Y)
152 aSession.finishOperation()
153 assert(model.dof(aSketchFeature) == 7)
154 #=========================================================================
155 # Create the Rotation constraint
156 #=========================================================================
157 aSession.startOperation()
158 aMultiRotation = aSketchFeature.addFeature("SketchMultiRotation")
159 aRotList = aMultiRotation.reflist("MultiRotationList")
160 for aFeature in aFeaturesList:
161     aResult = modelAPI_ResultConstruction(aFeature.lastResult())
162     assert(aResult is not None)
163     aRotList.append(aResult)
164
165 aValueType = aMultiRotation.string("AngleType")
166 aValueType.setValue("SingleValue")
167
168 aCenter = aMultiRotation.refattr("MultiRotationCenter")
169 aCenter.setAttr(aRotationPointPoint)
170
171 anAngle = aMultiRotation.real("MultiRotationAngle")
172 anAngle.setValue(ANGLE)
173
174 anAngle = aMultiRotation.string("AngleType")
175 anAngle.setValue("SingleAngle")
176
177 aNbCopies = aMultiRotation.integer("MultiRotationObjects")
178 aNbCopies.setValue(2)
179 aMultiRotation.execute()
180 aSession.finishOperation()
181 assert(model.dof(aSketchFeature) == 7)
182 #=========================================================================
183 # Verify the objects are moved for the specified distance
184 #=========================================================================
185 aRotated = aMultiRotation.reflist("ConstraintEntityB")
186 checkRotation(aRotated, aNbCopies.value(), CENTER_X, CENTER_Y, ANGLE)
187 #=========================================================================
188 # Change number of copies and verify Rotation
189 #=========================================================================
190 aSession.startOperation()
191 aNbCopies.setValue(3)
192 aSession.finishOperation()
193 aRotated = aMultiRotation.reflist("ConstraintEntityB")
194 checkRotation(aRotated, aNbCopies.value(), CENTER_X, CENTER_Y, ANGLE)
195 assert(model.dof(aSketchFeature) == 7)
196
197 #=========================================================================
198 # Create new feature and add it into the Rotation
199 #=========================================================================
200 aSession.startOperation()
201 aLine = createLine(aSketchFeature)
202 aSession.finishOperation()
203 aSession.startOperation()
204 aResult = modelAPI_ResultConstruction(aLine.lastResult())
205 assert(aResult is not None)
206 aRotList.append(aResult)
207 aSession.finishOperation()
208 checkRotation(aRotated, aNbCopies.value(), CENTER_X, CENTER_Y, ANGLE)
209 assert(model.dof(aSketchFeature) == 11)
210 #=========================================================================
211 # Move line and check the copies are moved too
212 #=========================================================================
213 aSession.startOperation()
214 aStartPoint = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
215 aStartPoint.setValue(12., 5.)
216 aSession.finishOperation()
217 checkRotation(aRotated, aNbCopies.value(), CENTER_X, CENTER_Y, ANGLE)
218 assert(model.dof(aSketchFeature) == 11)
219 #=========================================================================
220 # Change number of copies and verify Rotation
221 #=========================================================================
222 aSession.startOperation()
223 aNbCopies.setValue(2)
224 aSession.finishOperation()
225 checkRotation(aRotated, aNbCopies.value(), CENTER_X, CENTER_Y, ANGLE)
226 assert(model.dof(aSketchFeature) == 11)
227
228 #=========================================================================
229 # Remove a feature from the Rotation
230 #=========================================================================
231 aSession.startOperation()
232 aRemoveIt = aRotList.object(0)
233 aRotList.remove(aRemoveIt)
234 aSession.finishOperation()
235 checkRotation(aRotated, aNbCopies.value(), CENTER_X, CENTER_Y, ANGLE)
236 assert(model.dof(aSketchFeature) == 11)
237
238 #=========================================================================
239 # Clear the list of rotated features
240 #=========================================================================
241 aSession.startOperation()
242 aRotList.clear()
243 checkRotation(aRotated, 1, CENTER_X, CENTER_Y, ANGLE)
244 # Add line once again
245 aRotList.append(aResult)
246 aSession.finishOperation()
247 checkRotation(aRotated, aNbCopies.value(), CENTER_X, CENTER_Y, ANGLE)
248 assert(model.dof(aSketchFeature) == 11)
249 #=========================================================================
250 # End of test
251 #=========================================================================
252
253 assert(model.checkPythonDump())