]> SALOME platform Git repositories - modules/shaper.git/blob - src/PythonAPI/modeler/sketcher/sketch.py
Salome HOME
20ddfd842805cb7d5fc89fa04628c1af910c0037
[modules/shaper.git] / src / PythonAPI / modeler / sketcher / sketch.py
1 """Sketch Feature Interface
2 Author: Daniel Brunier-Coulin with contribution by Mikhail Ponikarov
3 Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 """
5
6 from ModelAPI    import *
7 from GeomDataAPI import *
8 from GeomAlgoAPI import *
9 import modeler.sketcher.point as sk_point
10 import modeler.sketcher.line as sk_line
11 from modeler.sketcher.circle import Circle
12
13 def addSketch(doc, plane):
14     """Add a Sketch feature to the Part or PartSet and return an interface
15     on it.
16     
17     A Sketch object is instanciated with a feature as input parameter
18     it provides an interface for manipulation of the feature data.
19     :return: interface on the feature
20     :rtype: Sketch object"""
21     feature = featureToCompositeFeature( doc.addFeature("Sketch") )
22     return Sketch(feature, plane)
23
24 class Sketch():
25     """Interface on a Sketch feature."""
26     def __init__(self, feature, plane):
27         """Initialize a 2D Sketch on the given plane
28         The plane can be defined either by:
29         - a 3D axis system (geom.Ax3),
30         - an existing face identified by its topological name.
31         """
32         self._feature = feature
33         self._selection = None     # Entities used for building the result shape
34         #   self.resultype ="Face" # Type of Sketch result
35         if isinstance(plane, str):
36             self.__sketchOnFace(plane)
37         else:
38             self.__sketchOnPlane(plane)
39
40     def __sketchOnPlane (self, plane):
41         o  = plane.location()
42         d  = plane.direction()
43         dx = plane.xDirection()
44         geomDataAPI_Point( 
45             self._feature.data().attribute("Origin") 
46             ).setValue( o.x(), o.y(), o.z() )
47         geomDataAPI_Dir( 
48             self._feature.data().attribute("DirX") 
49             ).setValue( dx.x(), dx.y(), dx.z() )
50         geomDataAPI_Dir( 
51             self._feature.data().attribute("Norm") 
52             ).setValue( d.x(),  d.y(),  d.z()  )
53
54     def __sketchOnFace (self, plane):
55         self._feature.data().selection("External").selectSubShape("FACE", plane)
56
57
58 # Creation of Geometries
59
60     def addPoint (self, *args):
61         """Adds a point to this Sketch."""
62         point_interface = sk_point.addPoint(self._feature, *args)
63         return point_interface
64
65     def addLine (self, *args):
66         """Adds a line to this Sketch."""
67         line_interface = sk_line.addLine(self._feature, *args)
68         return line_interface
69
70     def addPolyline (self, *coords):
71         """Adds a poly-line to this Sketch.
72         
73         The end of consecutive segments are defined as coincident.
74         """
75         c0 = coords[0]
76         c1 = coords[1]
77         polyline = []
78         line_1 = self.addLine(c0, c1)
79         polyline.append(line_1)
80         # Adding and connecting next lines
81         for c2 in coords[2:]:
82             line_2 = self.addLine(c1, c2)
83             self.setCoincident(line_1.endPointData(), line_2.startPointData())
84             polyline.append(line_2)
85             c1 = c2
86             line_1 = line_2
87         return polyline
88
89     def addPolygon (self, *coords):
90         """Add a polygon to this Sketch.
91         The end of consecutive segments are defined as coincident.
92         """
93         pg = self.addPolyline(*coords)
94         # Closing the poly-line supposed being defined by at least 3 points
95         c0 = coords[0]
96         cn = coords[len(coords)-1]
97         ln = self.addLine(cn, c0)
98         self.setCoincident( pg[len(coords)-2].endPointData(), ln.startPointData() )
99         self.setCoincident( ln.endPointData(), pg[0].startPointData() )
100         pg.append(ln)
101         return pg
102
103     def addCircle (self, *args):
104         """Adds a circle to this Sketch."""
105         return Circle(self._feature, *args)
106
107
108     # Creation of Geometrical and Dimensional Constraints
109
110     def setCoincident (self, p1, p2):
111         """Set coincident the two given points and add the corresponding 
112         constraint to this Sketch."""
113         constraint = self._feature.addFeature("SketchConstraintCoincidence")
114         constraint.data().refattr("ConstraintEntityA").setAttr(p1)
115         constraint.data().refattr("ConstraintEntityB").setAttr(p2)
116         return constraint
117
118     def setParallel (self, l1, l2):
119         """Set parallel the two given lines and add the corresponding 
120         constraint to this Sketch."""
121         constraint = self._feature.addFeature("SketchConstraintParallel")
122         constraint.data().refattr("ConstraintEntityA").setObject(l1)
123         constraint.data().refattr("ConstraintEntityB").setObject(l2)
124         return constraint
125
126     def setPerpendicular (self, l1, l2):
127         """Set perpendicular the two given lines and add the corresponding 
128         constraint to this Sketch."""
129         constraint = self._feature.addFeature("SketchConstraintPerpendicular")
130         constraint.data().refattr("ConstraintEntityA").setObject(l1)
131         constraint.data().refattr("ConstraintEntityB").setObject(l2)
132         return constraint
133
134     def setDistance (self, point, line, length):
135         """Set the distance between the given point and line, and add
136         the corresponding constraint to this Sketch."""
137         constraint = self._feature.addFeature("SketchConstraintDistance")
138         if isinstance(line, str):
139             # Add the edge identified by the given topological name 
140             # to this Sketch
141             line = self.addLine(line).result()   
142         constraint.data().refattr("ConstraintEntityA").setAttr(point)
143         constraint.data().refattr("ConstraintEntityB").setObject(line)
144         constraint.data().real("ConstraintValue").setValue(length)
145         self._feature.execute()
146         return constraint
147
148     def setLength (self, line, length):
149         """Set the length of the given line and add the corresponding 
150         constraint to this Sketch."""
151         constraint = self._feature.addFeature("SketchConstraintLength")
152         constraint.data().refattr("ConstraintEntityA").setObject(line)
153         constraint.data().real("ConstraintValue").setValue(length)
154         self._feature.execute()
155         return constraint
156
157     def setRadius (self, circle, radius):
158         """Set the radius of the given circle and add the corresponding 
159         constraint to this Sketch."""
160         constraint = self._feature.addFeature("SketchConstraintRadius")
161         constraint.data().refattr("ConstraintEntityA").setObject(circle)
162         constraint.data().real("ConstraintValue").setValue(radius)
163         return constraint
164
165
166     # Edition of Dimensional Constraints
167
168     def setValue (self, constraint, value):
169         """Modify the value of the given dimensional constraint."""
170         constraint.data().real("ConstraintValue").setValue(value)
171
172
173 # Getters
174
175     def selectFace (self, *args):
176         """Select the geometrical entities of this Sketch on which 
177         the result Face must be built.
178         
179         When no entity is given, the face is based on all existing 
180         geometry of this Sketch.
181         """
182         #self.resultype ="Face"
183         if   len(args) == 0:
184             self._selection = modelAPI_ResultConstruction( 
185                 self._feature.firstResult()).shape()
186         elif len(args) == 1:
187             self._selection = args[0].shape()
188         else:
189             raise Exception("not yet implemented")
190         return self
191
192     def buildShape (self):
193         """Builds the result Shape of this Sketch according to the selected geometrical entities."""
194         o  = geomDataAPI_Point( self._feature.data().attribute("Origin") ).pnt()
195         dx = geomDataAPI_Dir( self._feature.data().attribute("DirX") ).dir()
196         n  = geomDataAPI_Dir( self._feature.data().attribute("Norm") ).dir()
197
198         faces = ShapeList()      # The faces are kept otherwise they are destroyed at exit
199         GeomAlgoAPI_SketchBuilder.createFaces(o, dx, n, self._selection, faces)
200     #TODO: Deal with several faces 
201         return faces[0]
202
203     def result (self):
204         """Returns the result data of this Feature."""
205         return self._feature.firstResult()