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