]> SALOME platform Git repositories - modules/shaper.git/blob - src/PythonAPI/model/sketcher/tools.py
Salome HOME
Simplification and refactoring of unit tests for SketchPlugin
[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 import math
7
8 def addPolyline(sketch, *coords):
9     """Add a poly-line to sketch.
10
11     The end of consecutive segments are defined as coincident.
12     """
13     c0 = coords[0]
14     c1 = coords[1]
15     polyline = []
16     line_1 = sketch.addLine(c0, c1)
17     polyline.append(line_1)
18     # Adding and connecting next lines
19     for c2 in coords[2:]:
20         line_2 = sketch.addLine(c1, c2)
21         sketch.setCoincident(line_1.endPoint(), line_2.startPoint())
22         polyline.append(line_2)
23         c1 = c2
24         line_1 = line_2
25     return polyline
26
27
28 def addPolygon(sketch, *coords):
29     """Add a polygon to sketch.
30
31     The end of consecutive segments are defined as coincident.
32     """
33     pg = addPolyline(sketch, *coords)
34     # Closing the poly-line supposed being defined by at least 3 points
35     c0 = coords[0]
36     cn = coords[len(coords) - 1]
37     ln = sketch.addLine(cn, c0)
38     sketch.setCoincident(
39         pg[len(coords) - 2].endPoint(), ln.startPoint()
40         )
41     sketch.setCoincident(
42         ln.endPoint(), pg[0].startPoint()
43         )
44     pg.append(ln)
45     return pg
46
47 def dof(sketch):
48     """ Extract degrees of freedom for the given sketch
49     """
50     aSketch = sketch
51     if issubclass(type(aSketch), ModelHighAPI.ModelHighAPI_Interface):
52         aSketch = sketch.feature()
53     return int(filter(str.isdigit, aSketch.string("SolverDOF").value()))
54
55 def distancePointPoint(thePoint1, thePoint2):
56     aGeomPnt1 = toPoint(thePoint1)
57     aGeomPnt2 = toPoint(thePoint2)
58     return aGeomPnt1.distance(aGeomPnt2)
59
60 def distancePointLine(thePoint, theLine):
61     aPoint = toPoint(thePoint)
62     aLine = toSketchFeature(theLine)
63
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)
70
71 def lastSubFeature(theSketch, theKind):
72     """
73     obtains last feature of given kind from the sketch
74     """
75     for anIndex in range(theSketch.numberOfSubs() - 1, -1, -1):
76         aSub = theSketch.subFeature(anIndex)
77         if (aSub.getKind() == theKind):
78             return aSub
79
80 def toSketchFeature(theEntity):
81     """ Converts entity to sketch feature if possible
82     """
83     if issubclass(type(theEntity), ModelHighAPI.ModelHighAPI_Interface):
84         return theEntity.feature()
85     else:
86         return theEntity
87
88 def toPoint(thePoint):
89     if issubclass(type(thePoint), GeomDataAPI_Point2D):
90         return thePoint.pnt()
91     else:
92         aPoint = toSketchFeature(thePoint)
93         return geomDataAPI_Point2D(aPoint.attribute("PointCoordinates")).pnt()