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