]> SALOME platform Git repositories - modules/shaper.git/blob - src/PythonAPI/model/sketcher/tools.py
Salome HOME
49a73bfbdeaac0b8183824abac7f1590db18aed7
[modules/shaper.git] / src / PythonAPI / model / sketcher / tools.py
1 # Author: Sergey Pokhodenko
2 # Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3
4 import ModelHighAPI
5 from GeomDataAPI import *
6 from ModelAPI import *
7 import math
8
9 def addPolyline(sketch, *coords):
10     """Add a poly-line to sketch.
11
12     The end of consecutive segments are defined as coincident.
13     """
14     c0 = coords[0]
15     c1 = coords[1]
16     polyline = []
17     line_1 = sketch.addLine(c0, c1)
18     polyline.append(line_1)
19     # Adding and connecting next lines
20     for c2 in coords[2:]:
21         line_2 = sketch.addLine(c1, c2)
22         sketch.setCoincident(line_1.endPoint(), line_2.startPoint())
23         polyline.append(line_2)
24         c1 = c2
25         line_1 = line_2
26     return polyline
27
28
29 def addPolygon(sketch, *coords):
30     """Add a polygon to sketch.
31
32     The end of consecutive segments are defined as coincident.
33     """
34     pg = addPolyline(sketch, *coords)
35     # Closing the poly-line supposed being defined by at least 3 points
36     c0 = coords[0]
37     cn = coords[len(coords) - 1]
38     ln = sketch.addLine(cn, c0)
39     sketch.setCoincident(
40         pg[len(coords) - 2].endPoint(), ln.startPoint()
41         )
42     sketch.setCoincident(
43         ln.endPoint(), pg[0].startPoint()
44         )
45     pg.append(ln)
46     return pg
47
48 def dof(sketch):
49     """ Extract degrees of freedom for the given sketch
50     """
51     aSketch = sketch
52     if issubclass(type(aSketch), ModelHighAPI.ModelHighAPI_Interface):
53         aSketch = sketch.feature()
54     return int(filter(str.isdigit, aSketch.string("SolverDOF").value()))
55
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])
60
61 def distancePointLine(thePoint, theLine):
62     aPoint = toList(thePoint)
63     aLine = toSketchFeature(theLine)
64
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)
71
72 def lastSubFeature(theSketch, theKind):
73     """
74     obtains last feature of given kind from the sketch
75     """
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):
80             return aSub
81
82 def toSketchFeature(theEntity):
83     """ Converts entity to sketch feature if possible
84     """
85     if issubclass(type(theEntity), ModelHighAPI.ModelHighAPI_Interface):
86         return theEntity.feature()
87     else:
88         return theEntity
89
90 def toList(thePoint):
91     if issubclass(type(thePoint), list):
92         return thePoint
93     elif issubclass(type(thePoint), GeomDataAPI_Point2D):
94         return [thePoint.x(), thePoint.y()]
95     else:
96         aFeature = toSketchFeature(thePoint)
97         aPoint = geomDataAPI_Point2D(aFeature.attribute("PointCoordinates"))
98         return [aPoint.x(), aPoint.y()]