MCPcopy Create free account
hub / github.com/FreeCAD/FreeCAD / copyObject

Method copyObject

src/App/Document.cpp:3638–3709  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3636}
3637
3638std::vector<DocumentObject*>
3639Document::copyObject(const std::vector<DocumentObject*>& objs, bool recursive, bool returnAll)
3640{
3641 std::vector<DocumentObject*> deps;
3642 if (!recursive) {
3643 deps = objs;
3644 }
3645 else {
3646 deps = getDependencyList(objs, DepNoXLinked | DepSort);
3647 }
3648
3649 if (!testStatus(TempDoc) && !isSaved() && PropertyXLink::hasXLink(deps)) {
3650 throw Base::RuntimeError(
3651 "Document must be saved at least once before link to external objects");
3652 }
3653
3654 MergeDocuments md(this);
3655 // if not copying recursively then suppress possible warnings
3656 md.setVerbose(recursive);
3657
3658 unsigned int memsize = 1000; // ~ for the meta-information
3659 for (auto it : deps) {
3660 memsize += it->getMemSize();
3661 }
3662
3663 // if less than ~10 MB
3664 bool use_buffer = (memsize < 0xA00000);
3665 std::string res;
3666 try {
3667 res.reserve(memsize);
3668 }
3669 catch (const std::bad_alloc&) {
3670 use_buffer = false;
3671 }
3672
3673 std::vector<DocumentObject*> imported;
3674 if (use_buffer) {
3675 Base::StringOStreambuf obuf(res);
3676 std::ostream ostr(&obuf);
3677 exportObjects(deps, ostr);
3678
3679 Base::StringIStreambuf ibuf(res);
3680 std::istream istr(nullptr);
3681 istr.rdbuf(&ibuf);
3682 imported = md.importObjects(istr);
3683 }
3684 else {
3685 static Base::FileInfo fi(Application::getTempFileName());
3686 Base::ofstream ostr(fi, std::ios::out | std::ios::binary);
3687 exportObjects(deps, ostr);
3688 ostr.close();
3689
3690 Base::ifstream istr(fi, std::ios::in | std::ios::binary);
3691 imported = md.importObjects(istr);
3692 }
3693
3694 if (returnAll || imported.size() != deps.size()) {
3695 return imported;

Callers 11

syncCopyOnChangeMethod · 0.45
makeCopyOnChangeMethod · 0.45
dropInDocumentMethod · 0.45
exerciseFunction · 0.45
ActivatedMethod · 0.45
make_bodyMethod · 0.45
get_svgFunction · 0.45
make_copyFunction · 0.45
updateMethod · 0.45
synchronizeJointsMethod · 0.45
testPlacementListMethod · 0.45

Calls 8

isSavedFunction · 0.85
reserveMethod · 0.80
testStatusFunction · 0.70
getMemSizeMethod · 0.45
importObjectsMethod · 0.45
closeMethod · 0.45
sizeMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected