1 # Author: Sergey Pokhodenko
2 # Copyright (C) 2014-20xx CEA/DEN, EDF R&D
5 from GeomDataAPI import *
9 def addPolyline(sketch, *coords):
10 """Add a poly-line to sketch.
12 The end of consecutive segments are defined as coincident.
17 line_1 = sketch.addLine(c0, c1)
18 polyline.append(line_1)
19 # Adding and connecting next lines
21 line_2 = sketch.addLine(c1, c2)
22 sketch.setCoincident(line_1.endPoint(), line_2.startPoint())
23 polyline.append(line_2)
29 def addPolygon(sketch, *coords):
30 """Add a polygon to sketch.
32 The end of consecutive segments are defined as coincident.
34 pg = addPolyline(sketch, *coords)
35 # Closing the poly-line supposed being defined by at least 3 points
37 cn = coords[len(coords) - 1]
38 ln = sketch.addLine(cn, c0)
40 pg[len(coords) - 2].endPoint(), ln.startPoint()
43 ln.endPoint(), pg[0].startPoint()
49 """ Extract degrees of freedom for the given sketch
52 if issubclass(type(aSketch), ModelHighAPI.ModelHighAPI_Interface):
53 aSketch = sketch.feature()
54 return int(filter(str.isdigit, aSketch.string("SolverDOF").value()))
56 def distancePointPoint(thePoint1, thePoint2):
57 aPnt1 = toList(thePoint1)
58 aPnt2 = toList(thePoint2)
59 return math.hypot(aPnt1[0] - aPnt2[0], aPnt1[1] - aPnt2[1])
61 def distancePointLine(thePoint, theLine):
62 aPoint = toList(thePoint)
63 aLine = toSketchFeature(theLine)
65 aLineStart = geomDataAPI_Point2D(aLine.attribute("StartPoint")).pnt().xy()
66 aLineEnd = geomDataAPI_Point2D(aLine.attribute("EndPoint")).pnt().xy()
67 aLineDir = aLineEnd.decreased(aLineStart)
68 aLineLen = aLineEnd.distance(aLineStart)
69 aCross = (aPoint[0] - aLineStart.x()) * aLineDir.y() - (aPoint[1] - aLineStart.y()) * aLineDir.x()
70 return math.fabs(aCross / aLineLen)
72 def lastSubFeature(theSketch, theKind):
74 obtains last feature of given kind from the sketch
76 aSketch = featureToCompositeFeature(toSketchFeature(theSketch))
77 for anIndex in range(aSketch.numberOfSubs() - 1, -1, -1):
78 aSub = aSketch.subFeature(anIndex)
79 if (aSub.getKind() == theKind):
82 def toSketchFeature(theEntity):
83 """ Converts entity to sketch feature if possible
85 if issubclass(type(theEntity), ModelHighAPI.ModelHighAPI_Interface):
86 return theEntity.feature()
91 if issubclass(type(thePoint), list):
93 elif issubclass(type(thePoint), GeomDataAPI_Point2D):
94 return [thePoint.x(), thePoint.y()]
96 aFeature = toSketchFeature(thePoint)
97 aPoint = geomDataAPI_Point2D(aFeature.attribute("PointCoordinates"))
98 return [aPoint.x(), aPoint.y()]