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