From: mpv Date: Mon, 7 Apr 2014 10:40:27 +0000 (+0400) Subject: Connect feature with group, document and index in the group X-Git-Tag: V_0.1~34^2~1 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=dc2758f96b7ea27b2c5a5629d98c7a47e6db0357;p=modules%2Fshaper.git Connect feature with group, document and index in the group --- diff --git a/src/Model/Model_Document.cxx b/src/Model/Model_Document.cxx index 38f05c85f..075866f56 100644 --- a/src/Model/Model_Document.cxx +++ b/src/Model/Model_Document.cxx @@ -153,34 +153,46 @@ void Model_Document::redo() myTransactionsAfterSave++; } -void Model_Document::addFeature( - std::shared_ptr theFeature, const std::string theGroupID) +shared_ptr Model_Document::addFeature(string theID) { - TDF_Label aGroupLab = groupLabel(theGroupID); + shared_ptr aFeature = ModelAPI_PluginManager::get()->createFeature(theID); + if (aFeature) { + dynamic_pointer_cast(aFeature->documentToAdd())->addFeature(aFeature); + } else { + // TODO: generate error that feature is not created + } + return aFeature; +} + +void Model_Document::addFeature(const std::shared_ptr theFeature) +{ + TDF_Label aGroupLab = groupLabel(theFeature->getGroup()); TDF_Label anObjLab = aGroupLab.NewChild(); std::shared_ptr aData(new Model_Object); aData->setLabel(anObjLab); + aData->setDocument(Model_Application::getApplication()->getDocument(myID)); theFeature->setData(aData); - setUniqueName(theFeature, theGroupID); + setUniqueName(theFeature); theFeature->initAttributes(); + // keep the feature ID to restore document later correctly TDataStd_Comment::Set(anObjLab, theFeature->getKind().c_str()); // event: model is updated static Event_ID anEvent = Event_Loop::eventByName(EVENT_MODEL_UPDATED); Event_Message anUpdateMsg(anEvent, this); Event_Loop::loop()->send(anUpdateMsg); - } -std::shared_ptr Model_Document::feature(TDF_Label& theLabel) +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, false); + Model_PluginManager::get()->createFeature(anID); std::shared_ptr aData(new Model_Object); aData->setLabel(theLabel); + aData->setDocument(Model_Application::getApplication()->getDocument(myID)); aResult->setData(aData); aResult->initAttributes(); return aResult; @@ -188,6 +200,17 @@ std::shared_ptr Model_Document::feature(TDF_Label& theLabel) return std::shared_ptr(); // not found } +int Model_Document::featureIndex(shared_ptr theFeature) +{ + if (theFeature->data()->document().get() != this) + return theFeature->data()->document()->featureIndex(theFeature); + shared_ptr anIter(featuresIterator(theFeature->getGroup())); + for(int anIndex = 0; anIter->more(); anIter->next(), anIndex++) + if (anIter->is(theFeature)) + return anIndex; + return -1; // not found +} + shared_ptr Model_Document::subDocument(string theDocID) { return Model_Application::getApplication()->getDocument(theDocID); @@ -229,11 +252,11 @@ TDF_Label Model_Document::groupLabel(const string theGroup) } void Model_Document::setUniqueName( - shared_ptr theFeature, const string theGroupID) + shared_ptr theFeature) { // first count all objects of such kind to start with index = count + 1 int aNumObjects = 0; - shared_ptr anIter = featuresIterator(theGroupID); + shared_ptr anIter = featuresIterator(theFeature->getGroup()); for(; anIter->more(); anIter->next()) { if (anIter->currentKind() == theFeature->getKind()) aNumObjects++; @@ -243,13 +266,13 @@ void Model_Document::setUniqueName( aNameStream<getKind()<<"_"<more();) { + for(anIter = featuresIterator(theFeature->getGroup()); anIter->more();) { if (anIter->currentName() == aName) { aNumObjects++; stringstream aNameStream; aNameStream<getKind()<<"_"<getGroup()); } else anIter->next(); } diff --git a/src/Model/Model_Document.h b/src/Model/Model_Document.h index b1e863e3f..a69d969ba 100644 --- a/src/Model/Model_Document.h +++ b/src/Model/Model_Document.h @@ -59,11 +59,9 @@ public: //! Redoes last operation MODEL_EXPORT void redo(); - //! 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 addFeature(std::shared_ptr theFeature, - const std::string theGroupID); + //! Adds to the document the new feature of the given feature id + //! \param creates feature and puts it in the document + MODEL_EXPORT virtual std::shared_ptr addFeature(std::string theID); //! Returns the existing feature by the label //! \param theLabel base label of the feature @@ -85,6 +83,10 @@ public: ///! Returns the vector of groups already added to the document MODEL_EXPORT virtual const std::vector& getGroups() const; + //! Returns the index of feature in the group (zero based) + //! \retruns -1 if not found + MODEL_EXPORT virtual int featureIndex(std::shared_ptr theFeature); + protected: //! Returns (creates if needed) the group label @@ -92,8 +94,10 @@ protected: //! 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); + void setUniqueName(std::shared_ptr theFeature); + + //! Adds to the document the new feature + void addFeature(const std::shared_ptr theFeature); //! Creates new document with binary file format Model_Document(const std::string theID); diff --git a/src/Model/Model_Iterator.cxx b/src/Model/Model_Iterator.cxx index ae62fa928..4e2cf0942 100644 --- a/src/Model/Model_Iterator.cxx +++ b/src/Model/Model_Iterator.cxx @@ -4,6 +4,8 @@ #include "Model_Iterator.h" #include "Model_Document.h" +#include "ModelAPI_Feature.h" +#include "Model_Object.h" #include #include @@ -49,6 +51,14 @@ int Model_Iterator::numIterationsLeft() return aResult; } +bool Model_Iterator::is(std::shared_ptr theFeature) +{ + return myIter.Value()->Label() == + dynamic_pointer_cast(theFeature->data())->label(); + +} + + 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 index 4da78e267..80fc76caf 100644 --- a/src/Model/Model_Iterator.h +++ b/src/Model/Model_Iterator.h @@ -37,6 +37,10 @@ public: /// \returns number of left iterations virtual int numIterationsLeft(); + /// Compares the current feature with the given one + /// \returns true if given feature equals to the current one + virtual bool is(std::shared_ptr theFeature); + protected: /// Initializes iterator /// \param theDoc document where the iteration is performed diff --git a/src/Model/Model_Object.h b/src/Model/Model_Object.h index ecaca8920..6b1f785f4 100644 --- a/src/Model/Model_Object.h +++ b/src/Model/Model_Object.h @@ -19,15 +19,20 @@ class ModelAPI_Attribute; * to get/set attributes from the document and compute result of an operation. */ -class Model_Object: public ModelAPI_Object +class MODEL_EXPORT 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; + std::shared_ptr myDoc; ///< document this feature belongs to + Model_Object(); + TDF_Label label() {return myLab;} + friend class Model_Document; + friend class Model_Iterator; public: /// Returns the name of the feature visible by the user in the object browser @@ -45,8 +50,14 @@ public: /// \param theAttrType type of the created attribute (received from the type method) virtual void addAttribute(std::string theID, std::string theAttrType); + /// Returns the document of this data + virtual std::shared_ptr document() {return myDoc;} + /// Puts feature to the document data sub-structure void setLabel(TDF_Label& theLab); + + /// Sets the document of this data + virtual void setDocument(const std::shared_ptr& theDoc) {myDoc = theDoc;} }; #endif diff --git a/src/Model/Model_PluginManager.cxx b/src/Model/Model_PluginManager.cxx index 4c3e3da6d..cca7b72fd 100644 --- a/src/Model/Model_PluginManager.cxx +++ b/src/Model/Model_PluginManager.cxx @@ -16,8 +16,7 @@ using namespace std; static Model_PluginManager* myImpl = new Model_PluginManager(); -std::shared_ptr Model_PluginManager::createFeature( - string theFeatureID, const bool theAddToDoc) +shared_ptr Model_PluginManager::createFeature(string theFeatureID) { if (this != myImpl) return myImpl->createFeature(theFeatureID); @@ -30,7 +29,7 @@ std::shared_ptr Model_PluginManager::createFeature( } if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) { std::shared_ptr aCreated = - myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID, theAddToDoc); + myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID); return aCreated; } } diff --git a/src/Model/Model_PluginManager.h b/src/Model/Model_PluginManager.h index 64811f82f..6da805cfa 100644 --- a/src/Model/Model_PluginManager.h +++ b/src/Model/Model_PluginManager.h @@ -27,10 +27,6 @@ class Model_PluginManager : public ModelAPI_PluginManager, public Event_Listener std::string myCurrentPluginName; ///< name of the plugin that must be loaded currently std::shared_ptr myCurrentDoc; ///< current working document public: - /// Creates the feature object using plugins functionality - MODEL_EXPORT virtual std::shared_ptr createFeature( - std::string theFeatureID, const bool theAddToDoc = true); - /// Returns the root document of the application (that may contains sub-documents) MODEL_EXPORT virtual std::shared_ptr rootDocument(); @@ -51,9 +47,12 @@ public: /// Is called only once, on startup of the application Model_PluginManager(); -private: +protected: /// Loads (if not done yet) the information about the features and plugins void LoadPluginsInfo(); + + /// Creates the feature object using plugins functionality + virtual std::shared_ptr createFeature(std::string theFeatureID); }; #endif diff --git a/src/ModelAPI/ModelAPI_Document.h b/src/ModelAPI/ModelAPI_Document.h index e2be71f9e..6002e905c 100644 --- a/src/ModelAPI/ModelAPI_Document.h +++ b/src/ModelAPI/ModelAPI_Document.h @@ -22,8 +22,7 @@ static const std::string CONSTRUCTIONS_GROUP = "Construction"; static const std::string PARTS_GROUP = "Parts"; /// Event ID that model is updated -static const char* EVENT_MODEL_UPDATED = "ModelUpdated"; - +static const char * EVENT_MODEL_UPDATED = "ModelUpdated"; /**\class Model_Document * \ingroup DataModel @@ -70,11 +69,9 @@ public: //! Redoes last operation MODELAPI_EXPORT virtual void redo() = 0; - //! 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 object - MODELAPI_EXPORT virtual void addFeature(std::shared_ptr theFeature, - const std::string theGroupID) = 0; + //! Adds to the document the new feature of the given feature id + //! \param creates feature and puts it in the document + MODELAPI_EXPORT virtual std::shared_ptr addFeature(std::string theID) = 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; @@ -90,15 +87,16 @@ public: MODELAPI_EXPORT virtual std::shared_ptr feature(const std::string& theGroupID, const int theIndex) = 0; + //! Returns the index of feature in the group (zero based) + MODELAPI_EXPORT virtual int featureIndex(std::shared_ptr theFeature) = 0; + ///! Returns the vector of groups already added to the document MODELAPI_EXPORT virtual const std::vector& getGroups() const = 0; protected: /// Only for SWIG wrapping it is here MODELAPI_EXPORT ModelAPI_Document() - { - } - ; + {} }; #endif diff --git a/src/ModelAPI/ModelAPI_Feature.h b/src/ModelAPI/ModelAPI_Feature.h index a33d05084..5690933e6 100644 --- a/src/ModelAPI/ModelAPI_Feature.h +++ b/src/ModelAPI/ModelAPI_Feature.h @@ -6,10 +6,12 @@ #define ModelAPI_Feature_HeaderFile #include "ModelAPI.h" +#include "ModelAPI_PluginManager.h" #include #include class ModelAPI_Object; +class ModelAPI_Document; /**\class ModelAPI_Feature * \ingroup DataModel @@ -23,7 +25,10 @@ class MODELAPI_EXPORT ModelAPI_Feature public: /// Returns the kind of a feature (like "Point") - virtual std::string getKind() = 0; + virtual const std::string& getKind() = 0; + + /// Returns to which group in the document must be added feature + virtual const std::string& getGroup() = 0; /// Request for initialization of data model of the feature: adding all attributes virtual void initAttributes() = 0; @@ -32,7 +37,12 @@ public: virtual void execute() = 0; /// Returns the data manager of this feature - std::shared_ptr data() {return myData;} + virtual std::shared_ptr data() {return myData;} + + /// Must return document where the new feature must be added to + /// By default it is current document + virtual std::shared_ptr documentToAdd() + {return ModelAPI_PluginManager::get()->currentDocument();} protected: /// Use plugin manager for features creation: this method is diff --git a/src/ModelAPI/ModelAPI_Iterator.h b/src/ModelAPI/ModelAPI_Iterator.h index 22d2066b3..73187982f 100644 --- a/src/ModelAPI/ModelAPI_Iterator.h +++ b/src/ModelAPI/ModelAPI_Iterator.h @@ -34,6 +34,9 @@ public: /// Don't changes the current position of iterator. Not fast: iterates the left items. /// \returns number of left iterations virtual int numIterationsLeft() = 0; + /// Compares the current feature with the given one + /// \returns true if given feature equals to the current one + virtual bool is(std::shared_ptr theFeature) = 0; protected: /// Use plugin manager for features creation: this method is diff --git a/src/ModelAPI/ModelAPI_Object.h b/src/ModelAPI/ModelAPI_Object.h index 7f78a35ce..cf81f99bf 100644 --- a/src/ModelAPI/ModelAPI_Object.h +++ b/src/ModelAPI/ModelAPI_Object.h @@ -11,6 +11,7 @@ class ModelAPI_AttributeDocRef; class ModelAPI_AttributeDouble; +class ModelAPI_Document; /**\class ModelAPI_Object * \ingroup DataModel @@ -39,6 +40,9 @@ public: /// \param theAttrType type of the created attribute (received from the type method) virtual void addAttribute(std::string theID, std::string theAttrType) = 0; + /// Returns the document of this data + virtual std::shared_ptr document() = 0; + protected: /// Objects are created for features automatically ModelAPI_Object() diff --git a/src/ModelAPI/ModelAPI_Plugin.h b/src/ModelAPI/ModelAPI_Plugin.h index f26cab7b0..0605d8b6f 100644 --- a/src/ModelAPI/ModelAPI_Plugin.h +++ b/src/ModelAPI/ModelAPI_Plugin.h @@ -20,8 +20,7 @@ class MODELAPI_EXPORT ModelAPI_Plugin { public: /// Creates the feature object of this plugin by the feature string ID - virtual std::shared_ptr createFeature( - std::string theFeatureID, const bool theAddToDoc) = 0; + virtual std::shared_ptr createFeature(std::string theFeatureID) = 0; protected: /// Is needed for python wrapping by swig diff --git a/src/ModelAPI/ModelAPI_PluginManager.h b/src/ModelAPI/ModelAPI_PluginManager.h index fdf8e69eb..3250bea97 100644 --- a/src/ModelAPI/ModelAPI_PluginManager.h +++ b/src/ModelAPI/ModelAPI_PluginManager.h @@ -23,10 +23,6 @@ class ModelAPI_Document; class MODELAPI_EXPORT ModelAPI_PluginManager { public: - /// Creates the feature object using plugins functionality - virtual std::shared_ptr createFeature(std::string theFeatureID, - const bool theAddToDoc = true) = 0; - /// Returns the real implementation (the alone instance per application) of the plugin manager static std::shared_ptr get(); @@ -51,7 +47,12 @@ public: ModelAPI_PluginManager(); protected: + /// Creates the feature object using plugins functionality + virtual std::shared_ptr createFeature(std::string theFeatureID) = 0; + static void SetPluginManager(std::shared_ptr theManager); + + friend class Model_Document; }; #endif diff --git a/src/PartSetPlugin/PartSetPlugin_Part.cxx b/src/PartSetPlugin/PartSetPlugin_Part.cxx index fd9d69d87..5dca088f8 100644 --- a/src/PartSetPlugin/PartSetPlugin_Part.cxx +++ b/src/PartSetPlugin/PartSetPlugin_Part.cxx @@ -28,3 +28,7 @@ void PartSetPlugin_Part::execute() aDocRef->setValue(aPartSetDoc->subDocument(data()->getName())); } } + +shared_ptr PartSetPlugin_Part::documentToAdd() { + return ModelAPI_PluginManager::get()->rootDocument(); +} diff --git a/src/PartSetPlugin/PartSetPlugin_Part.h b/src/PartSetPlugin/PartSetPlugin_Part.h index c40f1633d..f1ee43064 100644 --- a/src/PartSetPlugin/PartSetPlugin_Part.h +++ b/src/PartSetPlugin/PartSetPlugin_Part.h @@ -19,7 +19,12 @@ class PartSetPlugin_Part: public ModelAPI_Feature { public: /// Returns the kind of a feature - PARTSETPLUGIN_EXPORT virtual std::string getKind() {return "Part";} + PARTSETPLUGIN_EXPORT virtual const std::string& getKind() + {static std::string MY_KIND = "Part"; return MY_KIND;} + + /// Returns to which group in the document must be added feature + PARTSETPLUGIN_EXPORT virtual const std::string& getGroup() + {static std::string MY_GROUP = "Parts"; return MY_GROUP;} /// Creates a new part document if needed PARTSETPLUGIN_EXPORT virtual void execute(); @@ -27,6 +32,8 @@ public: /// Request for initialization of data model of the feature: adding all attributes PARTSETPLUGIN_EXPORT virtual void initAttributes(); + PARTSETPLUGIN_EXPORT virtual std::shared_ptr documentToAdd(); + /// Use plugin manager for features creation PartSetPlugin_Part(); }; diff --git a/src/PartSetPlugin/PartSetPlugin_Plugin.cxx b/src/PartSetPlugin/PartSetPlugin_Plugin.cxx index 7992ef085..333239037 100644 --- a/src/PartSetPlugin/PartSetPlugin_Plugin.cxx +++ b/src/PartSetPlugin/PartSetPlugin_Plugin.cxx @@ -15,28 +15,13 @@ PartSetPlugin_Plugin::PartSetPlugin_Plugin() ModelAPI_PluginManager::get()->registerPlugin(this); } -std::shared_ptr PartSetPlugin_Plugin::createFeature( - string theFeatureID, const bool theAddToDoc) +shared_ptr PartSetPlugin_Plugin::createFeature(string theFeatureID) { - std::shared_ptr aCreated; - bool isCurrent = true; // to create a feature in the current document - std::string aGroup; if (theFeatureID == "Part") { - aCreated = std::shared_ptr(new PartSetPlugin_Part); - isCurrent = false; // allways create in the root document - aGroup = PARTS_GROUP; + return shared_ptr(new PartSetPlugin_Part); } else if (theFeatureID == "Point") { - aCreated = std::shared_ptr(new PartSetPlugin_Point); - aGroup = CONSTRUCTIONS_GROUP; - } - - // add to a root document for the current moment - if (aCreated && theAddToDoc) { - shared_ptr aDoc = isCurrent ? - ModelAPI_PluginManager::get()->currentDocument() : - ModelAPI_PluginManager::get()->rootDocument(); - aDoc->addFeature(aCreated, aGroup); + return shared_ptr(new PartSetPlugin_Point); } // feature of such kind is not found - return aCreated; + return shared_ptr(); } diff --git a/src/PartSetPlugin/PartSetPlugin_Plugin.h b/src/PartSetPlugin/PartSetPlugin_Plugin.h index a4c7e336d..74f6e73c0 100644 --- a/src/PartSetPlugin/PartSetPlugin_Plugin.h +++ b/src/PartSetPlugin/PartSetPlugin_Plugin.h @@ -13,8 +13,7 @@ class PARTSETPLUGIN_EXPORT PartSetPlugin_Plugin: public ModelAPI_Plugin { public: /// Creates the feature object of this plugin by the feature string ID - virtual std::shared_ptr createFeature( - std::string theFeatureID, const bool theAddToDoc); + virtual std::shared_ptr createFeature(std::string theFeatureID); public: /// Is needed for python wrapping by swig diff --git a/src/PartSetPlugin/PartSetPlugin_Point.h b/src/PartSetPlugin/PartSetPlugin_Point.h index c5f3d2e14..4a8766ae6 100644 --- a/src/PartSetPlugin/PartSetPlugin_Point.h +++ b/src/PartSetPlugin/PartSetPlugin_Point.h @@ -23,7 +23,12 @@ class PartSetPlugin_Point: public ModelAPI_Feature { public: /// Returns the kind of a feature - PARTSETPLUGIN_EXPORT virtual std::string getKind() {return "Point";} + PARTSETPLUGIN_EXPORT virtual const std::string& getKind() + {static std::string MY_KIND = "Point"; return MY_KIND;} + + /// Returns to which group in the document must be added feature + PARTSETPLUGIN_EXPORT virtual const std::string& getGroup() + {static std::string MY_GROUP = "Construction"; return MY_GROUP;} /// Creates a new part document if needed PARTSETPLUGIN_EXPORT virtual void execute();