myTransactionsAfterSave++;
}
-void Model_Document::addFeature(
- std::shared_ptr<ModelAPI_Feature> theFeature, const std::string theGroupID)
+shared_ptr<ModelAPI_Feature> Model_Document::addFeature(string theID)
{
- TDF_Label aGroupLab = groupLabel(theGroupID);
+ shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_PluginManager::get()->createFeature(theID);
+ if (aFeature) {
+ dynamic_pointer_cast<Model_Document>(aFeature->documentToAdd())->addFeature(aFeature);
+ } else {
+ // TODO: generate error that feature is not created
+ }
+ return aFeature;
+}
+
+void Model_Document::addFeature(const std::shared_ptr<ModelAPI_Feature> theFeature)
+{
+ TDF_Label aGroupLab = groupLabel(theFeature->getGroup());
TDF_Label anObjLab = aGroupLab.NewChild();
std::shared_ptr<Model_Object> 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<ModelAPI_Feature> Model_Document::feature(TDF_Label& theLabel)
+shared_ptr<ModelAPI_Feature> 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<ModelAPI_Feature> aResult =
- Model_PluginManager::get()->createFeature(anID, false);
+ Model_PluginManager::get()->createFeature(anID);
std::shared_ptr<Model_Object> aData(new Model_Object);
aData->setLabel(theLabel);
+ aData->setDocument(Model_Application::getApplication()->getDocument(myID));
aResult->setData(aData);
aResult->initAttributes();
return aResult;
return std::shared_ptr<ModelAPI_Feature>(); // not found
}
+int Model_Document::featureIndex(shared_ptr<ModelAPI_Feature> theFeature)
+{
+ if (theFeature->data()->document().get() != this)
+ return theFeature->data()->document()->featureIndex(theFeature);
+ shared_ptr<ModelAPI_Iterator> 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<ModelAPI_Document> Model_Document::subDocument(string theDocID)
{
return Model_Application::getApplication()->getDocument(theDocID);
}
void Model_Document::setUniqueName(
- shared_ptr<ModelAPI_Feature> theFeature, const string theGroupID)
+ shared_ptr<ModelAPI_Feature> theFeature)
{
// first count all objects of such kind to start with index = count + 1
int aNumObjects = 0;
- shared_ptr<ModelAPI_Iterator> anIter = featuresIterator(theGroupID);
+ shared_ptr<ModelAPI_Iterator> anIter = featuresIterator(theFeature->getGroup());
for(; anIter->more(); anIter->next()) {
if (anIter->currentKind() == theFeature->getKind())
aNumObjects++;
aNameStream<<theFeature->getKind()<<"_"<<aNumObjects + 1;
string aName = aNameStream.str();
// check this is unique, if not, increase index by 1
- for(anIter = featuresIterator(theGroupID); anIter->more();) {
+ for(anIter = featuresIterator(theFeature->getGroup()); anIter->more();) {
if (anIter->currentName() == aName) {
aNumObjects++;
stringstream aNameStream;
aNameStream<<theFeature->getKind()<<"_"<<aNumObjects + 1;
// reinitialize iterator to make sure a new name is unique
- anIter = featuresIterator(theGroupID);
+ anIter = featuresIterator(theFeature->getGroup());
} else anIter->next();
}
//! 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<ModelAPI_Feature> 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<ModelAPI_Feature> addFeature(std::string theID);
//! Returns the existing feature by the label
//! \param theLabel base label of the feature
///! Returns the vector of groups already added to the document
MODEL_EXPORT virtual const std::vector<std::string>& 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<ModelAPI_Feature> theFeature);
+
protected:
//! Returns (creates if needed) the group label
//! Initializes feature with a unique name in this group (unique name is generated as
//! feature type + "_" + index
- void setUniqueName(
- std::shared_ptr<ModelAPI_Feature> theFeature, const std::string theGroupID);
+ void setUniqueName(std::shared_ptr<ModelAPI_Feature> theFeature);
+
+ //! Adds to the document the new feature
+ void addFeature(const std::shared_ptr<ModelAPI_Feature> theFeature);
//! Creates new document with binary file format
Model_Document(const std::string theID);
#include "Model_Iterator.h"
#include "Model_Document.h"
+#include "ModelAPI_Feature.h"
+#include "Model_Object.h"
#include <TDataStd_Comment.hxx>
#include <TDataStd_Name.hxx>
return aResult;
}
+bool Model_Iterator::is(std::shared_ptr<ModelAPI_Feature> theFeature)
+{
+ return myIter.Value()->Label() ==
+ dynamic_pointer_cast<Model_Object>(theFeature->data())->label();
+
+}
+
+
Model_Iterator::Model_Iterator(std::shared_ptr<Model_Document> theDoc, TDF_Label theLab)
: myDoc(theDoc), myIter(theLab, TDataStd_Comment::GetID(), Standard_False)
{}
/// \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<ModelAPI_Feature> theFeature);
+
protected:
/// Initializes iterator
/// \param theDoc document where the iteration is performed
* 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<std::string, std::shared_ptr<ModelAPI_Attribute> > myAttrs;
+ std::shared_ptr<ModelAPI_Document> 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
/// \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<ModelAPI_Document> 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<ModelAPI_Document>& theDoc) {myDoc = theDoc;}
};
#endif
static Model_PluginManager* myImpl = new Model_PluginManager();
-std::shared_ptr<ModelAPI_Feature> Model_PluginManager::createFeature(
- string theFeatureID, const bool theAddToDoc)
+shared_ptr<ModelAPI_Feature> Model_PluginManager::createFeature(string theFeatureID)
{
if (this != myImpl) return myImpl->createFeature(theFeatureID);
}
if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
std::shared_ptr<ModelAPI_Feature> aCreated =
- myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID, theAddToDoc);
+ myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
return aCreated;
}
}
std::string myCurrentPluginName; ///< name of the plugin that must be loaded currently
std::shared_ptr<ModelAPI_Document> myCurrentDoc; ///< current working document
public:
- /// Creates the feature object using plugins functionality
- MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature> 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<ModelAPI_Document> rootDocument();
/// 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<ModelAPI_Feature> createFeature(std::string theFeatureID);
};
#endif
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
//! 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<ModelAPI_Feature> 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<ModelAPI_Feature> 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<ModelAPI_Document> subDocument(std::string theDocID) = 0;
MODELAPI_EXPORT virtual std::shared_ptr<ModelAPI_Feature>
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<ModelAPI_Feature> theFeature) = 0;
+
///! Returns the vector of groups already added to the document
MODELAPI_EXPORT virtual const std::vector<std::string>& getGroups() const = 0;
protected:
/// Only for SWIG wrapping it is here
MODELAPI_EXPORT ModelAPI_Document()
- {
- }
- ;
+ {}
};
#endif
#define ModelAPI_Feature_HeaderFile
#include "ModelAPI.h"
+#include "ModelAPI_PluginManager.h"
#include <string>
#include <memory>
class ModelAPI_Object;
+class ModelAPI_Document;
/**\class ModelAPI_Feature
* \ingroup DataModel
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;
virtual void execute() = 0;
/// Returns the data manager of this feature
- std::shared_ptr<ModelAPI_Object> data() {return myData;}
+ virtual std::shared_ptr<ModelAPI_Object> data() {return myData;}
+
+ /// Must return document where the new feature must be added to
+ /// By default it is current document
+ virtual std::shared_ptr<ModelAPI_Document> documentToAdd()
+ {return ModelAPI_PluginManager::get()->currentDocument();}
protected:
/// Use plugin manager for features creation: this method is
/// 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<ModelAPI_Feature> theFeature) = 0;
protected:
/// Use plugin manager for features creation: this method is
class ModelAPI_AttributeDocRef;
class ModelAPI_AttributeDouble;
+class ModelAPI_Document;
/**\class ModelAPI_Object
* \ingroup DataModel
/// \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<ModelAPI_Document> document() = 0;
+
protected:
/// Objects are created for features automatically
ModelAPI_Object()
{
public:
/// Creates the feature object of this plugin by the feature string ID
- virtual std::shared_ptr<ModelAPI_Feature> createFeature(
- std::string theFeatureID, const bool theAddToDoc) = 0;
+ virtual std::shared_ptr<ModelAPI_Feature> createFeature(std::string theFeatureID) = 0;
protected:
/// Is needed for python wrapping by swig
class MODELAPI_EXPORT ModelAPI_PluginManager
{
public:
- /// Creates the feature object using plugins functionality
- virtual std::shared_ptr<ModelAPI_Feature> 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<ModelAPI_PluginManager> get();
ModelAPI_PluginManager();
protected:
+ /// Creates the feature object using plugins functionality
+ virtual std::shared_ptr<ModelAPI_Feature> createFeature(std::string theFeatureID) = 0;
+
static void SetPluginManager(std::shared_ptr<ModelAPI_PluginManager> theManager);
+
+ friend class Model_Document;
};
#endif
aDocRef->setValue(aPartSetDoc->subDocument(data()->getName()));
}
}
+
+shared_ptr<ModelAPI_Document> PartSetPlugin_Part::documentToAdd() {
+ return ModelAPI_PluginManager::get()->rootDocument();
+}
{
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();
/// Request for initialization of data model of the feature: adding all attributes
PARTSETPLUGIN_EXPORT virtual void initAttributes();
+ PARTSETPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Document> documentToAdd();
+
/// Use plugin manager for features creation
PartSetPlugin_Part();
};
ModelAPI_PluginManager::get()->registerPlugin(this);
}
-std::shared_ptr<ModelAPI_Feature> PartSetPlugin_Plugin::createFeature(
- string theFeatureID, const bool theAddToDoc)
+shared_ptr<ModelAPI_Feature> PartSetPlugin_Plugin::createFeature(string theFeatureID)
{
- std::shared_ptr<ModelAPI_Feature> aCreated;
- bool isCurrent = true; // to create a feature in the current document
- std::string aGroup;
if (theFeatureID == "Part") {
- aCreated = std::shared_ptr<ModelAPI_Feature>(new PartSetPlugin_Part);
- isCurrent = false; // allways create in the root document
- aGroup = PARTS_GROUP;
+ return shared_ptr<ModelAPI_Feature>(new PartSetPlugin_Part);
} else if (theFeatureID == "Point") {
- aCreated = std::shared_ptr<ModelAPI_Feature>(new PartSetPlugin_Point);
- aGroup = CONSTRUCTIONS_GROUP;
- }
-
- // add to a root document for the current moment
- if (aCreated && theAddToDoc) {
- shared_ptr<ModelAPI_Document> aDoc = isCurrent ?
- ModelAPI_PluginManager::get()->currentDocument() :
- ModelAPI_PluginManager::get()->rootDocument();
- aDoc->addFeature(aCreated, aGroup);
+ return shared_ptr<ModelAPI_Feature>(new PartSetPlugin_Point);
}
// feature of such kind is not found
- return aCreated;
+ return shared_ptr<ModelAPI_Feature>();
}
{
public:
/// Creates the feature object of this plugin by the feature string ID
- virtual std::shared_ptr<ModelAPI_Feature> createFeature(
- std::string theFeatureID, const bool theAddToDoc);
+ virtual std::shared_ptr<ModelAPI_Feature> createFeature(std::string theFeatureID);
public:
/// Is needed for python wrapping by swig
{
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();