1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_Sketch.h
4 // Created: 27 Mar 2014
5 // Author: Mikhail PONIKAROV
7 #ifndef SketchPlugin_Sketch_H_
8 #define SketchPlugin_Sketch_H_
10 #include "SketchPlugin.h"
11 #include <SketchPlugin_Feature.h>
12 #include <GeomAPI_Pnt.h>
13 #include <GeomAPI_Pln.h>
14 #include <GeomAPI_IPresentable.h>
15 #include <GeomAPI_ICustomPrs.h>
17 #include <GeomAPI_Ax3.h>
18 #include <GeomAPI_XYZ.h>
19 #include <GeomDataAPI_Point.h>
20 #include <GeomDataAPI_Dir.h>
23 #define YZ_PLANE_COLOR "225,0,0"
24 #define XZ_PLANE_COLOR "0,225,0"
25 #define XY_PLANE_COLOR "0,0,225"
27 /**\class SketchPlugin_Sketch
29 * \brief Feature for creation of the new part in PartSet.
31 class SketchPlugin_Sketch : public ModelAPI_CompositeFeature, public GeomAPI_ICustomPrs//, public GeomAPI_IPresentable
34 /// Sketch feature kind
35 inline static const std::string& ID()
37 static const std::string MY_SKETCH_ID("Sketch");
40 /// Origin point of the sketcher in 3D space
41 inline static const std::string& ORIGIN_ID()
43 static const std::string MY_ORIGIN_ID("Origin");
46 /// Vector X inside of the sketch plane
47 inline static const std::string& DIRX_ID()
49 static const std::string MY_DIRX_ID("DirX");
52 /// Vector Z, normal to the sketch plane
53 inline static const std::string& NORM_ID()
55 static const std::string MY_NORM_ID("Norm");
58 /// All features of this sketch (list of references)
59 inline static const std::string& FEATURES_ID()
61 static const std::string MY_FEATURES_ID("Features");
62 return MY_FEATURES_ID;
64 /// Sketch solver error
65 inline static const std::string& SOLVER_ERROR()
67 static const std::string MY_SOLVER_ERROR("SolverError");
68 return MY_SOLVER_ERROR;
71 /// Returns the kind of a feature
72 SKETCHPLUGIN_EXPORT virtual const std::string& getKind()
74 static std::string MY_KIND = SketchPlugin_Sketch::ID();
78 /// Creates a new part document if needed
79 SKETCHPLUGIN_EXPORT virtual void execute();
81 /// Request for initialization of data model of the feature: adding all attributes
82 SKETCHPLUGIN_EXPORT virtual void initAttributes();
85 /// \param theDeltaX the delta for X coordinate is moved
86 /// \param theDeltaY the delta for Y coordinate is moved
87 SKETCHPLUGIN_EXPORT virtual void move(const double theDeltaX, const double theDeltaY)
91 /// Converts a 2D sketch space point into point in 3D space
92 /// \param theX an X coordinate
93 /// \param theY an Y coordinate
94 std::shared_ptr<GeomAPI_Pnt> to3D(const double theX, const double theY) const
96 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
97 data()->attribute(ORIGIN_ID()));
98 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
99 data()->attribute(NORM_ID()));
100 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
101 data()->attribute(DIRX_ID()));
102 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
104 std::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
105 ->added(aY->xyz()->multiplied(theY));
107 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
110 /// Returns the point projected into the sketch plane
111 /// \param thePnt a source 3d point
112 std::shared_ptr<GeomAPI_Pnt2d> to2D(const std::shared_ptr<GeomAPI_Pnt>& thePnt) const
114 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
115 data()->attribute(ORIGIN_ID()));
116 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
117 data()->attribute(NORM_ID()));
118 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
119 data()->attribute(DIRX_ID()));
120 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
121 return thePnt->to2D(aC->pnt(), aX->dir(), aY);
124 /// Returns true if this feature must be displayed in the history (top level of Part tree)
125 SKETCHPLUGIN_EXPORT virtual bool isInHistory()
130 /// Use plugin manager for features creation
131 SketchPlugin_Sketch();
133 /// Returns the basis plane for the sketch
134 std::shared_ptr<GeomAPI_Pln> plane() const
136 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
137 data()->attribute(ORIGIN_ID()));
138 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
139 data()->attribute(NORM_ID()));
141 if (!anOrigin || !aNorm)
142 return std::shared_ptr<GeomAPI_Pln>();
144 return std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
147 /// Returns currently defined plane as an object of Ax3
148 std::shared_ptr<GeomAPI_Ax3> coordinatePlane() const
150 DataPtr aData = data();
151 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
152 aData->attribute(ORIGIN_ID()));
153 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
154 aData->attribute(DIRX_ID()));
155 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
156 aData->attribute(NORM_ID()));
158 return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(aC->pnt(), aX->dir(), aNorm->dir()));
161 /// Checks whether the plane is set in the sketch.
162 /// \returns the boolean state
163 bool isPlaneSet() const
165 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
166 data()->attribute(NORM_ID()));
168 return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
172 /// removes also all sub-sketch elements
173 SKETCHPLUGIN_EXPORT virtual void erase();
175 /// appends a feature to the sketch sub-elements container
176 SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature> addFeature(std::string theID);
178 /// Just to synchronise the container of sub-features
179 virtual void removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature);
181 /// Returns the number of sub-elements
182 SKETCHPLUGIN_EXPORT virtual int numberOfSubs(bool forTree = false) const;
184 /// Returns the sub-feature by zero-base index
185 SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature>
186 subFeature(const int theIndex, bool forTree = false);
188 /// Returns the sub-feature unique identifier in this composite feature by index
189 SKETCHPLUGIN_EXPORT virtual int subFeatureId(const int theIndex) const;
191 /// Returns true if feature or reuslt belong to this composite feature as subs
192 SKETCHPLUGIN_EXPORT virtual bool isSub(ObjectPtr theObject) const;
194 /// Construction result is allways recomuted on the fly
195 SKETCHPLUGIN_EXPORT virtual bool isPersistentResult() {return false;}
197 SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID);
199 /// Exchanges IDs of two given features: needed for fillet feature better naming (issue 769)
200 SKETCHPLUGIN_EXPORT virtual void exchangeIDs(std::shared_ptr<ModelAPI_Feature> theFeature1,
201 std::shared_ptr<ModelAPI_Feature> theFeature2);
204 /// \brief Create a result for the point in the attribute if the attribute is initialized
205 /// \param theFeature a source feature
206 /// \param theSketch a sketch intance
207 /// \param theAttributeID an attribute string
208 /// \param theIndex an index of the result
209 static void createPoint2DResult(ModelAPI_Feature* theFeature,
210 SketchPlugin_Sketch* theSketch,
211 const std::string& theAttributeID, const int theIndex);
213 /// Add new feature and fill the data of the feature by the data of the parameter feature.
214 /// The name of the created feature stays unique.
215 /// \param theFeature a source feature
216 /// \param theSketch a sketch intance
217 /// \param theIsCopy if true sets feature copy attribute to true.
218 /// \return a created feature
219 static FeaturePtr addUniqueNamedCopiedFeature(FeaturePtr theFeature,
220 SketchPlugin_Sketch* theSketch,
221 const bool theIsCopy = false);
223 /// Creates a plane of the sketch.
224 /// \param theSketch a sketch intance
225 static std::shared_ptr<GeomAPI_Ax3> plane(SketchPlugin_Sketch* theSketch);
227 /// Customize presentation of the feature
228 virtual bool customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
229 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
231 bool isCustomized = false;
232 // apply the color of the result to the presentation
233 if (theDefaultPrs.get())
234 isCustomized = theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
235 // set the sketch presentation bold
236 isCustomized = thePrs->setWidth(2) || isCustomized;