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