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