Salome HOME
Issue #2109 trim - wrong result due to constarints
[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 #ifdef SET_PLANES_COLOR_IN_PREFERENCES
24   #define YZ_PLANE_COLOR "225,0,0"
25   #define XZ_PLANE_COLOR "0,225,0"
26   #define XY_PLANE_COLOR "0,0,225"
27 #endif
28
29 /**\class SketchPlugin_Sketch
30  * \ingroup Plugins
31  * \brief Feature for creation of the new part in PartSet.
32  */
33 class SketchPlugin_Sketch : public ModelAPI_CompositeFeature, public GeomAPI_ICustomPrs
34 {
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   /// Sketch solver error
67   inline static const std::string& SOLVER_ERROR()
68   {
69     static const std::string MY_SOLVER_ERROR("SolverError");
70     return MY_SOLVER_ERROR;
71   }
72
73   /// Sketch solver error
74   inline static const std::string& SOLVER_DOF()
75   {
76     static const std::string MY_SOLVER_DOF("SolverDOF");
77     return MY_SOLVER_DOF;
78   }
79
80   /// Returns the kind of a feature
81   SKETCHPLUGIN_EXPORT virtual const std::string& getKind()
82   {
83     static std::string MY_KIND = SketchPlugin_Sketch::ID();
84     return MY_KIND;
85   }
86
87   /// Creates a new part document if needed
88   SKETCHPLUGIN_EXPORT virtual void execute();
89
90   /// Request for initialization of data model of the feature: adding all attributes
91   SKETCHPLUGIN_EXPORT virtual void initAttributes();
92
93   /// Moves the feature
94   /// \param theDeltaX the delta for X coordinate is moved
95   /// \param theDeltaY the delta for Y coordinate is moved
96   SKETCHPLUGIN_EXPORT virtual void move(const double theDeltaX, const double theDeltaY)
97   {
98   }
99
100   /// Converts a 2D sketch space point into point in 3D space
101   /// \param theX an X coordinate
102   /// \param theY an Y coordinate
103   std::shared_ptr<GeomAPI_Pnt> to3D(const double theX, const double theY) const
104   {
105     std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
106         data()->attribute(ORIGIN_ID()));
107     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
108         data()->attribute(NORM_ID()));
109     std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
110         data()->attribute(DIRX_ID()));
111     std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
112
113     std::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
114         ->added(aY->xyz()->multiplied(theY));
115
116     return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
117   }
118
119   /// Returns the point projected into the sketch plane
120   /// \param thePnt a source 3d point
121   std::shared_ptr<GeomAPI_Pnt2d> to2D(const std::shared_ptr<GeomAPI_Pnt>& thePnt) const
122   {
123     std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
124         data()->attribute(ORIGIN_ID()));
125     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
126         data()->attribute(NORM_ID()));
127     std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
128         data()->attribute(DIRX_ID()));
129     std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
130     return thePnt->to2D(aC->pnt(), aX->dir(), aY);
131   }
132
133   /// Returns true if this feature must be displayed in the history (top level of Part tree)
134   SKETCHPLUGIN_EXPORT virtual bool isInHistory()
135   {
136     return true;
137   }
138
139   /// Use plugin manager for features creation
140   SketchPlugin_Sketch();
141
142   /// Returns the basis plane for the sketch
143   std::shared_ptr<GeomAPI_Pln> plane() const
144   {
145     std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
146         data()->attribute(ORIGIN_ID()));
147     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
148         data()->attribute(NORM_ID()));
149
150     if (!anOrigin || !aNorm)
151       return std::shared_ptr<GeomAPI_Pln>();
152
153     return std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
154   }
155
156   /// Returns currently defined plane as an object of Ax3
157   std::shared_ptr<GeomAPI_Ax3> coordinatePlane() const
158   {
159     DataPtr aData = data();
160     std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
161       aData->attribute(ORIGIN_ID()));
162     std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
163       aData->attribute(DIRX_ID()));
164     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
165       aData->attribute(NORM_ID()));
166
167     return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(aC->pnt(), aX->dir(), aNorm->dir()));
168   }
169
170   /// Checks whether the plane is set in the sketch.
171   /// \returns the boolean state
172   bool isPlaneSet() const
173   {
174     std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
175         data()->attribute(NORM_ID()));
176
177     return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
178   }
179
180
181   /// appends a feature to the sketch sub-elements container
182   SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature> addFeature(std::string theID);
183
184   /// Just to synchronise the container of sub-features
185   virtual void removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature);
186
187   /// Returns the number of sub-elements
188   SKETCHPLUGIN_EXPORT virtual int numberOfSubs(bool forTree = false) const;
189
190   /// Returns the sub-feature by zero-base index
191   SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature>
192     subFeature(const int theIndex, bool forTree = false);
193
194   /// Returns the sub-feature unique identifier in this composite feature by index
195   SKETCHPLUGIN_EXPORT virtual int subFeatureId(const int theIndex) const;
196
197   /// Returns true if feature or reuslt belong to this composite feature as subs
198   SKETCHPLUGIN_EXPORT virtual bool isSub(ObjectPtr theObject) const;
199
200   /// Construction result is allways recomuted on the fly
201   SKETCHPLUGIN_EXPORT virtual bool isPersistentResult() {return false;}
202
203   SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID);
204
205   /// Exchanges IDs of two given features: needed for fillet feature better naming (issue 769)
206   SKETCHPLUGIN_EXPORT virtual void exchangeIDs(std::shared_ptr<ModelAPI_Feature> theFeature1,
207     std::shared_ptr<ModelAPI_Feature> theFeature2);
208
209
210   /// \brief Create a result for the point in the attribute if the attribute is initialized
211   /// \param theFeature a source feature
212   /// \param theSketch a sketch intance
213   /// \param theAttributeID an attribute string
214   /// \param theIndex an index of the result
215   static void createPoint2DResult(ModelAPI_Feature* theFeature,
216                                   SketchPlugin_Sketch* theSketch,
217                                   const std::string& theAttributeID, const int theIndex);
218
219   /// Add new feature and fill the data of the feature by the data of the parameter feature.
220   /// The name of the created feature stays unique.
221   /// \param theFeature a source feature
222   /// \param theSketch a sketch intance
223   /// \param theIsCopy if true sets feature copy attribute to true.
224   /// \return a created feature
225   static FeaturePtr addUniqueNamedCopiedFeature(FeaturePtr theFeature,
226                                                 SketchPlugin_Sketch* theSketch,
227                                                 const bool theIsCopy = false);
228
229   /// Creates a plane of the sketch.
230   /// \param theSketch a sketch intance
231   static std::shared_ptr<GeomAPI_Ax3> plane(SketchPlugin_Sketch* theSketch);
232
233   /// Customize presentation of the feature
234   virtual bool customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
235                                      std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
236   {
237     bool isCustomized = false;
238     // apply the color of the result to the presentation
239     if (theDefaultPrs.get())
240       isCustomized = theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
241     // set the sketch presentation bold
242     isCustomized = thePrs->setWidth(2) || isCustomized;
243
244     return isCustomized;
245   }
246
247 };
248
249 #endif