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