1 # Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 from GeomDataAPI import *
22 from ModelAPI import *
25 def addPolyline(sketch, *coords):
26 """Add a poly-line to sketch.
28 The end of consecutive segments are defined as coincident.
33 line_1 = sketch.addLine(c0, c1)
34 polyline.append(line_1)
35 # Adding and connecting next lines
37 line_2 = sketch.addLine(c1, c2)
38 sketch.setCoincident(line_1.endPoint(), line_2.startPoint())
39 polyline.append(line_2)
45 def addPolygon(sketch, *coords):
46 """Add a polygon to sketch.
48 The end of consecutive segments are defined as coincident.
50 pg = addPolyline(sketch, *coords)
51 # Closing the poly-line supposed being defined by at least 3 points
53 cn = coords[len(coords) - 1]
54 ln = sketch.addLine(cn, c0)
56 pg[len(coords) - 2].endPoint(), ln.startPoint()
59 ln.endPoint(), pg[0].startPoint()
65 """ Extract degrees of freedom for the given sketch
68 if issubclass(type(aSketch), ModelHighAPI.ModelHighAPI_Interface):
69 aSketch = sketch.feature()
70 return int(''.join(filter(str.isdigit, aSketch.string("SolverDOF").value())))
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])
77 def signedDistancePointLine(thePoint, theLine):
78 aPoint = toList(thePoint)
79 aLine = toSketchFeature(theLine)
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
88 def distancePointLine(thePoint, theLine):
89 return math.fabs(signedDistancePointLine(thePoint, theLine))
91 def lastSubFeature(theSketch, theKind):
93 obtains last feature of given kind from the sketch
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):
101 def toSketchFeature(theEntity):
102 """ Converts entity to sketch feature if possible
104 if issubclass(type(theEntity), ModelHighAPI.ModelHighAPI_Interface):
105 return theEntity.feature()
109 def toList(thePoint):
110 if issubclass(type(thePoint), list):
112 elif issubclass(type(thePoint), GeomDataAPI_Point2D):
113 return [thePoint.x(), thePoint.y()]
115 aFeature = toSketchFeature(thePoint)
116 aPoint = geomDataAPI_Point2D(aFeature.attribute("PointCoordinates"))
117 return [aPoint.x(), aPoint.y()]