// Author: Mikhail PONIKAROV
#include <Event_Message.h>
-
-Event_Message::Event_Message(const Event_ID theID, const void* theSender)
- : myEventId(theID), mySender((void*) theSender)
-{
-}
public:
//! Creates the message
- Event_Message(const Event_ID theID, const void* theSender = 0);
+ Event_Message(const Event_ID theID, const void* theSender = 0)
+ : myEventId(theID), mySender((void*) theSender) {}
+ //! do nothing in the destructor yet
virtual ~Event_Message() {}
//! Returns identifier of the message
myReal = TDataStd_Real::Set(theLabel, 0.);
}
}
-
#include <Model_Application.h>
#include <Model_PluginManager.h>
#include <Model_Iterator.h>
+#include <Event_Loop.h>
#include <TDataStd_Integer.hxx>
#include <TDataStd_Comment.hxx>
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_FEATURE_UPDATED);
+ ModelAPI_FeatureUpdatedMessage aMsg(theFeature);
+ Event_Loop::loop()->send(aMsg);
}
-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);
+ std::shared_ptr<ModelAPI_Feature> aResult =
+ 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);
return anIter->current();
}
+const vector<string>& Model_Document::getGroups() const
+{
+ return myGroupsNames;
+}
+
Model_Document::Model_Document(const std::string theID)
: myID(theID), myDoc(new TDocStd_Document("BinOcaf")) // binary OCAF format
{
{
if (myGroups.find(theGroup) == myGroups.end()) {
myGroups[theGroup] = myDoc->Main().FindChild(TAG_OBJECTS).NewChild();
+ myGroupsNames.push_back(theGroup);
}
return myGroups[theGroup];
}
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();
}
theFeature->data()->setName(aName);
}
+
+
+ModelAPI_FeatureUpdatedMessage::ModelAPI_FeatureUpdatedMessage(
+ shared_ptr<ModelAPI_Feature> theFeature)
+ : Event_Message(messageId(), 0), myFeature(theFeature)
+{}
+
+const Event_ID ModelAPI_FeatureUpdatedMessage::messageId()
+{
+ static Event_ID MY_ID = Event_Loop::eventByName("FeatureUpdated");
+ return MY_ID;
+}
+
+shared_ptr<ModelAPI_Feature> ModelAPI_FeatureUpdatedMessage::feature()
+{
+ return myFeature;
+}
#include <Model.h>
#include <ModelAPI_Document.h>
+#include <Event_Message.h>
+
#include <TDocStd_Document.hxx>
#include <map>
//! 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
MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature>
feature(const std::string& theGroupID, const int theIndex);
+ ///! 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);
Handle_TDocStd_Document myDoc; ///< OCAF document
int myTransactionsAfterSave; ///< number of transactions after the last "save" call, used for "IsModified" method
std::map<std::string, TDF_Label> myGroups; ///< root labels of the features groups identified by names
+ std::vector<std::string> myGroupsNames; ///< names of added groups to the document
+};
+
+/// Event ID that model is updated
+static const char * EVENT_FEATURE_UPDATED = "FeatureUpdated";
+
+/// Message that feature was changed (used for Object Browser update)
+class ModelAPI_FeatureUpdatedMessage : public Event_Message {
+ std::shared_ptr<ModelAPI_Feature> myFeature; ///< which feature is changed
+public:
+ /// sender is not important, all information is located in the feature
+ ModelAPI_FeatureUpdatedMessage(std::shared_ptr<ModelAPI_Feature> theFeature);
+
+ /// Returns the ID of this message
+ static const Event_ID messageId();
+
+ /// Returns the feature that has been updated
+ std::shared_ptr<ModelAPI_Feature> feature();
};
#endif
#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)
+shared_ptr<ModelAPI_Feature> Model_PluginManager::createFeature(string theFeatureID)
{
if (this != myImpl) return myImpl->createFeature(theFeatureID);
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);
-
/// 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
#include "ModelAPI_Attribute.h"
#include "ModelAPI_AttributeDocRef.h"
#include "ModelAPI_AttributeDouble.h"
+ #include "ModelAPI_Iterator.h"
%}
// to avoid error on this
%shared_ptr(ModelAPI_Attribute)
%shared_ptr(ModelAPI_AttributeDocRef)
%shared_ptr(ModelAPI_AttributeDouble)
+%shared_ptr(ModelAPI_Iterator)
// all supported interfaces
%include "ModelAPI_Document.h"
%include "ModelAPI_Attribute.h"
%include "ModelAPI_AttributeDocRef.h"
%include "ModelAPI_AttributeDouble.h"
+%include "ModelAPI_Iterator.h"
#include <ModelAPI.h>
#include <string>
#include <memory>
+#include <vector>
class ModelAPI_Feature;
class ModelAPI_Iterator;
//! 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
* \brief Functionality of the model object: to update result,
* to initialize attributes, etc.
*/
-
class MODELAPI_EXPORT ModelAPI_Feature
{
std::shared_ptr<ModelAPI_Object> myData; ///< manager of the data model of a 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;
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()
class MODELAPI_EXPORT ModelAPI_PluginManager
{
public:
- /// Creates the feature object using plugins functionality
- virtual std::shared_ptr<ModelAPI_Feature> createFeature(std::string theFeatureID) = 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)
+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
if (theFeatureID == "Part") {
- aCreated = std::shared_ptr<ModelAPI_Feature>(new PartSetPlugin_Part);
- isCurrent = false; // allways create in the root document
+ return shared_ptr<ModelAPI_Feature>(new PartSetPlugin_Part);
} else if (theFeatureID == "Point") {
- aCreated = std::shared_ptr<ModelAPI_Feature>(new PartSetPlugin_Point);
- }
-
- // add to a root document for the current moment
- if (aCreated) {
- shared_ptr<ModelAPI_Document> aDoc = isCurrent ?
- ModelAPI_PluginManager::get()->currentDocument() :
- ModelAPI_PluginManager::get()->rootDocument();
- aDoc->addFeature(aCreated, PARTS_GROUP);
+ return shared_ptr<ModelAPI_Feature>(new PartSetPlugin_Point);
}
// feature of such kind is not found
- return aCreated;
+ return shared_ptr<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();