Salome HOME
a5b1f789aeb2d518ceb1249bc3c40b724ed4cd6b
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintEqual.py
1 # Copyright (C) 2014-2021  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     TestConstraintEqual.py
22     Unit test of SketchPlugin_ConstraintEqual class
23
24     SketchPlugin_ConstraintEqual
25         static const std::string MY_CONSTRAINT_EQAUL_ID("SketchConstraintEqual");
26         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
27         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
28
29 """
30 from GeomDataAPI import *
31 from ModelAPI import *
32 import math
33 from salome.shaper import model
34
35 #=========================================================================
36 # Initialization of the test
37 #=========================================================================
38
39 __updated__ = "2015-03-16"
40
41 #=========================================================================
42 # Auxiliary functions
43 #=========================================================================
44 def externalSketch(theDoc):
45     aSketchFeature = featureToCompositeFeature(theDoc.addFeature("Sketch"))
46     origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
47     origin.setValue(0, 0, 0)
48     dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
49     dirx.setValue(1, 0, 0)
50     norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
51     norm.setValue(0, 0, 1)
52     # add circle defined by 3 points
53     circle = aSketchFeature.addFeature("SketchMacroCircle")
54     circle.string("circle_type").setValue("circle_type_by_three_points")
55     geomDataAPI_Point2D(circle.attribute("first_point")).setValue(-40., 50.)
56     geomDataAPI_Point2D(circle.attribute("second_point")).setValue(-50., 60.)
57     geomDataAPI_Point2D(circle.attribute("third_point")).setValue(-60., 50.)
58     # add line
59     line = aSketchFeature.addFeature("SketchLine")
60     lineStart = geomDataAPI_Point2D(line.attribute("StartPoint"))
61     lineEnd = geomDataAPI_Point2D(line.attribute("EndPoint"))
62     lineStart.setValue(-40., 35.)
63     lineEnd.setValue(-60., 25.)
64
65 def lineLength(theLine):
66     aLineStart = geomDataAPI_Point2D(theLine.attribute("StartPoint"))
67     aLineEnd = geomDataAPI_Point2D(theLine.attribute("EndPoint"))
68     aVecX = aLineStart.x() - aLineEnd.x()
69     aVecY = aLineStart.y() - aLineEnd.y()
70     return math.hypot(aVecX, aVecY)
71
72
73 #=========================================================================
74 # Start of test
75 #=========================================================================
76 aSession = ModelAPI_Session.get()
77 aDocument = aSession.moduleDocument()
78 #=========================================================================
79 # Creation external sketch
80 #=========================================================================
81 aSession.startOperation()
82 externalSketch(aDocument)
83 aSession.finishOperation()
84 #=========================================================================
85 # Creation of a sketch
86 #=========================================================================
87 aSession.startOperation()
88 aSketchCommonFeature = aDocument.addFeature("Sketch")
89 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
90 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
91 origin.setValue(0, 0, 0)
92 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
93 dirx.setValue(1, 0, 0)
94 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
95 norm.setValue(0, 0, 1)
96 aSession.finishOperation()
97 #=========================================================================
98 # Creation of an arc and a circle
99 #=========================================================================
100 aSession.startOperation()
101 aSketchArc = aSketchFeature.addFeature("SketchArc")
102 anArcCentr = geomDataAPI_Point2D(aSketchArc.attribute("center_point"))
103 anArcCentr.setValue(10., 10.)
104 anArcStartPoint = geomDataAPI_Point2D(aSketchArc.attribute("start_point"))
105 anArcStartPoint.setValue(0., 50.)
106 anArcEndPoint = geomDataAPI_Point2D(aSketchArc.attribute("end_point"))
107 anArcEndPoint.setValue(50., 0.)
108 aSession.finishOperation()
109 assert (model.dof(aSketchFeature) == 5)
110 # Circle
111 aSession.startOperation()
112 aSketchCircle = aSketchFeature.addFeature("SketchCircle")
113 anCircleCentr = geomDataAPI_Point2D(aSketchCircle.attribute("circle_center"))
114 aCircleRadius = aSketchCircle.real("circle_radius")
115 anCircleCentr.setValue(-25., -25.)
116 aCircleRadius.setValue(25.)
117 aSession.finishOperation()
118 assert (model.dof(aSketchFeature) == 8)
119 #=========================================================================
120 # A constraint to make equal radii of arc and circle
121 #=========================================================================
122 aSession.startOperation()
123 aConstraintEqRad = aSketchFeature.addFeature("SketchConstraintEqual")
124 aRefObjectA = aConstraintEqRad.refattr("ConstraintEntityA")
125 aRefObjectB = aConstraintEqRad.refattr("ConstraintEntityB")
126 aResultA = modelAPI_ResultConstruction(aSketchArc.lastResult())
127 aResultB = modelAPI_ResultConstruction(aSketchCircle.lastResult())
128 assert (aResultA is not None)
129 assert (aResultB is not None)
130 aRefObjectA.setObject(aResultA)
131 aRefObjectB.setObject(aResultB)
132 aConstraintEqRad.execute()
133 aSession.finishOperation()
134 aCircRadius = aCircleRadius.value();
135 anArcVecX = anArcStartPoint.x() - anArcCentr.x()
136 anArcVecY = anArcStartPoint.y() - anArcCentr.y()
137 anArcRadius = math.sqrt(anArcVecX**2 + anArcVecY**2)
138 assert (math.fabs(aCircRadius - anArcRadius) <= 1.e-10)
139 assert (model.dof(aSketchFeature) == 7)
140 #=========================================================================
141 # A constraint to make equal radii of arc and external circle
142 #=========================================================================
143 aSession.startOperation()
144 anExtCircle = aSketchFeature.addFeature("SketchCircle")
145 anExtCircleCenter = geomDataAPI_Point2D(anExtCircle.attribute("circle_center"))
146 anExtCircleRadius = anExtCircle.real("circle_radius")
147 anExtCircleCenter.setValue(-50., 50.)
148 anExtCircleRadius.setValue(10.)
149 anExtCircle.selection("External").selectSubShape("EDGE", "Sketch_1/SketchCircle_1_2")
150 aSession.finishOperation()
151 assert (model.dof(aSketchFeature) == 7)
152 aSession.startOperation()
153 aConstraintEqRad2 = aSketchFeature.addFeature("SketchConstraintEqual")
154 aRefObjectA = aConstraintEqRad2.refattr("ConstraintEntityA")
155 aRefObjectB = aConstraintEqRad2.refattr("ConstraintEntityB")
156 aRefObjectA.setObject(aSketchCircle.lastResult())
157 aRefObjectB.setObject(anExtCircle.lastResult())
158 aSession.finishOperation()
159 assert (math.fabs(anExtCircleRadius.value() - aCircleRadius.value()) < 1.e-10)
160 anArcVecX = anArcStartPoint.x() - anArcCentr.x()
161 anArcVecY = anArcStartPoint.y() - anArcCentr.y()
162 anArcRadius = math.sqrt(anArcVecX**2 + anArcVecY**2)
163 assert (math.fabs(anExtCircleRadius.value() - anArcRadius) <= 1.e-10)
164 assert (model.dof(aSketchFeature) == 6)
165
166 #=========================================================================
167 # Creation of two different lines
168 #=========================================================================
169 # First Line
170 aSession.startOperation()
171 aSketchLine1 = aSketchFeature.addFeature("SketchLine")
172 aLine1StartPoint = geomDataAPI_Point2D(aSketchLine1.attribute("StartPoint"))
173 aLine1EndPoint = geomDataAPI_Point2D(aSketchLine1.attribute("EndPoint"))
174 aLine1StartPoint.setValue(0., 15.)
175 aLine1EndPoint.setValue(20., 25.)
176 aSession.finishOperation()
177 # Second Line
178 aSession.startOperation()
179 aSketchLine2 = aSketchFeature.addFeature("SketchLine")
180 aLine2StartPoint = geomDataAPI_Point2D(aSketchLine2.attribute("StartPoint"))
181 aLine2EndPoint = geomDataAPI_Point2D(aSketchLine2.attribute("EndPoint"))
182 aLine2StartPoint.setValue(0., 0.)
183 aLine2EndPoint.setValue(-1., 10.)
184 aSession.finishOperation()
185 assert (model.dof(aSketchFeature) == 14)
186 #=========================================================================
187 # A constraint to make equal lengths of lines
188 #=========================================================================
189 aSession.startOperation()
190 aConstraintEqLen = aSketchFeature.addFeature("SketchConstraintEqual")
191 aRefObjectA = aConstraintEqLen.refattr("ConstraintEntityA")
192 aRefObjectB = aConstraintEqLen.refattr("ConstraintEntityB")
193 aResultA = modelAPI_ResultConstruction(aSketchLine1.firstResult())
194 aResultB = modelAPI_ResultConstruction(aSketchLine2.firstResult())
195 assert (aResultA is not None)
196 assert (aResultB is not None)
197 aRefObjectA.setObject(aResultA)
198 aRefObjectB.setObject(aResultB)
199 aConstraintEqLen.execute()
200 aSession.finishOperation()
201 aLine1Len = lineLength(aSketchLine1)
202 aLine2Len = lineLength(aSketchLine2)
203 assert (math.fabs(aLine1Len - aLine2Len) < 1.e-10)
204 assert (model.dof(aSketchFeature) == 13)
205 #=========================================================================
206 # A constraint to make equal length of line with external line
207 #=========================================================================
208 aSession.startOperation()
209 anExtLine = aSketchFeature.addFeature("SketchLine")
210 anExtLineStart = geomDataAPI_Point2D(anExtLine.attribute("StartPoint"))
211 anExtLineEnd = geomDataAPI_Point2D(anExtLine.attribute("EndPoint"))
212 anExtLineStart.setValue(-40., 35.)
213 anExtLineEnd.setValue(-60., 25.)
214 anExtLine.selection("External").selectSubShape("EDGE", "Sketch_1/SketchLine_1")
215 anExtLineLen = lineLength(anExtLine)
216 aSession.finishOperation()
217 assert (model.dof(aSketchFeature) == 13)
218 aSession.startOperation()
219 aConstraintEqLen2 = aSketchFeature.addFeature("SketchConstraintEqual")
220 aRefObjectA = aConstraintEqLen2.refattr("ConstraintEntityA")
221 aRefObjectB = aConstraintEqLen2.refattr("ConstraintEntityB")
222 aRefObjectA.setObject(anExtLine.lastResult())
223 aRefObjectB.setObject(aSketchLine2.lastResult())
224 aSession.finishOperation()
225 aLine1Len = lineLength(aSketchLine1)
226 aLine2Len = lineLength(aSketchLine2)
227 assert (math.fabs(aLine1Len - anExtLineLen) < 1.e-10)
228 assert (math.fabs(aLine2Len - anExtLineLen) < 1.e-10)
229 assert (model.dof(aSketchFeature) == 12)
230 #=========================================================================
231 # Remove costraint to check the DOF
232 #=========================================================================
233 aSession.startOperation()
234 aDocument.removeFeature(aConstraintEqLen2)
235 aSession.finishOperation()
236 assert (model.dof(aSketchFeature) == 13)
237 #=========================================================================
238 # End of test
239 #=========================================================================
240
241 assert(model.checkPythonDump())