From b3bfdaafcfd8c8fef8745512634366bf838f2f52 Mon Sep 17 00:00:00 2001 From: wmayer Date: Thu, 31 Dec 2015 15:32:47 +0100 Subject: [PATCH] + add overloaded addObject() method to Document class to add existing DocumentObject --- src/App/Document.cpp | 44 +++++++++++++++++++++++++++++++++++++++++++- src/App/Document.h | 12 +++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/App/Document.cpp b/src/App/Document.cpp index 69e849f6d..08f5937bd 100644 --- a/src/App/Document.cpp +++ b/src/App/Document.cpp @@ -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(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); diff --git a/src/App/Document.h b/src/App/Document.h index 43321acbd..1402f7cc4 100644 --- a/src/App/Document.h +++ b/src/App/Document.h @@ -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