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