Salome HOME
updated copyright message
[modules/shaper.git] / src / PythonAPI / model / sketcher / tools.py
1 # Copyright (C) 2014-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 import ModelHighAPI
21 from GeomAPI import *
22 from GeomDataAPI import *
23 from ModelAPI import *
24 import math
25
26 def addPolyline(sketch, *coords):
27     """Add a poly-line to sketch.
28
29     The end of consecutive segments are defined as coincident.
30     """
31     c0 = coords[0]
32     c1 = coords[1]
33     polyline = []
34     line_1 = sketch.addLine(c0, c1)
35     polyline.append(line_1)
36     # Adding and connecting next lines
37     for c2 in coords[2:]:
38         line_2 = sketch.addLine(c1, c2)
39         sketch.setCoincident(line_1.endPoint(), line_2.startPoint())
40         polyline.append(line_2)
41         c1 = c2
42         line_1 = line_2
43     return polyline
44
45
46 def addPolygon(sketch, *coords):
47     """Add a polygon to sketch.
48
49     The end of consecutive segments are defined as coincident.
50     """
51     pg = addPolyline(sketch, *coords)
52     # Closing the poly-line supposed being defined by at least 3 points
53     c0 = coords[0]
54     cn = coords[len(coords) - 1]
55     ln = sketch.addLine(cn, c0)
56     sketch.setCoincident(
57         pg[len(coords) - 2].endPoint(), ln.startPoint()
58         )
59     sketch.setCoincident(
60         ln.endPoint(), pg[0].startPoint()
61         )
62     pg.append(ln)
63     return pg
64
65 def dof(sketch):
66     """ Extract degrees of freedom for the given sketch
67     """
68     aSketch = sketch
69     if issubclass(type(aSketch), ModelHighAPI.ModelHighAPI_Interface):
70         aSketch = sketch.feature()
71     return int(''.join(filter(str.isdigit, aSketch.string("SolverDOF").value())))
72
73 def distancePointPoint(thePoint1, thePoint2):
74     aPnt1 = toList(thePoint1)
75     aPnt2 = toList(thePoint2)
76     return math.hypot(aPnt1[0] - aPnt2[0], aPnt1[1] - aPnt2[1])
77
78 def signedDistancePointLine(thePoint, theLine):
79     aPoint = toList(thePoint)
80     aLine = toSketchFeature(theLine)
81
82     aLineStart = geomDataAPI_Point2D(aLine.attribute("StartPoint")).pnt().xy()
83     aLineEnd = geomDataAPI_Point2D(aLine.attribute("EndPoint")).pnt().xy()
84     aLineDir = aLineEnd.decreased(aLineStart)
85     aLineLen = aLineEnd.distance(aLineStart)
86     aCross = (aPoint[0] - aLineStart.x()) * aLineDir.y() - (aPoint[1] - aLineStart.y()) * aLineDir.x()
87     return aCross / aLineLen
88
89 def distancePointLine(thePoint, theLine):
90     return math.fabs(signedDistancePointLine(thePoint, theLine))
91
92 def lastSubFeature(theSketch, theKind):
93     """
94     obtains last feature of given kind from the sketch
95     """
96     aSketch = featureToCompositeFeature(toSketchFeature(theSketch))
97     for anIndex in range(aSketch.numberOfSubs() - 1, -1, -1):
98         aSub = aSketch.subFeature(anIndex)
99         if (aSub.getKind() == theKind):
100             return aSub
101
102 def toSketchFeature(theEntity):
103     """ Converts entity to sketch feature if possible
104     """
105     if issubclass(type(theEntity), ModelHighAPI.ModelHighAPI_Interface):
106         return theEntity.feature()
107     else:
108         return theEntity
109
110 def toList(thePoint):
111     if issubclass(type(thePoint), list):
112         return thePoint
113     elif issubclass(type(thePoint), GeomDataAPI_Point2D):
114         return [thePoint.x(), thePoint.y()]
115     elif issubclass(type(thePoint), GeomAPI_Pnt2d):
116         return [thePoint.x(), thePoint.y()]
117     else:
118         aFeature = toSketchFeature(thePoint)
119         aPoint = geomDataAPI_Point2D(aFeature.attribute("PointCoordinates"))
120         return [aPoint.x(), aPoint.y()]