Salome HOME
69a2d6fc18dcc0532014b397faea2aef809c344a
[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 = len(theObjects)
94     for feat in theObjects:
95         assert(feat is not None)
96         if (feat.getKind() == "SketchLine"):
97             aLine.append(feat)
98         elif (feat.getKind() == "SketchArc"):
99             anArc.append(feat)
100     aFilletArc = anArc[-1]
101     assert(aFilletArc is not None)
102     anArc.pop()
103
104     anArcPoints = []
105     aPoint = geomDataAPI_Point2D(aFilletArc.attribute("ArcStartPoint"))
106     #print "ArcStartPoint " + repr(aPoint.x()) + " " + repr(aPoint.y())
107     anArcPoints.append((aPoint.x(), aPoint.y()))
108     aPoint = geomDataAPI_Point2D(aFilletArc.attribute("ArcEndPoint"))
109     #print "ArcEndPoint " + repr(aPoint.x()) + " " + repr(aPoint.y())
110     anArcPoints.append((aPoint.x(), aPoint.y()))
111     aPoint = geomDataAPI_Point2D(aFilletArc.attribute("ArcCenter"))
112     #print "ArcCenter " + repr(aPoint.x()) + " " + repr(aPoint.y())
113     aCenterX = aPoint.x()
114     aCenterY = aPoint.y()
115     aFilletRadius = math.hypot(anArcPoints[0][0]-aCenterX, anArcPoints[0][1]-aCenterY)
116
117     for line in aLine:
118         aStartPoint = geomDataAPI_Point2D(line.attribute("StartPoint"))
119         aEndPoint = geomDataAPI_Point2D(line.attribute("EndPoint"))
120
121         aLinePoints = []
122         aLinePoints.append((aStartPoint.x(), aStartPoint.y()))
123         #print "aLineStartPoint " + repr(aStartPoint.x()) + " " + repr(aStartPoint.y())
124         aLinePoints.append((aEndPoint.x(), aEndPoint.y()))
125         #print "aLineEndPoint " + repr(aEndPoint.x()) + " " + repr(aEndPoint.y())
126
127         aLineDirX = aEndPoint.x() - aStartPoint.x()
128         aLineDirY = aEndPoint.y() - aStartPoint.y()
129
130         for arcPt in anArcPoints:
131             for linePt in aLinePoints:
132                 if (math.hypot(linePt[0]-arcPt[0], linePt[1]-arcPt[1]) < 1.e-10):
133                     aDirX = linePt[0] - aCenterX
134                     aDirY = linePt[1] - aCenterY
135                     assert(math.fabs(math.hypot(aDirX, aDirY) - theRadius) < 1.e-7)
136                     aDot = aDirX * aLineDirX + aDirY * aLineDirY
137
138                     break;
139
140     if (aSize == 3):
141         for arc in anArc:
142             aStartPoint = geomDataAPI_Point2D(arc.attribute("ArcStartPoint"))
143             aEndPoint = geomDataAPI_Point2D(arc.attribute("ArcEndPoint"))
144             aCenterPoint = geomDataAPI_Point2D(arc.attribute("ArcCenter"))
145
146             aBaseArcPoints = []
147             aBaseArcPoints.append((aStartPoint.x(), aStartPoint.y()))
148             #print "anArcStartPoint " + repr(aStartPoint.x()) + " " + repr(aStartPoint.y())
149             aBaseArcPoints.append((aEndPoint.x(), aEndPoint.y()))
150             #print "anArcEndPoint " + repr(aEndPoint.x()) + " " + repr(aEndPoint.y())
151             #print "anArcCenter " + repr(aCenterPoint.x()) + " " + repr(aCenterPoint.y())
152
153             aRadius = math.hypot(aStartPoint.x()-aCenterPoint.x(), aStartPoint.y()-aCenterPoint.y())
154             aDist = math.hypot(aCenterPoint.x() - aCenterX, aCenterPoint.y() - aCenterY)
155             assert math.fabs(aFilletRadius + aRadius - aDist) < 1.e-7 or math.fabs(math.fabs(aFilletRadius - aRadius) - aDist) < 1.e-7, \
156                 "Fillet radius = {0}, Base arc radius = {1}, distance between centers = {2}".format(aFilletRadius, aRadius, aDist)
157
158
159 #=========================================================================
160 # Initialization of the test
161 #=========================================================================
162
163 __updated__ = "2015-09-18"
164
165 aSession = ModelAPI_Session.get()
166 aDocument = aSession.moduleDocument()
167 #=========================================================================
168 # Creation of a sketch
169 #=========================================================================
170 aSession.startOperation()
171 aSketchCommonFeature = aDocument.addFeature("Sketch")
172 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
173 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
174 origin.setValue(0, 0, 0)
175 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
176 dirx.setValue(1, 0, 0)
177 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
178 norm.setValue(0, 0, 1)
179 aSession.finishOperation()
180 #=========================================================================
181 # Initialize sketch by two lines
182 #=========================================================================
183 aSession.startOperation()
184 aFeaturesList = createSketch1(aSketchFeature)
185 aSession.finishOperation()
186 aSketchSubFeatures = []
187 for aSubIndex in range(0, aSketchFeature.numberOfSubs()):
188     aSketchSubFeatures.append(aSketchFeature.subFeature(aSubIndex))
189 #=========================================================================
190 # Global variables
191 #=========================================================================
192 FILLET_RADIUS1 = 3.
193 FILLET_RADIUS2 = 5.
194 #=========================================================================
195 # Create the Fillet
196 #=========================================================================
197 aSession.startOperation()
198 aFillet = aSketchFeature.addFeature("SketchConstraintFillet")
199 aRefAttrA = aFillet.data().refattrlist("ConstraintEntityA");
200 aRefAttrA.append(aEndPoint1)
201 aRefAttrA.append(aEndPoint2)
202 aRadius = aFillet.real("ConstraintValue")
203 aRadius.setValue(FILLET_RADIUS1)
204 aFillet.execute()
205 aResObjects = []
206 for aSubIndex in range(0, aSketchFeature.numberOfSubs()):
207     aSubFeature = aSketchFeature.subFeature(aSubIndex)
208     if aSubFeature not in aSketchSubFeatures:
209         if aSubFeature.getKind() == "SketchLine":
210             aResObjects.insert(0, aSubFeature)
211         elif aSubFeature.getKind() == "SketchArc":
212             aResObjects.append(aSubFeature)
213 #=========================================================================
214 # Verify the objects of fillet are created
215 #=========================================================================
216 assert(aResObjects)
217 checkFillet(aResObjects, FILLET_RADIUS1)
218 #=========================================================================
219 # Change Fillet radius
220 #=========================================================================
221 aRadius.setValue(FILLET_RADIUS2)
222 aSession.finishOperation()
223 checkFillet(aResObjects, FILLET_RADIUS2)
224
225 #=========================================================================
226 # Create another sketch
227 #=========================================================================
228 aSession.startOperation()
229 aSketchCommonFeature = aDocument.addFeature("Sketch")
230 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
231 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
232 origin.setValue(0, 0, 0)
233 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
234 dirx.setValue(1, 0, 0)
235 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
236 norm.setValue(0, 1, 0)
237 aSession.finishOperation()
238 #=========================================================================
239 # Initialize sketch by line and arc
240 #=========================================================================
241 aSession.startOperation()
242 aFeaturesList = createSketch2(aSketchFeature)
243 aSession.finishOperation()
244 aSketchSubFeatures = []
245 for aSubIndex in range(0, aSketchFeature.numberOfSubs()):
246     aSketchSubFeatures.append(aSketchFeature.subFeature(aSubIndex))
247 #=========================================================================
248 # Create the Fillet
249 #=========================================================================
250 aSession.startOperation()
251 aFillet = aSketchFeature.addFeature("SketchConstraintFillet")
252 aRefAttrA = aFillet.data().refattrlist("ConstraintEntityA");
253 aRefAttrA.append(aStartPoint1)
254 aRadius = aFillet.real("ConstraintValue")
255 aRadius.setValue(FILLET_RADIUS1)
256 aFillet.execute()
257 aResObjects = []
258 for aSubIndex in range(0, aSketchFeature.numberOfSubs()):
259     aSubFeature = aSketchFeature.subFeature(aSubIndex)
260     if aSubFeature not in aSketchSubFeatures:
261         if aSubFeature.getKind() == "SketchLine":
262             aResObjects.insert(0, aSubFeature)
263         elif aSubFeature.getKind() == "SketchArc":
264             aResObjects.append(aSubFeature)
265 #=========================================================================
266 # Verify the objects of fillet are created
267 #=========================================================================
268 assert(aResObjects)
269 checkFillet(aResObjects, FILLET_RADIUS1)
270 #=========================================================================
271 # Change Fillet radius
272 #=========================================================================
273 aRadius.setValue(FILLET_RADIUS2)
274 aSession.finishOperation()
275 checkFillet(aResObjects, FILLET_RADIUS2)
276 #=========================================================================
277 # End of test
278 #=========================================================================
279
280 import model
281 assert(model.checkPythonDump())