From: mpv Date: Wed, 2 Apr 2014 11:54:46 +0000 (+0400) Subject: Added object, attribute and data model organization in the document. X-Git-Tag: V_0.1~41^2~2 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=1a0a0306f6d0f56c009b29cba0b53e8d1abd4edf;p=modules%2Fshaper.git Added object, attribute and data model organization in the document. --- diff --git a/src/Model/CMakeLists.txt b/src/Model/CMakeLists.txt index c7b23b3a0..ae05dd926 100644 --- a/src/Model/CMakeLists.txt +++ b/src/Model/CMakeLists.txt @@ -8,14 +8,18 @@ SET(PROJECT_HEADERS Model_Application.h Model_Document.h Model_PluginManager.h - Model_Feature.h + Model_Object.h + Model_Iterator.h + Model_AttributeDocRef.h ) SET(PROJECT_SOURCES Model_Application.cxx Model_Document.cxx Model_PluginManager.cxx - Model_Feature.cxx + Model_Object.cxx + Model_Iterator.cxx + Model_AttributeDocRef.cxx ) ADD_DEFINITIONS(-DMODEL_EXPORTS ${CAS_DEFINITIONS}) diff --git a/src/Model/Model_Application.cxx b/src/Model/Model_Application.cxx index ac1eeb896..aab352e75 100644 --- a/src/Model/Model_Application.cxx +++ b/src/Model/Model_Application.cxx @@ -28,7 +28,7 @@ std::shared_ptr Model_Application::getDocument(std::string theDo if (myDocs.find(theDocID) != myDocs.end()) return myDocs[theDocID]; - std::shared_ptr aNew(new Model_Document); + std::shared_ptr aNew(new Model_Document(theDocID)); myDocs[theDocID] = aNew; return aNew; } diff --git a/src/Model/Model_AttributeDocRef.cxx b/src/Model/Model_AttributeDocRef.cxx new file mode 100644 index 000000000..07712d0b6 --- /dev/null +++ b/src/Model/Model_AttributeDocRef.cxx @@ -0,0 +1,31 @@ +// File: ModelAPI_AttributeDocRef.cxx +// Created: 2 Apr 2014 +// Author: Mikhail PONIKAROV + +#include "Model_AttributeDocRef.h" +#include "Model_Application.h" + +using namespace std; + +void Model_AttributeDocRef::setValue(std::shared_ptr theDoc) +{ + myComment->Set(TCollection_ExtendedString(theDoc->id().c_str())); +} + +std::shared_ptr Model_AttributeDocRef::value() +{ + if (myComment->Get().Length()) + return Model_Application::getApplication()->getDocument( + TCollection_AsciiString(myComment->Get()).ToCString()); + // not initialized + return std::shared_ptr(); +} + +Model_AttributeDocRef::Model_AttributeDocRef(TDF_Label& theLabel) +{ + // check the attribute could be already presented in this doc (after load document) + if (!theLabel.FindAttribute(TDataStd_Comment::GetID(), myComment)) { + // create attribute: not initialized by value yet, just empty string + myComment = TDataStd_Comment::Set(theLabel, ""); + } +} diff --git a/src/Model/Model_AttributeDocRef.h b/src/Model/Model_AttributeDocRef.h new file mode 100644 index 000000000..221855724 --- /dev/null +++ b/src/Model/Model_AttributeDocRef.h @@ -0,0 +1,35 @@ +// File: Model_AttributeDocRef.h +// Created: 2 Apr 2014 +// Author: Mikhail PONIKAROV + +#ifndef Model_AttributeDocRef_HeaderFile +#define Model_AttributeDocRef_HeaderFile + +#include "Model.h" +#include "ModelAPI_AttributeDocRef.h" +#include +#include + +/**\class Model_AttributeDocRef + * \ingroup DataModel + * \brief Attribute that contains reference to another document. + */ + +class MODEL_EXPORT Model_AttributeDocRef : public ModelAPI_AttributeDocRef +{ + Handle_TDataStd_Comment myComment; ///< reference to document is identified as string-id +public: + /// Defines the document referenced from this attribute + virtual void setValue(std::shared_ptr theDoc); + + /// Returns document referenced from this attribute + virtual std::shared_ptr value(); + +protected: + /// Initializes attibutes + Model_AttributeDocRef(TDF_Label& theLabel); + + friend class Model_Object; +}; + +#endif diff --git a/src/Model/Model_Document.cxx b/src/Model/Model_Document.cxx index ffce8a452..6555a22db 100644 --- a/src/Model/Model_Document.cxx +++ b/src/Model/Model_Document.cxx @@ -3,9 +3,14 @@ // Author: Mikhail PONIKAROV #include -#include +#include +#include +#include +#include +#include #include +#include static const int UNDO_LIMIT = 10; // number of possible undo operations @@ -15,7 +20,7 @@ static const int TAG_HISTORY = 3; // tag of the history sub-tree (Root for Model using namespace std; -bool Model_Document::Load(const char* theFileName) +bool Model_Document::load(const char* theFileName) { bool myIsError = Standard_False; /* @@ -56,7 +61,7 @@ bool Model_Document::Load(const char* theFileName) return !myIsError; } -bool Model_Document::Save(const char* theFileName) +bool Model_Document::save(const char* theFileName) { bool myIsError = true; /* @@ -94,78 +99,137 @@ bool Model_Document::Save(const char* theFileName) return !myIsError; } -void Model_Document::Close() +void Model_Document::close() { myDoc->Close(); } -void Model_Document::StartOperation() +void Model_Document::startOperation() { myDoc->NewCommand(); } -void Model_Document::FinishOperation() +void Model_Document::finishOperation() { myDoc->CommitCommand(); myTransactionsAfterSave++; } -void Model_Document::AbortOperation() +void Model_Document::abortOperation() { myDoc->AbortCommand(); } -bool Model_Document::IsOperation() +bool Model_Document::isOperation() { return myDoc->HasOpenCommand() == Standard_True ; } -bool Model_Document::IsModified() +bool Model_Document::isModified() { return myTransactionsAfterSave != 0; } -bool Model_Document::CanUndo() +bool Model_Document::canUndo() { return myDoc->GetAvailableUndos() > 0; } -void Model_Document::Undo() +void Model_Document::undo() { myDoc->Undo(); myTransactionsAfterSave--; } -bool Model_Document::CanRedo() +bool Model_Document::canRedo() { return myDoc->GetAvailableRedos() > 0; } -void Model_Document::Redo() +void Model_Document::redo() { myDoc->Redo(); myTransactionsAfterSave++; } -void Model_Document::AddObject( - std::shared_ptr theFeature, const int theGroupID) +void Model_Document::addFeature( + std::shared_ptr theFeature, const std::string theGroupID) { - std::shared_ptr aModelFeature = - std::dynamic_pointer_cast(theFeature); - if (aModelFeature) { - TDF_Label aGroupLab = myDoc->Main().FindChild(TAG_OBJECTS).FindChild(theGroupID + 1); - TDF_Label anObjLab = aGroupLab.NewChild(); - aModelFeature->setLabel(anObjLab); + TDF_Label aGroupLab = groupLabel(theGroupID); + TDF_Label anObjLab = aGroupLab.NewChild(); + std::shared_ptr aData(new Model_Object); + aData->setLabel(anObjLab); + theFeature->setData(aData); + setUniqueName(theFeature, theGroupID); + theFeature->initAttributes(); + TDataStd_Comment::Set(anObjLab, theFeature->getKind().c_str()); +} + +std::shared_ptr Model_Document::feature(TDF_Label& theLabel) +{ + Handle(TDataStd_Comment) aFeatureID; + if (theLabel.FindAttribute(TDataStd_Comment::GetID(), aFeatureID)) { + string anID(TCollection_AsciiString(aFeatureID->Get()).ToCString()); + std::shared_ptr aResult = Model_PluginManager::get()->createFeature(anID); + std::shared_ptr aData(new Model_Object); + aData->setLabel(theLabel); + aResult->setData(aData); + aResult->initAttributes(); + return aResult; } + return std::shared_ptr(); // not found +} + +shared_ptr Model_Document::subDocument(string theDocID) +{ + return Model_Application::getApplication()->getDocument(theDocID); +} + +shared_ptr Model_Document::featuresIterator(const string theGroup) +{ + shared_ptr aThis(Model_Application::getApplication()->getDocument(myID)); + return shared_ptr(new Model_Iterator(aThis, groupLabel(theGroup))); } -Model_Document::Model_Document() - : myDoc(new TDocStd_Document("BinOcaf")) // binary OCAF format +Model_Document::Model_Document(const std::string theID) + : myID(theID), myDoc(new TDocStd_Document("BinOcaf")) // binary OCAF format { myDoc->SetUndoLimit(UNDO_LIMIT); myTransactionsAfterSave = 0; } -Model_Document::~Model_Document() +TDF_Label Model_Document::groupLabel(const string theGroup) +{ + if (myGroups.find(theGroup) == myGroups.end()) { + myGroups[theGroup] = myDoc->Main().FindChild(TAG_OBJECTS).NewChild(); + } + return myGroups[theGroup]; +} + +void Model_Document::setUniqueName( + shared_ptr theFeature, const string theGroupID) { + // first count all objects of such kind to start with index = count + 1 + int aNumObjects = 0; + shared_ptr anIter = featuresIterator(theGroupID); + for(; anIter->More(); anIter->Next()) { + if (anIter->CurrentKind() == theFeature->getKind()) + aNumObjects++; + } + // generate candidate name + stringstream aNameStream; + aNameStream<getKind()<<"_"<More();) { + if (anIter->CurrentName() == aName) { + aNumObjects++; + stringstream aNameStream; + aNameStream<getKind()<<"_"<Next(); + } + + theFeature->data()->setName(aName); } diff --git a/src/Model/Model_Document.h b/src/Model/Model_Document.h index 22ad95fa0..bc7d6db74 100644 --- a/src/Model/Model_Document.h +++ b/src/Model/Model_Document.h @@ -1,4 +1,4 @@ -// File: Model_Document.cxx +// File: Model_Document.h // Created: 28 Feb 2014 // Author: Mikhail PONIKAROV @@ -8,6 +8,7 @@ #include #include #include +#include class Handle_Model_Document; @@ -24,56 +25,79 @@ class Model_Document: public ModelAPI_Document { public: - //! Creates new document with binary file format - Model_Document(); - //! Deletes all high-level data, managed this document - ~Model_Document(); - //! Loads the OCAF document from the file. //! \param theFileName full name of the file to load //! \param theStudyID identifier of the SALOME study to associate with loaded file //! \returns true if file was loaded successfully - MODEL_EXPORT bool Load(const char* theFileName); + MODEL_EXPORT bool load(const char* theFileName); //! Saves the OCAF document to the file. //! \param theFileName full name of the file to store //! \returns true if file was stored successfully - MODEL_EXPORT bool Save(const char* theFileName); + MODEL_EXPORT bool save(const char* theFileName); //! Removes document data - MODEL_EXPORT void Close(); + MODEL_EXPORT void close(); //! Starts a new operation (opens a tansaction) - MODEL_EXPORT void StartOperation(); + MODEL_EXPORT void startOperation(); //! Finishes the previously started operation (closes the transaction) - MODEL_EXPORT void FinishOperation(); + MODEL_EXPORT void finishOperation(); //! Aborts the operation - MODEL_EXPORT void AbortOperation(); + MODEL_EXPORT void abortOperation(); //! Returns true if operation has been started, but not yet finished or aborted - MODEL_EXPORT bool IsOperation(); + MODEL_EXPORT bool isOperation(); //! Returns true if document was modified (since creation/opening) - MODEL_EXPORT bool IsModified(); + MODEL_EXPORT bool isModified(); //! Returns True if there are available Undos - MODEL_EXPORT bool CanUndo(); + MODEL_EXPORT bool canUndo(); //! Undoes last operation - MODEL_EXPORT void Undo(); + MODEL_EXPORT void undo(); //! Returns True if there are available Redos - MODEL_EXPORT bool CanRedo(); + MODEL_EXPORT bool canRedo(); //! Redoes last operation - MODEL_EXPORT void Redo(); + MODEL_EXPORT void redo(); - //! Adds to the document the new object of the given group id + //! Adds to the document the new feature of the given group id //! \param theFeature a feature object that will be connected to the document in this method //! \param theGroupID identifier of the groups of objects (must be greater than zero) - MODEL_EXPORT virtual void AddObject(std::shared_ptr theFeature, - const int theGroupID); + MODEL_EXPORT virtual void addFeature(std::shared_ptr theFeature, + const std::string theGroupID); + + //! Returns the existing feature by the label + //! \param theLabel base label of the feature + MODEL_EXPORT virtual std::shared_ptr feature(TDF_Label& theLabel); + + //! Adds a new sub-document by the identifier, or returns existing one if it is already exist + MODEL_EXPORT virtual std::shared_ptr subDocument(std::string theDocID); + + //! Creates an iterator of the features by the specific groups + MODEL_EXPORT virtual std::shared_ptr featuresIterator( + const std::string theGroup); + + MODEL_EXPORT virtual const std::string& id() const {return myID;} protected: + //! Returns (creates if needed) the group label + TDF_Label groupLabel(const std::string theGroup); + + //! Initializes feature with a unique name in this group (unique name is generated as + //! feature type + "_" + index + void setUniqueName( + std::shared_ptr theFeature, const std::string theGroupID); + + //! Creates new document with binary file format + Model_Document(const std::string theID); + + friend class Model_Application; + private: + std::string myID; ///< identifier of the document in the application Handle_TDocStd_Document myDoc; ///< OCAF document int myTransactionsAfterSave; ///< number of transactions after the last "save" call, used for "IsModified" method + std::map myGroups; ///< root labels of the features groups identified by names }; #endif diff --git a/src/Model/Model_Feature.cxx b/src/Model/Model_Feature.cxx deleted file mode 100644 index 10455a7fc..000000000 --- a/src/Model/Model_Feature.cxx +++ /dev/null @@ -1,16 +0,0 @@ -// File: Model_Feature.hxx -// Created: 21 Mar 2014 -// Author: Mikhail PONIKAROV - -#include - -using namespace std; - -Model_Feature::Model_Feature() -{ -} - -void Model_Feature::setLabel(TDF_Label& theLab) -{ - myLab = theLab; -} diff --git a/src/Model/Model_Feature.h b/src/Model/Model_Feature.h deleted file mode 100644 index ac930c078..000000000 --- a/src/Model/Model_Feature.h +++ /dev/null @@ -1,31 +0,0 @@ -// File: Model_Feature.hxx -// Created: 21 Mar 2014 -// Author: Mikhail PONIKAROV - -#ifndef Model_Feature_HeaderFile -#define Model_Feature_HeaderFile - -#include "Model.h" -#include -#include - -/**\class Model_Feature - * \ingroup DataModel - * \brief General object of the application that allows - * to get/set attributes from the document and compute result of an operation. - */ - -class Model_Feature: public ModelAPI_Feature -{ - TDF_Label myLab; ///< label of the feature in the document - - Model_Feature(); - friend class Model_PluginManager; -public: - /// Returns the kind of a feature (like "Point") - virtual std::string GetKind() = 0; - - void setLabel(TDF_Label& theLab); -}; - -#endif diff --git a/src/Model/Model_Iterator.cxx b/src/Model/Model_Iterator.cxx new file mode 100644 index 000000000..917d89288 --- /dev/null +++ b/src/Model/Model_Iterator.cxx @@ -0,0 +1,45 @@ +// File: Model_Iterator.hxx +// Created: 1 Apr 2014 +// Author: Mikhail PONIKAROV + +#include "Model_Iterator.h" +#include "Model_Document.h" +#include +#include + +using namespace std; + +void Model_Iterator::Next() +{ + return myIter.Next(); +} + +bool Model_Iterator::More() +{ + return myIter.More(); +} + +shared_ptr Model_Iterator::Current() +{ + TDF_Label aLab = myIter.Value()->Label(); + return myDoc->feature(aLab); +} + +string Model_Iterator::CurrentKind() +{ + return string(TCollection_AsciiString( + Handle(TDataStd_Comment)::DownCast(myIter.Value())->Get()).ToCString()); +} + +string Model_Iterator::CurrentName() +{ + TDF_Label aLab = myIter.Value()->Label(); + Handle(TDataStd_Name) aName; + if (aLab.FindAttribute(TDataStd_Name::GetID(), aName)) + return string(TCollection_AsciiString(aName->Get()).ToCString()); + return ""; // name is not found +} + +Model_Iterator::Model_Iterator(std::shared_ptr theDoc, TDF_Label theLab) + : myDoc(theDoc), myIter(theLab, TDataStd_Comment::GetID(), Standard_False) +{} diff --git a/src/Model/Model_Iterator.h b/src/Model/Model_Iterator.h new file mode 100644 index 000000000..2ec50ab3a --- /dev/null +++ b/src/Model/Model_Iterator.h @@ -0,0 +1,46 @@ +// File: Model_Iterator.h +// Created: 1 Apr 2014 +// Author: Mikhail PONIKAROV + +#ifndef Model_Iterator_HeaderFile +#define Model_Iterator_HeaderFile + +#include "Model.h" +#include "ModelAPI_Iterator.h" +#include +#include + +class Model_Document; + +/**\class Model_Iterator + * \ingroup DataModel + * \brief Allows to iterate features of the document. Is created by the document + * (see method featuresIterator). + */ + +class MODEL_EXPORT Model_Iterator : public ModelAPI_Iterator +{ + std::shared_ptr myDoc; ///< the document of iterated objects + TDF_ChildIDIterator myIter; ///< iterator of the features-labels +public: + /// Iterates to the next feature + virtual void Next(); + /// Returns true if the current iteration is valid and next iteration is possible + virtual bool More(); + /// Returns the currently iterated feature + virtual std::shared_ptr Current(); + /// Returns the kind of the current feature (faster than Current()->getKind()) + virtual std::string CurrentKind(); + /// Returns the name of the current feature (faster than Current()->getName()) + virtual std::string CurrentName(); + +protected: + /// Initializes iterator + /// \param theDoc document where the iteration is performed + /// \param theLab label of the features group to iterate + Model_Iterator(std::shared_ptr theDoc, TDF_Label theLab); + + friend class Model_Document; +}; + +#endif diff --git a/src/Model/Model_Object.cxx b/src/Model/Model_Object.cxx new file mode 100644 index 000000000..735080954 --- /dev/null +++ b/src/Model/Model_Object.cxx @@ -0,0 +1,58 @@ +// File: Model_Object.hxx +// Created: 21 Mar 2014 +// Author: Mikhail PONIKAROV + +#include +#include +#include + +using namespace std; + +Model_Object::Model_Object() +{ +} + +void Model_Object::setLabel(TDF_Label& theLab) +{ + myLab = theLab; +} + +string Model_Object::getName() +{ + Handle(TDataStd_Name) aName; + if (myLab.FindAttribute(TDataStd_Name::GetID(), aName)) + return string(TCollection_AsciiString(aName->Get()).ToCString()); + return ""; // not defined +} + +void Model_Object::setName(string theName) +{ + TDataStd_Name::Set(myLab, theName.c_str()); +} + +void Model_Object::addAttribute(string theID, string theAttrType) +{ + TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1); + ModelAPI_Attribute* anAttr = 0; + if (theAttrType == ModelAPI_AttributeDocRef::type()) + anAttr = new Model_AttributeDocRef(anAttrLab); + if (anAttr) + myAttrs[theID] = std::shared_ptr(anAttr); + else + ; // TODO: generate error on unknown attribute request and/or add mechanism for customization +} + +shared_ptr Model_Object::docRef(const string theID) +{ + map >::iterator aFound = myAttrs.find(theID); + if (aFound == myAttrs.end()) { + // TODO: generate error on unknown attribute request and/or add mechanism for customization + return std::shared_ptr(); + } + shared_ptr aRes = + dynamic_pointer_cast(aFound->second); + if (!aRes) { + // TODO: generate error on invalid attribute type request + } + return aRes; +} diff --git a/src/Model/Model_Object.h b/src/Model/Model_Object.h new file mode 100644 index 000000000..d96f90b18 --- /dev/null +++ b/src/Model/Model_Object.h @@ -0,0 +1,50 @@ +// File: Model_Object.hxx +// Created: 21 Mar 2014 +// Author: Mikhail PONIKAROV + +#ifndef Model_Object_HeaderFile +#define Model_Object_HeaderFile + +#include "Model.h" +#include +#include + +#include + +class ModelAPI_Attribute; + +/**\class Model_Object + * \ingroup DataModel + * \brief General object of the application that allows + * to get/set attributes from the document and compute result of an operation. + */ + +class Model_Object: public ModelAPI_Object +{ + TDF_Label myLab; ///< label of the feature in the document + /// All attributes of the object identified by the attribute ID + std::map > myAttrs; + + Model_Object(); + + friend class Model_Document; + +public: + /// Returns the name of the feature visible by the user in the object browser + virtual std::string getName(); + /// Defines the name of the feature visible by the user in the object browser + virtual void setName(std::string theName); + /// Returns the attribute that references to another document + std::shared_ptr docRef(const std::string theID); + + /// Initializes object by the attributes: must be called just after the object is created + /// for each attribute of the object + /// \param theID identifier of the attribute that can be referenced by this ID later + /// \param theAttrType type of the created attribute (received from the type method) + virtual void addAttribute(std::string theID, std::string theAttrType); + + /// Puts feature to the document data sub-structure + void setLabel(TDF_Label& theLab); +}; + +#endif diff --git a/src/Model/Model_PluginManager.cxx b/src/Model/Model_PluginManager.cxx index 2ef8883f4..f2b7a5594 100644 --- a/src/Model/Model_PluginManager.cxx +++ b/src/Model/Model_PluginManager.cxx @@ -5,7 +5,7 @@ #include #include #include -#include +#include #include #include #include @@ -30,6 +30,7 @@ std::shared_ptr Model_PluginManager::createFeature(string theF if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) { std::shared_ptr aCreated = myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID); + return aCreated; } } diff --git a/src/Model/Model_PluginManager.h b/src/Model/Model_PluginManager.h index 47b326ac2..196ebd6ca 100644 --- a/src/Model/Model_PluginManager.h +++ b/src/Model/Model_PluginManager.h @@ -18,11 +18,10 @@ class Model_Document; * plugin contains which feature, loads and stores reference to loaded plugins by * the feature functionality request. */ - class Model_PluginManager : public ModelAPI_PluginManager, public Event_Listener { bool myPluginsInfoLoaded; ///< it true if plugins information is loaded - /// map of feature IDs to plugin name and object + /// map of feature IDs to plugin name std::map myPlugins; std::map myPluginObjs; ///< instances of the already plugins std::string myCurrentPluginName; ///< name of the plugin that must be loaded currently diff --git a/src/ModelAPI/CMakeLists.txt b/src/ModelAPI/CMakeLists.txt index 0a4868ddc..950cbc530 100644 --- a/src/ModelAPI/CMakeLists.txt +++ b/src/ModelAPI/CMakeLists.txt @@ -12,7 +12,11 @@ SET(PROJECT_HEADERS ModelAPI_PluginManager.h ModelAPI_Plugin.h ModelAPI_Feature.h + ModelAPI_Iterator.h + ModelAPI_Object.h ModelAPI_Document.h + ModelAPI_Attribute.h + ModelAPI_AttributeDocRef.h ) SET(PROJECT_SOURCES diff --git a/src/ModelAPI/ModelAPI.i b/src/ModelAPI/ModelAPI.i index bbb3d7a41..7490fc636 100644 --- a/src/ModelAPI/ModelAPI.i +++ b/src/ModelAPI/ModelAPI.i @@ -5,6 +5,7 @@ #include "ModelAPI_Document.h" #include "ModelAPI_PluginManager.h" #include "ModelAPI_Feature.h" + #include "ModelAPI_Object.h" %} // to avoid error on this @@ -13,13 +14,17 @@ // standard definitions %include "typemaps.i" %include "std_string.i" +%include // boost pointers // %include -// %shared_ptr(ModelAPI_PluginManager) -// %shared_ptr(ModelAPI_Feature) +%shared_ptr(ModelAPI_Document) +%shared_ptr(ModelAPI_PluginManager) +%shared_ptr(ModelAPI_Feature) +%shared_ptr(ModelAPI_Object) // all supported interfaces %include "ModelAPI_Document.h" %include "ModelAPI_PluginManager.h" %include "ModelAPI_Feature.h" +%include "ModelAPI_Object.h" diff --git a/src/ModelAPI/ModelAPI_Attribute.h b/src/ModelAPI/ModelAPI_Attribute.h new file mode 100644 index 000000000..cd7d36a54 --- /dev/null +++ b/src/ModelAPI/ModelAPI_Attribute.h @@ -0,0 +1,29 @@ +// File: ModelAPI_Attribute.h +// Created: 2 Apr 2014 +// Author: Mikhail PONIKAROV + +#ifndef ModelAPI_Attribute_HeaderFile +#define ModelAPI_Attribute_HeaderFile + +#include "ModelAPI.h" +#include + +/**\class ModelAPI_Attribute + * \ingroup DataModel + * \brief Generic attribute of the Object. + */ + +class MODELAPI_EXPORT ModelAPI_Attribute +{ +public: + + /// Returns the type of this class of attributes, not static method + virtual std::string attributeType() = 0; + +protected: + /// Objects are created for features automatically + ModelAPI_Attribute() + {} +}; + +#endif diff --git a/src/ModelAPI/ModelAPI_AttributeDocRef.h b/src/ModelAPI/ModelAPI_AttributeDocRef.h new file mode 100644 index 000000000..e9a602dcc --- /dev/null +++ b/src/ModelAPI/ModelAPI_AttributeDocRef.h @@ -0,0 +1,37 @@ +// File: ModelAPI_AttributeDocRef.h +// Created: 2 Apr 2014 +// Author: Mikhail PONIKAROV + +#ifndef ModelAPI_AttributeDocRef_HeaderFile +#define ModelAPI_AttributeDocRef_HeaderFile + +#include "ModelAPI_Attribute.h" +#include "ModelAPI_Document.h" + +/**\class ModelAPI_AttributeDocRef + * \ingroup DataModel + * \brief Attribute that contains reference to another document. + */ + +class MODELAPI_EXPORT ModelAPI_AttributeDocRef : public ModelAPI_Attribute +{ +public: + /// Defines the document referenced from this attribute + virtual void setValue(std::shared_ptr theDoc) = 0; + + /// Returns document referenced from this attribute + virtual std::shared_ptr value() = 0; + + /// Returns the type of this class of attributes + static std::string type() {return "DocRef";} + + /// Returns the type of this class of attributes, not static method + virtual std::string attributeType() {return type();} + +protected: + /// Objects are created for features automatically + ModelAPI_AttributeDocRef() + {} +}; + +#endif diff --git a/src/ModelAPI/ModelAPI_Document.h b/src/ModelAPI/ModelAPI_Document.h index 6f7205264..90a6a9184 100644 --- a/src/ModelAPI/ModelAPI_Document.h +++ b/src/ModelAPI/ModelAPI_Document.h @@ -6,9 +6,19 @@ #define ModelAPI_Document_HeaderFile #include +#include #include class ModelAPI_Feature; +class ModelAPI_Iterator; + +/// Common groups identifiers +/// Group of parameters +static const std::string PARAMETERS_GROUP = "Parameters"; +/// Group of constructions +static const std::string CONSTRUCTIONS_GROUP = "Construction"; +/// Group of parts +static const std::string PARTS_GROUP = "Parts"; /**\class Model_Document * \ingroup DataModel @@ -18,50 +28,57 @@ class ModelAPI_Feature; * Also it provides acces to this data: open/save, transactions management etc. * to provide access to all stored data. */ - class ModelAPI_Document { public: - //! Loads the OCAF document from the file. //! \param theFileName full name of the file to load //! \param theStudyID identifier of the SALOME study to associate with loaded file //! \returns true if file was loaded successfully - MODELAPI_EXPORT virtual bool Load(const char* theFileName) = 0; + MODELAPI_EXPORT virtual bool load(const char* theFileName) = 0; //! Saves the OCAF document to the file. //! \param theFileName full name of the file to store //! \returns true if file was stored successfully - MODELAPI_EXPORT virtual bool Save(const char* theFileName) = 0; + MODELAPI_EXPORT virtual bool save(const char* theFileName) = 0; //! Removes document data - MODELAPI_EXPORT virtual void Close() = 0; + MODELAPI_EXPORT virtual void close() = 0; //! Starts a new operation (opens a tansaction) - MODELAPI_EXPORT virtual void StartOperation() = 0; + MODELAPI_EXPORT virtual void startOperation() = 0; //! Finishes the previously started operation (closes the transaction) - MODELAPI_EXPORT virtual void FinishOperation() = 0; + MODELAPI_EXPORT virtual void finishOperation() = 0; //! Aborts the operation - MODELAPI_EXPORT virtual void AbortOperation() = 0; + MODELAPI_EXPORT virtual void abortOperation() = 0; //! Returns true if operation has been started, but not yet finished or aborted - MODELAPI_EXPORT virtual bool IsOperation() = 0; + MODELAPI_EXPORT virtual bool isOperation() = 0; //! Returns true if document was modified (since creation/opening) - MODELAPI_EXPORT virtual bool IsModified() = 0; + MODELAPI_EXPORT virtual bool isModified() = 0; //! Returns True if there are available Undos - MODELAPI_EXPORT virtual bool CanUndo() = 0; + MODELAPI_EXPORT virtual bool canUndo() = 0; //! Undoes last operation - MODELAPI_EXPORT virtual void Undo() = 0; + MODELAPI_EXPORT virtual void undo() = 0; //! Returns True if there are available Redos - MODELAPI_EXPORT virtual bool CanRedo() = 0; + MODELAPI_EXPORT virtual bool canRedo() = 0; //! Redoes last operation - MODELAPI_EXPORT virtual void Redo() = 0; + MODELAPI_EXPORT virtual void redo() = 0; - //! Adds to the document the new object of the given group id + //! Adds to the document the new feature of the given group id //! \param theFeature a feature object that will be connected to the document in this method - //! \param theGroupID identifier of the groups of objects (must be greater than zero) - MODELAPI_EXPORT virtual void AddObject(std::shared_ptr theFeature, - const int theGroupID) = 0; + //! \param theGroupID identifier of the groups of object + MODELAPI_EXPORT virtual void addFeature(std::shared_ptr theFeature, + const std::string theGroupID) = 0; + + ///! Adds a new sub-document by the identifier, or returns existing one if it is already exist + MODELAPI_EXPORT virtual std::shared_ptr subDocument(std::string theDocID) = 0; + + ///! Creates an iterator of the features by the specific groups + MODELAPI_EXPORT virtual std::shared_ptr featuresIterator( + const std::string theGroup) = 0; + + MODELAPI_EXPORT virtual const std::string& id() const = 0; protected: /// Only for SWIG wrapping it is here diff --git a/src/ModelAPI/ModelAPI_Feature.h b/src/ModelAPI/ModelAPI_Feature.h index 2c9339c74..a33d05084 100644 --- a/src/ModelAPI/ModelAPI_Feature.h +++ b/src/ModelAPI/ModelAPI_Feature.h @@ -7,27 +7,42 @@ #include "ModelAPI.h" #include +#include -class ModelAPI_Feature; +class ModelAPI_Object; /**\class ModelAPI_Feature * \ingroup DataModel - * \brief General object of the application that allows - * to get/set attributes from the document and compute result of an operation. + * \brief Functionality of the model object: to update result, + * to initialize attributes, etc. */ class MODELAPI_EXPORT ModelAPI_Feature { + std::shared_ptr myData; ///< manager of the data model of a feature + public: /// Returns the kind of a feature (like "Point") - virtual std::string GetKind() = 0; + virtual std::string getKind() = 0; + + /// Request for initialization of data model of the feature: adding all attributes + virtual void initAttributes() = 0; + + /// Computes or recomputes the result + virtual void execute() = 0; + + /// Returns the data manager of this feature + std::shared_ptr data() {return myData;} protected: /// Use plugin manager for features creation: this method is /// defined here only for SWIG-wrapping ModelAPI_Feature() - { - } + {} + + /// Sets the data manager of an object (document does) + void setData(std::shared_ptr theData) {myData = theData;} + friend class Model_Document; }; #endif diff --git a/src/ModelAPI/ModelAPI_Iterator.h b/src/ModelAPI/ModelAPI_Iterator.h new file mode 100644 index 000000000..84afc7381 --- /dev/null +++ b/src/ModelAPI/ModelAPI_Iterator.h @@ -0,0 +1,42 @@ +// File: ModelAPI_Iterator.hxx +// Created: 1 Apr 2014 +// Author: Mikhail PONIKAROV + +#ifndef ModelAPI_Iterator_HeaderFile +#define ModelAPI_Iterator_HeaderFile + +#include "ModelAPI.h" +#include +#include + +class ModelAPI_Feature; +class ModelAPI_Document; + +/**\class ModelAPI_Iterator + * \ingroup DataModel + * \brief Allows to iterate features of the document. Is created by the document + * (see method featuresIterator). + */ + +class MODELAPI_EXPORT ModelAPI_Iterator +{ +public: + /// Iterates to the next feature + virtual void Next() = 0; + /// Returns true if the current iteration is valid and next iteration is possible + virtual bool More() = 0; + /// Returns the currently iterated feature + virtual std::shared_ptr Current() = 0; + /// Returns the kind of the current feature (faster than Current()->getKind()) + virtual std::string CurrentKind() = 0; + /// Returns the name of the current feature (faster than Current()->getName()) + virtual std::string CurrentName() = 0; + +protected: + /// Use plugin manager for features creation: this method is + /// defined here only for SWIG-wrapping + ModelAPI_Iterator() + {} +}; + +#endif diff --git a/src/ModelAPI/ModelAPI_Object.h b/src/ModelAPI/ModelAPI_Object.h new file mode 100644 index 000000000..490e5617a --- /dev/null +++ b/src/ModelAPI/ModelAPI_Object.h @@ -0,0 +1,45 @@ +// File: ModelAPI_Object.hxx +// Created: 21 Mar 2014 +// Author: Mikhail PONIKAROV + +#ifndef ModelAPI_Object_HeaderFile +#define ModelAPI_Object_HeaderFile + +#include "ModelAPI.h" +#include +#include + +class ModelAPI_AttributeDocRef; + +/**\class ModelAPI_Object + * \ingroup DataModel + * \brief General object of the application that allows + * to get/set attributes from the document and compute result of an operation. + */ + +class MODELAPI_EXPORT ModelAPI_Object +{ +public: + + /// Returns the name of the feature visible by the user in the object browser + virtual std::string getName() = 0; + + /// Defines the name of the feature visible by the user in the object browser + virtual void setName(std::string theName) = 0; + + /// Returns the attribute that references to another document + virtual std::shared_ptr docRef(const std::string theID) = 0; + + /// Initializes object by the attributes: must be called just after the object is created + /// for each attribute of the object + /// \param theID identifier of the attribute that can be referenced by this ID later + /// \param theAttrType type of the created attribute (received from the type method) + virtual void addAttribute(std::string theID, std::string theAttrType) = 0; + +protected: + /// Objects are created for features automatically + ModelAPI_Object() + {} +}; + +#endif diff --git a/src/ModelAPI/ModelAPI_PluginManager.cxx b/src/ModelAPI/ModelAPI_PluginManager.cxx index 6ec5a9d7a..4e9406a9a 100644 --- a/src/ModelAPI/ModelAPI_PluginManager.cxx +++ b/src/ModelAPI/ModelAPI_PluginManager.cxx @@ -7,8 +7,18 @@ #include // to avoid unresolved ModelAPI_Feature() #include +// to avoid unresolved ModelAPI_Object() +#include // to avoid unresolved ModelAPI_Plugin() #include +// to avoid unresolved ModelAPI_Iterator() +#include +// to avoid unresolved ModelAPI_Iterator() +#include +// to avoid unresolved ModelAPI_Attribute() +#include +// to avoid unresolved ModelAPI_AttributeDocRef() +#include #ifdef WIN32 #include @@ -62,6 +72,7 @@ string library(const string& theLibName) return aLibName; } +// for debug purpose only (cerr), before the error management system is implemented #include void ModelAPI_PluginManager::loadLibrary(const string theLibName) { diff --git a/src/PartSetPlugin/PartSetPlugin_NewPart.cxx b/src/PartSetPlugin/PartSetPlugin_NewPart.cxx index 7959ef044..2b2c59bc3 100644 --- a/src/PartSetPlugin/PartSetPlugin_NewPart.cxx +++ b/src/PartSetPlugin/PartSetPlugin_NewPart.cxx @@ -2,8 +2,26 @@ // Created: 27 Mar 2014 // Author: Mikhail PONIKAROV -#include "PartSetPlugin_NewPart.hxx" +#include "PartSetPlugin_NewPart.h" +#include "ModelAPI_PluginManager.h" +#include "ModelAPI_Document.h" +#include "ModelAPI_Object.h" +#include "ModelAPI_AttributeDocRef.h" + +using namespace std; PartSetPlugin_NewPart::PartSetPlugin_NewPart() { } + +void PartSetPlugin_NewPart::initAttributes() +{ + data()->addAttribute(ModelAPI_AttributeDocRef::type(), PART_DOC_REF); +} + +void PartSetPlugin_NewPart::execute() +{ + shared_ptr aPartSetDoc = ModelAPI_PluginManager::get()->rootDocument(); + data()->setName(string("Part_") + "1"); + aPartSetDoc->subDocument(string("Part_") + "1"); +} diff --git a/src/PartSetPlugin/PartSetPlugin_NewPart.h b/src/PartSetPlugin/PartSetPlugin_NewPart.h index 9b824c0ee..1294d4ddb 100644 --- a/src/PartSetPlugin/PartSetPlugin_NewPart.h +++ b/src/PartSetPlugin/PartSetPlugin_NewPart.h @@ -8,16 +8,24 @@ #include "PartSetPlugin.h" #include +/// part reference attribute +const std::string PART_DOC_REF = "PartDocument"; + /**\class PartSetPlugin_NewPart * \ingroup DataModel * \brief Feature for creation of the new part in PartSet. */ - class PartSetPlugin_NewPart: public ModelAPI_Feature { public: /// Returns the kind of a feature - PARTSETPLUGIN_EXPORT virtual std::string GetKind() {return "new_part";} + PARTSETPLUGIN_EXPORT virtual std::string getKind() {return "new_part";} + + /// Creates a new part document if needed + PARTSETPLUGIN_EXPORT virtual void execute(); + + /// Request for initialization of data model of the feature: adding all attributes + PARTSETPLUGIN_EXPORT virtual void initAttributes(); /// Use plugin manager for features creation PartSetPlugin_NewPart(); diff --git a/src/PartSetPlugin/PartSetPlugin_Plugin.cxx b/src/PartSetPlugin/PartSetPlugin_Plugin.cxx index 91baf0bc4..61fbc5a0b 100644 --- a/src/PartSetPlugin/PartSetPlugin_Plugin.cxx +++ b/src/PartSetPlugin/PartSetPlugin_Plugin.cxx @@ -5,9 +5,6 @@ using namespace std; -// group identification of the newly created parts in the Part Set document -const int MY_PARTS_GROUP = 1; - // the only created instance of this plugin static PartSetPlugin_Plugin* MY_INSTANCE = new PartSetPlugin_Plugin(); @@ -21,11 +18,11 @@ std::shared_ptr PartSetPlugin_Plugin::createFeature(string the { std::shared_ptr aCreated; if (theFeatureID == "new_part") { - aCreated = std::shared_ptr(new PartSetPlugin_NewPart()); + aCreated = std::shared_ptr(new PartSetPlugin_NewPart); } // add to a root document for the current moment if (aCreated) - ModelAPI_PluginManager::get()->rootDocument()->AddObject(aCreated, MY_PARTS_GROUP); + ModelAPI_PluginManager::get()->rootDocument()->addFeature(aCreated, PARTS_GROUP); // feature of such kind is not found return aCreated; }