1 // Copyright (C) 2014-2024 CEA, EDF
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #ifndef ModelAPI_Feature_H_
21 #define ModelAPI_Feature_H_
24 #include <ModelAPI_Object.h>
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_Document.h>
33 typedef std::list<std::shared_ptr<GeomAPI_Shape> > ListOfShape;
35 /**\class ModelAPI_Feature
37 * \brief Feature function that represents the particular functionality
38 * of this operation. Produces results by the arguments.
40 class ModelAPI_Feature : public ModelAPI_Object
42 ///< list of current results of this feature
43 std::list<std::shared_ptr<ModelAPI_Result> > myResults;
44 ///< is feature disabled or not
46 ///< is feature stable (not editing)
50 /// Returns the unique kind of a feature (like "Point")
51 virtual const std::string& getKind() = 0;
53 /// Returns the group identifier of all features
54 inline static std::string group()
56 static std::string MY_GROUP = "Features";
60 /// Returns document this feature belongs to
61 virtual std::shared_ptr<ModelAPI_Document> document() const
63 return ModelAPI_Object::document();
66 /// Returns the group identifier of this result
67 virtual std::string groupName()
72 /// Computes or recomputes the results
73 virtual void execute() = 0;
75 /// Computes the attribute value on the base of other attributes if the value can be computed
76 /// \param theAttributeId an attribute index to be computed
77 /// \return a boolean value about it is computed
78 virtual bool compute(const std::string& /*theAttributeId*/) { return false; };
80 /// Registers error during the execution, causes the ExecutionFailed state
81 MODELAPI_EXPORT virtual void setError(const std::string& theError,
83 bool isTranslate = true);
85 /// Returns error, arose during the execution
86 virtual std::string error() const {
87 return data()->error();
90 /// returns the current results of the feature
91 MODELAPI_EXPORT const std::list<std::shared_ptr<ModelAPI_Result> >& results();
92 /// returns the first result in the list or NULL reference
93 MODELAPI_EXPORT std::shared_ptr<ModelAPI_Result> firstResult() const;
94 /// returns the last result in the list or NULL reference
95 MODELAPI_EXPORT std::shared_ptr<ModelAPI_Result> lastResult();
96 /// sets the alone result
97 MODELAPI_EXPORT void setResult(const std::shared_ptr<ModelAPI_Result>& theResult);
98 /// sets the result by index (zero based), results before this must be set before
99 MODELAPI_EXPORT void setResult(const std::shared_ptr<ModelAPI_Result>& theResult,
101 /// removes all results starting from the given index (zero-based)
102 /// \param theSinceIndex - index of the deleted result and all after also will be deleted
103 /// \param theForever - removes results for long period (not just because of feature disabling)
104 /// \param theFlush - if it is false, REDISPLAY message is not flushed
105 MODELAPI_EXPORT void removeResults(const int theSinceIndex,
106 const bool theForever = true, const bool theFlush = true);
107 /// removes all results from the feature
108 MODELAPI_EXPORT void eraseResults(const bool theForever = true);
109 /// removes all fields from this feature: results, data, etc
110 MODELAPI_EXPORT virtual void erase();
111 /// removes the result from the list of feature (not doing in disabled): normally this
112 /// method is not used from features. only internally
113 MODELAPI_EXPORT void eraseResultFromList(const std::shared_ptr<ModelAPI_Result>& theResult);
115 /// Returns true if result is persistent (stored in document) and on undo-redo, save-open
116 /// it is not needed to recompute it.
117 virtual bool isPersistentResult() {return true;}
119 /// Returns true if this feature must not be created: this is just an action
120 /// that is not stored in the features history and data model (like "delete part").
121 virtual bool isAction()
126 /// Returns true if this feature is used as macro: creates other features and then removed.
127 /// \returns false by default
128 MODELAPI_EXPORT virtual bool isMacro() const;
130 /// Returns true if preview update during the edition needed. Otherwise the update-mechanism
131 /// calls the \a execute function only on "apply" of the operation
132 /// \returns true by default
133 MODELAPI_EXPORT virtual bool isPreviewNeeded() const;
135 /// Must return document where the new feature must be added to
136 /// By default it is empty: it is added to the document this method is called to
137 MODELAPI_EXPORT virtual const std::string& documentToAdd();
139 /// Enables/disables the feature. The disabled feature has no results and does not participate in
141 /// \returns true if state is really changed
142 MODELAPI_EXPORT virtual bool setDisabled(const bool theFlag);
144 /// Returns the feature is disabled or not.
145 MODELAPI_EXPORT virtual bool isDisabled();
147 /// To virtually destroy the fields of successors
148 MODELAPI_EXPORT virtual ~ModelAPI_Feature();
150 /// Returns the feature by the object (result).
151 MODELAPI_EXPORT static std::shared_ptr<ModelAPI_Feature> feature(ObjectPtr theObject);
153 /// Set the stable feature flag. If feature is currently editing then it is not stable.
154 /// \returns true if state is really changed
155 MODELAPI_EXPORT virtual bool setStable(const bool theFlag);
157 /// Returns the feature is stable or not.
158 MODELAPI_EXPORT virtual bool isStable();
160 /// Returns the feature is editable or not. Most of features are editable.
161 MODELAPI_EXPORT virtual bool isEditable();
163 /// Performs some custom feature specific functionality (normally called by some GUI button)
164 /// \param theActionId an action key
165 /// \return a boolean value about it is performed
166 MODELAPI_EXPORT virtual bool customAction(const std::string& theActionId);
168 /// Returns all the vertices produced by this feature
169 MODELAPI_EXPORT virtual ListOfShape
170 vertices(const bool theOnlyUnique = false);
172 /// Returns all the edges produced by this feature
173 MODELAPI_EXPORT virtual ListOfShape
174 edges(const bool theOnlyUnique = false);
176 /// Returns all the wires produced by this feature
177 MODELAPI_EXPORT virtual ListOfShape
178 wires(const bool theOnlyUnique = false);
180 /// Returns all the faces produced by this feature
181 MODELAPI_EXPORT virtual ListOfShape
182 faces(const bool theOnlyUnique = false);
184 /// Returns all the shells produced by this feature
185 MODELAPI_EXPORT virtual ListOfShape
186 shells(const bool theOnlyUnique = false);
188 /// Returns all the solids produced by this feature
189 MODELAPI_EXPORT virtual ListOfShape
190 solids(const bool theOnlyUnique = false);
193 // Helper methods, aliases for data()->method()
194 // -----------------------------------------------------------------------------------------------
195 /// Returns the name stored in the attribute
196 inline std::wstring name()
198 return data()->name();
200 /// Returns the Boolean attribute by the identifier
201 inline std::shared_ptr<ModelAPI_AttributeBoolean> boolean(const std::string& theID)
203 return data()->boolean(theID);
205 /// Returns the document reference attribute
206 inline std::shared_ptr<ModelAPI_AttributeDocRef> document(const std::string& theID)
208 return data()->document(theID);
210 /// Returns the real attribute by the identifier
211 inline std::shared_ptr<ModelAPI_AttributeDouble> real(const std::string& theID)
213 return data()->real(theID);
215 /// Returns the integer attribute by the identifier
216 inline std::shared_ptr<ModelAPI_AttributeInteger> integer(const std::string& theID)
218 return data()->integer(theID);
220 /// Returns the integer array attribute by the identifier
221 inline std::shared_ptr<ModelAPI_AttributeIntArray> intArray(const std::string& theID)
223 return data()->intArray(theID);
225 /// Returns the reference attribute by the identifier
226 inline std::shared_ptr<ModelAPI_AttributeRefAttr> refattr(const std::string& theID)
228 return data()->refattr(theID);
230 /// Returns the refattrlist attribute by the identifier
231 inline std::shared_ptr<ModelAPI_AttributeRefAttrList> refattrlist(const std::string& theID)
233 return data()->refattrlist(theID);
235 /// Returns the reference attribute by the identifier
236 inline std::shared_ptr<ModelAPI_AttributeReference> reference(const std::string& theID)
238 return data()->reference(theID);
240 /// Returns the list of references attribute by the identifier
241 inline std::shared_ptr<ModelAPI_AttributeRefList> reflist(const std::string& theID)
243 return data()->reflist(theID);
245 /// Returns the shape selection attribute by the identifier
246 inline std::shared_ptr<ModelAPI_AttributeSelection> selection(const std::string& theID)
248 return data()->selection(theID);
250 /// Returns the list of shape selections attribute by the identifier
251 inline std::shared_ptr<ModelAPI_AttributeSelectionList> selectionList(const std::string& theID)
253 return data()->selectionList(theID);
255 /// Returns the string attribute by the identifier
256 inline std::shared_ptr<ModelAPI_AttributeString> string(const std::string& theID)
258 return data()->string(theID);
260 /// Returns the string array attribute by the identifier
261 inline std::shared_ptr<ModelAPI_AttributeStringArray> stringArray(const std::string& theID)
263 return data()->stringArray(theID);
265 /// Returns the tables attribute by the identifier
266 inline std::shared_ptr<ModelAPI_AttributeTables> tables(const std::string& theID)
268 return data()->tables(theID);
270 /// Returns the attribute by the identifier
271 inline std::shared_ptr<ModelAPI_Attribute> attribute(const std::string& theID)
273 return data()->attribute(theID);
277 /// Print warning message to python console
278 void showErrorMessage();
281 /// This method is called just after creation of the object: it must initialize
282 /// all fields, normally initialized in the constructor
283 MODELAPI_EXPORT virtual void init();
285 friend class Model_Document;
286 friend class Model_Objects;
289 //! Pointer on feature object
290 typedef std::shared_ptr<ModelAPI_Feature> FeaturePtr;
292 //! An interface for performing special copy actions. To give feature which is moved (a group)
293 //! over this feature.
294 class ModelAPI_FeatureCopyInterface {
296 virtual ~ModelAPI_FeatureCopyInterface() {}
298 /// An algorithm to update the moved feature by the separate Copy feature
299 /// \param theContext the original context object
300 /// \param theValue the original shape
301 /// \param theCopies resulting copy-context will be appended here
302 virtual void getCopies(ObjectPtr theContext, std::shared_ptr<GeomAPI_Shape> theValue,
303 std::list<ObjectPtr>& theCopyContext,
304 std::list<std::shared_ptr<GeomAPI_Shape> >& theCopyVals) = 0;