Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / PythonAPI / model / sketcher / tools.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 import ModelHighAPI
22 import GeomDataAPI
23
24 def addPolyline(sketch, *coords):
25     """Add a poly-line to sketch.
26
27     The end of consecutive segments are defined as coincident.
28     """
29     c0 = coords[0]
30     c1 = coords[1]
31     polyline = []
32     line_1 = sketch.addLine(c0, c1)
33     polyline.append(line_1)
34     # Adding and connecting next lines
35     for c2 in coords[2:]:
36         line_2 = sketch.addLine(c1, c2)
37         sketch.setCoincident(line_1.endPoint(), line_2.startPoint())
38         polyline.append(line_2)
39         c1 = c2
40         line_1 = line_2
41     return polyline
42
43
44 def addPolygon(sketch, *coords):
45     """Add a polygon to sketch.
46
47     The end of consecutive segments are defined as coincident.
48     """
49     pg = addPolyline(sketch, *coords)
50     # Closing the poly-line supposed being defined by at least 3 points
51     c0 = coords[0]
52     cn = coords[len(coords) - 1]
53     ln = sketch.addLine(cn, c0)
54     sketch.setCoincident(
55         pg[len(coords) - 2].endPoint(), ln.startPoint()
56         )
57     sketch.setCoincident(
58         ln.endPoint(), pg[0].startPoint()
59         )
60     pg.append(ln)
61     return pg
62
63 def dof(sketch):
64     """ Extract degrees of freedom for the given sketch
65     """
66     aSketch = sketch
67     if issubclass(type(aSketch), ModelHighAPI.ModelHighAPI_Interface):
68         aSketch = sketch.feature()
69     return int(filter(str.isdigit, aSketch.string("SolverDOF").value()))
70
71 def distancePointPoint(thePoint1, thePoint2):
72     aGeomPnt1 = thePoint1
73     aGeomPnt2 = thePoint2
74     if issubclass(type(thePoint1), GeomDataAPI.GeomDataAPI_Point2D):
75         aGeomPnt1 = thePoint1.pnt()
76     if issubclass(type(thePoint2), GeomDataAPI.GeomDataAPI_Point2D):
77         aGeomPnt2 = thePoint2.pnt()
78     return aGeomPnt1.distance(aGeomPnt2)
79
80 def lastSubFeature(theSketch, theKind):
81     """
82     obtains last feature of given kind from the sketch
83     """
84     for anIndex in range(theSketch.numberOfSubs() - 1, -1, -1):
85         aSub = theSketch.subFeature(anIndex)
86         if (aSub.getKind() == theKind):
87             return aSub