Salome HOME
Dimensions move using ModelAPI_ObjectMovedMessage mechanism. Move by deltas is obsole...
[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   /// 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
111   {
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())));
119
120     std::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
121         ->added(aY->xyz()->multiplied(theY));
122
123     return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
124   }
125
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
129   {
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);
138   }
139
140   /// Returns true if this feature must be displayed in the history (top level of Part tree)
141   SKETCHPLUGIN_EXPORT virtual bool isInHistory()
142   {
143     return true;
144   }
145
146   /// Use plugin manager for features creation
147   SketchPlugin_Sketch();
148
149   /// Returns the basis plane for the sketch
150   std::shared_ptr<GeomAPI_Pln> plane() const
151   {
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()));
156
157     if (!anOrigin || !aNorm)
158       return std::shared_ptr<GeomAPI_Pln>();
159
160     return std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
161   }
162
163   /// Returns currently defined plane as an object of Ax3
164   std::shared_ptr<GeomAPI_Ax3> coordinatePlane() const
165   {
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()));
173
174     return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(aC->pnt(), aX->dir(), aNorm->dir()));
175   }
176
177   /// Checks whether the plane is set in the sketch.
178   /// \returns the boolean state
179   bool isPlaneSet() const
180   {
181     std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
182         data()->attribute(NORM_ID()));
183
184     return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
185   }
186
187
188   /// appends a feature to the sketch sub-elements container
189   SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature> addFeature(std::string theID);
190
191   /// Just to synchronise the container of sub-features
192   virtual void removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature);
193
194   /// Returns the number of sub-elements
195   SKETCHPLUGIN_EXPORT virtual int numberOfSubs(bool forTree = false) const;
196
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);
200
201   /// Returns the sub-feature unique identifier in this composite feature by index
202   SKETCHPLUGIN_EXPORT virtual int subFeatureId(const int theIndex) const;
203
204   /// Returns true if feature or reuslt belong to this composite feature as subs
205   SKETCHPLUGIN_EXPORT virtual bool isSub(ObjectPtr theObject) const;
206
207   /// Construction result is allways recomuted on the fly
208   SKETCHPLUGIN_EXPORT virtual bool isPersistentResult() {return false;}
209
210   SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID);
211
212   /// Exchanges IDs of two given features: needed for fillet feature better naming (issue 769)
213   SKETCHPLUGIN_EXPORT virtual void exchangeIDs(std::shared_ptr<ModelAPI_Feature> theFeature1,
214     std::shared_ptr<ModelAPI_Feature> theFeature2);
215
216
217   /// \brief Create a result for the point in the attribute if the attribute is initialized
218   /// \param theFeature a source feature
219   /// \param theSketch a sketch intance
220   /// \param theAttributeID an attribute string
221   /// \param theIndex an index of the result
222   static void createPoint2DResult(ModelAPI_Feature* theFeature,
223                                   SketchPlugin_Sketch* theSketch,
224                                   const std::string& theAttributeID, const int theIndex);
225
226   /// Add new feature and fill the data of the feature by the data of the parameter feature.
227   /// The name of the created feature stays unique.
228   /// \param theFeature a source feature
229   /// \param theSketch a sketch intance
230   /// \param theIsCopy if true sets feature copy attribute to true.
231   /// \return a created feature
232   static FeaturePtr addUniqueNamedCopiedFeature(FeaturePtr theFeature,
233                                                 SketchPlugin_Sketch* theSketch,
234                                                 const bool theIsCopy = false);
235
236   /// Creates a plane of the sketch.
237   /// \param theSketch a sketch intance
238   static std::shared_ptr<GeomAPI_Ax3> plane(SketchPlugin_Sketch* theSketch);
239
240   /// Customize presentation of the feature
241   virtual bool customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
242                                      std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
243   {
244     bool isCustomized = false;
245     // apply the color of the result to the presentation
246     if (theDefaultPrs.get())
247       isCustomized = theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
248     // set the sketch presentation bold
249     isCustomized = thePrs->setWidth(2) || isCustomized;
250
251     return isCustomized;
252   }
253
254 };
255
256 #endif