Salome HOME
Update of documentation in the Model package
[modules/shaper.git] / src / Model / Model_Document.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_Document.h
4 // Created:     28 Feb 2014
5 // Author:      Mikhail PONIKAROV
6
7 #ifndef Model_Document_H_
8 #define Model_Document_H_
9
10 #include <Model.h>
11 #include <ModelAPI_Document.h>
12 #include <ModelAPI_Feature.h>
13 #include <ModelAPI_Result.h>
14 #include <ModelAPI_ResultParameter.h>
15
16 #include <TDocStd_Document.hxx>
17 #include <map>
18 #include <set>
19
20 class Handle_Model_Document;
21 class Model_Objects;
22 class ModelAPI_AttributeSelectionList;
23
24 /**\class Model_Document
25  * \ingroup DataModel
26  * \brief Document for internal data structure of any object storage.
27  * Document contains all data that must be stored/retrived in the file.
28  * Also it provides acces to this data: open/save, transactions management etc.
29  */
30 class Model_Document : public ModelAPI_Document
31 {
32  public:
33   //! Returns the kind of the document: "PartSet", "Part", or something else.
34   //! This kind is used for feature buttons enable/disable depending on active document
35   //! (it uses workbench "document" identifier in XML configuration file for this)
36   MODEL_EXPORT virtual const std::string& kind() const {return myKind;}
37
38   //! Loads the OCAF document from the file.
39   //! \param theFileName full name of the file to load
40   //! \param theThis the common shared pointer to the document to manage with it later
41   //! \returns true if file was loaded successfully
42   MODEL_EXPORT virtual bool load(const char* theFileName, DocumentPtr theThis);
43
44   //! Saves the OCAF document to the file.
45   //! \param theFileName full name of the file to store
46   //! \param theResults the result full file names that were stored by "save"
47   //! \returns true if file was stored successfully
48   MODEL_EXPORT virtual bool save(const char* theFileName, std::list<std::string>& theResults);
49
50   //! Removes document data
51   //! \param theForever if it is false, document is just hiden (to keep possibility make it back on Undo/Redo)
52   MODEL_EXPORT virtual void close(const bool theForever = false);
53
54   //! Starts a new operation (opens a tansaction)
55   MODEL_EXPORT virtual void startOperation();
56   //! Finishes the previously started operation (closes the transaction)
57   //! \returns true if transaction in this document is not empty and really was performed
58   MODEL_EXPORT virtual bool finishOperation();
59   //! Aborts the operation 
60   MODEL_EXPORT virtual void abortOperation();
61   //! Returns true if operation has been started, but not yet finished or aborted
62   MODEL_EXPORT virtual bool isOperation() const;
63   //! Returns true if document was modified (since creation/opening)
64   MODEL_EXPORT virtual bool isModified();
65
66   //! Returns True if there are available Undos
67   MODEL_EXPORT virtual bool canUndo();
68   //! Undoes last operation
69   MODEL_EXPORT virtual void undo();
70   //! Returns True if there are available Redos
71   MODEL_EXPORT virtual bool canRedo();
72   //! Redoes last operation
73   MODEL_EXPORT virtual void redo();
74
75   //! Adds to the document the new feature of the given feature id
76   //! \param theID creates feature and puts it in the document
77   //! \param theMakeCurrent to make current this new feature in this document
78   MODEL_EXPORT virtual FeaturePtr addFeature(std::string theID, const bool theMakeCurrent = true);
79
80   //! Return a list of features, which refers to the feature
81   //! \param theFeature a feature
82   //! \param theRefs a list of reference features
83   //! \param isSendError a flag whether the error message should be send
84   MODEL_EXPORT virtual void refsToFeature(FeaturePtr theFeature,
85                                           std::set<FeaturePtr>& theRefs,
86                                           const bool isSendError = true);
87
88   //! Removes the feature from the document (with result)
89   //! \param theFeature a removed feature
90   MODEL_EXPORT virtual void removeFeature(FeaturePtr theFeature);
91
92   //! Moves the feature to make it after the given one in the history.
93   MODEL_EXPORT virtual void moveFeature(FeaturePtr theMoved, FeaturePtr theAfterThis);
94
95   //! Returns the first found object in the group by the object name
96   //! \param theGroupID group that contains an object
97   //! \param theName name of the object to search
98   //! \returns null if such object is not found
99   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Object> objectByName(
100     const std::string& theGroupID, const std::string& theName);
101
102   //! Returns the object index in the group. Object must be visible. Otherwise returns -1.
103   //! \param theObject object of this document
104   //! \returns index started from zero, or -1 if object is invisible or belongs to another document
105   MODEL_EXPORT virtual const int index(std::shared_ptr<ModelAPI_Object> theObject);
106
107   //! Adds a new sub-document by the identifier, or returns existing one if it is already exist
108   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Document> subDocument(std::string theDocID);
109
110   //! Internal sub-document by ID
111   MODEL_EXPORT virtual std::shared_ptr<Model_Document> subDoc(std::string theDocID);
112
113   ///! Returns the id of the document
114   MODEL_EXPORT virtual const std::string& id() const
115   {
116     return myID;
117   }
118
119   //! Returns the feature in the group by the index (started from zero)
120   //! \param theGroupID group that contains a feature
121   //! \param theIndex zero-based index of feature in the group
122   MODEL_EXPORT virtual ObjectPtr object(const std::string& theGroupID, const int theIndex);
123
124   //! Returns the number of features in the group
125   MODEL_EXPORT virtual int size(const std::string& theGroupID);
126
127   //! Returns the feature that is currently edited in this document, normally
128   //! this is the latest created feature
129   //! \param theVisible use visible features only: flag is true for Object Browser functionality
130   //! \returns null if next created feature must be the first
131   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature> currentFeature(const bool theVisible);
132
133   //! Sets the current feature: all features below will be disabled, new features
134   //! will be appended after this one.
135   //! \param theCurrent the selected feature as current: blow it everythin become disabled
136   //! \param theVisible use visible features only: flag is true for Object Browser functionality
137   //! \param theFlushUpdates if it is true method calls flushes for creation/redisplay/delete messages
138   MODEL_EXPORT virtual void setCurrentFeature(std::shared_ptr<ModelAPI_Feature> theCurrent,
139     const bool theVisible, const bool theFlushUpdates = true);
140
141   //! Makes the current feature one feature upper
142   MODEL_EXPORT virtual void setCurrentFeatureUp();
143
144   //! Returns the number of all features: in the history or not
145   MODEL_EXPORT virtual int numInternalFeatures();
146   //! Returns the feature by zero-based index: features in the history or not
147   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature> internalFeature(const int theIndex);
148   //! Performs synchronization of transactions with the module document:
149   //! If some document is not active (by undo of activation) but in memory,
150   //! on activation the transactions must be synchronised because all redos performed 
151   //! wihtout this participation
152   MODEL_EXPORT virtual void synchronizeTransactions();
153
154
155   /// Creates a construction cresults
156   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultConstruction> createConstruction(
157       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
158   /// Creates a body results
159   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultBody> createBody(
160       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
161   /// Creates a part results
162   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultPart> createPart(
163       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
164   //! Copies a part result, keeping the reference to origin
165   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultPart> copyPart(
166     const std::shared_ptr<ModelAPI_ResultPart>& theOrigin,
167     const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
168   /// Creates a group result
169   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultGroup> createGroup(
170       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
171   /// Creates a parameter result
172   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultParameter> createParameter(
173       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
174
175   //! Returns a feature by result (owner of result)
176   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature>
177     feature(const std::shared_ptr<ModelAPI_Result>& theResult);
178
179   ///! Returns true if parametric updater need to execute feature on recomputartion
180   ///! On abort, undo or redo it is not necessary: results in document are updated automatically
181   bool& executeFeatures() {return myExecuteFeatures;}
182
183   //! Registers the name of the shape for the topological naming needs
184   void addNamingName(const TDF_Label theLabel, std::string theName);
185   //! Returns the label, keeper of the name  for the topological naming needs
186   TDF_Label findNamingName(std::string theName);
187   //! Returns the result by name of the result (names of results must be unique, used for naming
188   //! selection by name.
189   ResultPtr findByName(const std::string theName);
190
191   ///! Returns all features of the document including the hidden features which are not in
192   ///! history. Not very fast method, for calling once, not in big cycles.
193   MODEL_EXPORT virtual std::list<std::shared_ptr<ModelAPI_Feature> > allFeatures();
194
195   /// Returns the global identifier of the current transaction (needed for the update algo)
196   MODEL_EXPORT virtual int transactionID();
197   /// Increases the transaction ID
198   MODEL_EXPORT virtual void incrementTransactionID();
199   /// Decreases the transaction ID
200   MODEL_EXPORT virtual void decrementTransactionID();
201
202   /// Returns true if document is opened and valid
203   MODEL_EXPORT virtual bool isOpened();
204
205   /// Returns the last feature in the document (even not visible or disabled)
206   /// \returns null if there is no features
207   FeaturePtr lastFeature();
208
209  protected:
210   //! Returns (creates if needed) the general label
211   TDF_Label generalLabel() const;
212
213   //! Creates new document with binary file format
214   Model_Document(const std::string theID, const std::string theKind);
215
216   //! Returns the internal OCCT document of this interface
217   Handle_TDocStd_Document document()
218   {
219     return myDoc;
220   }
221
222   //! performs compactification of all nested operations into one
223   //! \returns true if resulting transaction is not empty and can be undoed
224   void compactNested();
225
226   //! Returns all sub documents
227   const std::set<std::string> subDocuments(const bool theActivatedOnly) const;
228
229   //! The implementation of undo: with or without recoursive calls in the sub-documents
230   void undoInternal(const bool theWithSubs, const bool theSynchronize);
231
232   //! Stores the Id of the current operation (normally is called for the root document)
233   void operationId(const std::string& theId);
234
235   //! Returns the list of Ids of the operations that can be undoed (called for the root document)
236   std::list<std::string> undoList() const;
237
238   //! Returns the list of Ids of the operations that can be redoed (called for the root document)
239   std::list<std::string> redoList() const;
240
241   //! Internally makes document know that feature was removed or added in history after creation
242   virtual void updateHistory(const std::shared_ptr<ModelAPI_Object> theObject);
243   //! Internally makes document know that feature was removed or added in history after creation
244   virtual void updateHistory(const std::string theGroup);
245
246   //! Returns true if the document is root module document
247   bool isRoot() const;
248
249   //! Sets shared pointer to this
250   void setThis(DocumentPtr theDoc);
251
252   //! Returns the objects manager
253   Model_Objects* objects() {return myObjs;}
254
255   ///! Informs the document that it becomes active and some actions must be performed
256   virtual void setActive(const bool theFlag);
257   //! Returns true if this document is currently active
258   virtual bool isActive() const;
259
260   //! Returns the selection attribute that is used for calculation of selection externally from the document
261   std::shared_ptr<ModelAPI_AttributeSelectionList> selectionInPartFeature();
262
263   friend class Model_Application;
264   friend class Model_Session;
265   friend class Model_Update;
266   friend class Model_AttributeReference;
267   friend class Model_AttributeRefAttr;
268   friend class Model_AttributeRefList;
269   friend class Model_ResultPart;
270   friend class Model_ResultCompSolid;
271   friend class DFBrowser;
272
273  private:
274   std::string myID;  ///< identifier of the document in the application
275   std::string myKind;  ///< kind of the document in the application
276   Handle_TDocStd_Document myDoc;  ///< OCAF document
277
278   Model_Objects *myObjs; ///< data manager of this document
279
280   //! counter value of transaction on the last "save" call, used for "IsModified" method
281   int myTransactionSave;
282   //! number of nested transactions performed (list becasue may be nested inside of nested)
283   //! the list is empty if not nested transaction is performed
284   std::list<int> myNestedNum;
285
286   //! Information related to the every user-transaction
287   struct Transaction {
288     int myOCAFNum; ///< number of OCAF transactions related to each "this" transaction, may be 0
289     std::string myId; ///< user-identifier string of transaction
290     //! default constructor with default Id
291     Transaction(): myOCAFNum(0), myId("") {}
292   };
293
294   //! transaction indexes (related to myTransactionsAfterSave) and info about the real transactions
295   //! in myDocument connected to this operation (may be zero for empty transaction)
296   std::list<Transaction> myTransactions;
297   //! list of info about transactions undone (first is oldest undone)
298   std::list<Transaction> myRedos;
299
300   //! Optimization for finding the shape-label by topological naming names
301   std::map<std::string, TDF_Label> myNamingNames;
302   //! If it is true, features are not executed on update (on abort, undo, redo)
303   bool myExecuteFeatures;
304
305   bool myIsActive; ///< flag that stores the active/not active state
306
307   //! The selection feature, if needed
308   FeaturePtr mySelectionFeature;
309 };
310
311 #endif