Salome HOME
ba4f11863fab13d644dfdc6e11920386aa9d1bbf
[modules/shaper.git] / src / SketchPlugin / Test / TestCreateMacroBSpline.py
1 # Copyright (C) 2020-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     Test creation of B-spline curve by references to another features
22 """
23
24 import unittest
25 from salome.shaper import model
26
27 from GeomAPI import *
28 from GeomDataAPI import *
29 from ModelAPI import *
30 from SketchAPI import *
31
32 __updated__ = "2020-01-31"
33
34 class TestMacroBSpline(unittest.TestCase):
35   def setUp(self):
36     model.begin()
37     self.myDocument = model.moduleDocument()
38     self.mySketch = model.addSketch(self.myDocument, model.defaultPlane("XOY"))
39     self.myPoint = self.mySketch.addPoint(50., 50.)
40     self.myLine = self.mySketch.addLine(30., -1., 70., 19.)
41
42     self.myPoles = [[GeomAPI_Pnt2d(50., 50.), self.myPoint.coordinates()],
43                     GeomAPI_Pnt2d(70., 70.),
44                     (80., 30.),
45                     [GeomAPI_Pnt2d(50., 10.), self.myLine.result()],
46                     GeomAPI_Pnt2d(20., 10.)
47                    ]
48
49     self.myDegree = 3;
50     self.myDOF = 6
51     self.myNbPoints = 1
52     self.myNbLines = 1
53     self.myNbSplines = 0
54     self.myNbSplinesP = 0
55     self.myNbCoincidences = 0
56     self.myNbInternal = 0
57
58   def tearDown(self):
59     self.checkDOF()
60     model.end()
61     model.testNbSubFeatures(self.mySketch, "SketchPoint", self.myNbPoints)
62     model.testNbSubFeatures(self.mySketch, "SketchLine", self.myNbLines)
63     model.testNbSubFeatures(self.mySketch, "SketchBSpline", self.myNbSplines)
64     model.testNbSubFeatures(self.mySketch, "SketchBSplinePeriodic", self.myNbSplinesP)
65     model.testNbSubFeatures(self.mySketch, "SketchMacroBSpline", 0)
66     model.testNbSubFeatures(self.mySketch, "SketchMacroBSplinePeriodic", 0)
67     model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidence", self.myNbCoincidences)
68     model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidenceInternal", self.myNbInternal)
69
70
71   def checkDOF(self):
72     self.assertEqual(model.dof(self.mySketch), self.myDOF)
73
74
75   def test_bspline_non_periodic(self):
76     """ Test 1. Create B-spline curve by poles and references to other shapes
77     """
78     self.mySpline = self.mySketch.addSpline(poles = self.myPoles)
79     self.myDOF += len(self.myPoles) * 2 - 3
80     self.myNbSplines += 1
81     self.myNbPoints += len(self.myPoles)
82     self.myNbLines += len(self.myPoles) - 1
83     self.myNbCoincidences += 2
84     self.myNbInternal += len(self.myPoles) * 3 - 2
85     model.do()
86
87     assert(self.mySpline.feature())
88     assert(self.mySpline.feature().error() == "")
89     assert(self.mySpline.degree().value() == self.myDegree)
90
91   def test_bspline_periodic(self):
92     """ Test 2. Create periodic B-spline curve by poles and references to other shapes
93     """
94     self.mySpline = self.mySketch.addSpline(poles = self.myPoles, periodic = True)
95     self.myDOF += len(self.myPoles) * 2 - 3
96     self.myNbSplinesP += 1
97     self.myNbPoints += len(self.myPoles)
98     self.myNbLines += len(self.myPoles)
99     self.myNbCoincidences += 2
100     self.myNbInternal += len(self.myPoles) * 3
101     model.do()
102
103     assert(self.mySpline.feature())
104     assert(self.mySpline.feature().error() == "")
105     assert(self.mySpline.degree().value() == self.myDegree)
106
107   def test_bspline_lowlevel(self):
108     """ Test 3. Create macro B-spline on low-level to test attributeRefAttrList
109     """
110     model.end()
111     session = ModelAPI_Session.get()
112     sketch = featureToCompositeFeature(self.mySketch.feature())
113     session.startOperation()
114     bspline = sketch.addFeature("SketchMacroBSpline")
115     poles = geomDataAPI_Point2DArray(bspline.attribute("poles"))
116     weights = bspline.data().realArray("weights")
117     polesRef = bspline.data().refattrlist("poles_ref")
118     bspline.boolean("need_control_poly").setValue(False)
119
120     poles.setSize(3); weights.setSize(3)
121     poles.setPnt(0, GeomAPI_Pnt2d(50., 50.)); weights.setValue(0, 1.0); polesRef.append(self.myPoint.coordinates())
122     poles.setPnt(1, GeomAPI_Pnt2d(50., 10.)); weights.setValue(1, 1.0); polesRef.append(self.myLine.defaultResult())
123     poles.setPnt(2, GeomAPI_Pnt2d(20., 10.)); weights.setValue(2, 1.0); polesRef.append(None)
124
125     assert(polesRef.isInList(self.myPoint.coordinates()))
126     assert(not polesRef.isInList(self.myLine.startPoint()))
127     assert(polesRef.isInList(self.myLine.defaultResult()))
128     assert(not polesRef.isInList(self.myPoint.defaultResult()))
129
130     assert(polesRef.isAttribute(0))
131     assert(not polesRef.isAttribute(1))
132     assert(not polesRef.isAttribute(2))
133     assert(not polesRef.isAttribute(3))
134
135     assert(polesRef.attribute(0) is not None)
136     assert(polesRef.attribute(1) is None)
137     assert(polesRef.attribute(2) is None)
138     assert(polesRef.attribute(3) is None)
139     assert(polesRef.object(0) is not None)
140     assert(polesRef.object(1) is not None)
141     assert(polesRef.object(2) is None)
142     assert(polesRef.object(3) is None)
143
144     polesRef.remove(self.myPoint.coordinates())
145     polesRef.remove(self.myLine.defaultResult())
146     assert(polesRef.size() == 1)
147     polesRef.removeLast()
148
149     polesRef.append(self.myPoint.coordinates())
150     polesRef.append(None)
151     polesRef.append(self.myLine.defaultResult())
152     polesRef.removeLast()
153     polesRef.append(self.myLine.defaultResult())
154
155     session.finishOperation()
156     model.begin()
157     self.myDOF += 6
158     self.myNbSplines += 1
159
160 if __name__ == "__main__":
161     test_program = unittest.main(exit=False)
162     assert test_program.result.wasSuccessful(), "Test failed"
163     assert model.checkPythonDump()