]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Sketch.h
Salome HOME
aadbe9caf47a04a25e689740bd394a0bd8682a88
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Sketch.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        SketchPlugin_Sketch.h
4 // Created:     27 Mar 2014
5 // Author:      Mikhail PONIKAROV
6
7 #ifndef SketchPlugin_Sketch_H_
8 #define SketchPlugin_Sketch_H_
9
10 #include "SketchPlugin.h"
11 #include <SketchPlugin_Feature.h>
12 #include <GeomAPI_Pnt.h>
13 #include <GeomAPI_Pln.h>
14 #include <GeomAPI_IPresentable.h>
15 #include <GeomAPI_ICustomPrs.h>
16
17 #include <GeomAPI_Ax3.h>
18 #include <GeomAPI_XYZ.h>
19 #include <GeomDataAPI_Point.h>
20 #include <GeomDataAPI_Dir.h>
21 #include <list>
22
23 #define YZ_PLANE_COLOR "225,0,0"
24 #define XZ_PLANE_COLOR "0,225,0"
25 #define XY_PLANE_COLOR "0,0,225"
26
27 /**\class SketchPlugin_Sketch
28  * \ingroup Plugins
29  * \brief Feature for creation of the new part in PartSet.
30  */
31 class SketchPlugin_Sketch : public ModelAPI_CompositeFeature, public GeomAPI_ICustomPrs//, public GeomAPI_IPresentable
32 {
33   /// Optimization: indexes of sketch sub-elements are persistent, so, no problems in synchronization.
34   std::map<int, std::shared_ptr<ModelAPI_Feature> > mySubs;
35  public:
36   /// Sketch feature kind
37   inline static const std::string& ID()
38   {
39     static const std::string MY_SKETCH_ID("Sketch");
40     return MY_SKETCH_ID;
41   }
42   /// Origin point of the sketcher in 3D space
43   inline static const std::string& ORIGIN_ID()
44   {
45     static const std::string MY_ORIGIN_ID("Origin");
46     return MY_ORIGIN_ID;
47   }
48   /// Vector X inside of the sketch plane
49   inline static const std::string& DIRX_ID()
50   {
51     static const std::string MY_DIRX_ID("DirX");
52     return MY_DIRX_ID;
53   }
54   /// Vector Z, normal to the sketch plane
55   inline static const std::string& NORM_ID()
56   {
57     static const std::string MY_NORM_ID("Norm");
58     return MY_NORM_ID;
59   }
60   /// All features of this sketch (list of references)
61   inline static const std::string& FEATURES_ID()
62   {
63     static const std::string MY_FEATURES_ID("Features");
64     return MY_FEATURES_ID;
65   }
66
67   /// Returns the kind of a feature
68   SKETCHPLUGIN_EXPORT virtual const std::string& getKind()
69   {
70     static std::string MY_KIND = SketchPlugin_Sketch::ID();
71     return MY_KIND;
72   }
73
74   /// Creates a new part document if needed
75   SKETCHPLUGIN_EXPORT virtual void execute();
76
77   /// Request for initialization of data model of the feature: adding all attributes
78   SKETCHPLUGIN_EXPORT virtual void initAttributes();
79
80   /// Moves the feature
81   /// \param theDeltaX the delta for X coordinate is moved
82   /// \param theDeltaY the delta for Y coordinate is moved
83   SKETCHPLUGIN_EXPORT virtual void move(const double theDeltaX, const double theDeltaY)
84   {
85   }
86
87   /// Converts a 2D sketch space point into point in 3D space
88   /// \param theX an X coordinate
89   /// \param theY an Y coordinate
90   std::shared_ptr<GeomAPI_Pnt> to3D(const double theX, const double theY) const
91   {
92     std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
93         data()->attribute(ORIGIN_ID()));
94     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
95         data()->attribute(NORM_ID()));
96     std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
97         data()->attribute(DIRX_ID()));
98     std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
99
100     std::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
101         ->added(aY->xyz()->multiplied(theY));
102
103     return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
104   }
105   
106   /// Returns the point projected into the sketch plane
107   /// \param thePnt a source 3d point
108   std::shared_ptr<GeomAPI_Pnt2d> to2D(const std::shared_ptr<GeomAPI_Pnt>& thePnt) const
109   {
110     std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
111         data()->attribute(ORIGIN_ID()));
112     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
113         data()->attribute(NORM_ID()));
114     std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
115         data()->attribute(DIRX_ID()));
116     std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
117     return thePnt->to2D(aC->pnt(), aX->dir(), aY);
118   }
119
120   /// Returns true if this feature must be displayed in the history (top level of Part tree)
121   SKETCHPLUGIN_EXPORT virtual bool isInHistory()
122   {
123     return true;
124   }
125
126   /// Use plugin manager for features creation
127   SketchPlugin_Sketch();
128
129   /// Returns the basis plane for the sketch
130   std::shared_ptr<GeomAPI_Pln> plane() const
131   {
132     std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
133         data()->attribute(ORIGIN_ID()));
134     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
135         data()->attribute(NORM_ID()));
136
137     if (!anOrigin || !aNorm)
138       return std::shared_ptr<GeomAPI_Pln>();
139
140     return std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
141   }
142
143   /// Returns currently defined plane as an object of Ax3
144   std::shared_ptr<GeomAPI_Ax3> coordinatePlane() const
145   {
146     DataPtr aData = data();
147     std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
148       aData->attribute(ORIGIN_ID()));
149     std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
150       aData->attribute(DIRX_ID()));
151     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
152       aData->attribute(NORM_ID()));
153
154     return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(aC->pnt(), aX->dir(), aNorm->dir()));
155   }
156
157   /// Checks whether the plane is set in the sketch.
158   /// \returns the boolean state
159   bool isPlaneSet() const
160   {
161     std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
162         data()->attribute(NORM_ID()));
163
164     return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
165   }
166
167
168   /// removes also all sub-sketch elements
169   SKETCHPLUGIN_EXPORT virtual void erase();
170
171   /// appends a feature to the sketch sub-elements container
172   SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature> addFeature(std::string theID);
173
174   /// Just to synchronise the container of sub-features
175   virtual void removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature);
176
177   /// Returns the number of sub-elements
178   SKETCHPLUGIN_EXPORT virtual int numberOfSubs(bool forTree = false) const;
179
180   /// Returns the sub-feature by zero-base index
181   SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature> 
182     subFeature(const int theIndex, bool forTree = false);
183
184   /// Returns the sub-feature unique identifier in this composite feature by index
185   SKETCHPLUGIN_EXPORT virtual int subFeatureId(const int theIndex) const;
186
187   /// Returns true if feature or reuslt belong to this composite feature as subs
188   SKETCHPLUGIN_EXPORT virtual bool isSub(ObjectPtr theObject) const;
189
190   /// Construction result is allways recomuted on the fly
191   SKETCHPLUGIN_EXPORT virtual bool isPersistentResult() {return false;}
192
193   SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID);
194
195   /// \brief Create a result for the point in the attribute if the attribute is initialized
196   /// \param theAttributeID an attribute string
197   /// \param theIndex an index of the result
198   static void createPoint2DResult(ModelAPI_Feature* theFeature,
199                                   SketchPlugin_Sketch* theSketch,
200                                   const std::string& theAttributeID, const int theIndex);
201   
202   /// Add new feature and fill the data of the feature by the data of the parameter feature.
203   /// The name of the created feature stays unique.
204   /// \param theFeature a source feature
205   /// \return a created feature
206   static FeaturePtr addUniqueNamedCopiedFeature(FeaturePtr aFeature,
207                                                 SketchPlugin_Sketch* theSketch);
208
209   /// Creates a plane of the sketch.
210   /// \param theSketch a sketch intance
211   static std::shared_ptr<GeomAPI_Ax3> plane(SketchPlugin_Sketch* theSketch);
212
213   /// Customize presentation of the feature
214   virtual bool customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
215                                      std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
216   {
217     bool isCustomized = false;
218     // apply the color of the result to the presentation
219     if (theDefaultPrs.get())
220       isCustomized = theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
221     // set the sketch presentation bold    
222     isCustomized = thePrs->setWidth(2) || isCustomized;
223   
224     return isCustomized;
225   }
226
227 };
228
229 #endif