Salome HOME
453a68891533a020f914f2dca1664663cb8385c1
[modules/shaper.git] / src / SketchPlugin / Test / TestFillet.py
1 """
2     TestFillet.py
3     Unit test of SketchPlugin_ConstraintFillet class
4
5     SketchPlugin_ConstraintFillet
6         static const std::string MY_CONSTRAINT_FILLET_ID("SketchConstraintFillet");
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
17 #=========================================================================
18 # Auxiliary functions
19 #=========================================================================
20 aStartPoint1 = []
21
22 def createSketch1(theSketch):
23     global aEndPoint1, aEndPoint2
24     # Initialize sketch by two lines with coincident boundary
25     allFeatures = []
26     # Line1
27     aSketchLine1 = theSketch.addFeature("SketchLine")
28     aStartPoint1 = geomDataAPI_Point2D(aSketchLine1.attribute("StartPoint"))
29     aEndPoint1   = geomDataAPI_Point2D(aSketchLine1.attribute("EndPoint"))
30     aStartPoint1.setValue(-10., -10.)
31     aEndPoint1.setValue(-10., 10.)
32     allFeatures.append(aSketchLine1)
33     # Line2
34     aSketchLine2 = theSketch.addFeature("SketchLine")
35     aStartPoint2 = geomDataAPI_Point2D(aSketchLine2.attribute("StartPoint"))
36     aEndPoint2   = geomDataAPI_Point2D(aSketchLine2.attribute("EndPoint"))
37     aStartPoint2.setValue(-10., 10.)
38     aEndPoint2.setValue(10., 10.)
39     allFeatures.append(aSketchLine2)
40     # Line3
41     aSketchLine3 = theSketch.addFeature("SketchLine")
42     aStartPoint3 = geomDataAPI_Point2D(aSketchLine3.attribute("StartPoint"))
43     aEndPoint3   = geomDataAPI_Point2D(aSketchLine3.attribute("EndPoint"))
44     aStartPoint3.setValue(10., 10.)
45     aEndPoint3.setValue(10., -10.)
46     allFeatures.append(aSketchLine3)
47     # Coincidence1
48     aCoincidence1 = theSketch.addFeature("SketchConstraintCoincidence")
49     aCoincidence1.refattr("ConstraintEntityA").setAttr(aEndPoint1)
50     aCoincidence1.refattr("ConstraintEntityB").setAttr(aStartPoint2)
51     # Coincidence2
52     aCoincidence2 = theSketch.addFeature("SketchConstraintCoincidence")
53     aCoincidence2.refattr("ConstraintEntityA").setAttr(aEndPoint2)
54     aCoincidence2.refattr("ConstraintEntityB").setAttr(aStartPoint3)
55
56     theSketch.execute()
57     return allFeatures
58
59
60 def createSketch2(theSketch):
61     global aStartPoint1
62     # Initialize sketch by line and arc with coincident boundary
63     allFeatures = []
64     # Line
65     aSketchLine = theSketch.addFeature("SketchLine")
66     aStartPoint1 = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint"))
67     aEndPoint1   = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint"))
68     aStartPoint1.setValue(10., 10.)
69     aEndPoint1.setValue(30., 5.)
70     allFeatures.append(aSketchLine)
71     # Arc
72     aSketchArc = theSketch.addFeature("SketchArc")
73     aStartPoint2 = geomDataAPI_Point2D(aSketchArc.attribute("ArcStartPoint"))
74     aEndPoint2   = geomDataAPI_Point2D(aSketchArc.attribute("ArcEndPoint"))
75     aCenterPoint = geomDataAPI_Point2D(aSketchArc.attribute("ArcCenter"))
76     aCenterPoint.setValue(20., 10.)
77     aStartPoint2.setValue(10., 10.)
78     aEndPoint2.setValue(20., 0.)
79     allFeatures.append(aSketchArc)
80     # Coincidence
81     aCoincidence = theSketch.addFeature("SketchConstraintCoincidence")
82     aCoincidence.refattr("ConstraintEntityA").setAttr(aStartPoint1)
83     aCoincidence.refattr("ConstraintEntityB").setAttr(aStartPoint2)
84
85     theSketch.execute()
86     return allFeatures
87
88 def checkFillet(theObjects, theRadius):
89     # Verify the arc and lines are connected smoothly
90     print "Check Fillet"
91     aLine = []
92     anArc = []
93     aSize = theObjects.size()
94     for i in range (0, aSize):
95         feat = ModelAPI_Feature.feature(theObjects.object(i))
96         assert(feat is not None)
97         if (feat.getKind() == "SketchLine"):
98             aLine.append(feat)
99         elif (feat.getKind() == "SketchArc"):
100             anArc.append(feat)
101     aFilletArc = anArc[-1]
102     assert(aFilletArc is not None)
103     anArc.pop()
104
105     anArcPoints = []
106     aPoint = geomDataAPI_Point2D(aFilletArc.attribute("ArcStartPoint"))
107     print "ArcStartPoint " + repr(aPoint.x()) + " " + repr(aPoint.y())
108     anArcPoints.append((aPoint.x(), aPoint.y()))
109     aPoint = geomDataAPI_Point2D(aFilletArc.attribute("ArcEndPoint"))
110     print "ArcEndPoint " + repr(aPoint.x()) + " " + repr(aPoint.y())
111     anArcPoints.append((aPoint.x(), aPoint.y()))
112     aPoint = geomDataAPI_Point2D(aFilletArc.attribute("ArcCenter"))
113     print "ArcCenter " + repr(aPoint.x()) + " " + repr(aPoint.y())
114     aCenterX = aPoint.x()
115     aCenterY = aPoint.y()
116     aFilletRadius = math.hypot(anArcPoints[0][0]-aCenterX, anArcPoints[0][1]-aCenterY)
117
118     for line in aLine:
119         aStartPoint = geomDataAPI_Point2D(line.attribute("StartPoint"))
120         aEndPoint = geomDataAPI_Point2D(line.attribute("EndPoint"))
121
122         aLinePoints = []
123         aLinePoints.append((aStartPoint.x(), aStartPoint.y()))
124         print "aLineStartPoint " + repr(aStartPoint.x()) + " " + repr(aStartPoint.y())
125         aLinePoints.append((aEndPoint.x(), aEndPoint.y()))
126         print "aLineEndPoint " + repr(aEndPoint.x()) + " " + repr(aEndPoint.y())
127
128         aLineDirX = aEndPoint.x() - aStartPoint.x()
129         aLineDirY = aEndPoint.y() - aStartPoint.y()
130
131         for arcPt in anArcPoints:
132             for linePt in aLinePoints:
133                 if (math.hypot(linePt[0]-arcPt[0], linePt[1]-arcPt[1]) < 1.e-10):
134                     aDirX = linePt[0] - aCenterX
135                     aDirY = linePt[1] - aCenterY
136                     assert(math.fabs(math.hypot(aDirX, aDirY) - theRadius) < 1.e-7)
137                     aDot = aDirX * aLineDirX + aDirY * aLineDirY
138
139                     break;
140
141     if (aSize == 3):
142         for arc in anArc:
143             aStartPoint = geomDataAPI_Point2D(arc.attribute("ArcStartPoint"))
144             aEndPoint = geomDataAPI_Point2D(arc.attribute("ArcEndPoint"))
145             aCenterPoint = geomDataAPI_Point2D(arc.attribute("ArcCenter"))
146
147             aBaseArcPoints = []
148             aBaseArcPoints.append((aStartPoint.x(), aStartPoint.y()))
149             print "anArcStartPoint " + repr(aStartPoint.x()) + " " + repr(aStartPoint.y())
150             aBaseArcPoints.append((aEndPoint.x(), aEndPoint.y()))
151             print "anArcEndPoint " + repr(aEndPoint.x()) + " " + repr(aEndPoint.y())
152             print "anArcCenter " + repr(aCenterPoint.x()) + " " + repr(aCenterPoint.y())
153
154             aRadius = math.hypot(aStartPoint.x()-aCenterPoint.x(), aStartPoint.y()-aCenterPoint.y())
155             aDist = math.hypot(aCenterPoint.x() - aCenterX, aCenterPoint.y() - aCenterY)
156             assert math.fabs(aFilletRadius + aRadius - aDist) < 1.e-7 or math.fabs(math.fabs(aFilletRadius - aRadius) - aDist) < 1.e-7, \
157                 "Fillet radius = {0}, Base arc radius = {1}, distance between centers = {2}".format(aFilletRadius, aRadius, aDist)
158
159
160 #=========================================================================
161 # Initialization of the test
162 #=========================================================================
163
164 __updated__ = "2015-09-18"
165
166 aSession = ModelAPI_Session.get()
167 aDocument = aSession.moduleDocument()
168 #=========================================================================
169 # Creation of a sketch
170 #=========================================================================
171 aSession.startOperation()
172 aSketchCommonFeature = aDocument.addFeature("Sketch")
173 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
174 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
175 origin.setValue(0, 0, 0)
176 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
177 dirx.setValue(1, 0, 0)
178 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
179 norm.setValue(0, 0, 1)
180 aSession.finishOperation()
181 #=========================================================================
182 # Initialize sketch by two lines
183 #=========================================================================
184 aSession.startOperation()
185 aFeaturesList = createSketch1(aSketchFeature)
186 aSession.finishOperation()
187 #=========================================================================
188 # Global variables
189 #=========================================================================
190 FILLET_RADIUS1 = 3.
191 FILLET_RADIUS2 = 5.
192 #=========================================================================
193 # Create the Fillet
194 #=========================================================================
195 aSession.startOperation()
196 aFillet = aSketchFeature.addFeature("SketchConstraintFillet")
197 aRefAttrA = aFillet.data().refattrlist("ConstraintEntityA");
198 aRefAttrA.append(aEndPoint1)
199 aRefAttrA.append(aEndPoint2)
200 aRadius = aFillet.real("ConstraintValue")
201 aRadius.setValue(FILLET_RADIUS1)
202 aFillet.execute()
203 aResObjects = aFillet.reflist("ConstraintEntityB")
204 #=========================================================================
205 # Verify the objects of fillet are created
206 #=========================================================================
207 assert(aResObjects)
208 checkFillet(aResObjects, FILLET_RADIUS1)
209 #=========================================================================
210 # Change Fillet radius
211 #=========================================================================
212 aRadius.setValue(FILLET_RADIUS2)
213 aSession.finishOperation()
214 checkFillet(aResObjects, FILLET_RADIUS2)
215
216 #=========================================================================
217 # Create another sketch
218 #=========================================================================
219 aSession.startOperation()
220 aSketchCommonFeature = aDocument.addFeature("Sketch")
221 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
222 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
223 origin.setValue(0, 0, 0)
224 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
225 dirx.setValue(1, 0, 0)
226 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
227 norm.setValue(0, 1, 0)
228 aSession.finishOperation()
229 #=========================================================================
230 # Initialize sketch by line and arc
231 #=========================================================================
232 aSession.startOperation()
233 aFeaturesList = createSketch2(aSketchFeature)
234 aSession.finishOperation()
235 #=========================================================================
236 # Create the Fillet
237 #=========================================================================
238 aSession.startOperation()
239 aFillet = aSketchFeature.addFeature("SketchConstraintFillet")
240 aRefAttrA = aFillet.data().refattrlist("ConstraintEntityA");
241 aRefAttrA.append(aStartPoint1)
242 aRadius = aFillet.real("ConstraintValue")
243 aRadius.setValue(FILLET_RADIUS1)
244 aFillet.execute()
245 aResObjects = aFillet.reflist("ConstraintEntityB")
246 #=========================================================================
247 # Verify the objects of fillet are created
248 #=========================================================================
249 assert(aResObjects)
250 checkFillet(aResObjects, FILLET_RADIUS1)
251 #=========================================================================
252 # Change Fillet radius
253 #=========================================================================
254 aRadius.setValue(FILLET_RADIUS2)
255 aSession.finishOperation()
256 checkFillet(aResObjects, FILLET_RADIUS2)
257 #=========================================================================
258 # End of test
259 #=========================================================================