+ reimplement copyObject based on merge document

This commit is contained in:
wmayer 2014-12-13 21:43:20 +01:00
parent 157ab9508e
commit ff7e33b95d
5 changed files with 24 additions and 93 deletions

View File

@ -75,6 +75,7 @@ recompute path. Also enables more complicated dependencies beyond trees.
#include "Application.h"
#include "DocumentObject.h"
#include "PropertyLinks.h"
#include "MergeDocuments.h"
#include <Base/Console.h>
#include <Base/Exception.h>
@ -1656,92 +1657,34 @@ void Document::breakDependency(DocumentObject* pcObject, bool clear)
}
}
DocumentObject* Document::_copyObject(DocumentObject* obj, std::map<DocumentObject*,
DocumentObject*>& copy_map, bool recursive,
bool keepdigitsatend)
DocumentObject* Document::copyObject(DocumentObject* obj, bool recursive, bool /*keepdigitsatend*/)
{
if (!obj) return 0;
// remove number from end to avoid lengthy names
std::string objname = obj->getNameInDocument();
if (!keepdigitsatend) {
size_t lastpos = objname.length()-1;
while (objname[lastpos] >= 48 && objname[lastpos] <= 57)
lastpos--;
objname = objname.substr(0, lastpos+1);
}
DocumentObject* copy = addObject(obj->getTypeId().getName(),objname.c_str());
if (!copy) return 0;
copy->addDynamicProperties(obj);
std::vector<DocumentObject*> objs;
objs.push_back(obj);
copy_map[obj] = copy;
std::map<std::string,App::Property*> props;
copy->getPropertyMap(props);
for (std::map<std::string,App::Property*>::iterator it = props.begin(); it != props.end(); ++it) {
App::Property* prop = obj->getPropertyByName(it->first.c_str());
if (prop && prop->getTypeId() == it->second->getTypeId()) {
if (prop->getTypeId() == PropertyLink::getClassTypeId()) {
DocumentObject* link = static_cast<PropertyLink*>(prop)->getValue();
std::map<DocumentObject*, DocumentObject*>::iterator pt = copy_map.find(link);
if (pt != copy_map.end()) {
// the object has already been copied
static_cast<PropertyLink*>(it->second)->setValue(pt->second);
}
else if (recursive) {
DocumentObject* link_copy = _copyObject(link, copy_map, recursive, keepdigitsatend);
copy_map[link] = link_copy;
static_cast<PropertyLink*>(it->second)->setValue(link_copy);
}
else if (link && link->getDocument() == this) {
//static_cast<PropertyLink*>(it->second)->setValue(link);
}
}
else if (prop->getTypeId() == PropertyLinkList::getClassTypeId()) {
std::vector<DocumentObject*> links = static_cast<PropertyLinkList*>(prop)->getValues();
if (recursive) {
std::vector<DocumentObject*> links_copy;
for (std::vector<DocumentObject*>::iterator jt = links.begin(); jt != links.end(); ++jt) {
std::map<DocumentObject*, DocumentObject*>::iterator pt = copy_map.find(*jt);
if (pt != copy_map.end()) {
// the object has already been copied
links_copy.push_back(pt->second);
}
else {
links_copy.push_back(_copyObject(*jt, copy_map, recursive, keepdigitsatend));
copy_map[*jt] = links_copy.back();
}
}
static_cast<PropertyLinkList*>(it->second)->setValues(links_copy);
}
else {
std::vector<DocumentObject*> links_ref;
//for (std::vector<DocumentObject*>::iterator jt = links.begin(); jt != links.end(); ++jt) {
// if ((*jt)->getDocument() == this)
// links_ref.push_back(*jt);
//}
static_cast<PropertyLinkList*>(it->second)->setValues(links_ref);
}
}
else {
std::auto_ptr<Property> data(prop->Copy());
if (data.get()) {
it->second->Paste(*data);
}
}
}
MergeDocuments md(this);
if (recursive) {
objs = getDependencyList(objs);
}
// unmark to be not re-computed later
copy->onFinishDuplicating();
copy->purgeTouched();
return copy;
}
unsigned int memsize=1000; // ~ for the meta-information
for (std::vector<App::DocumentObject*>::iterator it = objs.begin(); it != objs.end(); ++it)
memsize += (*it)->getMemSize();
DocumentObject* Document::copyObject(DocumentObject* obj, bool recursive, bool keepdigitsatend)
{
std::map<DocumentObject*, DocumentObject*> copy_map;
DocumentObject* copy = _copyObject(obj, copy_map, recursive, keepdigitsatend);
return copy;
QByteArray res;
res.reserve(memsize);
Base::ByteArrayOStreambuf obuf(res);
std::ostream ostr(&obuf);
this->exportObjects(objs, ostr);
Base::ByteArrayIStreambuf ibuf(res);
std::istream istr(0);
istr.rdbuf(&ibuf);
std::vector<App::DocumentObject*> newObj = md.importObjects(istr);
if (newObj.empty())
return 0;
else
return newObj.back();
}
DocumentObject* Document::moveObject(DocumentObject* obj, bool recursive)

View File

@ -294,8 +294,6 @@ protected:
void _remObject(DocumentObject* pcObject);
void _addObject(DocumentObject* pcObject, const char* pObjectName);
DocumentObject* _copyObject(DocumentObject* obj, std::map<DocumentObject*,
DocumentObject*>&, bool recursive=false, bool keepdigitsatend=false);
/// checks if a valid transaction is open
void _checkTransaction(DocumentObject* pcObject);
void breakDependency(DocumentObject* pcObject, bool clear);

View File

@ -196,8 +196,6 @@ protected:
virtual void onChanged(const Property* prop);
/// get called after a document has been fully restored
virtual void onDocumentRestored() {}
/// get called after duplicating an object
virtual void onFinishDuplicating() {}
/// get called after setting the document
virtual void onSettingDocument() {}

View File

@ -1656,13 +1656,6 @@ void SketchObject::onDocumentRestored()
}
}
void SketchObject::onFinishDuplicating()
{
Constraints.acceptGeometry(getCompleteGeometry());
rebuildVertexIndex();
onDocumentRestored();
}
void SketchObject::getGeoVertexIndex(int VertexId, int &GeoId, PointPos &PosId) const
{
if (VertexId < 0 || VertexId >= int(VertexId2GeoId.size())) {

View File

@ -163,7 +163,6 @@ protected:
/// get called by the container when a property has changed
virtual void onChanged(const App::Property* /*prop*/);
virtual void onDocumentRestored();
virtual void onFinishDuplicating();
private:
std::vector<Part::Geometry *> ExternalGeo;