Salome HOME
Merge branch 'Pre_2.8.0_development'
[modules/shaper.git] / src / SketchPlugin / Test / TestCreateArcByTangentEdge.py
1 ## Copyright (C) 2014-2017  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
18 ## email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 ##
20
21 """
22     TestCreateArc.py
23
24     static const std::string MY_SKETCH_ARC_ID("SketchArc");
25     static const std::string MY_CENTER_ID = "center_point";
26     static const std::string MY_START_ID = "start_point";
27     static const std::string MY_END_ID = "end_point";
28     data()->addAttribute(SketchPlugin_Arc::CENTER_ID(), GeomDataAPI_Point2D::typeId());
29     data()->addAttribute(SketchPlugin_Arc::START_ID(),  GeomDataAPI_Point2D::typeId());
30     data()->addAttribute(SketchPlugin_Arc::END_ID(),    GeomDataAPI_Point2D::typeId());
31 """
32
33 #=========================================================================
34 # Initialization of the test
35 #=========================================================================
36 from GeomDataAPI import *
37 from ModelAPI import *
38 from SketchAPI import SketchAPI_Sketch
39 import math
40 from salome.shaper import model
41
42 __updated__ = "2017-04-06"
43
44 TOLERANCE = 1.e-7
45
46 #=========================================================================
47 # Auxiliary functions
48 #=========================================================================
49
50 def verifyLastArc(theSketch, theCenter, theStart, theEnd):
51     """
52     subroutine to verify position of last arc in the sketch
53     """
54     aLastArc = model.lastSubFeature(theSketch, "SketchArc")
55     model.assertArc(aLastArc, theCenter, theStart, theEnd)
56
57 def verifyTangent(theFeature1, theFeature2):
58     anArcs = []
59     aLines = []
60     aFeatures = [theFeature1, theFeature2]
61     for feat in aFeatures:
62         if feat.getKind() == "SketchLine":
63             aLines.append(feat)
64         elif feat.getKind() == "SketchArc":
65             anArcs.append(feat)
66     if len(anArcs) == 2:
67         verifyArcArcTangent(anArcs[0], anArcs[1])
68     elif len(anArcs) == 1 and len(aLines) == 1:
69         verifyArcLineTangent(anArcs[0], aLines[0])
70
71 def verifyArcArcTangent(theArc1, theArc2):
72     aCenter1 = geomDataAPI_Point2D(theArc1.attribute("center_point"))
73     aStart1  = geomDataAPI_Point2D(theArc1.attribute("start_point"))
74     aRadius1 = model.distancePointPoint(aStart1, aCenter1)
75
76     aCenter2 = geomDataAPI_Point2D(theArc2.attribute("center_point"))
77     aStart2  = geomDataAPI_Point2D(theArc2.attribute("start_point"))
78     aRadius2 = model.distancePointPoint(aStart2, aCenter2)
79
80     aDistCC = model.distancePointPoint(aCenter1, aCenter2)
81     aRSum = aRadius1 + aRadius2
82     aRDiff = math.fabs(aRadius1 - aRadius2)
83     assert math.fabs(aRSum - aDistCC) < TOLERANCE or math.fabs(aRDiff - aDistCC) < TOLERANCE, "Arcs do not tangent"
84
85 def verifyArcLineTangent(theArc, theLine):
86     aCenter = geomDataAPI_Point2D(theArc.attribute("center_point"))
87     aStart  = geomDataAPI_Point2D(theArc.attribute("start_point"))
88     aRadius = model.distancePointPoint(aStart, aCenter)
89
90     aDistCL = model.distancePointLine(aCenter, theLine)
91     assert math.fabs(aDistCL - aRadius) < TOLERANCE, "Arc and line do not tangent"
92
93 def verifyPointOnLine(thePoint, theLine):
94     aDistance = model.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())