Salome HOME
Copyright update 2022
[modules/shaper.git] / src / SketchPlugin / Test / TestCreateArcByTangentEdge.py
1 # Copyright (C) 2014-2022  CEA/DEN, EDF R&D
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 """
21     TestCreateArc.py
22
23     static const std::string MY_SKETCH_ARC_ID("SketchArc");
24     static const std::string MY_CENTER_ID = "center_point";
25     static const std::string MY_START_ID = "start_point";
26     static const std::string MY_END_ID = "end_point";
27     data()->addAttribute(SketchPlugin_Arc::CENTER_ID(), GeomDataAPI_Point2D::typeId());
28     data()->addAttribute(SketchPlugin_Arc::START_ID(),  GeomDataAPI_Point2D::typeId());
29     data()->addAttribute(SketchPlugin_Arc::END_ID(),    GeomDataAPI_Point2D::typeId());
30 """
31
32 #=========================================================================
33 # Initialization of the test
34 #=========================================================================
35 from GeomDataAPI import *
36 from ModelAPI import *
37 from SketchAPI import SketchAPI_Sketch
38 import math
39 from salome.shaper import model
40
41 __updated__ = "2017-04-06"
42
43 TOLERANCE = 1.e-7
44
45 #=========================================================================
46 # Auxiliary functions
47 #=========================================================================
48
49 def verifyLastArc(theSketch, theCenter, theStart, theEnd):
50     """
51     subroutine to verify position of last arc in the sketch
52     """
53     aLastArc = model.lastSubFeature(theSketch, "SketchArc")
54     model.assertArc(aLastArc, theCenter, theStart, theEnd)
55
56 def verifyTangent(theFeature1, theFeature2):
57     anArcs = []
58     aLines = []
59     aFeatures = [theFeature1, theFeature2]
60     for feat in aFeatures:
61         if feat.getKind() == "SketchLine":
62             aLines.append(feat)
63         elif feat.getKind() == "SketchArc":
64             anArcs.append(feat)
65     if len(anArcs) == 2:
66         verifyArcArcTangent(anArcs[0], anArcs[1])
67     elif len(anArcs) == 1 and len(aLines) == 1:
68         verifyArcLineTangent(anArcs[0], aLines[0])
69
70 def verifyArcArcTangent(theArc1, theArc2):
71     aCenter1 = geomDataAPI_Point2D(theArc1.attribute("center_point"))
72     aStart1  = geomDataAPI_Point2D(theArc1.attribute("start_point"))
73     aRadius1 = model.distancePointPoint(aStart1, aCenter1)
74
75     aCenter2 = geomDataAPI_Point2D(theArc2.attribute("center_point"))
76     aStart2  = geomDataAPI_Point2D(theArc2.attribute("start_point"))
77     aRadius2 = model.distancePointPoint(aStart2, aCenter2)
78
79     aDistCC = model.distancePointPoint(aCenter1, aCenter2)
80     aRSum = aRadius1 + aRadius2
81     aRDiff = math.fabs(aRadius1 - aRadius2)
82     assert math.fabs(aRSum - aDistCC) < TOLERANCE or math.fabs(aRDiff - aDistCC) < TOLERANCE, "Arcs do not tangent"
83
84 def verifyArcLineTangent(theArc, theLine):
85     aCenter = geomDataAPI_Point2D(theArc.attribute("center_point"))
86     aStart  = geomDataAPI_Point2D(theArc.attribute("start_point"))
87     aRadius = model.distancePointPoint(aStart, aCenter)
88
89     aDistCL = model.distancePointLine(aCenter, theLine)
90     assert math.fabs(aDistCL - aRadius) < TOLERANCE, "Arc and line do not tangent"
91
92 def verifyPointOnLine(thePoint, theLine):
93     aDistance = model.distancePointLine(thePoint, theLine)
94     assert aDistance < TOLERANCE, "Point is not on Line, distance: {0}".format(aDistance)
95
96
97
98 aSession = ModelAPI_Session.get()
99 aDocument = aSession.moduleDocument()
100 #=========================================================================
101 # Creation of a sketch
102 #=========================================================================
103 aSession.startOperation()
104 aSketchCommonFeature = aDocument.addFeature("Sketch")
105 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
106 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
107 origin.setValue(0, 0, 0)
108 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
109 dirx.setValue(1, 0, 0)
110 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
111 norm.setValue(0, 0, 1)
112 aSession.finishOperation()
113 aSketch = SketchAPI_Sketch(aSketchFeature)
114
115 # auxiliary line
116 aLineStartPnt = [0., 0.]
117 aLineEndPnt = [50., 0.]
118 aSession.startOperation()
119 aSketchLine = aSketchFeature.addFeature("SketchLine")
120 aLineStart = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint"))
121 aLineEnd = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint"))
122 aLineStart.setValue(aLineStartPnt[0], aLineStartPnt[1])
123 aLineEnd.setValue(aLineEndPnt[0], aLineEndPnt[1])
124 aSession.finishOperation()
125
126 #=========================================================================
127 # Test 1. Create an arc, tangent to the line
128 #=========================================================================
129 anArcEndPnt = [80., 20.]
130 aSession.startOperation()
131 anArc = aSketchFeature.addFeature("SketchMacroArc")
132 assert (anArc.getKind() == "SketchMacroArc")
133 anArcTgPnt = anArc.refattr("tangent_point")
134 assert (not anArcTgPnt.isInitialized())
135 anArcEnd = geomDataAPI_Point2D(anArc.attribute("end_point_3"))
136 assert (not anArcEnd.isInitialized())
137 anArcType = anArc.string("arc_type")
138 assert (not anArcType.isInitialized())
139 # initialize attributes
140 anArcType.setValue("by_tangent_edge")
141 anArcTgPnt.setAttr(aLineEnd)
142 anArcEnd.setValue(anArcEndPnt[0], anArcEndPnt[1])
143 aSession.finishOperation()
144 verifyLastArc(aSketchFeature, [], aLineEndPnt, anArcEndPnt)
145 aLastArc = model.lastSubFeature(aSketchFeature, "SketchArc")
146 verifyTangent(aLastArc, aSketchLine)
147 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 1)
148 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 1)
149
150 #=========================================================================
151 # Test 2. Create an arc, tangent to the previous arc
152 #=========================================================================
153 aPrevArc = aLastArc
154 aPrevArcEnd = geomDataAPI_Point2D(aPrevArc.attribute("end_point"))
155 anArcEndPnt = [50., 100.]
156 aSession.startOperation()
157 anArc = aSketchFeature.addFeature("SketchMacroArc")
158 anArcTgPnt = anArc.refattr("tangent_point")
159 anArcEnd = geomDataAPI_Point2D(anArc.attribute("end_point_3"))
160 anArcType = anArc.string("arc_type")
161 # initialize attributes
162 anArcType.setValue("by_tangent_edge")
163 anArcTgPnt.setAttr(aPrevArcEnd)
164 anArcEnd.setValue(anArcEndPnt[0], anArcEndPnt[1])
165 aSession.finishOperation()
166 verifyLastArc(aSketchFeature, [], [aPrevArcEnd.x(), aPrevArcEnd.y()], anArcEndPnt)
167 aLastArc = model.lastSubFeature(aSketchFeature, "SketchArc")
168 verifyTangent(aLastArc, aPrevArc)
169 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 2)
170 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 2)
171
172 #=========================================================================
173 # Test 3. Create an arc, tangent to the previous arc with end point on the line
174 #=========================================================================
175 aPrevArc = aLastArc
176 aPrevArcEnd = geomDataAPI_Point2D(aPrevArc.attribute("end_point"))
177 aSession.startOperation()
178 anArc = aSketchFeature.addFeature("SketchMacroArc")
179 anArcTgPnt = anArc.refattr("tangent_point")
180 anArcEnd = geomDataAPI_Point2D(anArc.attribute("end_point_3"))
181 anArcEndRef = anArc.refattr("end_point_ref")
182 anArcType = anArc.string("arc_type")
183 # initialize attributes
184 anArcType.setValue("by_tangent_edge")
185 anArcTgPnt.setAttr(aPrevArcEnd)
186 anArcEndRef.setObject(aSketchLine.lastResult())
187 anArcEnd.setValue(aLineStartPnt[0], aLineStartPnt[1])
188 aSession.finishOperation()
189 verifyLastArc(aSketchFeature, [], [aPrevArcEnd.x(), aPrevArcEnd.y()], [])
190 aLastArc = model.lastSubFeature(aSketchFeature, "SketchArc")
191 verifyTangent(aLastArc, aPrevArc)
192 aLastArcEnd = geomDataAPI_Point2D(aLastArc.attribute("end_point"))
193 verifyPointOnLine(aLastArcEnd, aSketchLine)
194 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 4)
195 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 3)
196
197 #=========================================================================
198 # End of test
199 #=========================================================================
200
201 assert(model.checkPythonDump())