1 // Copyright (C) 2014-2023 CEA, EDF
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.get() && aNorm.get() && anOrigin->isInitialized() && aNorm->isInitialized())
164 return std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
166 return std::shared_ptr<GeomAPI_Pln>();
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 if (!aNorm->isInitialized() || !aX->isInitialized() ||
181 aNorm->dir()->cross(aX->dir())->squareModulus() < 1.e-14)
182 return std::shared_ptr<GeomAPI_Ax3>();
184 return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(aC->pnt(), aX->dir(), aNorm->dir()));
187 /// Checks whether the plane is set in the sketch.
188 /// \returns the boolean state
189 bool isPlaneSet() const
191 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
192 data()->attribute(NORM_ID()));
194 return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
198 /// appends a feature to the sketch sub-elements container
199 SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature> addFeature(std::string theID);
201 /// Just to synchronise the container of sub-features
202 virtual void removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature);
204 /// Returns the number of sub-elements
205 SKETCHPLUGIN_EXPORT virtual int numberOfSubs(bool forTree = false) const;
207 /// Returns the sub-feature by zero-base index
208 SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature>
209 subFeature(const int theIndex, bool forTree = false);
211 /// Returns the sub-feature unique identifier in this composite feature by index
212 SKETCHPLUGIN_EXPORT virtual int subFeatureId(const int theIndex) const;
214 /// Returns true if feature or reuslt belong to this composite feature as subs
215 SKETCHPLUGIN_EXPORT virtual bool isSub(ObjectPtr theObject) const;
217 SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID);
219 /// Performs some custom feature specific functionality (normally called by some GUI button)
220 /// \param theActionId an action key
221 /// \return a boolean value about it is performed
222 SKETCHPLUGIN_EXPORT virtual bool customAction(const std::string& theActionId);
224 /// \brief Create a result for the point in the attribute if the attribute is initialized
225 /// \param theFeature a source feature
226 /// \param theSketch a sketch intance
227 /// \param theAttributeID an attribute string
228 /// \param theIndex an index of the result
229 static void createPoint2DResult(ModelAPI_Feature* theFeature,
230 SketchPlugin_Sketch* theSketch,
231 const std::string& theAttributeID, const int theIndex);
233 /// \brief Create a result for the segment given by a pair of attributes
234 /// \param theFeature a source feature
235 /// \param theSketch a sketch intance
236 /// \param theStartAttrID an attribute string
237 /// \param theEndAttrID an attribute string
238 /// \param theIndex an index of the result
239 static void createLine2DResult(ModelAPI_Feature* theFeature,
240 SketchPlugin_Sketch* theSketch,
241 const std::string& theStartAttrID,
242 const std::string& theEndAttrID,
243 const int theIndex = 0);
245 /// Add new feature and fill the data of the feature by the data of the parameter feature.
246 /// The name of the created feature stays unique.
247 /// \param theFeature a source feature
248 /// \param theSketch a sketch intance
249 /// \param theIsCopy if true sets feature copy attribute to true.
250 /// \return a created feature
251 static FeaturePtr addUniqueNamedCopiedFeature(FeaturePtr theFeature,
252 SketchPlugin_Sketch* theSketch,
253 const bool theIsCopy = false);
255 /// Creates a plane of the sketch.
256 /// \param theSketch a sketch intance
257 static std::shared_ptr<GeomAPI_Ax3> plane(SketchPlugin_Sketch* theSketch);
259 /// Customize presentation of the feature
260 //virtual bool customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
261 // std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
263 // bool isCustomized = false;
264 // // apply the color of the result to the presentation
265 // if (theDefaultPrs.get())
266 // isCustomized = theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
267 // // set the sketch presentation bold
268 // isCustomized = thePrs->setWidth(2) || isCustomized;
270 // return isCustomized;
274 /// Substitute all links to external objects by newly created features.
275 /// \return \c true, if all links updated.
276 bool removeLinksToExternal();
278 /// Update projected coordinate axes
279 void updateCoordinateAxis(ObjectPtr theSub, std::shared_ptr<GeomAPI_Ax3> thePlane);
282 std::shared_ptr<GeomAPI_Ax3> myPlane;