Salome HOME
Fixed crash when attribute External in feature Projection changed.
[modules/shaper.git] / src / ModelAPI / ModelAPI_Feature.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModelAPI_Feature.hxx
4 // Created:     21 Mar 2014
5 // Author:      Mikhail PONIKAROV
6
7 #ifndef ModelAPI_Feature_H_
8 #define ModelAPI_Feature_H_
9
10 #include <ModelAPI.h>
11 #include <ModelAPI_Object.h>
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_Document.h>
14
15 #include <memory>
16
17 #include <list>
18 #include <string>
19
20 /**\class ModelAPI_Feature
21  * \ingroup DataModel
22  * \brief Feature function that represents the particular functionality
23  * of this operation. Produces results by the arguments.
24  */
25 class ModelAPI_Feature : public ModelAPI_Object
26 {
27   ///< list of current results of this feature
28   std::list<std::shared_ptr<ModelAPI_Result> > myResults;
29   ///< is feature disabled or not
30   bool myIsDisabled;
31   ///< is feature is stable (not editing)
32   bool myIsStable;
33
34  public:
35   /// Returns the unique kind of a feature (like "Point")
36   virtual const std::string& getKind() = 0;
37
38   /// Returns the group identifier of all features
39   inline static std::string group()
40   {
41     static std::string MY_GROUP = "Features";
42     return MY_GROUP;
43   }
44
45   /// Returns document this feature belongs to
46   virtual std::shared_ptr<ModelAPI_Document> document() const
47   {
48     return ModelAPI_Object::document();
49   }
50
51   /// Returns the group identifier of this result
52   virtual std::string groupName()
53   {
54     return group();
55   }
56
57   /// Computes or recomputes the results
58   virtual void execute() = 0;
59
60   /// Computes the attribute value on the base of other attributes if the value can be computed
61   /// \param theAttributeId an attribute index to be computed
62   /// \return a boolean value about it is computed
63   virtual bool compute(const std::string& theAttributeId) { return false; };
64
65   /// Registers error during the execution, causes the ExecutionFailed state
66   MODELAPI_EXPORT virtual void setError(const std::string& theError,
67                                         bool isSend = true,
68                                         bool isTranslate = true);
69
70   /// Returns error, arose during the execution
71   virtual std::string error() const {
72     return data()->error();
73   }
74
75   /// returns the current results of the feature
76   MODELAPI_EXPORT const std::list<std::shared_ptr<ModelAPI_Result> >& results();
77   /// returns the first result in the list or NULL reference
78   MODELAPI_EXPORT std::shared_ptr<ModelAPI_Result> firstResult() const;
79   /// returns the last result in the list or NULL reference
80   MODELAPI_EXPORT std::shared_ptr<ModelAPI_Result> lastResult();
81   /// sets the alone result
82   MODELAPI_EXPORT void setResult(const std::shared_ptr<ModelAPI_Result>& theResult);
83   /// sets the result by index (zero based), results before this must be set before
84   MODELAPI_EXPORT void setResult(const std::shared_ptr<ModelAPI_Result>& theResult,
85                                  const int theIndex);
86   /// removes the result from the feature
87   MODELAPI_EXPORT void removeResult(const std::shared_ptr<ModelAPI_Result>& theResult);
88   /// removes all results starting from the given index (zero-based)
89   /// \param theSinceIndex - index of the deleted result and all after also will be deleted
90   /// \param theForever - removes results for long period (not just because of feature disabling)
91   /// \param theFlush - if it is false, REDISPLAY message is not flushed
92   MODELAPI_EXPORT void removeResults(const int theSinceIndex,
93     const bool theForever = true, const bool theFlush = true);
94   /// removes all results from the feature
95   MODELAPI_EXPORT void eraseResults(const bool theForever = true);
96   /// removes all fields from this feature: results, data, etc
97   MODELAPI_EXPORT virtual void erase();
98   /// removes the result from the list of feature (not doing in disabled): normally this
99   /// method is not used from features. only internally
100   MODELAPI_EXPORT void eraseResultFromList(const std::shared_ptr<ModelAPI_Result>& theResult);
101
102   /// Returns true if result is persistent (stored in document) and on undo-redo, save-open
103   /// it is not needed to recompute it.
104   virtual bool isPersistentResult() {return true;}
105
106   /// Returns true if this feature must not be created: this is just an action
107   /// that is not stored in the features history and data model (like "delete part").
108   virtual bool isAction()
109   {
110     return false;
111   }
112
113   /// Returns true if this feature is used as macro: creates other features and then removed.
114   /// \returns false by default
115   MODELAPI_EXPORT virtual bool isMacro() const;
116
117   /// Returns true if preview update during the edition needed. Otherwise the update-mechanism
118   /// calls the \a execute function only on "apply" of the operation
119   /// \returns true by default
120   MODELAPI_EXPORT virtual bool isPreviewNeeded() const;
121
122   /// Must return document where the new feature must be added to
123   /// By default it is empty: it is added to the document this method is called to
124   MODELAPI_EXPORT virtual const std::string& documentToAdd();
125
126   /// Enables/disables the feature. The disabled feature has no results and does not participate in
127   /// any calculation.
128   /// \returns true if state is really changed
129   MODELAPI_EXPORT virtual bool setDisabled(const bool theFlag);
130
131   /// Returns the feature is disabled or not.
132   MODELAPI_EXPORT virtual bool isDisabled();
133
134   /// To virtually destroy the fields of successors
135   MODELAPI_EXPORT virtual ~ModelAPI_Feature();
136
137   /// Returns the feature by the object (result).
138   MODELAPI_EXPORT static std::shared_ptr<ModelAPI_Feature> feature(ObjectPtr theObject);
139
140   /// Set the stable feature flag. If feature is currently editing then it is not stable.
141   /// \returns true if state is really changed
142   MODELAPI_EXPORT virtual bool setStable(const bool theFlag);
143
144   /// Returns the feature is stable or not.
145   MODELAPI_EXPORT virtual bool isStable();
146
147   /// Performs some custom feature specific functionality (normally called by some GUI button)
148   /// \param theActionId an action key
149   /// \return a boolean value about it is performed
150   MODELAPI_EXPORT virtual bool customAction(const std::string& theActionId);
151
152  //
153  // Helper methods, aliases for data()->method()
154  // -----------------------------------------------------------------------------------------------
155   /// Returns the name stored in the attribute
156   inline std::string name()
157   {
158     return data()->name();
159   }
160   /// Returns the Boolean attribute by the identifier
161   inline std::shared_ptr<ModelAPI_AttributeBoolean> boolean(const std::string& theID)
162   {
163     return data()->boolean(theID);
164   }
165   /// Returns the document reference attribute
166   inline std::shared_ptr<ModelAPI_AttributeDocRef> document(const std::string& theID)
167   {
168     return data()->document(theID);
169   }
170   /// Returns the real attribute by the identifier
171   inline std::shared_ptr<ModelAPI_AttributeDouble> real(const std::string& theID)
172   {
173     return data()->real(theID);
174   }
175   /// Returns the integer attribute by the identifier
176   inline std::shared_ptr<ModelAPI_AttributeInteger> integer(const std::string& theID)
177   {
178     return data()->integer(theID);
179   }
180   /// Returns the integer array attribute by the identifier
181   inline std::shared_ptr<ModelAPI_AttributeIntArray> intArray(const std::string& theID)
182   {
183     return data()->intArray(theID);
184   }
185   /// Returns the reference attribute by the identifier
186   inline std::shared_ptr<ModelAPI_AttributeRefAttr> refattr(const std::string& theID)
187   {
188     return data()->refattr(theID);
189   }
190   /// Returns the reference attribute by the identifier
191   inline std::shared_ptr<ModelAPI_AttributeReference> reference(const std::string& theID)
192   {
193     return data()->reference(theID);
194   }
195   /// Returns the list of references attribute by the identifier
196   inline std::shared_ptr<ModelAPI_AttributeRefList> reflist(const std::string& theID)
197   {
198     return data()->reflist(theID);
199   }
200   /// Returns the shape selection attribute by the identifier
201   inline std::shared_ptr<ModelAPI_AttributeSelection> selection(const std::string& theID)
202   {
203     return data()->selection(theID);
204   }
205   /// Returns the list of shape selections attribute by the identifier
206   inline std::shared_ptr<ModelAPI_AttributeSelectionList> selectionList(const std::string& theID)
207   {
208     return data()->selectionList(theID);
209   }
210   /// Returns the string attribute by the identifier
211   inline std::shared_ptr<ModelAPI_AttributeString> string(const std::string& theID)
212   {
213     return data()->string(theID);
214   }
215   /// Returns the string array attribute by the identifier
216   inline std::shared_ptr<ModelAPI_AttributeStringArray> stringArray(const std::string& theID)
217   {
218     return data()->stringArray(theID);
219   }
220   /// Returns the tables attribute by the identifier
221   inline std::shared_ptr<ModelAPI_AttributeTables> tables(const std::string& theID)
222   {
223     return data()->tables(theID);
224   }
225   /// Returns the attribute by the identifier
226   inline std::shared_ptr<ModelAPI_Attribute> attribute(const std::string& theID)
227   {
228     return data()->attribute(theID);
229   }
230   protected:
231   /// This method is called just after creation of the object: it must initialize
232   /// all fields, normally initialized in the constructor
233   MODELAPI_EXPORT virtual void init();
234
235   friend class Model_Document;
236   friend class Model_Objects;
237 };
238
239 //! Pointer on feature object
240 typedef std::shared_ptr<ModelAPI_Feature> FeaturePtr;
241
242 #endif
243