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