Salome HOME
Add tools
[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_AttributeRefAttr::typeId());
9         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
10         data()->addAttribute(SketchPlugin_Constraint::ENTITY_C(), ModelAPI_AttributeRefList::typeId());
11
12 """
13 from GeomDataAPI import *
14 from ModelAPI import *
15 import math
16
17 #=========================================================================
18 # Auxiliary functions
19 #=========================================================================
20 def createSketch(theSketch):
21     # Initialize sketch by two lines with coincident boundary
22     allFeatures = []
23     # Line1
24     aSketchLine1 = theSketch.addFeature("SketchLine")
25     aStartPoint1 = geomDataAPI_Point2D(aSketchLine1.attribute("StartPoint"))
26     aEndPoint1   = geomDataAPI_Point2D(aSketchLine1.attribute("EndPoint"))
27     aStartPoint1.setValue(10., 5.)
28     aEndPoint1.setValue(-10., 10.)
29     allFeatures.append(aSketchLine1)
30     # Line2
31     aSketchLine2 = theSketch.addFeature("SketchLine")
32     aStartPoint2 = geomDataAPI_Point2D(aSketchLine2.attribute("StartPoint"))
33     aEndPoint2   = geomDataAPI_Point2D(aSketchLine2.attribute("EndPoint"))
34     aStartPoint2.setValue(10., 5.)
35     aEndPoint2.setValue(15., 20.)
36     allFeatures.append(aSketchLine2)
37     # Coincidence
38     aCoincidence = theSketch.addFeature("SketchConstraintCoincidence")
39     aCoincidence.refattr("ConstraintEntityA").setAttr(aStartPoint1)
40     aCoincidence.refattr("ConstraintEntityB").setAttr(aStartPoint2)
41     
42     theSketch.execute()
43     return allFeatures
44     
45 def checkFillet(theObjects, theRadius):
46     # Verify the arc and lines are connected smoothly
47     aLine = []
48     anArc = []
49     aSize = theObjects.size()
50     for i in range (0, aSize):
51         feat = ModelAPI_Feature.feature(theObjects.object(i))
52         assert(feat is not None)
53         if (feat.getKind() == "SketchLine"):
54             aLine.append(feat)
55         elif (feat.getKind() == "SketchArc"):
56             anArc.append(feat)
57     assert(anArc)
58     assert(anArc[0] is not None)
59     
60     anArcPoints = []
61     aPoint = geomDataAPI_Point2D(anArc[0].attribute("ArcStartPoint"))
62     anArcPoints.append((aPoint.x(), aPoint.y()))
63     aPoint = geomDataAPI_Point2D(anArc[0].attribute("ArcEndPoint"))
64     anArcPoints.append((aPoint.x(), aPoint.y()))
65     aPoint = geomDataAPI_Point2D(anArc[0].attribute("ArcCenter"))
66     aCenterX = aPoint.x()
67     aCenterY = aPoint.y()
68     
69     for line in aLine:
70         aStartPoint = geomDataAPI_Point2D(line.attribute("StartPoint"))
71         aEndPoint = geomDataAPI_Point2D(line.attribute("EndPoint"))
72         
73         aLinePoints = []
74         aLinePoints.append((aStartPoint.x(), aStartPoint.y()))
75         aLinePoints.append((aEndPoint.x(), aEndPoint.y()))
76         
77         aLineDirX = aEndPoint.x() - aStartPoint.x()
78         aLineDirY = aEndPoint.y() - aStartPoint.y()
79         
80         for arcPt in anArcPoints:
81             for linePt in aLinePoints:
82                 if (math.hypot(linePt[0]-arcPt[0], linePt[1]-arcPt[1]) < 1.e-10):
83                     aDirX = linePt[0] - aCenterX
84                     aDirY = linePt[1] - aCenterY
85                     assert(math.fabs(math.hypot(aDirX, aDirY) - theRadius) < 1.e-7)
86                     aDot = aDirX * aLineDirX + aDirY * aLineDirY
87                     assert(math.fabs(aDot) < 1.e-7)
88                     break;
89
90
91 #=========================================================================
92 # Initialization of the test
93 #=========================================================================
94
95 __updated__ = "2015-09-18"
96
97 aSession = ModelAPI_Session.get()
98 aDocument = aSession.moduleDocument()
99 #=========================================================================
100 # Creation of a sketch
101 #=========================================================================
102 aSession.startOperation()
103 aSketchCommonFeature = aDocument.addFeature("Sketch")
104 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
105 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
106 origin.setValue(0, 0, 0)
107 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
108 dirx.setValue(1, 0, 0)
109 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
110 norm.setValue(0, 0, 1)
111 aSession.finishOperation()
112 #=========================================================================
113 # Initialize sketch
114 #=========================================================================
115 aSession.startOperation()
116 aFeaturesList = createSketch(aSketchFeature)
117 aSession.finishOperation()
118 #=========================================================================
119 # Global variables
120 #=========================================================================
121 FILLET_RADIUS1 = 3.
122 FILLET_RADIUS2 = 5.
123 #=========================================================================
124 # Create the Fillet
125 #=========================================================================
126 aSession.startOperation()
127 aFillet = aSketchFeature.addFeature("SketchConstraintFillet")
128 aRefAttrA = aFillet.refattr("ConstraintEntityA");
129 aRefAttrB = aFillet.refattr("ConstraintEntityB");
130 aResConstr = modelAPI_ResultConstruction(aFeaturesList[0].lastResult())
131 assert(aResConstr)
132 aRefAttrA.setObject(aResConstr)
133 aResConstr = modelAPI_ResultConstruction(aFeaturesList[1].lastResult())
134 assert(aResConstr)
135 aRefAttrB.setObject(aResConstr)
136 aRadius = aFillet.real("ConstraintValue")
137 aRadius.setValue(FILLET_RADIUS1)
138 aFillet.execute()
139 aResObjects = aFillet.reflist("ConstraintEntityC")
140 #=========================================================================
141 # Verify the objects of fillet are created
142 #=========================================================================
143 assert(aResObjects)
144 checkFillet(aResObjects, FILLET_RADIUS1)
145 aSession.finishOperation()
146 #=========================================================================
147 # End of test
148 #=========================================================================