1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModelAPI_Feature.hxx
4 // Created: 21 Mar 2014
5 // Author: Mikhail PONIKAROV
7 #ifndef ModelAPI_Feature_H_
8 #define ModelAPI_Feature_H_
11 #include <ModelAPI_Object.h>
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_Document.h>
20 /**\class ModelAPI_Feature
22 * \brief Feature function that represents the particular functionality
23 * of this operation. Produces results by the arguments.
25 class ModelAPI_Feature : public ModelAPI_Object
27 ///< list of current results of this feature
28 std::list<std::shared_ptr<ModelAPI_Result> > myResults;
29 ///< is feature disabled or not
31 ///< is feature is stable (not editing)
35 /// Returns the unique kind of a feature (like "Point")
36 virtual const std::string& getKind() = 0;
38 /// Returns the group identifier of all features
39 inline static std::string group()
41 static std::string MY_GROUP = "Features";
45 /// Returns document this feature belongs to
46 virtual std::shared_ptr<ModelAPI_Document> document() const
48 return ModelAPI_Object::document();
51 /// Returns the group identifier of this result
52 virtual std::string groupName()
57 /// Computes or recomputes the results
58 virtual void execute() = 0;
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; };
65 /// Registers error during the execution, causes the ExecutionFailed state
66 MODELAPI_EXPORT virtual void setError(const std::string& theError,
68 bool isTranslate = true);
70 /// Returns error, arose during the execution
71 virtual std::string error() const {
72 return data()->error();
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,
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 theFlush - if it is false, REDISPLAY message is not flushed
91 MODELAPI_EXPORT void removeResults(const int theSinceIndex, const bool theFlush = true);
92 /// removes all results from the feature
93 MODELAPI_EXPORT void eraseResults();
94 /// removes all fields from this feature: results, data, etc
95 MODELAPI_EXPORT virtual void erase();
96 /// removes the result from the list of feature (not doing in disabled): normally this
97 /// method is not used from features. only internally
98 MODELAPI_EXPORT void eraseResultFromList(const std::shared_ptr<ModelAPI_Result>& theResult);
100 /// Returns true if result is persistent (stored in document) and on undo-redo, save-open
101 /// it is not needed to recompute it.
102 virtual bool isPersistentResult() {return true;}
104 /// Returns true if this feature must not be created: this is just an action
105 /// that is not stored in the features history and data model (like "delete part").
106 virtual bool isAction()
111 /// Returns true if this feature is used as macro: creates other features and then removed.
112 /// \returns false by default
113 MODELAPI_EXPORT virtual bool isMacro() const;
115 /// Returns true if preview update during the edition needed. Otherwise the update-mechanism
116 /// calls the \a execute function only on "apply" of the operation
117 /// \returns true by default
118 MODELAPI_EXPORT virtual bool isPreviewNeeded() const;
120 /// Must return document where the new feature must be added to
121 /// By default it is empty: it is added to the document this method is called to
122 MODELAPI_EXPORT virtual const std::string& documentToAdd();
124 /// Enables/disables the feature. The disabled feature has no results and does not participate in
126 /// \returns true if state is really changed
127 MODELAPI_EXPORT virtual bool setDisabled(const bool theFlag);
129 /// Returns the feature is disabled or not.
130 MODELAPI_EXPORT virtual bool isDisabled();
132 /// To virtually destroy the fields of successors
133 MODELAPI_EXPORT virtual ~ModelAPI_Feature();
135 /// Returns the feature by the object (result).
136 MODELAPI_EXPORT static std::shared_ptr<ModelAPI_Feature> feature(ObjectPtr theObject);
138 /// Set the stable feature flag. If feature is currently editing then it is not stable.
139 /// \returns true if state is really changed
140 MODELAPI_EXPORT virtual bool setStable(const bool theFlag);
142 /// Returns the feature is stable or not.
143 MODELAPI_EXPORT virtual bool isStable();
145 /// Performs some custom feature specific functionality (normally called by some GUI button)
146 /// \param theActionId an action key
147 /// \return a boolean value about it is performed
148 MODELAPI_EXPORT virtual bool customAction(const std::string& theActionId);
151 // Helper methods, aliases for data()->method()
152 // -----------------------------------------------------------------------------------------------
153 /// Returns the name stored in the attribute
154 inline std::string name()
156 return data()->name();
158 /// Returns the Boolean attribute by the identifier
159 inline std::shared_ptr<ModelAPI_AttributeBoolean> boolean(const std::string& theID)
161 return data()->boolean(theID);
163 /// Returns the document reference attribute
164 inline std::shared_ptr<ModelAPI_AttributeDocRef> document(const std::string& theID)
166 return data()->document(theID);
168 /// Returns the real attribute by the identifier
169 inline std::shared_ptr<ModelAPI_AttributeDouble> real(const std::string& theID)
171 return data()->real(theID);
173 /// Returns the integer attribute by the identifier
174 inline std::shared_ptr<ModelAPI_AttributeInteger> integer(const std::string& theID)
176 return data()->integer(theID);
178 /// Returns the integer array attribute by the identifier
179 inline std::shared_ptr<ModelAPI_AttributeIntArray> intArray(const std::string& theID)
181 return data()->intArray(theID);
183 /// Returns the reference attribute by the identifier
184 inline std::shared_ptr<ModelAPI_AttributeRefAttr> refattr(const std::string& theID)
186 return data()->refattr(theID);
188 /// Returns the reference attribute by the identifier
189 inline std::shared_ptr<ModelAPI_AttributeReference> reference(const std::string& theID)
191 return data()->reference(theID);
193 /// Returns the list of references attribute by the identifier
194 inline std::shared_ptr<ModelAPI_AttributeRefList> reflist(const std::string& theID)
196 return data()->reflist(theID);
198 /// Returns the shape selection attribute by the identifier
199 inline std::shared_ptr<ModelAPI_AttributeSelection> selection(const std::string& theID)
201 return data()->selection(theID);
203 /// Returns the list of shape selections attribute by the identifier
204 inline std::shared_ptr<ModelAPI_AttributeSelectionList> selectionList(const std::string& theID)
206 return data()->selectionList(theID);
208 /// Returns the string attribute by the identifier
209 inline std::shared_ptr<ModelAPI_AttributeString> string(const std::string& theID)
211 return data()->string(theID);
213 /// Returns the string array attribute by the identifier
214 inline std::shared_ptr<ModelAPI_AttributeStringArray> stringArray(const std::string& theID)
216 return data()->stringArray(theID);
218 /// Returns the tables attribute by the identifier
219 inline std::shared_ptr<ModelAPI_AttributeTables> tables(const std::string& theID)
221 return data()->tables(theID);
223 /// Returns the attribute by the identifier
224 inline std::shared_ptr<ModelAPI_Attribute> attribute(const std::string& theID)
226 return data()->attribute(theID);
229 /// This method is called just after creation of the object: it must initialize
230 /// all fields, normally initialized in the constructor
231 MODELAPI_EXPORT virtual void init();
233 friend class Model_Document;
234 friend class Model_Objects;
237 //! Pointer on feature object
238 typedef std::shared_ptr<ModelAPI_Feature> FeaturePtr;