Salome HOME
b38b69b90f8d88b165dc83354eba42ba0e16d0bb
[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   /// Returns the kind of a feature
94   SKETCHPLUGIN_EXPORT virtual const std::string& getKind()
95   {
96     static std::string MY_KIND = SketchPlugin_Sketch::ID();
97     return MY_KIND;
98   }
99
100   /// Creates a new part document if needed
101   SKETCHPLUGIN_EXPORT virtual void execute();
102
103   /// Request for initialization of data model of the feature: adding all attributes
104   SKETCHPLUGIN_EXPORT virtual void initAttributes();
105
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
110   {
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())));
118
119     std::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
120         ->added(aY->xyz()->multiplied(theY));
121
122     return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
123   }
124
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
128   {
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);
137   }
138
139   /// Returns true if this feature must be displayed in the history (top level of Part tree)
140   SKETCHPLUGIN_EXPORT virtual bool isInHistory()
141   {
142     return true;
143   }
144
145   /// Use plugin manager for features creation
146   SketchPlugin_Sketch();
147
148   /// Returns the basis plane for the sketch
149   std::shared_ptr<GeomAPI_Pln> plane() const
150   {
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()));
155
156     if (!anOrigin || !aNorm)
157       return std::shared_ptr<GeomAPI_Pln>();
158
159     return std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
160   }
161
162   /// Returns currently defined plane as an object of Ax3
163   std::shared_ptr<GeomAPI_Ax3> coordinatePlane() const
164   {
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()));
172
173     return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(aC->pnt(), aX->dir(), aNorm->dir()));
174   }
175
176   /// Checks whether the plane is set in the sketch.
177   /// \returns the boolean state
178   bool isPlaneSet() const
179   {
180     std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
181         data()->attribute(NORM_ID()));
182
183     return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
184   }
185
186
187   /// appends a feature to the sketch sub-elements container
188   SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature> addFeature(std::string theID);
189
190   /// Just to synchronise the container of sub-features
191   virtual void removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature);
192
193   /// Returns the number of sub-elements
194   SKETCHPLUGIN_EXPORT virtual int numberOfSubs(bool forTree = false) const;
195
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);
199
200   /// Returns the sub-feature unique identifier in this composite feature by index
201   SKETCHPLUGIN_EXPORT virtual int subFeatureId(const int theIndex) const;
202
203   /// Returns true if feature or reuslt belong to this composite feature as subs
204   SKETCHPLUGIN_EXPORT virtual bool isSub(ObjectPtr theObject) const;
205
206   SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID);
207
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);
216
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);
226
227   /// Creates a plane of the sketch.
228   /// \param theSketch a sketch intance
229   static std::shared_ptr<GeomAPI_Ax3> plane(SketchPlugin_Sketch* theSketch);
230
231   /// Customize presentation of the feature
232   virtual bool customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
233                                      std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
234   {
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;
241
242     return isCustomized;
243   }
244
245 };
246
247 #endif