1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #ifndef SketchPlugin_Sketch_H_
21 #define SketchPlugin_Sketch_H_
23 #include "SketchPlugin.h"
24 #include <SketchPlugin_Feature.h>
25 #include <GeomAPI_Pnt.h>
26 #include <GeomAPI_Pln.h>
27 #include <GeomAPI_IPresentable.h>
28 #include <GeomAPI_ICustomPrs.h>
30 #include <GeomAPI_Ax3.h>
31 #include <GeomAPI_XYZ.h>
32 #include <GeomDataAPI_Point.h>
33 #include <GeomDataAPI_Dir.h>
36 #ifdef SET_PLANES_COLOR_IN_PREFERENCES
37 #define YZ_PLANE_COLOR "225,0,0"
38 #define XZ_PLANE_COLOR "0,225,0"
39 #define XY_PLANE_COLOR "0,0,225"
42 /**\class SketchPlugin_Sketch
44 * \brief Feature for creation of the new part in PartSet.
46 class SketchPlugin_Sketch : public ModelAPI_CompositeFeature, public GeomAPI_ICustomPrs
49 /// Sketch feature kind
50 inline static const std::string& ID()
52 static const std::string MY_SKETCH_ID("Sketch");
55 /// Origin point of the sketcher in 3D space
56 inline static const std::string& ORIGIN_ID()
58 static const std::string MY_ORIGIN_ID("Origin");
61 /// Vector X inside of the sketch plane
62 inline static const std::string& DIRX_ID()
64 static const std::string MY_DIRX_ID("DirX");
67 /// Vector Z, normal to the sketch plane
68 inline static const std::string& NORM_ID()
70 static const std::string MY_NORM_ID("Norm");
73 /// All features of this sketch (list of references)
74 inline static const std::string& FEATURES_ID()
76 static const std::string MY_FEATURES_ID("Features");
77 return MY_FEATURES_ID;
79 /// Sketch solver error
80 inline static const std::string& SOLVER_ERROR()
82 static const std::string MY_SOLVER_ERROR("SolverError");
83 return MY_SOLVER_ERROR;
86 /// Sketch solver error
87 inline static const std::string& SOLVER_DOF()
89 static const std::string MY_SOLVER_DOF("SolverDOF");
93 /// Action ID to remove links to external entities while changing the sketch plane.
94 inline static const std::string& ACTION_REMOVE_EXTERNAL()
96 static const std::string MY_ACTION_REMOVE_EXTERNAL("RemoveExternalLinks");
97 return MY_ACTION_REMOVE_EXTERNAL;
100 /// Returns the kind of a feature
101 SKETCHPLUGIN_EXPORT virtual const std::string& getKind()
103 static std::string MY_KIND = SketchPlugin_Sketch::ID();
107 /// Creates a new part document if needed
108 SKETCHPLUGIN_EXPORT virtual void execute();
110 /// Request for initialization of data model of the feature: adding all attributes
111 SKETCHPLUGIN_EXPORT virtual void initAttributes();
113 /// Converts a 2D sketch space point into point in 3D space
114 /// \param theX an X coordinate
115 /// \param theY an Y coordinate
116 std::shared_ptr<GeomAPI_Pnt> to3D(const double theX, const double theY) const
118 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
119 data()->attribute(ORIGIN_ID()));
120 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
121 data()->attribute(NORM_ID()));
122 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
123 data()->attribute(DIRX_ID()));
124 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
126 std::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
127 ->added(aY->xyz()->multiplied(theY));
129 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
132 /// Returns the point projected into the sketch plane
133 /// \param thePnt a source 3d point
134 std::shared_ptr<GeomAPI_Pnt2d> to2D(const std::shared_ptr<GeomAPI_Pnt>& thePnt) const
136 std::shared_ptr<GeomDataAPI_Point> aC = 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()));
140 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
141 data()->attribute(DIRX_ID()));
142 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
143 return thePnt->to2D(aC->pnt(), aX->dir(), aY);
146 /// Returns true if this feature must be displayed in the history (top level of Part tree)
147 SKETCHPLUGIN_EXPORT virtual bool isInHistory()
152 /// Use plugin manager for features creation
153 SketchPlugin_Sketch();
155 /// Returns the basis plane for the sketch
156 std::shared_ptr<GeomAPI_Pln> plane() const
158 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
159 data()->attribute(ORIGIN_ID()));
160 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
161 data()->attribute(NORM_ID()));
163 if (!anOrigin || !aNorm)
164 return std::shared_ptr<GeomAPI_Pln>();
166 return std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
169 /// Returns currently defined plane as an object of Ax3
170 std::shared_ptr<GeomAPI_Ax3> coordinatePlane() const
172 DataPtr aData = data();
173 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
174 aData->attribute(ORIGIN_ID()));
175 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
176 aData->attribute(DIRX_ID()));
177 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
178 aData->attribute(NORM_ID()));
180 return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(aC->pnt(), aX->dir(), aNorm->dir()));
183 /// Checks whether the plane is set in the sketch.
184 /// \returns the boolean state
185 bool isPlaneSet() const
187 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
188 data()->attribute(NORM_ID()));
190 return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
194 /// appends a feature to the sketch sub-elements container
195 SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature> addFeature(std::string theID);
197 /// Just to synchronise the container of sub-features
198 virtual void removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature);
200 /// Returns the number of sub-elements
201 SKETCHPLUGIN_EXPORT virtual int numberOfSubs(bool forTree = false) const;
203 /// Returns the sub-feature by zero-base index
204 SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature>
205 subFeature(const int theIndex, bool forTree = false);
207 /// Returns the sub-feature unique identifier in this composite feature by index
208 SKETCHPLUGIN_EXPORT virtual int subFeatureId(const int theIndex) const;
210 /// Returns true if feature or reuslt belong to this composite feature as subs
211 SKETCHPLUGIN_EXPORT virtual bool isSub(ObjectPtr theObject) const;
213 SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID);
215 /// Performs some custom feature specific functionality (normally called by some GUI button)
216 /// \param theActionId an action key
217 /// \return a boolean value about it is performed
218 SKETCHPLUGIN_EXPORT virtual bool customAction(const std::string& theActionId);
220 /// \brief Create a result for the point in the attribute if the attribute is initialized
221 /// \param theFeature a source feature
222 /// \param theSketch a sketch intance
223 /// \param theAttributeID an attribute string
224 /// \param theIndex an index of the result
225 static void createPoint2DResult(ModelAPI_Feature* theFeature,
226 SketchPlugin_Sketch* theSketch,
227 const std::string& theAttributeID, const int theIndex);
229 /// Add new feature and fill the data of the feature by the data of the parameter feature.
230 /// The name of the created feature stays unique.
231 /// \param theFeature a source feature
232 /// \param theSketch a sketch intance
233 /// \param theIsCopy if true sets feature copy attribute to true.
234 /// \return a created feature
235 static FeaturePtr addUniqueNamedCopiedFeature(FeaturePtr theFeature,
236 SketchPlugin_Sketch* theSketch,
237 const bool theIsCopy = false);
239 /// Creates a plane of the sketch.
240 /// \param theSketch a sketch intance
241 static std::shared_ptr<GeomAPI_Ax3> plane(SketchPlugin_Sketch* theSketch);
243 /// Customize presentation of the feature
244 virtual bool customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
245 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
247 bool isCustomized = false;
248 // apply the color of the result to the presentation
249 if (theDefaultPrs.get())
250 isCustomized = theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
251 // set the sketch presentation bold
252 isCustomized = thePrs->setWidth(2) || isCustomized;
258 /// Substitute all links to external objects by newly created features.
259 /// \return \c true, if all links updated.
260 bool removeLinksToExternal();