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