Salome HOME
Simplification and refactoring of unit tests for SketchPlugin
[modules/shaper.git] / src / SketchPlugin / Test / TestCreateArcByTangentEdge.py
1 """
2     TestCreateArc.py
3
4     static const std::string MY_SKETCH_ARC_ID("SketchArc");
5     static const std::string MY_CENTER_ID = "center_point";
6     static const std::string MY_START_ID = "start_point";
7     static const std::string MY_END_ID = "end_point";
8     data()->addAttribute(SketchPlugin_Arc::CENTER_ID(), GeomDataAPI_Point2D::typeId());
9     data()->addAttribute(SketchPlugin_Arc::START_ID(),  GeomDataAPI_Point2D::typeId());
10     data()->addAttribute(SketchPlugin_Arc::END_ID(),    GeomDataAPI_Point2D::typeId());
11 """
12
13 #=========================================================================
14 # Initialization of the test
15 #=========================================================================
16 from GeomDataAPI import *
17 from ModelAPI import *
18 from SketchAPI import SketchAPI_Sketch
19 import math
20 from salome.shaper import model
21
22 __updated__ = "2017-04-06"
23
24 TOLERANCE = 1.e-7
25
26 #=========================================================================
27 # Auxiliary functions
28 #=========================================================================
29
30 def verifyLastArc(theSketch, theCenter, theStart, theEnd):
31     """
32     subroutine to verify position of last arc in the sketch
33     """
34     aLastArc = model.lastSubFeature(theSketch, "SketchArc")
35     model.assertArc(aLastArc, theCenter, theStart, theEnd)
36
37 def verifyTangent(theFeature1, theFeature2):
38     anArcs = []
39     aLines = []
40     aFeatures = [theFeature1, theFeature2]
41     for feat in aFeatures:
42         if feat.getKind() == "SketchLine":
43             aLines.append(feat)
44         elif feat.getKind() == "SketchArc":
45             anArcs.append(feat)
46     if len(anArcs) == 2:
47         verifyArcArcTangent(anArcs[0], anArcs[1])
48     elif len(anArcs) == 1 and len(aLines) == 1:
49         verifyArcLineTangent(anArcs[0], aLines[0])
50
51 def verifyArcArcTangent(theArc1, theArc2):
52     aCenter1 = geomDataAPI_Point2D(theArc1.attribute("center_point"))
53     aStart1  = geomDataAPI_Point2D(theArc1.attribute("start_point"))
54     aRadius1 = model.distancePointPoint(aStart1, aCenter1)
55
56     aCenter2 = geomDataAPI_Point2D(theArc2.attribute("center_point"))
57     aStart2  = geomDataAPI_Point2D(theArc2.attribute("start_point"))
58     aRadius2 = model.distancePointPoint(aStart2, aCenter2)
59
60     aDistCC = model.distancePointPoint(aCenter1, aCenter2)
61     aRSum = aRadius1 + aRadius2
62     aRDiff = math.fabs(aRadius1 - aRadius2)
63     assert math.fabs(aRSum - aDistCC) < TOLERANCE or math.fabs(aRDiff - aDistCC) < TOLERANCE, "Arcs do not tangent"
64
65 def verifyArcLineTangent(theArc, theLine):
66     aCenter = geomDataAPI_Point2D(theArc.attribute("center_point"))
67     aStart  = geomDataAPI_Point2D(theArc.attribute("start_point"))
68     aRadius = model.distancePointPoint(aStart, aCenter)
69
70     aDistCL = model.distancePointLine(aCenter, theLine)
71     assert math.fabs(aDistCL - aRadius) < TOLERANCE, "Arc and line do not tangent"
72
73 def verifyPointOnLine(thePoint, theLine):
74     aDistance = model.distancePointLine(thePoint, theLine)
75     assert aDistance < TOLERANCE, "Point is not on Line, distance: {0}".format(aDistance)
76
77
78
79 aSession = ModelAPI_Session.get()
80 aDocument = aSession.moduleDocument()
81 #=========================================================================
82 # Creation of a sketch
83 #=========================================================================
84 aSession.startOperation()
85 aSketchCommonFeature = aDocument.addFeature("Sketch")
86 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
87 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
88 origin.setValue(0, 0, 0)
89 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
90 dirx.setValue(1, 0, 0)
91 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
92 norm.setValue(0, 0, 1)
93 aSession.finishOperation()
94 aSketch = SketchAPI_Sketch(aSketchFeature)
95
96 # auxiliary line
97 aLineStartPnt = [0., 0.]
98 aLineEndPnt = [50., 0.]
99 aSession.startOperation()
100 aSketchLine = aSketchFeature.addFeature("SketchLine")
101 aLineStart = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint"))
102 aLineEnd = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint"))
103 aLineStart.setValue(aLineStartPnt[0], aLineStartPnt[1])
104 aLineEnd.setValue(aLineEndPnt[0], aLineEndPnt[1])
105 aSession.finishOperation()
106
107 #=========================================================================
108 # Test 1. Create an arc, tangent to the line
109 #=========================================================================
110 anArcEndPnt = [80., 20.]
111 aSession.startOperation()
112 anArc = aSketchFeature.addFeature("SketchMacroArc")
113 assert (anArc.getKind() == "SketchMacroArc")
114 anArcTgPnt = anArc.refattr("tangent_point")
115 assert (not anArcTgPnt.isInitialized())
116 anArcEnd = geomDataAPI_Point2D(anArc.attribute("end_point_3"))
117 assert (not anArcEnd.isInitialized())
118 anArcType = anArc.string("arc_type")
119 assert (not anArcType.isInitialized())
120 # initialize attributes
121 anArcType.setValue("by_tangent_edge")
122 anArcTgPnt.setAttr(aLineEnd)
123 anArcEnd.setValue(anArcEndPnt[0], anArcEndPnt[1])
124 aSession.finishOperation()
125 verifyLastArc(aSketchFeature, [], aLineEndPnt, anArcEndPnt)
126 aLastArc = model.lastSubFeature(aSketchFeature, "SketchArc")
127 verifyTangent(aLastArc, aSketchLine)
128 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 1)
129 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 1)
130
131 #=========================================================================
132 # Test 2. Create an arc, tangent to the previous arc
133 #=========================================================================
134 aPrevArc = aLastArc
135 aPrevArcEnd = geomDataAPI_Point2D(aPrevArc.attribute("end_point"))
136 anArcEndPnt = [50., 100.]
137 aSession.startOperation()
138 anArc = aSketchFeature.addFeature("SketchMacroArc")
139 anArcTgPnt = anArc.refattr("tangent_point")
140 anArcEnd = geomDataAPI_Point2D(anArc.attribute("end_point_3"))
141 anArcType = anArc.string("arc_type")
142 # initialize attributes
143 anArcType.setValue("by_tangent_edge")
144 anArcTgPnt.setAttr(aPrevArcEnd)
145 anArcEnd.setValue(anArcEndPnt[0], anArcEndPnt[1])
146 aSession.finishOperation()
147 verifyLastArc(aSketchFeature, [], [aPrevArcEnd.x(), aPrevArcEnd.y()], anArcEndPnt)
148 aLastArc = model.lastSubFeature(aSketchFeature, "SketchArc")
149 verifyTangent(aLastArc, aPrevArc)
150 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 2)
151 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 2)
152
153 #=========================================================================
154 # Test 3. Create an arc, tangent to the previous arc with end point on the line
155 #=========================================================================
156 aPrevArc = aLastArc
157 aPrevArcEnd = geomDataAPI_Point2D(aPrevArc.attribute("end_point"))
158 aSession.startOperation()
159 anArc = aSketchFeature.addFeature("SketchMacroArc")
160 anArcTgPnt = anArc.refattr("tangent_point")
161 anArcEnd = geomDataAPI_Point2D(anArc.attribute("end_point_3"))
162 anArcEndRef = anArc.refattr("end_point_ref")
163 anArcType = anArc.string("arc_type")
164 # initialize attributes
165 anArcType.setValue("by_tangent_edge")
166 anArcTgPnt.setAttr(aPrevArcEnd)
167 anArcEndRef.setObject(aSketchLine.lastResult())
168 anArcEnd.setValue(aLineStartPnt[0], aLineStartPnt[1])
169 aSession.finishOperation()
170 verifyLastArc(aSketchFeature, [], [aPrevArcEnd.x(), aPrevArcEnd.y()], [])
171 aLastArc = model.lastSubFeature(aSketchFeature, "SketchArc")
172 verifyTangent(aLastArc, aPrevArc)
173 aLastArcEnd = geomDataAPI_Point2D(aLastArc.attribute("end_point"))
174 verifyPointOnLine(aLastArcEnd, aSketchLine)
175 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 4)
176 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 3)
177
178 #=========================================================================
179 # End of test
180 #=========================================================================
181
182 assert(model.checkPythonDump())