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

Method addObject

src/App/DocumentPyImp.cpp:325–419  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

323}
324
325PyObject* DocumentPy::addObject(PyObject* args, PyObject* kwd)
326{
327 char *sType, *sName = nullptr, *sViewType = nullptr;
328 PyObject* obj = nullptr;
329 PyObject* view = nullptr;
330 PyObject* attach = Py_False;
331 static const std::array<const char*, 7>
332 kwlist {"type", "name", "objProxy", "viewProxy", "attach", "viewType", nullptr};
333 if (!Base::Wrapped_ParseTupleAndKeywords(args,
334 kwd,
335 "s|sOOO!s",
336 kwlist,
337 &sType,
338 &sName,
339 &obj,
340 &view,
341 &PyBool_Type,
342 &attach,
343 &sViewType)) {
344 return nullptr;
345 }
346
347 DocumentObject* pcFtr = nullptr;
348
349 if (!obj || !Base::asBoolean(attach)) {
350 pcFtr = getDocumentPtr()->addObject(sType, sName, true, sViewType);
351 }
352 else {
353 Base::Type type =
354 Base::Type::getTypeIfDerivedFrom(sType, DocumentObject::getClassTypeId(), true);
355 if (type.isBad()) {
356 std::stringstream str;
357 str << "'" << sType << "' is not a document object type";
358 throw Base::TypeError(str.str());
359 }
360 pcFtr = static_cast<DocumentObject*>(type.createInstance());
361 }
362 // the type instance could be a null pointer
363 if (!pcFtr) {
364 std::stringstream str;
365 str << "No document object found of type '" << sType << "'" << std::ends;
366 throw Py::TypeError(str.str());
367 }
368 // Allows one to hide the handling with Proxy in client python code
369 if (obj) {
370 try {
371 // the python binding class to the document object
372 Py::Object pyftr = Py::asObject(pcFtr->getPyObject());
373 // 'pyobj' is the python class with the implementation for DocumentObject
374 Py::Object pyobj(obj);
375 if (pyobj.hasAttr("__object__")) {
376 pyobj.setAttr("__object__", pyftr);
377 }
378 pyftr.setAttr("Proxy", pyobj);
379
380 if (Base::asBoolean(attach)) {
381 getDocumentPtr()->addObject(pcFtr, sName);
382

Callers

nothing calls this directly

Calls 12

asBooleanFunction · 0.85
new_reference_toFunction · 0.85
isNoneMethod · 0.80
ObjectClass · 0.70
isBadMethod · 0.45
strMethod · 0.45
createInstanceMethod · 0.45
getPyObjectMethod · 0.45
applyMethod · 0.45
reportExceptionMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected