Salome HOME
82df63c6f0c06f17b14da497585eca52bd18e66b
[modules/shaper.git] / src / SketchPlugin / Test / TestProjection.py
1 # Copyright (C) 2014-2021  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     TestProjection.py
22     Unit test of SketchPlugin_Projection class
23
24 """
25 from GeomDataAPI import *
26 from ModelAPI import *
27 import math
28 from salome.shaper import model
29
30 #=========================================================================
31 # Initialization of the test
32 #=========================================================================
33
34 __updated__ = "2016-05-16"
35
36
37 #=========================================================================
38 # Start of test
39 #=========================================================================
40 aSession = ModelAPI_Session.get()
41 aDocument = aSession.moduleDocument()
42 #=========================================================================
43 # Creation of a sketch
44 #=========================================================================
45 aSession.startOperation()
46 aSketchFeature = featureToCompositeFeature(aDocument.addFeature("Sketch"))
47 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
48 origin.setValue(0, 0, 0)
49 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
50 dirx.setValue(1, 0, 0)
51 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
52 norm.setValue(0, 0, 1)
53 aSession.finishOperation()
54 #=========================================================================
55 # Create a line, circle and arc
56 #=========================================================================
57 aSession.startOperation()
58 aLine = aSketchFeature.addFeature("SketchLine")
59 aLineStart = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
60 aLineEnd = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
61 aLineStart.setValue(10., 10.)
62 aLineEnd.setValue(40., 30.)
63
64 aCircle = aSketchFeature.addFeature("SketchCircle")
65 aCircleCenter = geomDataAPI_Point2D(aCircle.attribute("circle_center"))
66 aCircleRadius = aCircle.real("circle_radius")
67 aCircleCenter.setValue(-25., -25)
68 aCircleRadius.setValue(25.)
69
70 anArc = aSketchFeature.addFeature("SketchArc")
71 anArcCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
72 anArcStart = geomDataAPI_Point2D(anArc.attribute("start_point"))
73 anArcEnd = geomDataAPI_Point2D(anArc.attribute("end_point"))
74 anArcCenter.setValue(10., 10.)
75 anArcStart.setValue(50., 0.)
76 anArcEnd.setValue(0., 50.)
77 aSession.finishOperation()
78 assert (model.dof(aSketchFeature) == 12)
79 #=========================================================================
80 # Create another sketch
81 #=========================================================================
82 aSession.startOperation()
83 aSketchFeature = featureToCompositeFeature(aDocument.addFeature("Sketch"))
84 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
85 origin.setValue(0, 0, 10)
86 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
87 dirx.setValue(1, 0, 0)
88 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
89 norm.setValue(0, 0, 1)
90 aSession.finishOperation()
91 #=========================================================================
92 # Project all features onto the new sketch
93 #=========================================================================
94 aSession.startOperation()
95 aLineProjector = aSketchFeature.addFeature("SketchProjection")
96 aLineProjector.selection("ExternalFeature").selectSubShape("EDGE", "Sketch_1/SketchLine_1")
97 aLineProjector.boolean("IncludeToResult").setValue(False)
98 aLineProjector.execute()
99
100 aCircleProjector = aSketchFeature.addFeature("SketchProjection")
101 aCircleProjector.selection("ExternalFeature").selectSubShape("EDGE", "Sketch_1/SketchCircle_1_2")
102 aCircleProjector.boolean("IncludeToResult").setValue(False)
103 aCircleProjector.execute()
104
105 anArcProjector = aSketchFeature.addFeature("SketchProjection")
106 anArcProjector.selection("ExternalFeature").selectSubShape("EDGE", "Sketch_1/SketchArc_1_2")
107 anArcProjector.boolean("IncludeToResult").setValue(False)
108 anArcProjector.execute()
109 aSession.finishOperation()
110 assert (model.dof(aSketchFeature) == 0)
111 #=========================================================================
112 # Check projection coordinates
113 #=========================================================================
114 aProjLine = ModelAPI_Feature.feature(aLineProjector.refattr("ProjectedFeature").object())
115 assert(aProjLine)
116 aProjLineStart = geomDataAPI_Point2D(aProjLine.attribute("StartPoint"))
117 aProjLineEnd = geomDataAPI_Point2D(aProjLine.attribute("EndPoint"))
118 assert(math.fabs(aProjLineStart.x() - aLineStart.x()) < 1.e-10)
119 assert(math.fabs(aProjLineStart.y() - aLineStart.y()) < 1.e-10)
120 assert(math.fabs(aProjLineEnd.x() - aLineEnd.x()) < 1.e-10)
121 assert(math.fabs(aProjLineEnd.y() - aLineEnd.y()) < 1.e-10)
122
123 aProjCircle = ModelAPI_Feature.feature(aCircleProjector.refattr("ProjectedFeature").object())
124 assert(aProjCircle)
125 aProjCircleCenter = geomDataAPI_Point2D(aProjCircle.attribute("circle_center"))
126 aProjCircleRadius = aProjCircle.real("circle_radius")
127 assert(math.fabs(aProjCircleCenter.x() - aCircleCenter.x()) < 1.e-10)
128 assert(math.fabs(aProjCircleCenter.y() - aCircleCenter.y()) < 1.e-10)
129 assert(math.fabs(aProjCircleRadius.value() - aCircleRadius.value()) < 1.e-10)
130
131 aProjArc = ModelAPI_Feature.feature(anArcProjector.refattr("ProjectedFeature").object())
132 aProjArcCenter = geomDataAPI_Point2D(aProjArc.attribute("center_point"))
133 aProjArcStart = geomDataAPI_Point2D(aProjArc.attribute("start_point"))
134 aProjArcEnd = geomDataAPI_Point2D(aProjArc.attribute("end_point"))
135 assert(math.fabs(aProjArcCenter.x() - anArcCenter.x()) < 1.e-10)
136 assert(math.fabs(aProjArcCenter.y() - anArcCenter.y()) < 1.e-10)
137 assert(math.fabs(aProjArcStart.x() - anArcStart.x()) < 1.e-10)
138 assert(math.fabs(aProjArcStart.y() - anArcStart.y()) < 1.e-10)
139 assert(math.fabs(aProjArcEnd.x() - anArcEnd.x()) < 1.e-10)
140 assert(math.fabs(aProjArcEnd.y() - anArcEnd.y()) < 1.e-10)
141 #=========================================================================
142 # Move original feature and check the projection is agreed
143 #=========================================================================
144 aSession.startOperation()
145 aLineStart.setValue(20., 0.)
146 aSession.finishOperation()
147 assert(math.fabs(aProjLineStart.x() - aLineStart.x()) < 1.e-10)
148 assert(math.fabs(aProjLineStart.y() - aLineStart.y()) < 1.e-10)
149 assert(math.fabs(aProjLineEnd.x() - aLineEnd.x()) < 1.e-10)
150 assert(math.fabs(aProjLineEnd.y() - aLineEnd.y()) < 1.e-10)
151 assert (model.dof(aSketchFeature) == 0)
152 #=========================================================================
153 # End of test
154 #=========================================================================
155
156 assert(model.checkPythonDump())