1 ## Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 ## See http:##www.salome-platform.org/ or
18 ## email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
23 Unit test of SketchPlugin_Fillet class
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());
33 from GeomDataAPI import *
34 from ModelAPI import *
36 from salome.shaper import model
38 #=========================================================================
40 #=========================================================================
43 def createSketch1(theSketch):
44 global aEndPoint1, aEndPoint2
45 # Initialize sketch by three lines with coincident boundaries
48 aSession.startOperation()
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)
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)
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)
71 aCoincidence1 = theSketch.addFeature("SketchConstraintCoincidence")
72 aCoincidence1.refattr("ConstraintEntityA").setAttr(aEndPoint1)
73 aCoincidence1.refattr("ConstraintEntityB").setAttr(aStartPoint2)
75 aCoincidence2 = theSketch.addFeature("SketchConstraintCoincidence")
76 aCoincidence2.refattr("ConstraintEntityA").setAttr(aEndPoint2)
77 aCoincidence2.refattr("ConstraintEntityB").setAttr(aStartPoint3)
79 aSession.finishOperation()
83 def createSketch2(theSketch):
85 # Initialize sketch by line and arc with coincident boundary
88 aSession.startOperation()
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)
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)
106 aCoincidence = theSketch.addFeature("SketchConstraintCoincidence")
107 aCoincidence.refattr("ConstraintEntityA").setAttr(aStartPoint1)
108 aCoincidence.refattr("ConstraintEntityB").setAttr(aStartPoint2)
110 aSession.finishOperation()
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])
126 def checkArcLineSmoothness(theArc, theLine):
127 aCenter = geomDataAPI_Point2D(theArc.attribute("center_point"))
128 aDistance = distancePointLine(aCenter, theLine)
129 aRadius = arcRadius(theArc)
130 assert(math.fabs(aRadius - aDistance) < TOLERANCE)
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)
142 def getCoincidences(theSketch):
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)
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]
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)
165 def distancePointLine(thePoint, theLine):
166 aLineStart = geomDataAPI_Point2D(theLine.attribute("StartPoint"))
167 aLineEnd = geomDataAPI_Point2D(theLine.attribute("EndPoint"))
168 aLength = model.distancePointPoint(aLineStart, aLineEnd)
170 aDir1x, aDir1y = aLineEnd.x() - aLineStart.x(), aLineEnd.y() - aLineStart.y()
171 aDir2x, aDir2y = thePoint.x() - aLineStart.x(), thePoint.y() - aLineStart.y()
172 aCross = aDir1x * aDir2y - aDir1y * aDir2x
173 return math.fabs(aCross) / aLength
176 #=========================================================================
177 # Initialization of the test
178 #=========================================================================
180 __updated__ = "2015-09-18"
182 aSession = ModelAPI_Session.get()
183 aDocument = aSession.moduleDocument()
184 #=========================================================================
185 # Creation of a sketch
186 #=========================================================================
187 aSession.startOperation()
188 aSketchCommonFeature = aDocument.addFeature("Sketch")
189 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
190 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
191 origin.setValue(0, 0, 0)
192 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
193 dirx.setValue(1, 0, 0)
194 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
195 norm.setValue(0, 0, 1)
196 aSession.finishOperation()
197 #=========================================================================
198 # Initialize sketch by three connected lines
199 #=========================================================================
200 createSketch1(aSketchFeature)
201 assert (model.dof(aSketchFeature) == 8)
202 #=========================================================================
204 #=========================================================================
205 aSession.startOperation()
206 aFillet = aSketchFeature.addFeature("SketchFillet")
207 aFillet.refattr("fillet_point").setAttr(aEndPoint1);
208 aSession.finishOperation()
209 aSession.startOperation()
210 aFillet = aSketchFeature.addFeature("SketchFillet")
211 aFillet.refattr("fillet_point").setAttr(aEndPoint2);
212 aSession.finishOperation()
213 #=========================================================================
214 # Verify the objects of fillet are created
215 #=========================================================================
216 checkSmoothness(aSketchFeature)
217 assert (model.dof(aSketchFeature) == 10)
218 #=========================================================================
219 # Move a line and check the fillet is correct
220 #=========================================================================
221 DELTA_X = DELTA_Y = 10.
222 aSession.startOperation()
223 aEndPoint1.setValue(aEndPoint1.x() + DELTA_X, aEndPoint1.y() + DELTA_Y)
224 aSession.finishOperation()
225 checkSmoothness(aSketchFeature)
226 assert (model.dof(aSketchFeature) == 10)
229 #=========================================================================
230 # Create another sketch
231 #=========================================================================
232 aSession.startOperation()
233 aSketchCommonFeature = aDocument.addFeature("Sketch")
234 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
235 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
236 origin.setValue(0, 0, 0)
237 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
238 dirx.setValue(1, 0, 0)
239 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
240 norm.setValue(0, 1, 0)
241 aSession.finishOperation()
242 #=========================================================================
243 # Initialize sketch by line and arc
244 #=========================================================================
245 createSketch2(aSketchFeature)
246 assert (model.dof(aSketchFeature) == 7)
247 #=========================================================================
249 #=========================================================================
250 aSession.startOperation()
251 aFillet = aSketchFeature.addFeature("SketchFillet")
252 aFillet.refattr("fillet_point").setAttr(aStartPoint1)
253 aSession.finishOperation()
254 #=========================================================================
255 # Verify the objects of fillet are created
256 #=========================================================================
257 checkSmoothness(aSketchFeature)
258 assert (model.dof(aSketchFeature) == 8)
259 #=========================================================================
260 # Move a line and check the fillet is correct
261 #=========================================================================
264 aSession.startOperation()
265 aStartPoint1.setValue(aStartPoint1.x() + DELTA_X, aStartPoint1.y() + DELTA_Y)
266 aSession.finishOperation()
267 checkSmoothness(aSketchFeature)
268 assert (model.dof(aSketchFeature) == 8)
269 #=========================================================================
271 #=========================================================================
273 # TODO: Improve Fillet test case by moving one of filleted objectes and check coincidence and tangency are correct
275 assert(model.checkPythonDump())