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