Salome HOME
235275f30def55d0e1555a8decaca2640006f56b
[modules/shaper.git] / src / ModelAPI / ModelAPI_Feature.h
1 // Copyright (C) 2014-2021  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 email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef ModelAPI_Feature_H_
21 #define ModelAPI_Feature_H_
22
23 #include <ModelAPI.h>
24 #include <ModelAPI_Object.h>
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_Document.h>
27
28 #include <memory>
29
30 #include <list>
31 #include <string>
32
33 /**\class ModelAPI_Feature
34  * \ingroup DataModel
35  * \brief Feature function that represents the particular functionality
36  * of this operation. Produces results by the arguments.
37  */
38 class ModelAPI_Feature : public ModelAPI_Object
39 {
40   ///< list of current results of this feature
41   std::list<std::shared_ptr<ModelAPI_Result> > myResults;
42   ///< is feature disabled or not
43   bool myIsDisabled;
44   ///< is feature is stable (not editing)
45   bool myIsStable;
46
47  public:
48   /// Returns the unique kind of a feature (like "Point")
49   virtual const std::string& getKind() = 0;
50
51   /// Returns the group identifier of all features
52   inline static std::string group()
53   {
54     static std::string MY_GROUP = "Features";
55     return MY_GROUP;
56   }
57
58   /// Returns document this feature belongs to
59   virtual std::shared_ptr<ModelAPI_Document> document() const
60   {
61     return ModelAPI_Object::document();
62   }
63
64   /// Returns the group identifier of this result
65   virtual std::string groupName()
66   {
67     return group();
68   }
69
70   /// Computes or recomputes the results
71   virtual void execute() = 0;
72
73   /// Computes the attribute value on the base of other attributes if the value can be computed
74   /// \param theAttributeId an attribute index to be computed
75   /// \return a boolean value about it is computed
76   virtual bool compute(const std::string& /*theAttributeId*/) { return false; };
77
78   /// Registers error during the execution, causes the ExecutionFailed state
79   MODELAPI_EXPORT virtual void setError(const std::string& theError,
80                                         bool isSend = true,
81                                         bool isTranslate = true);
82
83   /// Returns error, arose during the execution
84   virtual std::string error() const {
85     return data()->error();
86   }
87
88   /// returns the current results of the feature
89   MODELAPI_EXPORT const std::list<std::shared_ptr<ModelAPI_Result> >& results();
90   /// returns the first result in the list or NULL reference
91   MODELAPI_EXPORT std::shared_ptr<ModelAPI_Result> firstResult() const;
92   /// returns the last result in the list or NULL reference
93   MODELAPI_EXPORT std::shared_ptr<ModelAPI_Result> lastResult();
94   /// sets the alone result
95   MODELAPI_EXPORT void setResult(const std::shared_ptr<ModelAPI_Result>& theResult);
96   /// sets the result by index (zero based), results before this must be set before
97   MODELAPI_EXPORT void setResult(const std::shared_ptr<ModelAPI_Result>& theResult,
98                                  const int theIndex);
99   /// removes all results starting from the given index (zero-based)
100   /// \param theSinceIndex - index of the deleted result and all after also will be deleted
101   /// \param theForever - removes results for long period (not just because of feature disabling)
102   /// \param theFlush - if it is false, REDISPLAY message is not flushed
103   MODELAPI_EXPORT void removeResults(const int theSinceIndex,
104     const bool theForever = true, const bool theFlush = true);
105   /// removes all results from the feature
106   MODELAPI_EXPORT void eraseResults(const bool theForever = true);
107   /// removes all fields from this feature: results, data, etc
108   MODELAPI_EXPORT virtual void erase();
109   /// removes the result from the list of feature (not doing in disabled): normally this
110   /// method is not used from features. only internally
111   MODELAPI_EXPORT void eraseResultFromList(const std::shared_ptr<ModelAPI_Result>& theResult);
112
113   /// Returns true if result is persistent (stored in document) and on undo-redo, save-open
114   /// it is not needed to recompute it.
115   virtual bool isPersistentResult() {return true;}
116
117   /// Returns true if this feature must not be created: this is just an action
118   /// that is not stored in the features history and data model (like "delete part").
119   virtual bool isAction()
120   {
121     return false;
122   }
123
124   /// Returns true if this feature is used as macro: creates other features and then removed.
125   /// \returns false by default
126   MODELAPI_EXPORT virtual bool isMacro() const;
127
128   /// Returns true if preview update during the edition needed. Otherwise the update-mechanism
129   /// calls the \a execute function only on "apply" of the operation
130   /// \returns true by default
131   MODELAPI_EXPORT virtual bool isPreviewNeeded() const;
132
133   /// Must return document where the new feature must be added to
134   /// By default it is empty: it is added to the document this method is called to
135   MODELAPI_EXPORT virtual const std::string& documentToAdd();
136
137   /// Enables/disables the feature. The disabled feature has no results and does not participate in
138   /// any calculation.
139   /// \returns true if state is really changed
140   MODELAPI_EXPORT virtual bool setDisabled(const bool theFlag);
141
142   /// Returns the feature is disabled or not.
143   MODELAPI_EXPORT virtual bool isDisabled();
144
145   /// To virtually destroy the fields of successors
146   MODELAPI_EXPORT virtual ~ModelAPI_Feature();
147
148   /// Returns the feature by the object (result).
149   MODELAPI_EXPORT static std::shared_ptr<ModelAPI_Feature> feature(ObjectPtr theObject);
150
151   /// Set the stable feature flag. If feature is currently editing then it is not stable.
152   /// \returns true if state is really changed
153   MODELAPI_EXPORT virtual bool setStable(const bool theFlag);
154
155   /// Returns the feature is stable or not.
156   MODELAPI_EXPORT virtual bool isStable();
157
158   /// Performs some custom feature specific functionality (normally called by some GUI button)
159   /// \param theActionId an action key
160   /// \return a boolean value about it is performed
161   MODELAPI_EXPORT virtual bool customAction(const std::string& theActionId);
162
163  //
164  // Helper methods, aliases for data()->method()
165  // -----------------------------------------------------------------------------------------------
166   /// Returns the name stored in the attribute
167   inline std::wstring name()
168   {
169     return data()->name();
170   }
171   /// Returns the Boolean attribute by the identifier
172   inline std::shared_ptr<ModelAPI_AttributeBoolean> boolean(const std::string& theID)
173   {
174     return data()->boolean(theID);
175   }
176   /// Returns the document reference attribute
177   inline std::shared_ptr<ModelAPI_AttributeDocRef> document(const std::string& theID)
178   {
179     return data()->document(theID);
180   }
181   /// Returns the real attribute by the identifier
182   inline std::shared_ptr<ModelAPI_AttributeDouble> real(const std::string& theID)
183   {
184     return data()->real(theID);
185   }
186   /// Returns the integer attribute by the identifier
187   inline std::shared_ptr<ModelAPI_AttributeInteger> integer(const std::string& theID)
188   {
189     return data()->integer(theID);
190   }
191   /// Returns the integer array attribute by the identifier
192   inline std::shared_ptr<ModelAPI_AttributeIntArray> intArray(const std::string& theID)
193   {
194     return data()->intArray(theID);
195   }
196   /// Returns the reference attribute by the identifier
197   inline std::shared_ptr<ModelAPI_AttributeRefAttr> refattr(const std::string& theID)
198   {
199     return data()->refattr(theID);
200   }
201   /// Returns the refattrlist attribute by the identifier
202   inline std::shared_ptr<ModelAPI_AttributeRefAttrList> refattrlist(const std::string& theID)
203   {
204     return data()->refattrlist(theID);
205   }
206   /// Returns the reference attribute by the identifier
207   inline std::shared_ptr<ModelAPI_AttributeReference> reference(const std::string& theID)
208   {
209     return data()->reference(theID);
210   }
211   /// Returns the list of references attribute by the identifier
212   inline std::shared_ptr<ModelAPI_AttributeRefList> reflist(const std::string& theID)
213   {
214     return data()->reflist(theID);
215   }
216   /// Returns the shape selection attribute by the identifier
217   inline std::shared_ptr<ModelAPI_AttributeSelection> selection(const std::string& theID)
218   {
219     return data()->selection(theID);
220   }
221   /// Returns the list of shape selections attribute by the identifier
222   inline std::shared_ptr<ModelAPI_AttributeSelectionList> selectionList(const std::string& theID)
223   {
224     return data()->selectionList(theID);
225   }
226   /// Returns the string attribute by the identifier
227   inline std::shared_ptr<ModelAPI_AttributeString> string(const std::string& theID)
228   {
229     return data()->string(theID);
230   }
231   /// Returns the string array attribute by the identifier
232   inline std::shared_ptr<ModelAPI_AttributeStringArray> stringArray(const std::string& theID)
233   {
234     return data()->stringArray(theID);
235   }
236   /// Returns the tables attribute by the identifier
237   inline std::shared_ptr<ModelAPI_AttributeTables> tables(const std::string& theID)
238   {
239     return data()->tables(theID);
240   }
241   /// Returns the attribute by the identifier
242   inline std::shared_ptr<ModelAPI_Attribute> attribute(const std::string& theID)
243   {
244     return data()->attribute(theID);
245   }
246   protected:
247   /// This method is called just after creation of the object: it must initialize
248   /// all fields, normally initialized in the constructor
249   MODELAPI_EXPORT virtual void init();
250
251   friend class Model_Document;
252   friend class Model_Objects;
253 };
254
255 //! Pointer on feature object
256 typedef std::shared_ptr<ModelAPI_Feature> FeaturePtr;
257
258 //! An interface for performing special copy actions. To give feature which is moved (a group)
259 //! over this feature.
260 class ModelAPI_FeatureCopyInterface {
261 public:
262   virtual ~ModelAPI_FeatureCopyInterface() {}
263
264   /// An algorithm to update the moved feature by the separate Copy feature
265   /// \param theContext the original context object
266   /// \param theValue the original shape
267   /// \param theCopies resulting copy-context will be appended here
268   virtual void getCopies(ObjectPtr theContext, std::shared_ptr<GeomAPI_Shape> theValue,
269                          std::list<ObjectPtr>& theCopyContext,
270                          std::list<std::shared_ptr<GeomAPI_Shape> >& theCopyVals) = 0;
271 };
272
273 #endif