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