1 # Copyright (C) 2014-2022 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
22 from GeomDataAPI import *
23 from ModelAPI import *
26 def addPolyline(sketch, *coords):
27 """Add a poly-line to sketch.
29 The end of consecutive segments are defined as coincident.
34 line_1 = sketch.addLine(c0, c1)
35 polyline.append(line_1)
36 # Adding and connecting next lines
38 line_2 = sketch.addLine(c1, c2)
39 sketch.setCoincident(line_1.endPoint(), line_2.startPoint())
40 polyline.append(line_2)
46 def addPolygon(sketch, *coords):
47 """Add a polygon to sketch.
49 The end of consecutive segments are defined as coincident.
51 pg = addPolyline(sketch, *coords)
52 # Closing the poly-line supposed being defined by at least 3 points
54 cn = coords[len(coords) - 1]
55 ln = sketch.addLine(cn, c0)
57 pg[len(coords) - 2].endPoint(), ln.startPoint()
60 ln.endPoint(), pg[0].startPoint()
66 """ Extract degrees of freedom for the given sketch
69 if issubclass(type(aSketch), ModelHighAPI.ModelHighAPI_Interface):
70 aSketch = sketch.feature()
71 return int(''.join(filter(str.isdigit, aSketch.string("SolverDOF").value())))
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])
78 def signedDistancePointLine(thePoint, theLine):
79 aPoint = toList(thePoint)
80 aLine = toSketchFeature(theLine)
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
89 def distancePointLine(thePoint, theLine):
90 return math.fabs(signedDistancePointLine(thePoint, theLine))
92 def lastSubFeature(theSketch, theKind):
94 obtains last feature of given kind from the sketch
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):
102 def toSketchFeature(theEntity):
103 """ Converts entity to sketch feature if possible
105 if issubclass(type(theEntity), ModelHighAPI.ModelHighAPI_Interface):
106 return theEntity.feature()
110 def toList(thePoint):
111 if issubclass(type(thePoint), list):
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()]
118 aFeature = toSketchFeature(thePoint)
119 aPoint = geomDataAPI_Point2D(aFeature.attribute("PointCoordinates"))
120 return [aPoint.x(), aPoint.y()]