Salome HOME
createPoint2DResult() to be used in Circle/Translation/Rotation objects. May be Sketc...
[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_Ax3.h>
16 #include <GeomAPI_XYZ.h>
17 #include <GeomDataAPI_Point.h>
18 #include <GeomDataAPI_Dir.h>
19 #include <list>
20
21 #define YZ_PLANE_COLOR "#ff0000"
22 #define XZ_PLANE_COLOR "#00ff00"
23 #define XY_PLANE_COLOR "#0000ff"
24
25 /**\class SketchPlugin_Sketch
26  * \ingroup Plugins
27  * \brief Feature for creation of the new part in PartSet.
28  */
29 class SketchPlugin_Sketch : public ModelAPI_CompositeFeature//, public GeomAPI_IPresentable
30 {
31  public:
32   /// Sketch feature kind
33   inline static const std::string& ID()
34   {
35     static const std::string MY_SKETCH_ID("Sketch");
36     return MY_SKETCH_ID;
37   }
38   /// Origin point of the sketcher in 3D space
39   inline static const std::string& ORIGIN_ID()
40   {
41     static const std::string MY_ORIGIN_ID("Origin");
42     return MY_ORIGIN_ID;
43   }
44   /// Vector X inside of the sketch plane
45   inline static const std::string& DIRX_ID()
46   {
47     static const std::string MY_DIRX_ID("DirX");
48     return MY_DIRX_ID;
49   }
50   /// Vector Z, normal to the sketch plane
51   inline static const std::string& NORM_ID()
52   {
53     static const std::string MY_NORM_ID("Norm");
54     return MY_NORM_ID;
55   }
56   /// All features of this sketch (list of references)
57   inline static const std::string& FEATURES_ID()
58   {
59     static const std::string MY_FEATURES_ID("Features");
60     return MY_FEATURES_ID;
61   }
62
63   /// Returns the kind of a feature
64   SKETCHPLUGIN_EXPORT virtual const std::string& getKind()
65   {
66     static std::string MY_KIND = SketchPlugin_Sketch::ID();
67     return MY_KIND;
68   }
69
70   /// Creates a new part document if needed
71   SKETCHPLUGIN_EXPORT virtual void execute();
72
73   /// Request for initialization of data model of the feature: adding all attributes
74   SKETCHPLUGIN_EXPORT virtual void initAttributes();
75
76   /// Moves the feature
77   /// \param theDeltaX the delta for X coordinate is moved
78   /// \param theDeltaY the delta for Y coordinate is moved
79   SKETCHPLUGIN_EXPORT virtual void move(const double theDeltaX, const double theDeltaY)
80   {
81   }
82
83   /// Return the distance between the feature and the point
84   /// \param thePoint the point
85   virtual double distanceToPoint(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
86   {
87     return 0;
88   }
89
90   /// Converts a 2D sketch space point into point in 3D space
91   /// \param theX an X coordinate
92   /// \param theY an Y coordinate
93   std::shared_ptr<GeomAPI_Pnt> to3D(const double theX, const double theY) const
94   {
95     std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
96         data()->attribute(ORIGIN_ID()));
97     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
98         data()->attribute(NORM_ID()));
99     std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
100         data()->attribute(DIRX_ID()));
101     std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
102
103     std::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
104         ->added(aY->xyz()->multiplied(theY));
105
106     return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
107   }
108   
109   /// Returns the point projected into the sketch plane
110   /// \param thePnt a source 3d point
111   std::shared_ptr<GeomAPI_Pnt2d> to2D(const std::shared_ptr<GeomAPI_Pnt>& thePnt) const
112   {
113     std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
114         data()->attribute(ORIGIN_ID()));
115     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
116         data()->attribute(NORM_ID()));
117     std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
118         data()->attribute(DIRX_ID()));
119     std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
120     return thePnt->to2D(aC->pnt(), aX->dir(), aY);
121   }
122
123   /// Returns true if this feature must be displayed in the history (top level of Part tree)
124   SKETCHPLUGIN_EXPORT virtual bool isInHistory()
125   {
126     return true;
127   }
128
129   /// Use plugin manager for features creation
130   SketchPlugin_Sketch();
131
132   /// Returns the basis plane for the sketch
133   std::shared_ptr<GeomAPI_Pln> plane() const
134   {
135     std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
136         data()->attribute(ORIGIN_ID()));
137     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
138         data()->attribute(NORM_ID()));
139
140     if (!anOrigin || !aNorm)
141       return std::shared_ptr<GeomAPI_Pln>();
142
143     return std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
144   }
145
146   /// Returns currently defined plane as an object of Ax3
147   std::shared_ptr<GeomAPI_Ax3> coordinatePlane() const
148   {
149     DataPtr aData = data();
150     std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
151       aData->attribute(ORIGIN_ID()));
152     std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
153       aData->attribute(DIRX_ID()));
154     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
155       aData->attribute(NORM_ID()));
156
157     return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(aC->pnt(), aX->dir(), aNorm->dir()));
158   }
159
160   /// Checks whether the plane is set in the sketch.
161   /// \returns the boolean state
162   bool isPlaneSet() const
163   {
164     std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
165         data()->attribute(NORM_ID()));
166
167     return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
168   }
169
170
171   /// removes also all sub-sketch elements
172   SKETCHPLUGIN_EXPORT virtual void erase();
173
174   /// appends a feature to the sketch sub-elements container
175   SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature> addFeature(std::string theID);
176
177   /// Just to synchronise the container of sub-features
178   virtual void removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature);
179
180   /// Returns the number of sub-elements
181   SKETCHPLUGIN_EXPORT virtual int numberOfSubs() const;
182
183   /// Returns the sub-feature by zero-base index
184   SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature> 
185     subFeature(const int theIndex) const;
186
187   /// Returns the sub-feature unique identifier in this composite feature by zero-base index
188   SKETCHPLUGIN_EXPORT virtual int subFeatureId(const int theIndex) const;
189
190   /// Returns true if feature or reuslt belong to this composite feature as subs
191   SKETCHPLUGIN_EXPORT virtual bool isSub(ObjectPtr theObject) const;
192
193   /// Construction result is allways recomuted on the fly
194   SKETCHPLUGIN_EXPORT virtual bool isPersistentResult() {return false;}
195
196   SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID);
197
198   /// \brief Create a result for the point in the attribute if the attribute is initialized
199   /// \param theAttributeID an attribute string
200   /// \param theIndex an index of the result
201   static void createPoint2DResult(ModelAPI_Feature* theFeature,
202                                   SketchPlugin_Sketch* theSketch,
203                                   const std::string& theAttributeID, const int theIndex);
204 };
205
206 #endif