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

Method addObjects

src/App/Document.cpp:3372–3409  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3370}
3371
3372std::vector<DocumentObject*>
3373Document::addObjects(const char* sType, const std::vector<std::string>& objectNames, bool isNew)
3374{
3375 Base::Type type =
3376 Base::Type::getTypeIfDerivedFrom(sType, DocumentObject::getClassTypeId(), true);
3377 if (type.isBad()) {
3378 std::stringstream str;
3379 str << "'" << sType << "' is not a document object type";
3380 throw Base::TypeError(str.str());
3381 }
3382
3383 std::vector<DocumentObject*> objects;
3384 objects.resize(objectNames.size());
3385 std::generate(objects.begin(), objects.end(), [&] {
3386 return static_cast<DocumentObject*>(type.createInstance());
3387 });
3388 // the type instance could be a null pointer, it is enough to check the first element
3389 if (!objects.empty() && !objects[0]) {
3390 objects.clear();
3391 return objects;
3392 }
3393
3394 for (auto it = objects.begin(); it != objects.end(); ++it) {
3395 size_t index = std::distance(objects.begin(), it);
3396 DocumentObject* pcObject = *it;
3397 pcObject->setDocument(this);
3398
3399 // Add the object but only activate the last one
3400 bool isLast = index == (objects.size() - 1);
3401 _addObject(pcObject,
3402 objectNames[index].c_str(),
3403 AddObjectOption::SetNewStatus
3404 | (isNew ? AddObjectOption::DoSetup : AddObjectOption::None)
3405 | (isLast ? AddObjectOption::ActivateObject : AddObjectOption::None));
3406 }
3407
3408 return objects;
3409}
3410
3411void Document::addObject(DocumentObject* obj, const char* name)
3412{

Callers 1

TEST_FFunction · 0.45

Calls 12

generateFunction · 0.50
isBadMethod · 0.45
strMethod · 0.45
resizeMethod · 0.45
sizeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
createInstanceMethod · 0.45
emptyMethod · 0.45
clearMethod · 0.45
setDocumentMethod · 0.45
c_strMethod · 0.45

Tested by

no test coverage detected