]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_Document.h
Salome HOME
f190c55dfb811521199fc8081e39f2bf73f63f64
[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 <NCollection_DataMap.hxx>
18 #include <TDF_Label.hxx>
19 #include <map>
20 #include <set>
21
22 class Handle_Model_Document;
23
24 // for TDF_Label map usage
25 static Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper);
26 static Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2);
27
28 /**\class Model_Document
29  * \ingroup DataModel
30  * \brief Document for internal data structure of any object storage.
31  * Document contains all data that must be stored/retrived in the file.
32  * Also it provides acces to this data: open/save, transactions management etc.
33  */
34 class Model_Document : public ModelAPI_Document
35 {
36  public:
37   //! Returns the kind of the document: "PartSet", "Part", or something else.
38   //! This kind is used for feature buttons enable/disable depending on active document
39   //! (it uses workbench "document" identifier in XML configuration file for this)
40   MODEL_EXPORT virtual const std::string& kind() const {return myKind;}
41
42   //! Loads the OCAF document from the file.
43   //! \param theFileName full name of the file to load
44   //! \returns true if file was loaded successfully
45   MODEL_EXPORT virtual bool load(const char* theFileName);
46
47   //! Saves the OCAF document to the file.
48   //! \param theFileName full name of the 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(const char* theFileName, std::list<std::string>& theResults);
52
53   //! Removes document data
54   //! \param theForever if it is false, document is just hiden (to keep possibility make it back on Undo/Redo)
55   MODEL_EXPORT virtual void close(const bool theForever = false);
56
57   //! Starts a new operation (opens a tansaction)
58   MODEL_EXPORT virtual void startOperation();
59   //! Finishes the previously started operation (closes the transaction)
60   //! \returns true if transaction in this document is not empty and really was performed
61   MODEL_EXPORT virtual bool finishOperation();
62   //! Aborts the operation 
63   MODEL_EXPORT virtual void abortOperation();
64   //! Returns true if operation has been started, but not yet finished or aborted
65   MODEL_EXPORT virtual bool isOperation() const;
66   //! Returns true if document was modified (since creation/opening)
67   MODEL_EXPORT virtual bool isModified();
68
69   //! Returns True if there are available Undos
70   MODEL_EXPORT virtual bool canUndo();
71   //! Undoes last operation
72   MODEL_EXPORT virtual void undo();
73   //! Returns True if there are available Redos
74   MODEL_EXPORT virtual bool canRedo();
75   //! Redoes last operation
76   MODEL_EXPORT virtual void redo();
77
78   //! Adds to the document the new feature of the given feature id
79   //! \param theID creates feature and puts it in the document
80   MODEL_EXPORT virtual FeaturePtr addFeature(std::string theID);
81
82   //! Return a list of features, which refers to the feature
83   //! \param theFeature a feature
84   //! \param theRefs a list of reference features
85   //! \param isSendError a flag whether the error message should be send
86   MODEL_EXPORT virtual void refsToFeature(FeaturePtr theFeature,
87                                           std::set<FeaturePtr>& theRefs,
88                                           const bool isSendError = true);
89
90   //! Removes the feature from the document (with result)
91   //! \param theFeature a removed feature
92   MODEL_EXPORT virtual void removeFeature(FeaturePtr theFeature);
93
94   //! Returns the existing feature by the label
95   //! \param theLabel base label of the feature
96   MODEL_EXPORT virtual FeaturePtr feature(TDF_Label& theLabel) const;
97
98   //! Returns the existing object: result or feature
99   //! \param theLabel base label of the object
100   MODEL_EXPORT virtual ObjectPtr object(TDF_Label theLabel);
101
102   //! Returns the first found object in the group by the object name
103   //! \param theGroupID group that contains an object
104   //! \param theName name of the object to search
105   //! \returns null if such object is not found
106   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Object> objectByName(
107     const std::string& theGroupID, const std::string& theName);
108
109   //! Returns the object index in the group. Object must be visible. Otherwise returns -1.
110   //! \param theObject object of this document
111   //! \returns index started from zero, or -1 if object is invisible or belongs to another document
112   MODEL_EXPORT virtual const int index(std::shared_ptr<ModelAPI_Object> theObject);
113
114   //! Adds a new sub-document by the identifier, or returns existing one if it is already exist
115   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Document> subDocument(std::string theDocID);
116
117   //! Internal sub-document by ID
118   MODEL_EXPORT virtual std::shared_ptr<Model_Document> subDoc(std::string theDocID);
119
120   ///! Returns the id of hte document
121   MODEL_EXPORT virtual const std::string& id() const
122   {
123     return myID;
124   }
125
126   //! Returns the feature in the group by the index (started from zero)
127   //! \param theGroupID group that contains a feature
128   //! \param theIndex zero-based index of feature in the group
129   //! \param theHidden if it is true, it counts also the features that are not in tree
130   MODEL_EXPORT virtual ObjectPtr object(const std::string& theGroupID, const int theIndex,
131                                         const bool theHidden = false);
132
133   //! Returns the number of features in the group
134   //! If theHidden is true, it counts also the features that are not in tree
135   MODEL_EXPORT virtual int size(const std::string& theGroupID, const bool theHidden = false);
136
137   //! Returns the feature that is currently edited in this document, normally
138   //! this is the latest created feature
139   //! \param theVisible use visible features only: flag is true for Object Browser functionality
140   //! \returns null if next created feature must be the first
141   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature> currentFeature(const bool theVisible);
142
143   //! Sets the current feature: all features below will be disabled, new features
144   //! will be appended after this one.
145   //! \param theCurrent the selected feature as current: blow it everythin become disabled
146   //! \param theVisible use visible features only: flag is true for Object Browser functionality
147   MODEL_EXPORT virtual void setCurrentFeature(std::shared_ptr<ModelAPI_Feature> theCurrent,
148     const bool theVisible);
149
150   /// Creates a construction cresults
151   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultConstruction> createConstruction(
152       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
153   /// Creates a body results
154   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultBody> createBody(
155       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
156   /// Creates a part results
157   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultPart> createPart(
158       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
159   /// Creates a group results
160   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultGroup> createGroup(
161       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
162
163   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultParameter> createParameter(
164       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
165
166   //! Returns a feature by result (owner of result)
167   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature>
168     feature(const std::shared_ptr<ModelAPI_Result>& theResult);
169
170   ///! Returns true if parametric updater need to execute feature on recomputartion
171   ///! On abort, undo or redo it is not necessary: results in document are updated automatically
172   bool executeFeatures() {return myExecuteFeatures;}
173
174   //! Registers the name of the shape for the topological naming needs
175   void addNamingName(const TDF_Label theLabel, std::string theName);
176   //! Returns the label, keeper of the name  for the topological naming needs
177   TDF_Label findNamingName(std::string theName);
178   //! Returns the result by name of the result (names of results must be unique, used for naming
179   //! selection by name.
180   ResultPtr findByName(const std::string theName);
181
182  protected:
183
184   //! Returns (creates if needed) the features label
185   TDF_Label featuresLabel() const;
186   //! Returns (creates if needed) the general label
187   TDF_Label generalLabel() const;
188
189   //! Initializes feature with a unique name in this group (unique name is generated as 
190   //! feature type + "_" + index
191   void setUniqueName(FeaturePtr theFeature);
192
193   //! Synchronizes myFeatures list with the updated document
194   //! \param theMarkUpdated causes the "update" event for all features
195   //! \param theUpdateReferences causes the update of back-references
196   //! \param theFlush makes flush all events in the end of all modifications of this method
197   void synchronizeFeatures(const bool theMarkUpdated, const bool theUpdateReferences,
198     const bool theFlush);
199   //! Synchronizes the BackReferences list in Data of Features and Results
200   void synchronizeBackRefs();
201
202   //! Creates new document with binary file format
203   Model_Document(const std::string theID, const std::string theKind);
204
205   //! Returns the internal OCCT document of this interface
206   Handle_TDocStd_Document document()
207   {
208     return myDoc;
209   }
210
211   //! performs compactification of all nested operations into one
212   //! \returns true if resulting transaction is not empty and can be undoed
213   void compactNested();
214
215   //! Initializes the data fields of the feature
216   void initData(ObjectPtr theObj, TDF_Label theLab, const int theTag);
217
218   //! Allows to store the result in the data tree of the document (attaches 'data' of result to tree)
219   MODEL_EXPORT virtual void storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
220                                         std::shared_ptr<ModelAPI_Result> theResult,
221                                         const int theResultIndex = 0);
222
223   //! returns the label of result by index; creates this label if it was not created before
224   TDF_Label resultLabel(const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex);
225
226   //! Updates the results list of the feature basing on the current data tree
227   void updateResults(FeaturePtr theFeature);
228
229   //! Returns all sub documents
230   const std::set<std::string> subDocuments(const bool theActivatedOnly) 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   MODEL_EXPORT virtual void addToHistory(const std::shared_ptr<ModelAPI_Object> theObject);
246
247   /// Returns true if the document is root module document
248   bool isRoot() const;
249
250   friend class Model_Application;
251   friend class Model_Session;
252   friend class Model_Update;
253   friend class Model_AttributeReference;
254   friend class DFBrowser;
255
256  private:
257   std::string myID;  ///< identifier of the document in the application
258   std::string myKind;  ///< kind of the document in the application
259   Handle_TDocStd_Document myDoc;  ///< OCAF document
260
261   /// counter value of transaction on the last "save" call, used for "IsModified" method
262   int myTransactionSave;
263   /// number of nested transactions performed (list becasue may be nested inside of nested)
264   /// the list is empty if not nested transaction is performed
265   std::list<int> myNestedNum;
266
267   /// Information related to the every user-transaction
268   struct Transaction {
269     int myOCAFNum; ///< number of OCAF transactions related to each "this" transaction, may be 0
270     std::string myId; ///< user-identifier string of transaction
271     /// default constructor with default Id
272     Transaction(): myOCAFNum(0), myId("") {}
273   };
274
275   /// transaction indexes (related to myTransactionsAfterSave) and info about the real transactions
276   /// in myDocument connected to this operation (may be zero for empty transaction)
277   std::list<Transaction> myTransactions;
278   /// list of info about transactions undone (first is oldest undone)
279   std::list<Transaction> myRedos;
280   /// All features managed by this document (not only in history of OB)
281   /// For optimization mapped by labels
282   NCollection_DataMap<TDF_Label, FeaturePtr> myObjs;
283   /// Optimization for finding the shape-label by topological naming names
284   std::map<std::string, TDF_Label> myNamingNames;
285   /// If it is true, features are not executed on update (on abort, undo, redo)
286   bool myExecuteFeatures;
287 };
288
289 #endif