diff --git a/src/Mod/PartDesign/Gui/TaskFeaturePick.cpp b/src/Mod/PartDesign/Gui/TaskFeaturePick.cpp index 12144e2a23..10dbe8c322 100644 --- a/src/Mod/PartDesign/Gui/TaskFeaturePick.cpp +++ b/src/Mod/PartDesign/Gui/TaskFeaturePick.cpp @@ -29,6 +29,8 @@ #include #endif +#include + #include #include #include @@ -351,9 +353,9 @@ TaskFeaturePick::makeCopy(App::DocumentObject* obj, std::string sub, bool indepe // we do know that the created instance is a document object, as obj is one. But we do not // know which exact type - auto name = std::string("Copy") + std::string(obj->getNameInDocument()); - copy = App::GetApplication().getActiveDocument()->addObject(obj->getTypeId().getName(), - name.c_str()); + auto* doc = App::GetApplication().getActiveDocument(); + const auto name = fmt::format("Copy{}", obj->getNameInDocument()); + copy = doc->addObject(obj->getTypeId().getName(), name.c_str()); // copy over all properties std::vector props; @@ -387,35 +389,23 @@ TaskFeaturePick::makeCopy(App::DocumentObject* obj, std::string sub, bool indepe // we are a independent copy, therefore no external geometry was copied. WE therefore // can delete all constraints - if (obj->isDerivedFrom()) { - static_cast(copy)->delConstraintsToExternal(); + if (auto* sketchObj = Base::freecad_dynamic_cast(obj)) { + sketchObj->delConstraintsToExternal(); } } } else { - std::string name; - if (!independent) { - name = std::string("Reference"); - } - else { - name = std::string("Copy"); - } - name += std::string(obj->getNameInDocument()); - - std::string entity; - if (!sub.empty()) { - entity = sub; - } + const std::string name = (!independent ? std::string("Reference") : std::string("Copy")) + + obj->getNameInDocument(); + const std::string entity = sub; Part::PropertyPartShape* shapeProp = nullptr; // TODO Replace it with commands (2015-09-11, Fat-Zer) if (obj->isDerivedFrom()) { - copy = App::GetApplication().getActiveDocument()->addObject(obj->getTypeId().getName(), - name.c_str()); - - assert(copy->isDerivedFrom()); + auto* doc = App::GetApplication().getActiveDocument(); + copy = doc->addObject(name.c_str()); // we need to reference the individual datums and make again datums. This is important // as datum adjust their size dependent on the part size, hence simply copying the shape @@ -453,43 +443,38 @@ TaskFeaturePick::makeCopy(App::DocumentObject* obj, std::string sub, bool indepe else if (obj->is() || obj->isDerivedFrom()) { - copy = App::GetApplication().getActiveDocument()->addObject("PartDesign::ShapeBinder", - name.c_str()); - + auto* doc = App::GetApplication().getActiveDocument(); + auto* shapeBinderObj = doc->addObject(name.c_str()); if (!independent) { - static_cast(copy)->Support.setValue(obj, entity.c_str()); + shapeBinderObj->Support.setValue(obj, entity.c_str()); } else { - shapeProp = &static_cast(copy)->Shape; + shapeProp = &shapeBinderObj->Shape; } + copy = shapeBinderObj; } else if (obj->isDerivedFrom() || obj->isDerivedFrom()) { - copy = App::GetApplication().getActiveDocument()->addObject("PartDesign::ShapeBinder", - name.c_str()); - + auto* doc = App::GetApplication().getActiveDocument(); + auto* shapeBinderObj = doc->addObject(name.c_str()); if (!independent) { - static_cast(copy)->Support.setValue(obj, entity.c_str()); + shapeBinderObj->Support.setValue(obj, entity.c_str()); } else { - App::GeoFeature* geo = static_cast(obj); std::vector subvalues; subvalues.push_back(entity); Part::TopoShape shape = - PartDesign::ShapeBinder::buildShapeFromReferences(geo, subvalues); - static_cast(copy)->Shape.setValue(shape); + PartDesign::ShapeBinder::buildShapeFromReferences(shapeBinderObj, subvalues); + shapeBinderObj->Shape.setValue(shape); } + copy = shapeBinderObj; } if (independent && shapeProp) { - if (entity.empty()) { - shapeProp->setValue(static_cast(obj)->Shape.getValue()); - } - else { - shapeProp->setValue( - static_cast(obj)->Shape.getShape().getSubShape(entity.c_str())); - } + auto* featureObj = static_cast(obj); + shapeProp->setValue(entity.empty() ? featureObj->Shape.getValue() + : featureObj->Shape.getShape().getSubShape(entity.c_str())); } }