Salome HOME
436a1adbfec81f98050d257a3084e5993634eafe
[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  public:
32   /// Returns the unique kind of a feature (like "Point")
33   virtual const std::string& getKind() = 0;
34
35   /// Returns the group identifier of all features
36   inline static std::string group()
37   {
38     static std::string MY_GROUP = "Features";
39     return MY_GROUP;
40   }
41
42   /// Returns document this feature belongs to
43   virtual std::shared_ptr<ModelAPI_Document> document() const
44   {
45     return ModelAPI_Object::document();
46   }
47
48   /// Returns the group identifier of this result
49   virtual std::string groupName()
50   {
51     return group();
52   }
53
54   /// Computes or recomputes the results
55   virtual void execute() = 0;
56
57   /// Computes the attribute value on the base of other attributes if the value can be computed
58   /// \param theAttributeId an attribute index to be computed
59   /// \return a boolean value about it is computed
60   virtual bool compute(const std::string& theAttributeId) { return false; };
61
62   /// Registers error during the execution, causes the ExecutionFailed state
63   virtual void setError(const std::string& theError, bool isSend = true) {
64     data()->setError(theError, isSend);
65   }
66
67   /// Returns error, arose during the execution
68   virtual std::string error() const {
69     return data()->error();
70   }
71
72   /// returns the current results of the feature
73   MODELAPI_EXPORT const std::list<std::shared_ptr<ModelAPI_Result> >& results();
74   /// returns the first result in the list or NULL reference
75   MODELAPI_EXPORT std::shared_ptr<ModelAPI_Result> firstResult();
76   /// returns the last result in the list or NULL reference
77   MODELAPI_EXPORT std::shared_ptr<ModelAPI_Result> lastResult();
78   /// sets the alone result
79   MODELAPI_EXPORT void setResult(const std::shared_ptr<ModelAPI_Result>& theResult);
80   /// sets the result by index (zero based), results before this must be set before
81   MODELAPI_EXPORT void setResult(const std::shared_ptr<ModelAPI_Result>& theResult,
82                                  const int theIndex);
83   /// removes the result from the feature
84   MODELAPI_EXPORT void removeResult(const std::shared_ptr<ModelAPI_Result>& theResult);
85   /// removes all results starting from the gived index (zero-based)
86   /// \param theSinceIndex - index of the deleted result and all after also will be deleted
87   /// \param theFlush - if it is false, REDISPLAY message is not flushed
88   MODELAPI_EXPORT void removeResults(const int theSinceIndex, const bool theFlush = true);
89   /// removes all results from the feature
90   MODELAPI_EXPORT void eraseResults();
91   /// removes all fields from this feature: results, data, etc
92   MODELAPI_EXPORT virtual void erase();
93
94   /// Returns true if result is persistent (stored in document) and on undo-redo, save-open
95   /// it is not needed to recompute it.
96   virtual bool isPersistentResult() {return true;}
97
98   /// Returns true if this feature must not be created: this is just an action
99   /// that is not stored in the features history and data model (like "delete part").
100   virtual bool isAction()
101   {
102     return false;
103   }
104
105   /// Returns true if this feature is used as macro: creates other features and then removed.
106   /// \returns false by default
107   MODELAPI_EXPORT virtual bool isMacro() const;
108
109   /// Returns true if preview update during the edition needed. Otherwise the update-mechanism
110   /// calls the \a execute function only on "apply" of the operation
111   /// \returns true by default
112   MODELAPI_EXPORT virtual bool isPreviewNeeded() const;
113
114   /// Must return document where the new feature must be added to
115   /// By default it is empty: it is added to the document this method is called to
116   MODELAPI_EXPORT virtual const std::string& documentToAdd();
117
118   /// Enables/disables the feature. The disabled feature has no results and does not participate in
119   /// any calculation.
120   /// \returns true if state is really changed
121   MODELAPI_EXPORT virtual bool setDisabled(const bool theFlag);
122
123   /// Returns the feature is disabled or not.
124   MODELAPI_EXPORT virtual bool isDisabled() const;
125
126   /// To virtually destroy the fields of successors
127   MODELAPI_EXPORT virtual ~ModelAPI_Feature();
128
129   /// Returns the feature by the object (result).
130   MODELAPI_EXPORT static std::shared_ptr<ModelAPI_Feature> feature(ObjectPtr theObject);
131
132  //
133  // Helper methods, aliases for data()->method()
134  // -----------------------------------------------------------------------------------------------
135   /// Returns the name stored in the attribute
136   inline std::string name()
137   {
138     return data()->name();
139   }
140   /// Returns the Boolean attribute by the identifier
141   inline std::shared_ptr<ModelAPI_AttributeBoolean> boolean(const std::string& theID)
142   {
143     return data()->boolean(theID);
144   }
145   /// Returns the document reference attribute
146   inline std::shared_ptr<ModelAPI_AttributeDocRef> document(const std::string& theID)
147   {
148     return data()->document(theID);
149   }
150   /// Returns the real attribute by the identifier
151   inline std::shared_ptr<ModelAPI_AttributeDouble> real(const std::string& theID)
152   {
153     return data()->real(theID);
154   }
155   /// Returns the integer attribute by the identifier
156   inline std::shared_ptr<ModelAPI_AttributeInteger> integer(const std::string& theID)
157   {
158     return data()->integer(theID);
159   }
160   /// Returns the reference attribute by the identifier
161   inline std::shared_ptr<ModelAPI_AttributeRefAttr> refattr(const std::string& theID)
162   {
163     return data()->refattr(theID);
164   }
165   /// Returns the reference attribute by the identifier
166   inline std::shared_ptr<ModelAPI_AttributeReference> reference(const std::string& theID)
167   {
168     return data()->reference(theID);
169   }
170   /// Returns the list of references attribute by the identifier
171   inline std::shared_ptr<ModelAPI_AttributeRefList> reflist(const std::string& theID)
172   {
173     return data()->reflist(theID);
174   }
175   /// Returns the shape selection attribute by the identifier
176   inline std::shared_ptr<ModelAPI_AttributeSelection> selection(const std::string& theID)
177   {
178     return data()->selection(theID);
179   }
180   /// Returns the list of shape selections attribute by the identifier
181   inline std::shared_ptr<ModelAPI_AttributeSelectionList> selectionList(const std::string& theID)
182   {
183     return data()->selectionList(theID);
184   }
185   /// Returns the string attribute by the identifier
186   inline std::shared_ptr<ModelAPI_AttributeString> string(const std::string& theID)
187   {
188     return data()->string(theID);
189   }
190   /// Returns the attribute by the identifier
191   inline std::shared_ptr<ModelAPI_Attribute> attribute(const std::string& theID)
192   {
193     return data()->attribute(theID);
194   }
195  // -----------------------------------------------------------------------------------------------
196 };
197
198 //! Pointer on feature object
199 typedef std::shared_ptr<ModelAPI_Feature> FeaturePtr;
200
201 #endif
202