Salome HOME
Adjust test cases according to redesigned Arc and Circle features
[modules/shaper.git] / src / SketchPlugin / Test / TestFillet.py
1 """
2     TestFillet.py
3     Unit test of SketchPlugin_Fillet class
4
5     SketchPlugin_Fillet
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());
11
12 """
13 from GeomDataAPI import *
14 from ModelAPI import *
15 import math
16 from salome.shaper import model
17
18 #=========================================================================
19 # Auxiliary functions
20 #=========================================================================
21 TOLERANCE = 1.e-7
22
23 def createSketch1(theSketch):
24     global aEndPoint1, aEndPoint2
25     # Initialize sketch by three lines with coincident boundaries
26     allFeatures = []
27
28     aSession.startOperation()
29     # Line1
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)
36     # Line2
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)
43     # Line3
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)
50     # Coincidence1
51     aCoincidence1 = theSketch.addFeature("SketchConstraintCoincidence")
52     aCoincidence1.refattr("ConstraintEntityA").setAttr(aEndPoint1)
53     aCoincidence1.refattr("ConstraintEntityB").setAttr(aStartPoint2)
54     # Coincidence2
55     aCoincidence2 = theSketch.addFeature("SketchConstraintCoincidence")
56     aCoincidence2.refattr("ConstraintEntityA").setAttr(aEndPoint2)
57     aCoincidence2.refattr("ConstraintEntityB").setAttr(aStartPoint3)
58
59     aSession.finishOperation()
60     return allFeatures
61
62
63 def createSketch2(theSketch):
64     global aStartPoint1
65     # Initialize sketch by line and arc with coincident boundary
66     allFeatures = []
67
68     aSession.startOperation()
69     # Line
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)
76     # Arc
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)
85     # Coincidence
86     aCoincidence = theSketch.addFeature("SketchConstraintCoincidence")
87     aCoincidence.refattr("ConstraintEntityA").setAttr(aStartPoint1)
88     aCoincidence.refattr("ConstraintEntityB").setAttr(aStartPoint2)
89
90     aSession.finishOperation()
91     return allFeatures
92
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])
105
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)
111
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)
121
122 def getCoincidences(theSketch):
123     aCoincidences = []
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)
131     return aCoincidences
132
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]
139
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)
144
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)
149
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
154
155
156 #=========================================================================
157 # Initialization of the test
158 #=========================================================================
159
160 __updated__ = "2015-09-18"
161
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 #=========================================================================
183 # Create the Fillet
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)
207
208
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 #=========================================================================
228 # Create the Fillet
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 #=========================================================================
242 DELTA_X = 1.
243 DELTA_Y = -2.
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 #=========================================================================
250 # End of test
251 #=========================================================================
252
253 # TODO: Improve Fillet test case by moving one of filleted objectes and check coincidence and tangency are correct
254
255 assert(model.checkPythonDump())