Salome HOME
a59e9299f399ac9d9720a39892e728c3f90e52ff
[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     aCenterPnt = geomDataAPI_Point2D(aLastArc.attribute("center_point"))
36     aStartPnt = geomDataAPI_Point2D(aLastArc.attribute("start_point"))
37     aEndPnt = geomDataAPI_Point2D(aLastArc.attribute("end_point"))
38     if len(theCenter):
39         verifyPointCoordinates(aCenterPnt, theCenter[0], theCenter[1])
40     if len(theStart):
41         verifyPointCoordinates(aStartPnt, theStart[0], theStart[1])
42     if len(theEnd):
43         verifyPointCoordinates(aEndPnt, theEnd[0], theEnd[1])
44     model.assertSketchArc(aLastArc)
45
46 def verifyPointCoordinates(thePoint, theX, theY):
47     assert thePoint.x() == theX and thePoint.y() == theY, "Wrong '{0}' point ({1}, {2}), expected ({3}, {4})".format(thePoint.id(), thePoint.x(), thePoint.y(), theX, theY)
48
49 def verifyTangent(theFeature1, theFeature2):
50     anArcs = []
51     aLines = []
52     aFeatures = [theFeature1, theFeature2]
53     for feat in aFeatures:
54         if feat.getKind() == "SketchLine":
55             aLines.append(feat)
56         elif feat.getKind() == "SketchArc":
57             anArcs.append(feat)
58     if len(anArcs) == 2:
59         verifyArcArcTangent(anArcs[0], anArcs[1])
60     elif len(anArcs) == 1 and len(aLines) == 1:
61         verifyArcLineTangent(anArcs[0], aLines[0])
62
63 def verifyArcArcTangent(theArc1, theArc2):
64     aCenter1 = geomDataAPI_Point2D(theArc1.attribute("center_point"))
65     aStart1  = geomDataAPI_Point2D(theArc1.attribute("start_point"))
66     aRadius1 = model.distancePointPoint(aStart1, aCenter1)
67
68     aCenter2 = geomDataAPI_Point2D(theArc2.attribute("center_point"))
69     aStart2  = geomDataAPI_Point2D(theArc2.attribute("start_point"))
70     aRadius2 = model.distancePointPoint(aStart2, aCenter2)
71
72     aDistCC = model.distancePointPoint(aCenter1, aCenter2)
73     aRSum = aRadius1 + aRadius2
74     aRDiff = math.fabs(aRadius1 - aRadius2)
75     assert math.fabs(aRSum - aDistCC) < TOLERANCE or math.fabs(aRDiff - aDistCC) < TOLERANCE, "Arcs do not tangent"
76
77 def verifyArcLineTangent(theArc, theLine):
78     aCenter = geomDataAPI_Point2D(theArc.attribute("center_point"))
79     aStart  = geomDataAPI_Point2D(theArc.attribute("start_point"))
80     aRadius = model.distancePointPoint(aStart, aCenter)
81
82     aDistCL = distancePointLine(aCenter, theLine)
83     assert math.fabs(aDistCL - aRadius) < TOLERANCE, "Arc and line do not tangent"
84
85 def distancePointLine(thePoint, theLine):
86     aLineStart = geomDataAPI_Point2D(theLine.attribute("StartPoint")).pnt().xy()
87     aLineEnd = geomDataAPI_Point2D(theLine.attribute("EndPoint")).pnt().xy()
88     aLineDir = aLineEnd.decreased(aLineStart)
89     aLineLen = aLineEnd.distance(aLineStart)
90     aPntDir = thePoint.pnt().xy().decreased(aLineStart)
91     return math.fabs(aPntDir.cross(aLineDir) / aLineLen)
92
93 def verifyPointOnLine(thePoint, theLine):
94     aDistance = distancePointLine(thePoint, theLine)
95     assert aDistance < TOLERANCE, "Point is not on Line, distance: {0}".format(aDistance)
96
97
98
99 aSession = ModelAPI_Session.get()
100 aDocument = aSession.moduleDocument()
101 #=========================================================================
102 # Creation of a sketch
103 #=========================================================================
104 aSession.startOperation()
105 aSketchCommonFeature = aDocument.addFeature("Sketch")
106 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
107 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
108 origin.setValue(0, 0, 0)
109 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
110 dirx.setValue(1, 0, 0)
111 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
112 norm.setValue(0, 0, 1)
113 aSession.finishOperation()
114 aSketch = SketchAPI_Sketch(aSketchFeature)
115
116 # auxiliary line
117 aLineStartPnt = [0., 0.]
118 aLineEndPnt = [50., 0.]
119 aSession.startOperation()
120 aSketchLine = aSketchFeature.addFeature("SketchLine")
121 aLineStart = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint"))
122 aLineEnd = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint"))
123 aLineStart.setValue(aLineStartPnt[0], aLineStartPnt[1])
124 aLineEnd.setValue(aLineEndPnt[0], aLineEndPnt[1])
125 aSession.finishOperation()
126
127 #=========================================================================
128 # Test 1. Create an arc, tangent to the line
129 #=========================================================================
130 anArcEndPnt = [80., 20.]
131 aSession.startOperation()
132 anArc = aSketchFeature.addFeature("SketchMacroArc")
133 assert (anArc.getKind() == "SketchMacroArc")
134 anArcTgPnt = anArc.refattr("tangent_point")
135 assert (not anArcTgPnt.isInitialized())
136 anArcEnd = geomDataAPI_Point2D(anArc.attribute("end_point_3"))
137 assert (not anArcEnd.isInitialized())
138 anArcType = anArc.string("arc_type")
139 assert (not anArcType.isInitialized())
140 # initialize attributes
141 anArcType.setValue("by_tangent_edge")
142 anArcTgPnt.setAttr(aLineEnd)
143 anArcEnd.setValue(anArcEndPnt[0], anArcEndPnt[1])
144 aSession.finishOperation()
145 verifyLastArc(aSketchFeature, [], aLineEndPnt, anArcEndPnt)
146 aLastArc = model.lastSubFeature(aSketchFeature, "SketchArc")
147 verifyTangent(aLastArc, aSketchLine)
148 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 1)
149 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 1)
150
151 #=========================================================================
152 # Test 2. Create an arc, tangent to the previous arc
153 #=========================================================================
154 aPrevArc = aLastArc
155 aPrevArcEnd = geomDataAPI_Point2D(aPrevArc.attribute("end_point"))
156 anArcEndPnt = [50., 100.]
157 aSession.startOperation()
158 anArc = aSketchFeature.addFeature("SketchMacroArc")
159 anArcTgPnt = anArc.refattr("tangent_point")
160 anArcEnd = geomDataAPI_Point2D(anArc.attribute("end_point_3"))
161 anArcType = anArc.string("arc_type")
162 # initialize attributes
163 anArcType.setValue("by_tangent_edge")
164 anArcTgPnt.setAttr(aPrevArcEnd)
165 anArcEnd.setValue(anArcEndPnt[0], anArcEndPnt[1])
166 aSession.finishOperation()
167 verifyLastArc(aSketchFeature, [], [aPrevArcEnd.x(), aPrevArcEnd.y()], anArcEndPnt)
168 aLastArc = model.lastSubFeature(aSketchFeature, "SketchArc")
169 verifyTangent(aLastArc, aPrevArc)
170 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 2)
171 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 2)
172
173 #=========================================================================
174 # Test 3. Create an arc, tangent to the previous arc with end point on the line
175 #=========================================================================
176 aPrevArc = aLastArc
177 aPrevArcEnd = geomDataAPI_Point2D(aPrevArc.attribute("end_point"))
178 aSession.startOperation()
179 anArc = aSketchFeature.addFeature("SketchMacroArc")
180 anArcTgPnt = anArc.refattr("tangent_point")
181 anArcEnd = geomDataAPI_Point2D(anArc.attribute("end_point_3"))
182 anArcEndRef = anArc.refattr("end_point_ref")
183 anArcType = anArc.string("arc_type")
184 # initialize attributes
185 anArcType.setValue("by_tangent_edge")
186 anArcTgPnt.setAttr(aPrevArcEnd)
187 anArcEndRef.setObject(aSketchLine.lastResult())
188 anArcEnd.setValue(aLineStartPnt[0], aLineStartPnt[1])
189 aSession.finishOperation()
190 verifyLastArc(aSketchFeature, [], [aPrevArcEnd.x(), aPrevArcEnd.y()], [])
191 aLastArc = model.lastSubFeature(aSketchFeature, "SketchArc")
192 verifyTangent(aLastArc, aPrevArc)
193 aLastArcEnd = geomDataAPI_Point2D(aLastArc.attribute("end_point"))
194 verifyPointOnLine(aLastArcEnd, aSketchLine)
195 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 4)
196 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 3)
197
198 #=========================================================================
199 # End of test
200 #=========================================================================
201
202 assert(model.checkPythonDump())