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 /// Converts a 2D sketch space point into point in 3D space
108 /// \param theX an X coordinate
109 /// \param theY an Y coordinate
110 std::shared_ptr<GeomAPI_Pnt> to3D(const double theX, const double theY) const
112 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
113 data()->attribute(ORIGIN_ID()));
114 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
115 data()->attribute(NORM_ID()));
116 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
117 data()->attribute(DIRX_ID()));
118 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
120 std::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
121 ->added(aY->xyz()->multiplied(theY));
123 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
126 /// Returns the point projected into the sketch plane
127 /// \param thePnt a source 3d point
128 std::shared_ptr<GeomAPI_Pnt2d> to2D(const std::shared_ptr<GeomAPI_Pnt>& thePnt) const
130 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
131 data()->attribute(ORIGIN_ID()));
132 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
133 data()->attribute(NORM_ID()));
134 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
135 data()->attribute(DIRX_ID()));
136 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
137 return thePnt->to2D(aC->pnt(), aX->dir(), aY);
140 /// Returns true if this feature must be displayed in the history (top level of Part tree)
141 SKETCHPLUGIN_EXPORT virtual bool isInHistory()
146 /// Use plugin manager for features creation
147 SketchPlugin_Sketch();
149 /// Returns the basis plane for the sketch
150 std::shared_ptr<GeomAPI_Pln> plane() const
152 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
153 data()->attribute(ORIGIN_ID()));
154 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
155 data()->attribute(NORM_ID()));
157 if (!anOrigin || !aNorm)
158 return std::shared_ptr<GeomAPI_Pln>();
160 return std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
163 /// Returns currently defined plane as an object of Ax3
164 std::shared_ptr<GeomAPI_Ax3> coordinatePlane() const
166 DataPtr aData = data();
167 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
168 aData->attribute(ORIGIN_ID()));
169 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
170 aData->attribute(DIRX_ID()));
171 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
172 aData->attribute(NORM_ID()));
174 return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(aC->pnt(), aX->dir(), aNorm->dir()));
177 /// Checks whether the plane is set in the sketch.
178 /// \returns the boolean state
179 bool isPlaneSet() const
181 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
182 data()->attribute(NORM_ID()));
184 return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
188 /// appends a feature to the sketch sub-elements container
189 SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature> addFeature(std::string theID);
191 /// Just to synchronise the container of sub-features
192 virtual void removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature);
194 /// Returns the number of sub-elements
195 SKETCHPLUGIN_EXPORT virtual int numberOfSubs(bool forTree = false) const;
197 /// Returns the sub-feature by zero-base index
198 SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature>
199 subFeature(const int theIndex, bool forTree = false);
201 /// Returns the sub-feature unique identifier in this composite feature by index
202 SKETCHPLUGIN_EXPORT virtual int subFeatureId(const int theIndex) const;
204 /// Returns true if feature or reuslt belong to this composite feature as subs
205 SKETCHPLUGIN_EXPORT virtual bool isSub(ObjectPtr theObject) const;
207 SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID);
209 /// \brief Create a result for the point in the attribute if the attribute is initialized
210 /// \param theFeature a source feature
211 /// \param theSketch a sketch intance
212 /// \param theAttributeID an attribute string
213 /// \param theIndex an index of the result
214 static void createPoint2DResult(ModelAPI_Feature* theFeature,
215 SketchPlugin_Sketch* theSketch,
216 const std::string& theAttributeID, const int theIndex);
218 /// Add new feature and fill the data of the feature by the data of the parameter feature.
219 /// The name of the created feature stays unique.
220 /// \param theFeature a source feature
221 /// \param theSketch a sketch intance
222 /// \param theIsCopy if true sets feature copy attribute to true.
223 /// \return a created feature
224 static FeaturePtr addUniqueNamedCopiedFeature(FeaturePtr theFeature,
225 SketchPlugin_Sketch* theSketch,
226 const bool theIsCopy = false);
228 /// Creates a plane of the sketch.
229 /// \param theSketch a sketch intance
230 static std::shared_ptr<GeomAPI_Ax3> plane(SketchPlugin_Sketch* theSketch);
232 /// Customize presentation of the feature
233 virtual bool customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
234 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
236 bool isCustomized = false;
237 // apply the color of the result to the presentation
238 if (theDefaultPrs.get())
239 isCustomized = theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
240 // set the sketch presentation bold
241 isCustomized = thePrs->setWidth(2) || isCustomized;