Merge pull request #19142 from hyarion/refactor/add-template-addobject

Add new addObject<T>() function
This commit is contained in:
Chris Hennes
2025-02-11 09:42:47 -06:00
committed by GitHub
95 changed files with 335 additions and 340 deletions

View File

@@ -866,7 +866,7 @@ private:
} else {
throw Py::TypeError("Expects argument of type DocumentObject, Shape, or Geometry");
}
Part::Feature *pcFeature = static_cast<Part::Feature*>(pcDoc->addObject("Part::Feature", name));
Part::Feature *pcFeature = pcDoc->addObject<Part::Feature>(name);
// copy the data
pcFeature->Shape.setValue(shape);
pcFeature->purgeTouched();

View File

@@ -34,7 +34,7 @@ namespace Part
*/
class PartExport CustomFeature : public Part::Feature
{
PROPERTY_HEADER_WITH_OVERRIDE(Part::UserFeature);
PROPERTY_HEADER_WITH_OVERRIDE(Part::CustomFeature);
public:
/// Constructor

View File

@@ -31,7 +31,7 @@ namespace Part
class CurveNet :public Part::Feature
{
PROPERTY_HEADER_WITH_OVERRIDE(Part::FeaturePartCurveNet);
PROPERTY_HEADER_WITH_OVERRIDE(Part::CurveNet);
public:
CurveNet();

View File

@@ -31,7 +31,7 @@ namespace Part
class ImportIges :public Part::Feature
{
PROPERTY_HEADER_WITH_OVERRIDE(Part::FeaturePartImportIges);
PROPERTY_HEADER_WITH_OVERRIDE(Part::ImportIges);
public:
ImportIges();

View File

@@ -33,7 +33,7 @@ namespace Part
class ImportStep :public Part::Feature
{
PROPERTY_HEADER_WITH_OVERRIDE(Part::FeaturePartImportStep);
PROPERTY_HEADER_WITH_OVERRIDE(Part::ImportStep);
public:
ImportStep();

View File

@@ -35,7 +35,7 @@ namespace Part
class PartExport ProjectOnSurface : public Part::Feature
{
PROPERTY_HEADER_WITH_OVERRIDE(ProjectOnSurface);
PROPERTY_HEADER_WITH_OVERRIDE(Part::ProjectOnSurface);
public:
ProjectOnSurface();

View File

@@ -103,8 +103,8 @@ int Part::ImportIgesParts(App::Document *pcDoc, const char* FileName)
if (aShape.ShapeType() == TopAbs_SOLID ||
aShape.ShapeType() == TopAbs_COMPOUND ||
aShape.ShapeType() == TopAbs_SHELL) {
App::DocumentObject* obj = pcDoc->addObject("Part::Feature", aName.c_str());
static_cast<Part::Feature*>(obj)->Shape.setValue(aShape);
auto* obj = pcDoc->addObject<Part::Feature>(aName.c_str());
obj->Shape.setValue(aShape);
}
else {
builder.Add(comp, aShape);
@@ -114,8 +114,7 @@ int Part::ImportIgesParts(App::Document *pcDoc, const char* FileName)
}
if (!emptyComp) {
std::string name = fi.fileNamePure();
Part::Feature *pcFeature = static_cast<Part::Feature*>(pcDoc->addObject
("Part::Feature", name.c_str()));
auto* pcFeature = pcDoc->addObject<Part::Feature>(name.c_str());
pcFeature->Shape.setValue(comp);
}
}

View File

@@ -132,7 +132,7 @@ int Part::ImportStepParts(App::Document *pcDoc, const char* Name)
//}
Part::Feature *pcFeature;
pcFeature = static_cast<Part::Feature*>(pcDoc->addObject("Part::Feature", name.c_str()));
pcFeature = pcDoc->addObject<Part::Feature>(name.c_str());
pcFeature->Shape.setValue(aSolid);
// This is a trick to access the GUI via Python and set the color property
@@ -167,7 +167,7 @@ int Part::ImportStepParts(App::Document *pcDoc, const char* Name)
// name += ws->Model()->StringLabel(ent)->ToCString();
//}
Part::Feature *pcFeature = static_cast<Part::Feature*>(pcDoc->addObject("Part::Feature", name.c_str()));
Part::Feature *pcFeature = pcDoc->addObject<Part::Feature>(name.c_str());
pcFeature->Shape.setValue(aShell);
}

View File

@@ -1575,7 +1575,7 @@ Feature* Feature::create(const TopoShape& shape, const char* name, App::Document
document = App::GetApplication().newDocument();
}
}
auto res = static_cast<Part::Feature*>(document->addObject("Part::Feature", name));
auto res = document->addObject<Part::Feature>(name);
res->Shape.setValue(shape);
res->purgeTouched();
return res;

View File

@@ -260,8 +260,7 @@ void CrossSections::apply()
App::Document* doc = (*it)->getDocument();
std::string s = (*it)->getNameInDocument();
s += "_cs";
Part::Feature* section = static_cast<Part::Feature*>
(doc->addObject("Part::Feature",s.c_str()));
auto* section = doc->addObject<Part::Feature>(s.c_str());
section->Shape.setValue(comp);
section->purgeTouched();
}

View File

@@ -165,8 +165,7 @@ DlgProjectionOnSurface::DlgProjectionOnSurface(QWidget* parent)
}
this->attachDocument(m_partDocument);
m_partDocument->openTransaction("Project on surface");
m_projectionObject = dynamic_cast<Part::Feature*>(
m_partDocument->addObject("Part::Feature", "Projection Object"));
m_projectionObject = m_partDocument->addObject<Part::Feature>("Projection Object");
if (!m_projectionObject) {
throw Base::ValueError(QString(tr("Can not create a projection object!!!")).toUtf8());
}
@@ -1557,8 +1556,7 @@ TaskProjectOnSurface::TaskProjectOnSurface(App::Document* doc)
{
setDocumentName(doc->getName());
doc->openTransaction(QT_TRANSLATE_NOOP("Command", "Project on surface"));
auto obj = doc->addObject("Part::ProjectOnSurface", "Projection");
auto feature = dynamic_cast<Part::ProjectOnSurface*>(obj);
auto feature = doc->addObject<Part::ProjectOnSurface>("Projection");
widget = new DlgProjectOnSurface(feature);
taskbox = new Gui::TaskView::TaskBox(Gui::BitmapFactory().pixmap("Part_ProjectionOnSurface"),
widget->windowTitle(), true, nullptr);

View File

@@ -606,7 +606,7 @@ void SectionCut::restoreVisibility()
Part::Box* SectionCut::createBox(const char* name, const Base::Vector3f& size) // NOLINT
{
// create a box
auto pcBox = dynamic_cast<Part::Box*>(doc->addObject("Part::Box", name));
auto pcBox = doc->addObject<Part::Box>(name);
if (!pcBox) {
throw Base::RuntimeError(std::string("SectionCut error: ")
+ std::string(name) + std::string(" could not be added\n"));
@@ -785,7 +785,7 @@ Part::Box* SectionCut::createZBox(const Base::Vector3f& pos, const Base::Vector3
Part::Cut* SectionCut::createCut(const char* name)
{
auto pcCut = dynamic_cast<Part::Cut*>(doc->addObject("Part::Cut", name));
auto pcCut = doc->addObject<Part::Cut>(name);
if (!pcCut) {
throw Base::RuntimeError(std::string("SectionCut error: ")
+ std::string(name) + std::string(" could not be added\n"));
@@ -1004,7 +1004,7 @@ std::vector<App::DocumentObject*> createLinks(App::Document* doc, const std::vec
}
newName += "_CutLink";
auto pcLink = dynamic_cast<App::Link*>(doc->addObject("App::Link", newName.c_str()));
auto pcLink = doc->addObject<App::Link>(newName.c_str());
if (!pcLink) {
throw Base::RuntimeError("'App::Link' could not be added");
}
@@ -1019,13 +1019,10 @@ std::vector<App::DocumentObject*> createLinks(App::Document* doc, const std::vec
// if the object is part of an App::Part container,
// the link needs to get the container placement
auto parents = itCuts->getInList();
if (!parents.empty()) {
if (auto parents = itCuts->getInList(); !parents.empty()) {
for (auto parent : parents) {
if (auto pcPartParent = dynamic_cast<App::Part*>(parent)) {
auto placement = Base::freecad_dynamic_cast<App::PropertyPlacement>(
pcPartParent->getPropertyByName("Placement"));
if (placement) {
if (auto placement = pcPartParent->getPropertyByName<App::PropertyPlacement>("Placement")) {
pcLink->Placement.setValue(placement->getValue());
}
}
@@ -2421,7 +2418,7 @@ App::DocumentObject* SectionCut::createBooleanFragments(
Part::Compound* SectionCut::createCompound(const std::vector<App::DocumentObject*>& links,
int transparency)
{
auto CutCompoundPart = dynamic_cast<Part::Compound*>(doc->addObject("Part::Compound", CompoundName));
auto CutCompoundPart = doc->addObject<Part::Compound>(CompoundName);
if (!CutCompoundPart) {
throw Base::RuntimeError((std::string("SectionCut error: ") + std::string(CompoundName)
+ std::string(" could not be added\n")).c_str());