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 from modeler.sketcher.circle import 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 return Circle(self._feature, *args)
108 # Creation of Geometrical and Dimensional Constraints
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)
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)
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)
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
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()
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()
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)
166 # Edition of Dimensional Constraints
168 def setValue (self, constraint, value):
169 """Modify the value of the given dimensional constraint."""
170 constraint.data().real("ConstraintValue").setValue(value)
175 def selectFace (self, *args):
176 """Select the geometrical entities of this Sketch on which
177 the result Face must be built.
179 When no entity is given, the face is based on all existing
180 geometry of this Sketch.
182 #self.resultype ="Face"
184 self._selection = modelAPI_ResultConstruction(
185 self._feature.firstResult()).shape()
187 self._selection = args[0].shape()
189 raise Exception("not yet implemented")
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()
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
204 """Returns the result data of this Feature."""
205 return self._feature.firstResult()