Salome HOME
13fd7026fbc9253cc3beb17b7d40c1fc492e9b40
[modules/shaper.git] / src / SketchPlugin / Test / TestCreateArcByTransversalLine.py
1 # Copyright (C) 2014-2023  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 #=========================================================================
22 # Initialization of the test
23 #=========================================================================
24 from GeomDataAPI import *
25 from ModelAPI import *
26 from SketchAPI import SketchAPI_Sketch
27 import math
28 from salome.shaper import model
29
30 __updated__ = "2019-08-16"
31
32 TOLERANCE = 1.e-7
33
34 #=========================================================================
35 # Auxiliary functions
36 #=========================================================================
37
38 def verifyLastArc(theSketch, theCenter, theStart, theEnd):
39     """
40     subroutine to verify position of last arc in the sketch
41     """
42     aLastArc = model.lastSubFeature(theSketch, "SketchArc")
43     model.assertArc(aLastArc, theCenter, theStart, theEnd)
44
45 def verifyArcLineTransversal(theArc, theLine):
46     aCenter = geomDataAPI_Point2D(theArc.attribute("center_point"))
47     aDistCL = model.distancePointLine(aCenter, theLine)
48     assert aDistCL < TOLERANCE, "Arc and line are not orthogonal"
49
50 def verifyPointOnArc(thePoint, theArc):
51     aCenter = geomDataAPI_Point2D(theArc.attribute("center_point"))
52     aStart = geomDataAPI_Point2D(theArc.attribute("start_point"))
53     aRadius = model.distancePointPoint(aStart, aCenter)
54
55     aDistPP = model.distancePointPoint(aCenter, thePoint)
56     assert math.fabs(aRadius - aDistPP) < TOLERANCE, "Point is not on Circle, distance: {0}".format(aDistPP)
57
58
59
60 aSession = ModelAPI_Session.get()
61 aDocument = aSession.moduleDocument()
62 #=========================================================================
63 # Creation of a sketch
64 #=========================================================================
65 aSession.startOperation()
66 aSketchCommonFeature = aDocument.addFeature("Sketch")
67 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
68 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
69 origin.setValue(0, 0, 0)
70 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
71 dirx.setValue(1, 0, 0)
72 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
73 norm.setValue(0, 0, 1)
74 aSession.finishOperation()
75 aSketch = SketchAPI_Sketch(aSketchFeature)
76
77 # auxiliary line
78 aLineStartPnt = [0., 0.]
79 aLineEndPnt = [50., 0.]
80 aSession.startOperation()
81 aSketchLine = aSketchFeature.addFeature("SketchLine")
82 aLineStart = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint"))
83 aLineEnd = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint"))
84 aLineStart.setValue(aLineStartPnt[0], aLineStartPnt[1])
85 aLineEnd.setValue(aLineEndPnt[0], aLineEndPnt[1])
86 aSession.finishOperation()
87
88 #=========================================================================
89 # Test 1. Create an arc, orthogonal to the line
90 #=========================================================================
91 anArcEndPnt = [80., 20.]
92 aSession.startOperation()
93 anArc = aSketchFeature.addFeature("SketchMacroArc")
94 assert (anArc.getKind() == "SketchMacroArc")
95 anArcTgPnt = anArc.refattr("transversal_point")
96 assert (not anArcTgPnt.isInitialized())
97 anArcEnd = geomDataAPI_Point2D(anArc.attribute("end_point_4"))
98 assert (not anArcEnd.isInitialized())
99 anArcType = anArc.string("arc_type")
100 assert (not anArcType.isInitialized())
101 # initialize attributes
102 anArcType.setValue("by_transversal_line")
103 anArcTgPnt.setAttr(aLineEnd)
104 anArcEnd.setValue(anArcEndPnt[0], anArcEndPnt[1])
105 aSession.finishOperation()
106 verifyLastArc(aSketchFeature, [], aLineEndPnt, anArcEndPnt)
107 aLastArc = model.lastSubFeature(aSketchFeature, "SketchArc")
108 verifyArcLineTransversal(aLastArc, aSketchLine)
109 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 1)
110 model.testNbSubFeatures(aSketch, "SketchConstraintPerpendicular", 1)
111
112 #=========================================================================
113 # Test 2. Create an arc, orthogonal to the previous arc (expect an error)
114 #=========================================================================
115 aPrevArc = aLastArc
116 aPrevArcEnd = geomDataAPI_Point2D(aPrevArc.attribute("end_point"))
117 anArcEndPnt = [50., 100.]
118 aSession.startOperation()
119 anArc = aSketchFeature.addFeature("SketchMacroArc")
120 anArcTgPnt = anArc.refattr("transversal_point")
121 anArcEnd = geomDataAPI_Point2D(anArc.attribute("end_point_4"))
122 anArcType = anArc.string("arc_type")
123 # initialize attributes
124 anArcType.setValue("by_transversal_line")
125 anArcTgPnt.setAttr(aPrevArcEnd)
126 anArcEnd.setValue(anArcEndPnt[0], anArcEndPnt[1])
127 aSession.finishOperation()
128 assert(anArc.error() != "")
129 # remove failed feature
130 aSession.startOperation()
131 aDocument.removeFeature(anArc)
132 aSession.finishOperation()
133
134 #=========================================================================
135 # Test 3. Create an arc, orthogonal to the line with end point on the arc
136 #=========================================================================
137 aPrevArc = model.lastSubFeature(aSketchFeature, "SketchArc")
138 aPrevArcEnd = geomDataAPI_Point2D(aPrevArc.attribute("end_point"))
139 aSession.startOperation()
140 anArc = aSketchFeature.addFeature("SketchMacroArc")
141 anArcTgPnt = anArc.refattr("transversal_point")
142 anArcEnd = geomDataAPI_Point2D(anArc.attribute("end_point_4"))
143 anArcEndRef = anArc.refattr("end_point_ref")
144 anArcType = anArc.string("arc_type")
145 # initialize attributes
146 anArcType.setValue("by_transversal_line")
147 anArcTgPnt.setAttr(aLineStart)
148 anArcEndRef.setObject(aPrevArc.lastResult())
149 anArcEnd.setValue(anArcEndPnt[0], anArcEndPnt[1])
150 aSession.finishOperation()
151 verifyLastArc(aSketchFeature, [], [aLineStart.x(), aLineStart.y()], [])
152 aLastArc = model.lastSubFeature(aSketchFeature, "SketchArc")
153 verifyArcLineTransversal(aLastArc, aSketchLine)
154 aLastArcEnd = geomDataAPI_Point2D(aLastArc.attribute("end_point"))
155 verifyPointOnArc(aLastArcEnd, aPrevArc)
156 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 3)
157 model.testNbSubFeatures(aSketch, "SketchConstraintPerpendicular", 2)
158
159 #=========================================================================
160 # End of test
161 #=========================================================================
162
163 assert(model.checkPythonDump())