]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/Test/TestFillet.py
Salome HOME
Improve the coverage report generator file
[modules/shaper.git] / src / SketchPlugin / Test / TestFillet.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     TestFillet.py
23     Unit test of SketchPlugin_Fillet class
24
25     SketchPlugin_Fillet
26         static const std::string MY_CONSTRAINT_FILLET_ID("SketchFillet");
27         data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
28         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
29         data()->addAttribute(SketchPlugin_Constraint::ENTITY_C(), ModelAPI_AttributeRefAttrList::typeId());
30
31 """
32 from GeomDataAPI import *
33 from ModelAPI import *
34 import math
35 from salome.shaper import model
36
37 #=========================================================================
38 # Auxiliary functions
39 #=========================================================================
40 TOLERANCE = 1.e-7
41
42 def createSketch1(theSketch):
43     global aEndPoint1, aEndPoint2
44     # Initialize sketch by three lines with coincident boundaries
45     allFeatures = []
46
47     aSession.startOperation()
48     # Line1
49     aSketchLine1 = theSketch.addFeature("SketchLine")
50     aStartPoint1 = geomDataAPI_Point2D(aSketchLine1.attribute("StartPoint"))
51     aEndPoint1   = geomDataAPI_Point2D(aSketchLine1.attribute("EndPoint"))
52     aStartPoint1.setValue(-10., -10.)
53     aEndPoint1.setValue(-10., 10.)
54     allFeatures.append(aSketchLine1)
55     # Line2
56     aSketchLine2 = theSketch.addFeature("SketchLine")
57     aStartPoint2 = geomDataAPI_Point2D(aSketchLine2.attribute("StartPoint"))
58     aEndPoint2   = geomDataAPI_Point2D(aSketchLine2.attribute("EndPoint"))
59     aStartPoint2.setValue(-10., 10.)
60     aEndPoint2.setValue(10., 10.)
61     allFeatures.append(aSketchLine2)
62     # Line3
63     aSketchLine3 = theSketch.addFeature("SketchLine")
64     aStartPoint3 = geomDataAPI_Point2D(aSketchLine3.attribute("StartPoint"))
65     aEndPoint3   = geomDataAPI_Point2D(aSketchLine3.attribute("EndPoint"))
66     aStartPoint3.setValue(10., 10.)
67     aEndPoint3.setValue(10., -10.)
68     allFeatures.append(aSketchLine3)
69     # Coincidence1
70     aCoincidence1 = theSketch.addFeature("SketchConstraintCoincidence")
71     aCoincidence1.refattr("ConstraintEntityA").setAttr(aEndPoint1)
72     aCoincidence1.refattr("ConstraintEntityB").setAttr(aStartPoint2)
73     # Coincidence2
74     aCoincidence2 = theSketch.addFeature("SketchConstraintCoincidence")
75     aCoincidence2.refattr("ConstraintEntityA").setAttr(aEndPoint2)
76     aCoincidence2.refattr("ConstraintEntityB").setAttr(aStartPoint3)
77
78     aSession.finishOperation()
79     return allFeatures
80
81
82 def createSketch2(theSketch):
83     global aStartPoint1
84     # Initialize sketch by line and arc with coincident boundary
85     allFeatures = []
86
87     aSession.startOperation()
88     # Line
89     aSketchLine = theSketch.addFeature("SketchLine")
90     aStartPoint1 = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint"))
91     aEndPoint1   = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint"))
92     aStartPoint1.setValue(10., 10.)
93     aEndPoint1.setValue(30., 15.)
94     allFeatures.append(aSketchLine)
95     # Arc
96     aSketchArc = theSketch.addFeature("SketchArc")
97     aStartPoint2 = geomDataAPI_Point2D(aSketchArc.attribute("start_point"))
98     aEndPoint2   = geomDataAPI_Point2D(aSketchArc.attribute("end_point"))
99     aCenterPoint = geomDataAPI_Point2D(aSketchArc.attribute("center_point"))
100     aCenterPoint.setValue(20., 10.)
101     aStartPoint2.setValue(10., 10.)
102     aEndPoint2.setValue(20., 0.)
103     allFeatures.append(aSketchArc)
104     # Coincidence
105     aCoincidence = theSketch.addFeature("SketchConstraintCoincidence")
106     aCoincidence.refattr("ConstraintEntityA").setAttr(aStartPoint1)
107     aCoincidence.refattr("ConstraintEntityB").setAttr(aStartPoint2)
108
109     aSession.finishOperation()
110     return allFeatures
111
112 def checkSmoothness(theSketch):
113     aPtPtCoincidences = getCoincidences(theSketch)
114     for coinc in aPtPtCoincidences:
115         aConnectedFeatures = connectedFeatures(coinc)
116         assert(len(aConnectedFeatures) == 2)
117         if aConnectedFeatures[0].getKind() == "SketchArc":
118             if aConnectedFeatures[1].getKind() == "SketchArc":
119                 checkArcArcSmoothness(aConnectedFeatures[0], aConnectedFeatures[1])
120             elif aConnectedFeatures[1].getKind() == "SketchLine":
121                 checkArcLineSmoothness(aConnectedFeatures[0], aConnectedFeatures[1])
122         elif aConnectedFeatures[0].getKind() == "SketchLine" and aConnectedFeatures[1].getKind() == "SketchArc":
123             checkArcLineSmoothness(aConnectedFeatures[1], aConnectedFeatures[0])
124
125 def checkArcLineSmoothness(theArc, theLine):
126     aCenter = geomDataAPI_Point2D(theArc.attribute("center_point"))
127     aDistance = model.distancePointLine(aCenter, theLine)
128     aRadius = arcRadius(theArc)
129     assert(math.fabs(aRadius - aDistance) < TOLERANCE)
130
131 def checkArcArcSmoothness(theArc1, theArc2):
132     aCenter1 = geomDataAPI_Point2D(theArc1.attribute("center_point"))
133     aCenter2 = geomDataAPI_Point2D(theArc2.attribute("center_point"))
134     aDistance = model.distancePointPoint(aCenter1, aCenter2)
135     aRadius1 = arcRadius(theArc1)
136     aRadius2 = arcRadius(theArc2)
137     aRadSum = aRadius1 + aRadius2
138     aRadDiff = math.fabs(aRadius1 - aRadius2)
139     assert(math.fabs(aDistance - aRadSum) < TOLERANCE or math.fabs(aDistance - aRadDiff) < TOLERANCE)
140
141 def getCoincidences(theSketch):
142     aCoincidences = []
143     for anIndex in range(0, theSketch.numberOfSubs()):
144         aSubFeature = theSketch.subFeature(anIndex)
145         if aSubFeature.getKind() == "SketchConstraintCoincidence":
146             anEntityA = aSubFeature.refattr("ConstraintEntityA")
147             anEntityB = aSubFeature.refattr("ConstraintEntityB")
148             if not anEntityA.isObject() and not anEntityB.isObject():
149                 aCoincidences.append(aSubFeature)
150     return aCoincidences
151
152 def connectedFeatures(theCoincidence):
153     anEntityA = theCoincidence.refattr("ConstraintEntityA")
154     anEntityB = theCoincidence.refattr("ConstraintEntityB")
155     aFeatureA = ModelAPI.ModelAPI_Feature.feature(anEntityA.attr().owner())
156     aFeatureB = ModelAPI.ModelAPI_Feature.feature(anEntityB.attr().owner())
157     return [aFeatureA, aFeatureB]
158
159 def arcRadius(theArc):
160     aCenter = geomDataAPI_Point2D(theArc.attribute("center_point"))
161     aStart = geomDataAPI_Point2D(theArc.attribute("start_point"))
162     return model.distancePointPoint(aCenter, aStart)
163
164
165 #=========================================================================
166 # Initialization of the test
167 #=========================================================================
168
169 __updated__ = "2015-09-18"
170
171 aSession = ModelAPI_Session.get()
172 aDocument = aSession.moduleDocument()
173 #=========================================================================
174 # Creation of a sketch
175 #=========================================================================
176 aSession.startOperation()
177 aSketchCommonFeature = aDocument.addFeature("Sketch")
178 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
179 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
180 origin.setValue(0, 0, 0)
181 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
182 dirx.setValue(1, 0, 0)
183 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
184 norm.setValue(0, 0, 1)
185 aSession.finishOperation()
186 #=========================================================================
187 # Initialize sketch by three connected lines
188 #=========================================================================
189 createSketch1(aSketchFeature)
190 assert (model.dof(aSketchFeature) == 8)
191 #=========================================================================
192 # Create the Fillet
193 #=========================================================================
194 aSession.startOperation()
195 aFillet = aSketchFeature.addFeature("SketchFillet")
196 aFillet.refattr("fillet_point").setAttr(aEndPoint1);
197 aSession.finishOperation()
198 aSession.startOperation()
199 aFillet = aSketchFeature.addFeature("SketchFillet")
200 aFillet.refattr("fillet_point").setAttr(aEndPoint2);
201 aSession.finishOperation()
202 #=========================================================================
203 # Verify the objects of fillet are created
204 #=========================================================================
205 checkSmoothness(aSketchFeature)
206 assert (model.dof(aSketchFeature) == 10)
207 #=========================================================================
208 # Move a line and check the fillet is correct
209 #=========================================================================
210 DELTA_X = DELTA_Y = 10.
211 aSession.startOperation()
212 aEndPoint1.setValue(aEndPoint1.x() + DELTA_X, aEndPoint1.y() + DELTA_Y)
213 aSession.finishOperation()
214 checkSmoothness(aSketchFeature)
215 assert (model.dof(aSketchFeature) == 10)
216
217
218 #=========================================================================
219 # Create another sketch
220 #=========================================================================
221 aSession.startOperation()
222 aSketchCommonFeature = aDocument.addFeature("Sketch")
223 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
224 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
225 origin.setValue(0, 0, 0)
226 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
227 dirx.setValue(1, 0, 0)
228 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
229 norm.setValue(0, 1, 0)
230 aSession.finishOperation()
231 #=========================================================================
232 # Initialize sketch by line and arc
233 #=========================================================================
234 createSketch2(aSketchFeature)
235 assert (model.dof(aSketchFeature) == 7)
236 #=========================================================================
237 # Create the Fillet
238 #=========================================================================
239 aSession.startOperation()
240 aFillet = aSketchFeature.addFeature("SketchFillet")
241 aFillet.refattr("fillet_point").setAttr(aStartPoint1)
242 aSession.finishOperation()
243 #=========================================================================
244 # Verify the objects of fillet are created
245 #=========================================================================
246 checkSmoothness(aSketchFeature)
247 assert (model.dof(aSketchFeature) == 8)
248 #=========================================================================
249 # Move a line and check the fillet is correct
250 #=========================================================================
251 DELTA_X = 1.
252 DELTA_Y = -2.
253 aSession.startOperation()
254 aStartPoint1.setValue(aStartPoint1.x() + DELTA_X, aStartPoint1.y() + DELTA_Y)
255 aSession.finishOperation()
256 checkSmoothness(aSketchFeature)
257 assert (model.dof(aSketchFeature) == 8)
258 #=========================================================================
259 # End of test
260 #=========================================================================
261
262 # TODO: Improve Fillet test case by moving one of filleted objectes and check coincidence and tangency are correct
263
264 assert(model.checkPythonDump())