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 /// Returns the kind of a feature
94 SKETCHPLUGIN_EXPORT virtual const std::string& getKind()
96 static std::string MY_KIND = SketchPlugin_Sketch::ID();
100 /// Creates a new part document if needed
101 SKETCHPLUGIN_EXPORT virtual void execute();
103 /// Request for initialization of data model of the feature: adding all attributes
104 SKETCHPLUGIN_EXPORT virtual void initAttributes();
106 /// Converts a 2D sketch space point into point in 3D space
107 /// \param theX an X coordinate
108 /// \param theY an Y coordinate
109 std::shared_ptr<GeomAPI_Pnt> to3D(const double theX, const double theY) const
111 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
112 data()->attribute(ORIGIN_ID()));
113 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
114 data()->attribute(NORM_ID()));
115 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
116 data()->attribute(DIRX_ID()));
117 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
119 std::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
120 ->added(aY->xyz()->multiplied(theY));
122 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
125 /// Returns the point projected into the sketch plane
126 /// \param thePnt a source 3d point
127 std::shared_ptr<GeomAPI_Pnt2d> to2D(const std::shared_ptr<GeomAPI_Pnt>& thePnt) const
129 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
130 data()->attribute(ORIGIN_ID()));
131 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
132 data()->attribute(NORM_ID()));
133 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
134 data()->attribute(DIRX_ID()));
135 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
136 return thePnt->to2D(aC->pnt(), aX->dir(), aY);
139 /// Returns true if this feature must be displayed in the history (top level of Part tree)
140 SKETCHPLUGIN_EXPORT virtual bool isInHistory()
145 /// Use plugin manager for features creation
146 SketchPlugin_Sketch();
148 /// Returns the basis plane for the sketch
149 std::shared_ptr<GeomAPI_Pln> plane() const
151 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
152 data()->attribute(ORIGIN_ID()));
153 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
154 data()->attribute(NORM_ID()));
156 if (!anOrigin || !aNorm)
157 return std::shared_ptr<GeomAPI_Pln>();
159 return std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
162 /// Returns currently defined plane as an object of Ax3
163 std::shared_ptr<GeomAPI_Ax3> coordinatePlane() const
165 DataPtr aData = data();
166 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
167 aData->attribute(ORIGIN_ID()));
168 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
169 aData->attribute(DIRX_ID()));
170 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
171 aData->attribute(NORM_ID()));
173 return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(aC->pnt(), aX->dir(), aNorm->dir()));
176 /// Checks whether the plane is set in the sketch.
177 /// \returns the boolean state
178 bool isPlaneSet() const
180 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
181 data()->attribute(NORM_ID()));
183 return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
187 /// appends a feature to the sketch sub-elements container
188 SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature> addFeature(std::string theID);
190 /// Just to synchronise the container of sub-features
191 virtual void removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature);
193 /// Returns the number of sub-elements
194 SKETCHPLUGIN_EXPORT virtual int numberOfSubs(bool forTree = false) const;
196 /// Returns the sub-feature by zero-base index
197 SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature>
198 subFeature(const int theIndex, bool forTree = false);
200 /// Returns the sub-feature unique identifier in this composite feature by index
201 SKETCHPLUGIN_EXPORT virtual int subFeatureId(const int theIndex) const;
203 /// Returns true if feature or reuslt belong to this composite feature as subs
204 SKETCHPLUGIN_EXPORT virtual bool isSub(ObjectPtr theObject) const;
206 SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID);
208 /// \brief Create a result for the point in the attribute if the attribute is initialized
209 /// \param theFeature a source feature
210 /// \param theSketch a sketch intance
211 /// \param theAttributeID an attribute string
212 /// \param theIndex an index of the result
213 static void createPoint2DResult(ModelAPI_Feature* theFeature,
214 SketchPlugin_Sketch* theSketch,
215 const std::string& theAttributeID, const int theIndex);
217 /// Add new feature and fill the data of the feature by the data of the parameter feature.
218 /// The name of the created feature stays unique.
219 /// \param theFeature a source feature
220 /// \param theSketch a sketch intance
221 /// \param theIsCopy if true sets feature copy attribute to true.
222 /// \return a created feature
223 static FeaturePtr addUniqueNamedCopiedFeature(FeaturePtr theFeature,
224 SketchPlugin_Sketch* theSketch,
225 const bool theIsCopy = false);
227 /// Creates a plane of the sketch.
228 /// \param theSketch a sketch intance
229 static std::shared_ptr<GeomAPI_Ax3> plane(SketchPlugin_Sketch* theSketch);
231 /// Customize presentation of the feature
232 virtual bool customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
233 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
235 bool isCustomized = false;
236 // apply the color of the result to the presentation
237 if (theDefaultPrs.get())
238 isCustomized = theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
239 // set the sketch presentation bold
240 isCustomized = thePrs->setWidth(2) || isCustomized;