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 #ifdef SET_PLANES_COLOR_IN_PREFERENCES
24 #define YZ_PLANE_COLOR "225,0,0"
25 #define XZ_PLANE_COLOR "0,225,0"
26 #define XY_PLANE_COLOR "0,0,225"
29 /**\class SketchPlugin_Sketch
31 * \brief Feature for creation of the new part in PartSet.
33 class SketchPlugin_Sketch : public ModelAPI_CompositeFeature, public GeomAPI_ICustomPrs
36 /// Sketch feature kind
37 inline static const std::string& ID()
39 static const std::string MY_SKETCH_ID("Sketch");
42 /// Origin point of the sketcher in 3D space
43 inline static const std::string& ORIGIN_ID()
45 static const std::string MY_ORIGIN_ID("Origin");
48 /// Vector X inside of the sketch plane
49 inline static const std::string& DIRX_ID()
51 static const std::string MY_DIRX_ID("DirX");
54 /// Vector Z, normal to the sketch plane
55 inline static const std::string& NORM_ID()
57 static const std::string MY_NORM_ID("Norm");
60 /// All features of this sketch (list of references)
61 inline static const std::string& FEATURES_ID()
63 static const std::string MY_FEATURES_ID("Features");
64 return MY_FEATURES_ID;
66 /// Sketch solver error
67 inline static const std::string& SOLVER_ERROR()
69 static const std::string MY_SOLVER_ERROR("SolverError");
70 return MY_SOLVER_ERROR;
73 /// Sketch solver error
74 inline static const std::string& SOLVER_DOF()
76 static const std::string MY_SOLVER_DOF("SolverDOF");
80 /// Returns the kind of a feature
81 SKETCHPLUGIN_EXPORT virtual const std::string& getKind()
83 static std::string MY_KIND = SketchPlugin_Sketch::ID();
87 /// Creates a new part document if needed
88 SKETCHPLUGIN_EXPORT virtual void execute();
90 /// Request for initialization of data model of the feature: adding all attributes
91 SKETCHPLUGIN_EXPORT virtual void initAttributes();
94 /// \param theDeltaX the delta for X coordinate is moved
95 /// \param theDeltaY the delta for Y coordinate is moved
96 SKETCHPLUGIN_EXPORT virtual void move(const double theDeltaX, const double theDeltaY)
100 /// Converts a 2D sketch space point into point in 3D space
101 /// \param theX an X coordinate
102 /// \param theY an Y coordinate
103 std::shared_ptr<GeomAPI_Pnt> to3D(const double theX, const double theY) const
105 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
106 data()->attribute(ORIGIN_ID()));
107 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
108 data()->attribute(NORM_ID()));
109 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
110 data()->attribute(DIRX_ID()));
111 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
113 std::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
114 ->added(aY->xyz()->multiplied(theY));
116 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
119 /// Returns the point projected into the sketch plane
120 /// \param thePnt a source 3d point
121 std::shared_ptr<GeomAPI_Pnt2d> to2D(const std::shared_ptr<GeomAPI_Pnt>& thePnt) const
123 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
124 data()->attribute(ORIGIN_ID()));
125 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
126 data()->attribute(NORM_ID()));
127 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
128 data()->attribute(DIRX_ID()));
129 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
130 return thePnt->to2D(aC->pnt(), aX->dir(), aY);
133 /// Returns true if this feature must be displayed in the history (top level of Part tree)
134 SKETCHPLUGIN_EXPORT virtual bool isInHistory()
139 /// Use plugin manager for features creation
140 SketchPlugin_Sketch();
142 /// Returns the basis plane for the sketch
143 std::shared_ptr<GeomAPI_Pln> plane() const
145 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
146 data()->attribute(ORIGIN_ID()));
147 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
148 data()->attribute(NORM_ID()));
150 if (!anOrigin || !aNorm)
151 return std::shared_ptr<GeomAPI_Pln>();
153 return std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
156 /// Returns currently defined plane as an object of Ax3
157 std::shared_ptr<GeomAPI_Ax3> coordinatePlane() const
159 DataPtr aData = data();
160 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
161 aData->attribute(ORIGIN_ID()));
162 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
163 aData->attribute(DIRX_ID()));
164 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
165 aData->attribute(NORM_ID()));
167 return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(aC->pnt(), aX->dir(), aNorm->dir()));
170 /// Checks whether the plane is set in the sketch.
171 /// \returns the boolean state
172 bool isPlaneSet() const
174 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
175 data()->attribute(NORM_ID()));
177 return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
181 /// appends a feature to the sketch sub-elements container
182 SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature> addFeature(std::string theID);
184 /// Just to synchronise the container of sub-features
185 virtual void removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature);
187 /// Returns the number of sub-elements
188 SKETCHPLUGIN_EXPORT virtual int numberOfSubs(bool forTree = false) const;
190 /// Returns the sub-feature by zero-base index
191 SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature>
192 subFeature(const int theIndex, bool forTree = false);
194 /// Returns the sub-feature unique identifier in this composite feature by index
195 SKETCHPLUGIN_EXPORT virtual int subFeatureId(const int theIndex) const;
197 /// Returns true if feature or reuslt belong to this composite feature as subs
198 SKETCHPLUGIN_EXPORT virtual bool isSub(ObjectPtr theObject) const;
200 /// Construction result is allways recomuted on the fly
201 SKETCHPLUGIN_EXPORT virtual bool isPersistentResult() {return false;}
203 SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID);
205 /// Exchanges IDs of two given features: needed for fillet feature better naming (issue 769)
206 SKETCHPLUGIN_EXPORT virtual void exchangeIDs(std::shared_ptr<ModelAPI_Feature> theFeature1,
207 std::shared_ptr<ModelAPI_Feature> theFeature2);
210 /// \brief Create a result for the point in the attribute if the attribute is initialized
211 /// \param theFeature a source feature
212 /// \param theSketch a sketch intance
213 /// \param theAttributeID an attribute string
214 /// \param theIndex an index of the result
215 static void createPoint2DResult(ModelAPI_Feature* theFeature,
216 SketchPlugin_Sketch* theSketch,
217 const std::string& theAttributeID, const int theIndex);
219 /// Add new feature and fill the data of the feature by the data of the parameter feature.
220 /// The name of the created feature stays unique.
221 /// \param theFeature a source feature
222 /// \param theSketch a sketch intance
223 /// \param theIsCopy if true sets feature copy attribute to true.
224 /// \return a created feature
225 static FeaturePtr addUniqueNamedCopiedFeature(FeaturePtr theFeature,
226 SketchPlugin_Sketch* theSketch,
227 const bool theIsCopy = false);
229 /// Creates a plane of the sketch.
230 /// \param theSketch a sketch intance
231 static std::shared_ptr<GeomAPI_Ax3> plane(SketchPlugin_Sketch* theSketch);
233 /// Customize presentation of the feature
234 virtual bool customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
235 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
237 bool isCustomized = false;
238 // apply the color of the result to the presentation
239 if (theDefaultPrs.get())
240 isCustomized = theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
241 // set the sketch presentation bold
242 isCustomized = thePrs->setWidth(2) || isCustomized;