1 """Sketch Feature Interface
2 Author: Daniel Brunier-Coulin with contribution by Mikhail Ponikarov
3 Copyright (C) 2014-20xx CEA/DEN, EDF R&D
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
13 def addSketch(doc, plane):
14 """Add a Sketch feature to the Part or PartSet and return an interface
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)
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.
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)
38 self.__sketchOnPlane(plane)
40 def __sketchOnPlane (self, plane):
43 dx = plane.xDirection()
45 self._feature.data().attribute("Origin")
46 ).setValue( o.x(), o.y(), o.z() )
48 self._feature.data().attribute("DirX")
49 ).setValue( dx.x(), dx.y(), dx.z() )
51 self._feature.data().attribute("Norm")
52 ).setValue( d.x(), d.y(), d.z() )
54 def __sketchOnFace (self, plane):
55 self._feature.data().selection("External").selectSubShape("FACE", plane)
58 # Creation of Geometries
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
65 def addLine (self, *args):
66 """Adds a line to this Sketch."""
67 line_interface = sk_line.addLine(self._feature, *args)
70 def addPolyline (self, *coords):
71 """Adds a poly-line to this Sketch.
73 The end of consecutive segments are defined as coincident.
78 line_1 = self.addLine(c0, c1)
79 polyline.append(line_1)
80 # Adding and connecting next lines
82 line_2 = self.addLine(c1, c2)
83 self.setCoincident(line_1.endPointData(), line_2.startPointData())
84 polyline.append(line_2)
89 def addPolygon (self, *coords):
90 """Add a polygon to this Sketch.
91 The end of consecutive segments are defined as coincident.
93 pg = self.addPolyline(*coords)
94 # Closing the poly-line supposed being defined by at least 3 points
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() )
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
109 # Creation of Geometrical and Dimensional Constraints
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)
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)
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)
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
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()
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()
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)
167 # Edition of Dimensional Constraints
169 def setValue (self, constraint, value):
170 """Modify the value of the given dimensional constraint."""
171 constraint.data().real("ConstraintValue").setValue(value)
176 def selectFace (self, *args):
177 """Select the geometrical entities of this Sketch on which
178 the result Face must be built.
180 When no entity is given, the face is based on all existing
181 geometry of this Sketch.
183 #self.resultype ="Face"
185 self._selection = modelAPI_ResultConstruction(
186 self._feature.firstResult()).shape()
188 self._selection = args[0].shape()
190 raise Exception("not yet implemented")
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()
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
205 """Returns the result data of this Feature."""
206 return self._feature.firstResult()