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