Salome HOME
462c1808aa09bee17b119b7f67aee94ba692b94b
[modules/shaper.git] / src / PythonFeaturesPlugin / sketch.py
1 from ModelAPI     import *
2 from GeomDataAPI  import *
3
4
5 # Initialization of the Sketch
6 # ----------------------------
7
8 def addTo(doc):
9   return modelAPI_CompositeFeature( doc.addFeature("Sketch") )
10
11
12 def setXOYPlane(sketch):
13   geomDataAPI_Point( sketch.attribute("Origin") ).setValue(0, 0, 0)
14   geomDataAPI_Dir( sketch.attribute("DirX") ).setValue(1, 0, 0)
15   geomDataAPI_Dir( sketch.attribute("DirY") ).setValue(0, 1, 0)
16   geomDataAPI_Dir( sketch.attribute("Norm") ).setValue(0, 0, 1)
17
18
19 # Point geometry
20 # --------------
21
22 def addPoint(x, y, sketch):
23   point = sketch.addFeature("SketchPoint")
24   geomDataAPI_Point2D( point.attribute("PointCoordindates") ).setValue(x, y)
25   point.execute()     #Required to get the result, if needed for creating constraints
26   return point
27
28
29 def getGeometry(point):
30   return geomDataAPI_Point2D( point.attribute("PointCoordindates") )
31
32
33 # Line geometry
34 # -------------
35
36 def addClosedBrokenLine(coords, sketch):
37   c0 = coords[0]
38   c1 = coords[1]
39   bl = []
40   l1 = sketch.addFeature("SketchLine")
41   geomDataAPI_Point2D( l1.attribute("StartPoint") ).setValue(c0.x(), c0.y())
42   geomDataAPI_Point2D( l1.attribute("EndPoint") ).setValue(c1.x(), c1.y())
43   l1.execute()
44   bl.append(l1)
45   l0 = l1
46
47   for c2 in coords[2:]:
48     l2 = sketch.addFeature("SketchLine")
49     geomDataAPI_Point2D( l2.attribute("StartPoint") ).setValue(c1.x(), c1.y())
50     geomDataAPI_Point2D( l2.attribute("EndPoint") ).setValue(c2.x(), c2.y())
51     l2.execute()
52     bl.append(l2)
53     constraint = sketch.addFeature("SketchConstraintCoincidence")
54     constraint.refattr("ConstraintEntityA").setAttr( l1.attribute("EndPoint") )
55     constraint.refattr("ConstraintEntityB").setAttr( l2.attribute("StartPoint") )
56     c1 = c2
57     l1 = l2
58
59   if len(coords) > 2:
60     l2 = sketch.addFeature("SketchLine")
61     geomDataAPI_Point2D( l2.attribute("StartPoint") ).setValue(c1.x(), c1.y())
62     geomDataAPI_Point2D( l2.attribute("EndPoint") ).setValue(c0.x(), c0.y())
63     l2.execute()
64     bl.append(l2)
65     constraint = sketch.addFeature("SketchConstraintCoincidence")
66     constraint.refattr("ConstraintEntityA").setAttr( l1.attribute("EndPoint") )
67     constraint.refattr("ConstraintEntityB").setAttr( l2.attribute("StartPoint") )
68
69     constraint = sketch.addFeature("SketchConstraintCoincidence")
70     constraint.refattr("ConstraintEntityA").setAttr( l2.attribute("EndPoint") )
71     constraint.refattr("ConstraintEntityB").setAttr( l0.attribute("StartPoint") )
72
73   return bl
74
75
76 def addLine(x1, y1, x2, y2, sketch):
77   line = sketch.addFeature("SketchLine")
78   geomDataAPI_Point2D( line.attribute("StartPoint") ).setValue(x1, y1)
79   geomDataAPI_Point2D( line.attribute("EndPoint") ).setValue(x2, y2)
80   line.execute()      #Required to get the result, if needed for creating constraints
81   return line
82
83
84 def getGeometry(line):
85   return line.firstResult()
86
87
88 def getStartPoint(line):
89   return geomDataAPI_Point2D( line.attribute("StartPoint") )
90
91
92 def getEndPoint(line):
93   return geomDataAPI_Point2D( line.attribute("EndPoint") )
94
95
96 # Constraints
97 # -----------
98
99 def makeCoincident(p1, p2, sketch):
100   constraint = sketch.addFeature("SketchConstraintCoincidence")
101   constraint.refattr("ConstraintEntityA").setAttr(p1)
102   constraint.refattr("ConstraintEntityB").setAttr(p2)
103
104
105 def makeParallel(l1, l2, sketch):
106   constraint = sketch.addFeature("SketchConstraintParallel")
107   constraint.refattr("ConstraintEntityA").setObject(l1)
108   constraint.refattr("ConstraintEntityB").setObject(l2)
109
110
111 def makePerpendicular(l1, l2, sketch):
112   constraint  = sketch.addFeature("SketchConstraintPerpendicular")
113   constraint.refattr("ConstraintEntityA").setObject(l1)
114   constraint.refattr("ConstraintEntityB").setObject(l2)