Salome HOME
updated copyright message
[modules/shaper.git] / src / SketchPlugin / Test / TestProjectionWithoutReference.py
1 # Copyright (C) 2020-2023  CEA, EDF
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     Test projection without the reference to the source geometry.
22 """
23
24 import unittest
25
26 from salome.shaper import model
27 from GeomAPI import *
28 from ModelAPI import *
29
30 __updated__ = "2020-07-07"
31
32 CURVES = []
33 PLANE = None
34
35 class TestProjectionWithoutRef(unittest.TestCase):
36   def setUp(self):
37     model.begin()
38     self.myDocument = model.moduleDocument()
39     self.mySketch = model.addSketch(partSet, model.selection("FACE", PLANE.name()))
40     self.myDOF = 0
41     self.myNbPoints = 1
42     self.myNbLines = 1
43     self.myNbCircles = 0
44     self.myNbArcs = 0
45     self.myNbEllipses = 2
46     self.myNbEllipticArcs = 2
47     self.myNbSplines = 1
48     self.myNbPeriodicSplines = 1
49     self.myNbProjections = 0
50     self.myNbFixedConstraints = 0
51     self.myNbEdgesInSketch = 0
52
53   def tearDown(self):
54     self.checkDOF()
55     model.end()
56     model.testNbSubFeatures(self.mySketch, "SketchPoint", self.myNbPoints)
57     model.testNbSubFeatures(self.mySketch, "SketchLine", self.myNbLines)
58     model.testNbSubFeatures(self.mySketch, "SketchCircle", self.myNbCircles)
59     model.testNbSubFeatures(self.mySketch, "SketchArc", self.myNbArcs)
60     model.testNbSubFeatures(self.mySketch, "SketchEllipse", self.myNbEllipses)
61     model.testNbSubFeatures(self.mySketch, "SketchEllipticArc", self.myNbEllipticArcs)
62     model.testNbSubFeatures(self.mySketch, "SketchBSpline", self.myNbSplines)
63     model.testNbSubFeatures(self.mySketch, "SketchBSplinePeriodic", self.myNbPeriodicSplines)
64     model.testNbSubFeatures(self.mySketch, "SketchProjection", self.myNbProjections)
65     model.testNbSubFeatures(self.mySketch, "SketchConstraintRigid", self.myNbFixedConstraints)
66     nbEdges = 0
67     exp = GeomAPI_ShapeExplorer(self.mySketch.defaultResult().shape(), GeomAPI_Shape.EDGE)
68     while exp.more(): nbEdges += 1; exp.next()
69     self.assertEqual(self.myNbEdgesInSketch, nbEdges)
70
71   def checkDOF(self):
72     self.assertEqual(model.dof(self.mySketch), self.myDOF)
73
74
75   def test_projection_withref_includeintoresult(self):
76     """ Test 1. Projection with the reference to the original shapes. Projected curves are composed into the sketch result.
77     """
78     for c in CURVES:
79       self.mySketch.addProjection(c, keepResult = True)
80     self.myNbProjections = len(CURVES)
81     self.myNbEdgesInSketch = len(CURVES) - 1
82
83   def test_projection_withref_notincludeintoresult(self):
84     """ Test 2. Projection with the reference to the original shapes. Projected curves are NOT included into the sketch result.
85     """
86     for c in CURVES:
87       self.mySketch.addProjection(c, keepResult = False)
88     self.myNbProjections = len(CURVES)
89
90   def test_projection_withoutref_noconstraints(self):
91     """ Test 3. Projection without the reference to the original shapes. No additional constraints applied.
92     """
93     for c in CURVES:
94       self.mySketch.addProjection(c, keepRefToOriginal = False)
95     model.do()
96     self.myNbEdgesInSketch = len(CURVES) - 1
97     self.myDOF += 2 + 4 + 5 + 7 + 5 + 7 + 6 * 2 + 6 * 2
98
99   def test_projection_withoutref_fixed(self):
100     """ Test 4. Projection without the reference to the original shapes. Additionally, Fixed constraints applied.
101     """
102     model.end()
103     # use the low-level API to access the necessary attributes
104     session = ModelAPI_Session.get()
105     for c in CURVES:
106       session.startOperation()
107       proj = featureToCompositeFeature(self.mySketch.feature()).addFeature("SketchProjection")
108       proj.boolean("IncludeToResult").setValue(False)
109       proj.string("keep_reference").setValue("False")
110       proj.boolean("make_fixed").setValue(True)
111       c.fillAttribute(proj.selection("ExternalFeature"))
112       session.finishOperation()
113     self.myNbEdgesInSketch = len(CURVES) - 1
114     self.myNbFixedConstraints = len(CURVES)
115     model.begin()
116
117
118 if __name__ == "__main__":
119     model.begin()
120     partSet = model.moduleDocument()
121     Sketch_1 = model.addSketch(partSet, model.defaultPlane("XOY"))
122     SketchPoint_1 = Sketch_1.addPoint(35, -40)
123     CURVES.append(model.selection("VERTEX", Sketch_1.name() + "/" + SketchPoint_1.name()))
124     SketchLine_1 = Sketch_1.addLine(20, -15, 40, 15)
125     CURVES.append(model.selection("EDGE", Sketch_1.name() + "/" + SketchLine_1.name()))
126     SketchCircle_1 = Sketch_1.addCircle(65, -30, 20)
127     CURVES.append(model.selection("EDGE", Sketch_1.name() + "/" + SketchCircle_1.defaultResult().data().name()))
128     SketchArc_1 = Sketch_1.addArc(60, 15, 80, 0, 50, 33, False)
129     CURVES.append(model.selection("EDGE", Sketch_1.name() + "/" + SketchArc_1.defaultResult().data().name()))
130     SketchEllipse_1 = Sketch_1.addEllipse(25, 30, 40, 30, 10)
131     CURVES.append(model.selection("EDGE", Sketch_1.name() + "/" + SketchEllipse_1.defaultResult().data().name()))
132     SketchEllipticArc_1 = Sketch_1.addEllipticArc(40, 70, 55, 70, 45, 50, 25, 56, False)
133     CURVES.append(model.selection("EDGE", Sketch_1.name() + "/" + SketchEllipticArc_1.defaultResult().data().name()))
134     SketchBSpline_1_poles = [(95, -50), (130, -10), (100, 10), (125, 45), (90, 70), (55, 45)]
135     SketchBSpline_1 = Sketch_1.addSpline(poles = SketchBSpline_1_poles)
136     CURVES.append(model.selection("EDGE", Sketch_1.name() + "/" + SketchBSpline_1.name()))
137     SketchBSplinePeriodic_1_poles = [(95, 80), (135, 90), (145, 55), (130, 30), (125, 70), (105, 60)]
138     SketchBSplinePeriodic_1 = Sketch_1.addSpline(poles = SketchBSplinePeriodic_1_poles, periodic = True)
139     CURVES.append(model.selection("EDGE", Sketch_1.name() + "/" + SketchBSplinePeriodic_1.name()))
140     model.do()
141     PLANE = model.addPlane(partSet, model.selection("FACE", "XOY"), model.selection("EDGE", "OY"), 45)
142     model.end()
143
144     test_program = unittest.main(exit=False)
145     assert test_program.result.wasSuccessful(), "Test failed"
146     assert model.checkPythonDump()