1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_Document.h
4 // Created: 28 Feb 2014
5 // Author: Mikhail PONIKAROV
7 #ifndef Model_Document_H_
8 #define Model_Document_H_
11 #include <ModelAPI_Document.h>
12 #include <ModelAPI_Feature.h>
13 #include <ModelAPI_Result.h>
14 #include <ModelAPI_ResultParameter.h>
16 #include <TDocStd_Document.hxx>
20 class Handle_Model_Document;
22 class ModelAPI_AttributeSelectionList;
24 /**\class Model_Document
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.
30 class Model_Document : public ModelAPI_Document
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;}
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);
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);
54 //! Removes document data
55 //! \param theForever if it is false, document is just hiden
56 //! (to keep possibility make it back on Undo/Redo)
57 MODEL_EXPORT virtual void close(const bool theForever = false);
59 //! Starts a new operation (opens a tansaction)
60 MODEL_EXPORT virtual void startOperation();
61 //! Finishes the previously started operation (closes the transaction)
62 //! \returns true if transaction in this document is not empty and really was performed
63 MODEL_EXPORT virtual bool finishOperation();
64 //! Aborts the operation
65 MODEL_EXPORT virtual void abortOperation();
66 //! Returns true if operation has been started, but not yet finished or aborted
67 MODEL_EXPORT virtual bool isOperation() const;
68 //! Returns true if document was modified (since creation/opening)
69 MODEL_EXPORT virtual bool isModified();
71 //! Returns True if there are available Undos
72 MODEL_EXPORT virtual bool canUndo();
73 //! Undoes last operation
74 MODEL_EXPORT virtual void undo();
75 //! Returns True if there are available Redos
76 MODEL_EXPORT virtual bool canRedo();
77 //! Redoes last operation
78 MODEL_EXPORT virtual void redo();
80 //! Adds to the document the new feature of the given feature id
81 //! \param theID creates feature and puts it in the document
82 //! \param theMakeCurrent to make current this new feature in this document
83 MODEL_EXPORT virtual FeaturePtr addFeature(std::string theID, const bool theMakeCurrent = true);
85 //! Return a list of features, which refers to the feature
86 //! \param theFeature a feature
87 //! \param theRefs a list of reference features
88 //! \param isSendError a flag whether the error message should be send
89 MODEL_EXPORT virtual void refsToFeature(FeaturePtr theFeature,
90 std::set<FeaturePtr>& theRefs,
91 const bool isSendError = true);
93 //! Removes the feature from the document (with result)
94 //! It is necessary to flush REDISPLAY signal manually after this method because
95 //! the method sends it, but for the performance purpose does not flush it
96 //! \param theFeature a removed feature
97 MODEL_EXPORT virtual void removeFeature(FeaturePtr theFeature);
99 //! Moves the feature to make it after the given one in the history.
100 MODEL_EXPORT virtual void moveFeature(FeaturePtr theMoved, FeaturePtr theAfterThis);
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);
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);
114 //! Internal sub-document by ID
115 MODEL_EXPORT virtual std::shared_ptr<Model_Document> subDoc(int theDocID);
117 ///! Returns the id of the document
118 MODEL_EXPORT virtual const int id() const
123 //! Returns the feature in the group by the index (started from zero)
124 //! \param theGroupID group that contains a feature
125 //! \param theIndex zero-based index of feature in the group
126 MODEL_EXPORT virtual ObjectPtr object(const std::string& theGroupID, const int theIndex);
128 //! Returns the number of features in the group
129 MODEL_EXPORT virtual int size(const std::string& theGroupID);
131 //! Returns the feature that is currently edited in this document, normally
132 //! this is the latest created feature
133 //! \param theVisible use visible features only: flag is true for Object Browser functionality
134 //! \returns null if next created feature must be the first
135 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature> currentFeature(const bool theVisible);
137 //! Sets the current feature: all features below will be disabled, new features
138 //! will be appended after this one.
139 //! \param theCurrent the selected feature as current: blow it everythin become disabled
140 //! \param theVisible use visible features only: flag is true for Object Browser functionality
141 MODEL_EXPORT virtual void setCurrentFeature(std::shared_ptr<ModelAPI_Feature> theCurrent,
142 const bool theVisible);
144 //! Makes the current feature one feature upper
145 MODEL_EXPORT virtual void setCurrentFeatureUp();
147 //! Returns the number of all features: in the history or not
148 MODEL_EXPORT virtual int numInternalFeatures();
149 //! Returns the feature by zero-based index: features in the history or not
150 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature> internalFeature(const int theIndex);
151 //! Performs synchronization of transactions with the module document:
152 //! If some document is not active (by undo of activation) but in memory,
153 //! on activation the transactions must be synchronised because all redos performed
154 //! wihtout this participation
155 MODEL_EXPORT virtual void synchronizeTransactions();
157 //! Returns feature by the id of the feature (produced by the Data "featureId" method)
158 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature> featureById(const int theId);
161 /// Creates a construction cresults
162 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultConstruction> createConstruction(
163 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
164 /// Creates a body results
165 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultBody> createBody(
166 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
167 /// Creates a part results
168 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultPart> createPart(
169 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
170 //! Copies a part result, keeping the reference to origin
171 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultPart> copyPart(
172 const std::shared_ptr<ModelAPI_ResultPart>& theOrigin,
173 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
174 /// Creates a group result
175 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultGroup> createGroup(
176 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
177 /// Creates a field result
178 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultField> createField(
179 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
180 /// Creates a parameter result
181 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultParameter> createParameter(
182 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
184 //! Returns a feature by result (owner of result)
185 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature>
186 feature(const std::shared_ptr<ModelAPI_Result>& theResult);
188 ///! Returns true if parametric updater need to execute feature on recomputartion
189 ///! On abort, undo or redo it is not necessary: results in document are updated automatically
190 bool& executeFeatures() {return myExecuteFeatures;}
192 //! Registers the name of the shape for the topological naming needs
193 void addNamingName(const TDF_Label theLabel, std::string theName);
194 //! Updates the name of some object
195 void changeNamingName(std::string theOldName, const std::string theNewName);
196 //! Returns the label, keeper of the name for the topological naming needs
197 TDF_Label findNamingName(std::string theName);
198 //! Returns the result by name of the result (names of results must be unique, used for naming
199 //! selection by name.
200 ResultPtr findByName(const std::string theName);
202 ///! Returns all features of the document including the hidden features which are not in
203 ///! history. Not very fast method, for calling once, not in big cycles.
204 MODEL_EXPORT virtual std::list<std::shared_ptr<ModelAPI_Feature> > allFeatures();
206 /// Returns the global identifier of the current transaction (needed for the update algo)
207 MODEL_EXPORT virtual int transactionID();
208 /// Increases the transaction ID
209 MODEL_EXPORT virtual void incrementTransactionID();
210 /// Decreases the transaction ID
211 MODEL_EXPORT virtual void decrementTransactionID();
213 /// Returns true if document is opened and valid
214 MODEL_EXPORT virtual bool isOpened();
216 /// Returns the last feature in the document (even not visible or disabled)
217 /// \returns null if there is no features
218 FeaturePtr lastFeature();
220 /// Returns the feature that produced the given face of the given result.
221 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature> producedByFeature(
222 std::shared_ptr<ModelAPI_Result> theResult,
223 const std::shared_ptr<GeomAPI_Shape>& theShape);
225 /// Returns true if theLater is in history of features creation later than theCurrent
226 MODEL_EXPORT virtual bool isLater(FeaturePtr theLater, FeaturePtr theCurrent) const;
229 //! Returns (creates if needed) the general label
230 TDF_Label generalLabel() const;
232 //! Creates new document with binary file format
233 Model_Document(const int theID, const std::string theKind);
235 //! Returns the internal OCCT document of this interface
236 Handle_TDocStd_Document document()
241 //! performs compactification of all nested operations into one
242 //! \returns true if resulting transaction is not empty and can be undoed
243 void compactNested();
245 //! Returns all loaded sub documents
246 const std::set<int> subDocuments() const;
248 //! The implementation of undo: with or without recoursive calls in the sub-documents
249 void undoInternal(const bool theWithSubs, const bool theSynchronize);
251 //! Stores the Id of the current operation (normally is called for the root document)
252 void operationId(const std::string& theId);
254 //! Returns the list of Ids of the operations that can be undoed (called for the root document)
255 std::list<std::string> undoList() const;
257 //! Returns the list of Ids of the operations that can be redoed (called for the root document)
258 std::list<std::string> redoList() const;
260 //! Internally makes document know that feature was removed or added in history after creation
261 virtual void updateHistory(const std::shared_ptr<ModelAPI_Object> theObject);
262 //! Internally makes document know that feature was removed or added in history after creation
263 virtual void updateHistory(const std::string theGroup);
265 //! Returns true if the document is root module document
268 //! Sets shared pointer to this
269 void setThis(DocumentPtr theDoc);
271 //! Returns the objects manager
272 Model_Objects* objects() {return myObjs;}
274 ///! Informs the document that it becomes active and some actions must be performed
275 virtual void setActive(const bool theFlag);
276 //! Returns true if this document is currently active
277 virtual bool isActive() const;
279 //! Returns the selection attribute that is used
280 //! for calculation of selection externally from the document
281 std::shared_ptr<ModelAPI_AttributeSelectionList> selectionInPartFeature();
283 /// Stores in the document boolean flags: states of the nodes in the object browser.
284 /// Normally is called outside of the transaction, just before "save".
285 virtual void storeNodesState(const std::list<bool>& theStates);
287 /// Returns the stored nodes states. Normally it is calles just after "open".
288 /// Appends the values to theStates list.
289 virtual void restoreNodesState(std::list<bool>& theStates) const;
291 /// Label that constains structures for selection of constructions of another document
292 TDF_Label extConstructionsLabel() const;
294 friend class Model_Application;
295 friend class Model_Session;
296 friend class Model_Update;
297 friend class Model_AttributeReference;
298 friend class Model_AttributeRefAttr;
299 friend class Model_AttributeRefList;
300 friend class Model_AttributeRefAttrList;
301 friend class Model_AttributeSelection;
302 friend class Model_ResultPart;
303 friend class Model_ResultCompSolid;
304 friend class Model_ResultConstruction;
305 friend class Model_SelectionNaming;
306 friend class DFBrowser;
309 int myID; ///< identifier of the document in the application
310 std::string myKind; ///< kind of the document in the application
311 Handle_TDocStd_Document myDoc; ///< OCAF document
313 Model_Objects *myObjs; ///< data manager of this document
315 //! counter value of transaction on the last "save" call, used for "IsModified" method
316 int myTransactionSave;
317 //! number of nested transactions performed (list becasue may be nested inside of nested)
318 //! the list is empty if not nested transaction is performed
319 std::list<int> myNestedNum;
321 //! Information related to the every user-transaction
323 int myOCAFNum; ///< number of OCAF transactions related to each "this" transaction, may be 0
324 std::string myId; ///< user-identifier string of transaction
325 //! default constructor with default Id
326 Transaction(): myOCAFNum(0), myId("") {}
329 //! transaction indexes (related to myTransactionsAfterSave) and info about the real transactions
330 //! in myDocument connected to this operation (may be zero for empty transaction)
331 std::list<Transaction> myTransactions;
332 //! list of info about transactions undone (first is oldest undone)
333 std::list<Transaction> myRedos;
335 //! Optimization for finding the shape-label by topological naming names
336 std::map<std::string, TDF_Label> myNamingNames;
337 //! If it is true, features are not executed on update (on abort, undo, redo)
338 bool myExecuteFeatures;
340 bool myIsActive; ///< flag that stores the active/not active state
342 //! The selection feature, if needed
343 FeaturePtr mySelectionFeature;