]> SALOME platform Git repositories - modules/shaper.git/blob - src/PythonAPI/modeler/sketcher/sketch.py
Salome HOME
[PythonAPI] added some more unit tests
[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 import modeler.sketcher.circle as sk_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         circle_interface = sk_circle.addCircle(self._feature, *args)
106         return circle_interface
107
108
109     # Creation of Geometrical and Dimensional Constraints
110
111     def setCoincident (self, p1, p2):
112         """Set coincident the two given points and add the corresponding 
113         constraint to this Sketch."""
114         constraint = self._feature.addFeature("SketchConstraintCoincidence")
115         constraint.data().refattr("ConstraintEntityA").setAttr(p1)
116         constraint.data().refattr("ConstraintEntityB").setAttr(p2)
117         return constraint
118
119     def setParallel (self, l1, l2):
120         """Set parallel the two given lines and add the corresponding 
121         constraint to this Sketch."""
122         constraint = self._feature.addFeature("SketchConstraintParallel")
123         constraint.data().refattr("ConstraintEntityA").setObject(l1)
124         constraint.data().refattr("ConstraintEntityB").setObject(l2)
125         return constraint
126
127     def setPerpendicular (self, l1, l2):
128         """Set perpendicular the two given lines and add the corresponding 
129         constraint to this Sketch."""
130         constraint = self._feature.addFeature("SketchConstraintPerpendicular")
131         constraint.data().refattr("ConstraintEntityA").setObject(l1)
132         constraint.data().refattr("ConstraintEntityB").setObject(l2)
133         return constraint
134
135     def setDistance (self, point, line, length):
136         """Set the distance between the given point and line, and add
137         the corresponding constraint to this Sketch."""
138         constraint = self._feature.addFeature("SketchConstraintDistance")
139         if isinstance(line, str):
140             # Add the edge identified by the given topological name 
141             # to this Sketch
142             line = self.addLine(line).result()   
143         constraint.data().refattr("ConstraintEntityA").setAttr(point)
144         constraint.data().refattr("ConstraintEntityB").setObject(line)
145         constraint.data().real("ConstraintValue").setValue(length)
146         self._feature.execute()
147         return constraint
148
149     def setLength (self, line, length):
150         """Set the length of the given line and add the corresponding 
151         constraint to this Sketch."""
152         constraint = self._feature.addFeature("SketchConstraintLength")
153         constraint.data().refattr("ConstraintEntityA").setObject(line)
154         constraint.data().real("ConstraintValue").setValue(length)
155         self._feature.execute()
156         return constraint
157
158     def setRadius (self, circle, radius):
159         """Set the radius of the given circle and add the corresponding 
160         constraint to this Sketch."""
161         constraint = self._feature.addFeature("SketchConstraintRadius")
162         constraint.data().refattr("ConstraintEntityA").setObject(circle)
163         constraint.data().real("ConstraintValue").setValue(radius)
164         return constraint
165
166
167     # Edition of Dimensional Constraints
168
169     def setValue (self, constraint, value):
170         """Modify the value of the given dimensional constraint."""
171         constraint.data().real("ConstraintValue").setValue(value)
172
173
174 # Getters
175
176     def selectFace (self, *args):
177         """Select the geometrical entities of this Sketch on which 
178         the result Face must be built.
179         
180         When no entity is given, the face is based on all existing 
181         geometry of this Sketch.
182         """
183         #self.resultype ="Face"
184         if   len(args) == 0:
185             self._selection = modelAPI_ResultConstruction( 
186                 self._feature.firstResult()).shape()
187         elif len(args) == 1:
188             self._selection = args[0].shape()
189         else:
190             raise Exception("not yet implemented")
191         return self
192
193     def buildShape (self):
194         """Builds the result Shape of this Sketch according to the selected geometrical entities."""
195         o  = geomDataAPI_Point( self._feature.data().attribute("Origin") ).pnt()
196         dx = geomDataAPI_Dir( self._feature.data().attribute("DirX") ).dir()
197         n  = geomDataAPI_Dir( self._feature.data().attribute("Norm") ).dir()
198
199         faces = ShapeList()      # The faces are kept otherwise they are destroyed at exit
200         GeomAlgoAPI_SketchBuilder.createFaces(o, dx, n, self._selection, faces)
201     #TODO: Deal with several faces 
202         return faces[0]
203
204     def result (self):
205         """Returns the result data of this Feature."""
206         return self._feature.firstResult()