1 # Author: Sergey Pokhodenko
2 # Copyright (C) 2014-20xx CEA/DEN, EDF R&D
5 from GeomDataAPI import *
8 def addPolyline(sketch, *coords):
9 """Add a poly-line to sketch.
11 The end of consecutive segments are defined as coincident.
16 line_1 = sketch.addLine(c0, c1)
17 polyline.append(line_1)
18 # Adding and connecting next lines
20 line_2 = sketch.addLine(c1, c2)
21 sketch.setCoincident(line_1.endPoint(), line_2.startPoint())
22 polyline.append(line_2)
28 def addPolygon(sketch, *coords):
29 """Add a polygon to sketch.
31 The end of consecutive segments are defined as coincident.
33 pg = addPolyline(sketch, *coords)
34 # Closing the poly-line supposed being defined by at least 3 points
36 cn = coords[len(coords) - 1]
37 ln = sketch.addLine(cn, c0)
39 pg[len(coords) - 2].endPoint(), ln.startPoint()
42 ln.endPoint(), pg[0].startPoint()
48 """ Extract degrees of freedom for the given sketch
51 if issubclass(type(aSketch), ModelHighAPI.ModelHighAPI_Interface):
52 aSketch = sketch.feature()
53 return int(filter(str.isdigit, aSketch.string("SolverDOF").value()))
55 def distancePointPoint(thePoint1, thePoint2):
56 aGeomPnt1 = toPoint(thePoint1)
57 aGeomPnt2 = toPoint(thePoint2)
58 return aGeomPnt1.distance(aGeomPnt2)
60 def distancePointLine(thePoint, theLine):
61 aPoint = toPoint(thePoint)
62 aLine = toSketchFeature(theLine)
64 aLineStart = geomDataAPI_Point2D(aLine.attribute("StartPoint")).pnt().xy()
65 aLineEnd = geomDataAPI_Point2D(aLine.attribute("EndPoint")).pnt().xy()
66 aLineDir = aLineEnd.decreased(aLineStart)
67 aLineLen = aLineEnd.distance(aLineStart)
68 aPntDir = aPoint.xy().decreased(aLineStart)
69 return math.fabs(aPntDir.cross(aLineDir) / aLineLen)
71 def lastSubFeature(theSketch, theKind):
73 obtains last feature of given kind from the sketch
75 for anIndex in range(theSketch.numberOfSubs() - 1, -1, -1):
76 aSub = theSketch.subFeature(anIndex)
77 if (aSub.getKind() == theKind):
80 def toSketchFeature(theEntity):
81 """ Converts entity to sketch feature if possible
83 if issubclass(type(theEntity), ModelHighAPI.ModelHighAPI_Interface):
84 return theEntity.feature()
88 def toPoint(thePoint):
89 if issubclass(type(thePoint), GeomDataAPI_Point2D):
92 aPoint = toSketchFeature(thePoint)
93 return geomDataAPI_Point2D(aPoint.attribute("PointCoordinates")).pnt()