1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #ifndef SketchPlugin_Sketch_H_
22 #define SketchPlugin_Sketch_H_
24 #include "SketchPlugin.h"
25 #include <SketchPlugin_Feature.h>
26 #include <GeomAPI_Pnt.h>
27 #include <GeomAPI_Pln.h>
28 #include <GeomAPI_IPresentable.h>
29 #include <GeomAPI_ICustomPrs.h>
31 #include <GeomAPI_Ax3.h>
32 #include <GeomAPI_XYZ.h>
33 #include <GeomDataAPI_Point.h>
34 #include <GeomDataAPI_Dir.h>
37 #ifdef SET_PLANES_COLOR_IN_PREFERENCES
38 #define YZ_PLANE_COLOR "225,0,0"
39 #define XZ_PLANE_COLOR "0,225,0"
40 #define XY_PLANE_COLOR "0,0,225"
43 /**\class SketchPlugin_Sketch
45 * \brief Feature for creation of the new part in PartSet.
47 class SketchPlugin_Sketch : public ModelAPI_CompositeFeature, public GeomAPI_ICustomPrs
50 /// Sketch feature kind
51 inline static const std::string& ID()
53 static const std::string MY_SKETCH_ID("Sketch");
56 /// Origin point of the sketcher in 3D space
57 inline static const std::string& ORIGIN_ID()
59 static const std::string MY_ORIGIN_ID("Origin");
62 /// Vector X inside of the sketch plane
63 inline static const std::string& DIRX_ID()
65 static const std::string MY_DIRX_ID("DirX");
68 /// Vector Z, normal to the sketch plane
69 inline static const std::string& NORM_ID()
71 static const std::string MY_NORM_ID("Norm");
74 /// All features of this sketch (list of references)
75 inline static const std::string& FEATURES_ID()
77 static const std::string MY_FEATURES_ID("Features");
78 return MY_FEATURES_ID;
80 /// Sketch solver error
81 inline static const std::string& SOLVER_ERROR()
83 static const std::string MY_SOLVER_ERROR("SolverError");
84 return MY_SOLVER_ERROR;
87 /// Sketch solver error
88 inline static const std::string& SOLVER_DOF()
90 static const std::string MY_SOLVER_DOF("SolverDOF");
94 /// Returns the kind of a feature
95 SKETCHPLUGIN_EXPORT virtual const std::string& getKind()
97 static std::string MY_KIND = SketchPlugin_Sketch::ID();
101 /// Creates a new part document if needed
102 SKETCHPLUGIN_EXPORT virtual void execute();
104 /// Request for initialization of data model of the feature: adding all attributes
105 SKETCHPLUGIN_EXPORT virtual void initAttributes();
107 /// Moves the feature
108 /// \param theDeltaX the delta for X coordinate is moved
109 /// \param theDeltaY the delta for Y coordinate is moved
110 SKETCHPLUGIN_EXPORT virtual void move(const double theDeltaX, const double theDeltaY)
114 /// Converts a 2D sketch space point into point in 3D space
115 /// \param theX an X coordinate
116 /// \param theY an Y coordinate
117 std::shared_ptr<GeomAPI_Pnt> to3D(const double theX, const double theY) const
119 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
120 data()->attribute(ORIGIN_ID()));
121 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
122 data()->attribute(NORM_ID()));
123 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
124 data()->attribute(DIRX_ID()));
125 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
127 std::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
128 ->added(aY->xyz()->multiplied(theY));
130 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
133 /// Returns the point projected into the sketch plane
134 /// \param thePnt a source 3d point
135 std::shared_ptr<GeomAPI_Pnt2d> to2D(const std::shared_ptr<GeomAPI_Pnt>& thePnt) const
137 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
138 data()->attribute(ORIGIN_ID()));
139 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
140 data()->attribute(NORM_ID()));
141 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
142 data()->attribute(DIRX_ID()));
143 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
144 return thePnt->to2D(aC->pnt(), aX->dir(), aY);
147 /// Returns true if this feature must be displayed in the history (top level of Part tree)
148 SKETCHPLUGIN_EXPORT virtual bool isInHistory()
153 /// Use plugin manager for features creation
154 SketchPlugin_Sketch();
156 /// Returns the basis plane for the sketch
157 std::shared_ptr<GeomAPI_Pln> plane() const
159 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
160 data()->attribute(ORIGIN_ID()));
161 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
162 data()->attribute(NORM_ID()));
164 if (!anOrigin || !aNorm)
165 return std::shared_ptr<GeomAPI_Pln>();
167 return std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
170 /// Returns currently defined plane as an object of Ax3
171 std::shared_ptr<GeomAPI_Ax3> coordinatePlane() const
173 DataPtr aData = data();
174 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
175 aData->attribute(ORIGIN_ID()));
176 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
177 aData->attribute(DIRX_ID()));
178 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
179 aData->attribute(NORM_ID()));
181 return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(aC->pnt(), aX->dir(), aNorm->dir()));
184 /// Checks whether the plane is set in the sketch.
185 /// \returns the boolean state
186 bool isPlaneSet() const
188 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
189 data()->attribute(NORM_ID()));
191 return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
195 /// appends a feature to the sketch sub-elements container
196 SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature> addFeature(std::string theID);
198 /// Just to synchronise the container of sub-features
199 virtual void removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature);
201 /// Returns the number of sub-elements
202 SKETCHPLUGIN_EXPORT virtual int numberOfSubs(bool forTree = false) const;
204 /// Returns the sub-feature by zero-base index
205 SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature>
206 subFeature(const int theIndex, bool forTree = false);
208 /// Returns the sub-feature unique identifier in this composite feature by index
209 SKETCHPLUGIN_EXPORT virtual int subFeatureId(const int theIndex) const;
211 /// Returns true if feature or reuslt belong to this composite feature as subs
212 SKETCHPLUGIN_EXPORT virtual bool isSub(ObjectPtr theObject) const;
214 /// Construction result is allways recomuted on the fly
215 SKETCHPLUGIN_EXPORT virtual bool isPersistentResult() {return false;}
217 SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID);
219 /// Exchanges IDs of two given features: needed for fillet feature better naming (issue 769)
220 SKETCHPLUGIN_EXPORT virtual void exchangeIDs(std::shared_ptr<ModelAPI_Feature> theFeature1,
221 std::shared_ptr<ModelAPI_Feature> theFeature2);
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 /// Add new feature and fill the data of the feature by the data of the parameter feature.
234 /// The name of the created feature stays unique.
235 /// \param theFeature a source feature
236 /// \param theSketch a sketch intance
237 /// \param theIsCopy if true sets feature copy attribute to true.
238 /// \return a created feature
239 static FeaturePtr addUniqueNamedCopiedFeature(FeaturePtr theFeature,
240 SketchPlugin_Sketch* theSketch,
241 const bool theIsCopy = false);
243 /// Creates a plane of the sketch.
244 /// \param theSketch a sketch intance
245 static std::shared_ptr<GeomAPI_Ax3> plane(SketchPlugin_Sketch* theSketch);
247 /// Customize presentation of the feature
248 virtual bool customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
249 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
251 bool isCustomized = false;
252 // apply the color of the result to the presentation
253 if (theDefaultPrs.get())
254 isCustomized = theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
255 // set the sketch presentation bold
256 isCustomized = thePrs->setWidth(2) || isCustomized;