Salome HOME
Create custom action for sketch plane update.
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Sketch.h
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef SketchPlugin_Sketch_H_
21 #define SketchPlugin_Sketch_H_
22
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>
29
30 #include <GeomAPI_Ax3.h>
31 #include <GeomAPI_XYZ.h>
32 #include <GeomDataAPI_Point.h>
33 #include <GeomDataAPI_Dir.h>
34 #include <list>
35
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"
40 #endif
41
42 /**\class SketchPlugin_Sketch
43  * \ingroup Plugins
44  * \brief Feature for creation of the new part in PartSet.
45  */
46 class SketchPlugin_Sketch : public ModelAPI_CompositeFeature, public GeomAPI_ICustomPrs
47 {
48  public:
49   /// Sketch feature kind
50   inline static const std::string& ID()
51   {
52     static const std::string MY_SKETCH_ID("Sketch");
53     return MY_SKETCH_ID;
54   }
55   /// Origin point of the sketcher in 3D space
56   inline static const std::string& ORIGIN_ID()
57   {
58     static const std::string MY_ORIGIN_ID("Origin");
59     return MY_ORIGIN_ID;
60   }
61   /// Vector X inside of the sketch plane
62   inline static const std::string& DIRX_ID()
63   {
64     static const std::string MY_DIRX_ID("DirX");
65     return MY_DIRX_ID;
66   }
67   /// Vector Z, normal to the sketch plane
68   inline static const std::string& NORM_ID()
69   {
70     static const std::string MY_NORM_ID("Norm");
71     return MY_NORM_ID;
72   }
73   /// All features of this sketch (list of references)
74   inline static const std::string& FEATURES_ID()
75   {
76     static const std::string MY_FEATURES_ID("Features");
77     return MY_FEATURES_ID;
78   }
79   /// Sketch solver error
80   inline static const std::string& SOLVER_ERROR()
81   {
82     static const std::string MY_SOLVER_ERROR("SolverError");
83     return MY_SOLVER_ERROR;
84   }
85
86   /// Sketch solver error
87   inline static const std::string& SOLVER_DOF()
88   {
89     static const std::string MY_SOLVER_DOF("SolverDOF");
90     return MY_SOLVER_DOF;
91   }
92
93   /// Action ID to remove links to external entities while changing the sketch plane.
94   inline static const std::string& ACTION_REMOVE_EXTERNAL()
95   {
96     static const std::string MY_ACTION_REMOVE_EXTERNAL("RemoveExternalLinks");
97     return MY_ACTION_REMOVE_EXTERNAL;
98   }
99
100   /// Returns the kind of a feature
101   SKETCHPLUGIN_EXPORT virtual const std::string& getKind()
102   {
103     static std::string MY_KIND = SketchPlugin_Sketch::ID();
104     return MY_KIND;
105   }
106
107   /// Creates a new part document if needed
108   SKETCHPLUGIN_EXPORT virtual void execute();
109
110   /// Request for initialization of data model of the feature: adding all attributes
111   SKETCHPLUGIN_EXPORT virtual void initAttributes();
112
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
117   {
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())));
125
126     std::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
127         ->added(aY->xyz()->multiplied(theY));
128
129     return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
130   }
131
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
135   {
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);
144   }
145
146   /// Returns true if this feature must be displayed in the history (top level of Part tree)
147   SKETCHPLUGIN_EXPORT virtual bool isInHistory()
148   {
149     return true;
150   }
151
152   /// Use plugin manager for features creation
153   SketchPlugin_Sketch();
154
155   /// Returns the basis plane for the sketch
156   std::shared_ptr<GeomAPI_Pln> plane() const
157   {
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()));
162
163     if (!anOrigin || !aNorm)
164       return std::shared_ptr<GeomAPI_Pln>();
165
166     return std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
167   }
168
169   /// Returns currently defined plane as an object of Ax3
170   std::shared_ptr<GeomAPI_Ax3> coordinatePlane() const
171   {
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()));
179
180     return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(aC->pnt(), aX->dir(), aNorm->dir()));
181   }
182
183   /// Checks whether the plane is set in the sketch.
184   /// \returns the boolean state
185   bool isPlaneSet() const
186   {
187     std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
188         data()->attribute(NORM_ID()));
189
190     return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
191   }
192
193
194   /// appends a feature to the sketch sub-elements container
195   SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature> addFeature(std::string theID);
196
197   /// Just to synchronise the container of sub-features
198   virtual void removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature);
199
200   /// Returns the number of sub-elements
201   SKETCHPLUGIN_EXPORT virtual int numberOfSubs(bool forTree = false) const;
202
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);
206
207   /// Returns the sub-feature unique identifier in this composite feature by index
208   SKETCHPLUGIN_EXPORT virtual int subFeatureId(const int theIndex) const;
209
210   /// Returns true if feature or reuslt belong to this composite feature as subs
211   SKETCHPLUGIN_EXPORT virtual bool isSub(ObjectPtr theObject) const;
212
213   SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID);
214
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);
219
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);
228
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);
238
239   /// Creates a plane of the sketch.
240   /// \param theSketch a sketch intance
241   static std::shared_ptr<GeomAPI_Ax3> plane(SketchPlugin_Sketch* theSketch);
242
243   /// Customize presentation of the feature
244   virtual bool customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
245                                      std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
246   {
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;
253
254     return isCustomized;
255   }
256
257 private:
258   /// Substitute all links to external objects by newly created features.
259   /// \return \c true, if all links updated.
260   bool removeLinksToExternal();
261 };
262
263 #endif