+ add overloaded addObject() method to Document class to add existing DocumentObject

This commit is contained in:
wmayer 2015-12-31 15:32:47 +01:00
parent ebbb68d978
commit b3bfdaafcf
2 changed files with 54 additions and 2 deletions

View File

@ -1923,7 +1923,7 @@ DocumentObject * Document::addObject(const char* sType, const char* pObjectName)
delete base;
std::stringstream str;
str << "'" << sType << "' is not a document object type";
throw Base::Exception(str.str());
throw Base::TypeError(str.str());
}
App::DocumentObject* pcObject = static_cast<App::DocumentObject*>(base);
@ -1968,6 +1968,48 @@ DocumentObject * Document::addObject(const char* sType, const char* pObjectName)
return pcObject;
}
void Document::addObject(DocumentObject* pcObject, const char* pObjectName)
{
if (pcObject->getDocument()) {
throw Base::RuntimeError("Document object is already added to a document");
}
pcObject->setDocument(this);
// do no transactions if we do a rollback!
if (!d->rollback) {
// Transaction stuff
if (d->activeTransaction)
d->activeTransaction->addObjectNew(pcObject);
// Undo stuff
if (d->activeUndoTransaction)
d->activeUndoTransaction->addObjectDel(pcObject);
}
// get unique name
string ObjectName;
if (pObjectName && pObjectName[0] != '\0')
ObjectName = getUniqueObjectName(pObjectName);
else
ObjectName = getUniqueObjectName(pcObject->getTypeId().getName());
d->activeObject = pcObject;
// insert in the name map
d->objectMap[ObjectName] = pcObject;
// cache the pointer to the name string in the Object (for performance of DocumentObject::getNameInDocument())
pcObject->pcNameInDocument = &(d->objectMap.find(ObjectName)->first);
// insert in the vector
d->objectArray.push_back(pcObject);
pcObject->Label.setValue( ObjectName );
// mark the object as new (i.e. set status bit 2) and send the signal
pcObject->StatusBits.set(2);
signalNewObject(*pcObject);
signalActivatedObject(*pcObject);
}
void Document::_addObject(DocumentObject* pcObject, const char* pObjectName)
{
std::string ObjectName = getUniqueObjectName(pObjectName);

View File

@ -161,8 +161,18 @@ public:
/** @name Object handling */
//@{
/// Add a feature of sType with sName (ASCII) to this document and set it active. Unicode names are set through the Label propery
/** Add a feature of sType with sName (ASCII) to this document and set it active.
* Unicode names are set through the Label property.
*/
DocumentObject *addObject(const char* sType, const char* pObjectName=0);
/** Add an existing feature with sName (ASCII) to this document and set it active.
* Unicode names are set through the Label property.
* This is an overloaded function of the function above and can be used to create
* a feature outside and add it to the document afterwards.
* \note The passed feature must not yet be added to a document, otherwise an exception
* is raisedd.
*/
void addObject(DocumentObject*, const char* pObjectName=0);
/// Remove a feature out of the document
void remObject(const char* sName);
/** Copy an object from another document to this document