PD: Light refactoring of TaskFeaturePick::makeCopy

This commit is contained in:
Benjamin Nauck
2025-01-27 22:21:14 +01:00
parent 2107e60304
commit 4b63d65ffa

View File

@@ -29,6 +29,8 @@
#include <QTimer>
#endif
#include <fmt/format.h>
#include <App/Document.h>
#include <App/Origin.h>
#include <App/Datums.h>
@@ -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<App::Property*> 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<Sketcher::SketchObject>()) {
static_cast<Sketcher::SketchObject*>(copy)->delConstraintsToExternal();
if (auto* sketchObj = Base::freecad_dynamic_cast<Sketcher::SketchObject>(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<Part::Datum>()) {
copy = App::GetApplication().getActiveDocument()->addObject(obj->getTypeId().getName(),
name.c_str());
assert(copy->isDerivedFrom<Part::Datum>());
auto* doc = App::GetApplication().getActiveDocument();
copy = doc->addObject<Part::Datum>(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<PartDesign::ShapeBinder>()
|| obj->isDerivedFrom<Part::Feature>()) {
copy = App::GetApplication().getActiveDocument()->addObject("PartDesign::ShapeBinder",
name.c_str());
auto* doc = App::GetApplication().getActiveDocument();
auto* shapeBinderObj = doc->addObject<PartDesign::ShapeBinder>(name.c_str());
if (!independent) {
static_cast<PartDesign::ShapeBinder*>(copy)->Support.setValue(obj, entity.c_str());
shapeBinderObj->Support.setValue(obj, entity.c_str());
}
else {
shapeProp = &static_cast<PartDesign::ShapeBinder*>(copy)->Shape;
shapeProp = &shapeBinderObj->Shape;
}
copy = shapeBinderObj;
}
else if (obj->isDerivedFrom<App::Plane>()
|| obj->isDerivedFrom<App::Line>()) {
copy = App::GetApplication().getActiveDocument()->addObject("PartDesign::ShapeBinder",
name.c_str());
auto* doc = App::GetApplication().getActiveDocument();
auto* shapeBinderObj = doc->addObject<PartDesign::ShapeBinder>(name.c_str());
if (!independent) {
static_cast<PartDesign::ShapeBinder*>(copy)->Support.setValue(obj, entity.c_str());
shapeBinderObj->Support.setValue(obj, entity.c_str());
}
else {
App::GeoFeature* geo = static_cast<App::GeoFeature*>(obj);
std::vector<std::string> subvalues;
subvalues.push_back(entity);
Part::TopoShape shape =
PartDesign::ShapeBinder::buildShapeFromReferences(geo, subvalues);
static_cast<PartDesign::ShapeBinder*>(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<Part::Feature*>(obj)->Shape.getValue());
}
else {
shapeProp->setValue(
static_cast<Part::Feature*>(obj)->Shape.getShape().getSubShape(entity.c_str()));
}
auto* featureObj = static_cast<Part::Feature*>(obj);
shapeProp->setValue(entity.empty() ? featureObj->Shape.getValue()
: featureObj->Shape.getShape().getSubShape(entity.c_str()));
}
}