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