Mod: Use new addObject<T>(...) that requires additional changes

This commit is contained in:
Benjamin Nauck
2025-02-09 14:09:56 +01:00
parent a1c5767643
commit 8cc98b9a88
19 changed files with 60 additions and 82 deletions

View File

@@ -158,7 +158,7 @@ App::DocumentObjectExecReturn* OriginGroupExtension::extensionExecute()
App::DocumentObject* OriginGroupExtension::getLocalizedOrigin(App::Document* doc)
{
App::DocumentObject* originObject = doc->addObject("App::Origin", "Origin");
auto* originObject = doc->addObject<App::Origin>("Origin");
QByteArray byteArray = tr("Origin").toUtf8();
originObject->Label.setValue(byteArray.constData());
return originObject;

View File

@@ -228,8 +228,7 @@ private:
std::string gcode = buffer.str();
Path::Toolpath path;
path.setFromGCode(gcode);
Path::Feature* object = static_cast<Path::Feature*>(
pcDoc->addObject("Path::Feature", file.fileNamePure().c_str()));
auto* object = pcDoc->addObject<Path::Feature>(file.fileNamePure().c_str());
object->Path.setValue(path);
pcDoc->recompute();
}
@@ -254,9 +253,8 @@ private:
if (!pcDoc) {
pcDoc = App::GetApplication().newDocument();
}
Path::PathPy* pPath = static_cast<Path::PathPy*>(pcObj);
Path::Feature* pcFeature =
static_cast<Path::Feature*>(pcDoc->addObject("Path::Feature", name));
auto* pPath = static_cast<Path::PathPy*>(pcObj);
auto* pcFeature = pcDoc->addObject<Path::Feature>(name);
Path::Toolpath* pa = pPath->getToolpathPtr();
if (!pa) {
throw Py::Exception(PyExc_ReferenceError, "object doesn't reference a valid path");

View File

@@ -168,8 +168,7 @@ private:
#ifdef FC_USE_VTK
if (FemPostPipeline::canRead(file)) {
FemPostPipeline* pcFeature = static_cast<FemPostPipeline*>(
pcDoc->addObject("Fem::FemPostPipeline", file.fileNamePure().c_str()));
auto* pcFeature = pcDoc->addObject<FemPostPipeline>(file.fileNamePure().c_str());
pcFeature->Label.setValue(file.fileNamePure().c_str());
pcFeature->read(file);

View File

@@ -619,7 +619,7 @@ App::DocumentObject* FemVTKTools::readResult(const char* filename, App::Document
}
}
App::DocumentObject* mesh = pcDoc->addObject("Fem::FemMeshObject", "ResultMesh");
auto* mesh = pcDoc->addObject<Fem::FemMeshObject>("ResultMesh");
std::unique_ptr<FemMesh> fmesh(new FemMesh());
importVTKMesh(dataset, fmesh.get());
static_cast<PropertyFemMesh*>(mesh->getPropertyByName("FemMesh"))->setValuePtr(fmesh.release());

View File

@@ -219,13 +219,12 @@ Measure::MeasureBase* TaskMeasure::createObject(const App::MeasureType* measureT
auto pyMeasureClass = measureType->pythonClass;
// Create a MeasurePython instance
auto featurePython = doc->addObject("Measure::MeasurePython", measureType->label.c_str());
_mMeasureObject = dynamic_cast<Measure::MeasureBase*>(featurePython);
_mMeasureObject = doc->addObject<Measure::MeasurePython>(measureType->label.c_str());
// Create an instance of the pyMeasureClass, the classe's initializer sets the object as
// proxy
Py::Tuple args(1);
args.setItem(0, Py::asObject(featurePython->getPyObject()));
args.setItem(0, Py::asObject(_mMeasureObject->getPyObject()));
PyObject* result = PyObject_CallObject(pyMeasureClass, args.ptr());
Py_XDECREF(result);
}
@@ -249,8 +248,7 @@ void TaskMeasure::update()
App::DocumentObject* sub = ob->getSubObject(sel.SubName);
// Resolve App::Link
if (sub->isDerivedFrom<App::Link>()) {
auto link = static_cast<App::Link*>(sub);
if (auto link = Base::freecad_dynamic_cast<App::Link>(sub)) {
sub = link->getLinkedObject(true);
}
@@ -341,7 +339,7 @@ void TaskMeasure::initViewObject()
dynamic_cast<MeasureGui::ViewProviderMeasureBase*>(viewObject)->positionAnno(_mMeasureObject);
// Set the ShowDelta Property if it exists on the measurements view object
auto* prop = dynamic_cast<App::PropertyBool*>(viewObject->getPropertyByName("ShowDelta"));
auto* prop = viewObject->getPropertyByName<App::PropertyBool>("ShowDelta");
setDeltaPossible(prop != nullptr);
if (prop) {
prop->setValue(showDelta->isChecked());
@@ -370,10 +368,9 @@ void TaskMeasure::ensureGroup(Measure::MeasureBase* measurement)
if (!obj || !obj->isValid() || !obj->isDerivedFrom<App::DocumentObjectGroup>()) {
obj = doc->addObject("App::DocumentObjectGroup",
measurementGroupName,
true,
"MeasureGui::ViewProviderMeasureGroup");
obj = doc->addObject<App::DocumentObjectGroup>(measurementGroupName,
true,
"MeasureGui::ViewProviderMeasureGroup");
}
auto group = static_cast<App::DocumentObjectGroup*>(obj);

View File

@@ -120,8 +120,7 @@ void Segmentation::accept()
std::string internalname = "Segments_";
internalname += myMesh->getNameInDocument();
App::DocumentObjectGroup* group = static_cast<App::DocumentObjectGroup*>(
document->addObject("App::DocumentObjectGroup", internalname.c_str()));
auto* group = document->addObject<App::DocumentObjectGroup>(internalname.c_str());
std::string labelname = "Segments ";
labelname += myMesh->Label.getValue();
group->Label.setValue(labelname);

View File

@@ -505,8 +505,7 @@ void SegmentationBestFit::accept()
std::string internalname = "Segments_";
internalname += myMesh->getNameInDocument();
App::DocumentObjectGroup* group = static_cast<App::DocumentObjectGroup*>(
document->addObject("App::DocumentObjectGroup", internalname.c_str()));
auto* group = document->addObject<App::DocumentObjectGroup>(internalname.c_str());
std::string labelname = "Segments ";
labelname += myMesh->Label.getValue();
group->Label.setValue(labelname);

View File

@@ -1630,7 +1630,7 @@ void ViewProviderMesh::splitMesh(const MeshCore::MeshKernel& toolMesh,
removeFacets(indices);
auto doc = App::GetApplication().getActiveDocument();
const char* name = pcObject->getNameInDocument();
auto splitMesh = dynamic_cast<Mesh::Feature*>(doc->addObject("Mesh::Feature", name));
auto splitMesh = doc->addObject<Mesh::Feature>(name);
// Note: deletes also kernel
splitMesh->Mesh.setValuePtr(kernel);
getObject()->purgeTouched();

View File

@@ -1556,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

@@ -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());

View File

@@ -295,8 +295,7 @@ private:
pcFeature->purgeTouched();
}
else {
Points::Feature* pcFeature = static_cast<Points::Feature*>(
pcDoc->addObject("Points::Feature", file.fileNamePure().c_str()));
auto* pcFeature = pcDoc->addObject<Points::Feature>(file.fileNamePure().c_str());
pcFeature->Points.setValue(reader->getPoints());
pcDoc->recomputeFeature(pcFeature);
pcFeature->purgeTouched();
@@ -413,8 +412,8 @@ private:
if (!pcDoc) {
pcDoc = App::GetApplication().newDocument();
}
PointsPy* pPoints = static_cast<PointsPy*>(pcObj);
Points::Feature* pcFeature = pcDoc->addObject<Points::Feature>(name);
auto* pPoints = static_cast<PointsPy*>(pcObj);
auto* pcFeature = pcDoc->addObject<Points::Feature>(name);
// copy the data
pcFeature->Points.setValue(*(pPoints->getPointKernelPtr()));
return Py::asObject(pcFeature->getPyObject());

View File

@@ -497,8 +497,7 @@ void CmdSegmentationFromComponents::activated(int)
for (auto it : sel) {
std::string internalname = "Segments_";
internalname += it->getNameInDocument();
App::DocumentObjectGroup* group = static_cast<App::DocumentObjectGroup*>(
doc->addObject("App::DocumentObjectGroup", internalname.c_str()));
auto* group = doc->addObject<App::DocumentObjectGroup>(internalname.c_str());
std::string labelname = "Segments ";
labelname += it->Label.getValue();
group->Label.setValue(labelname);

View File

@@ -138,8 +138,7 @@ void Segmentation::accept()
std::string internalname = "Segments_";
internalname += myMesh->getNameInDocument();
App::DocumentObjectGroup* group = static_cast<App::DocumentObjectGroup*>(
document->addObject("App::DocumentObjectGroup", internalname.c_str()));
auto* group = document->addObject<App::DocumentObjectGroup>(internalname.c_str());
std::string labelname = "Segments ";
labelname += myMesh->Label.getValue();
group->Label.setValue(labelname);

View File

@@ -295,8 +295,7 @@ void SegmentationManual::createSegment()
algo.GetFacetsFlag(facets, MeshCore::MeshFacet::SELECTED);
std::unique_ptr<Mesh::MeshObject> segment(mesh.meshFromSegment(facets));
Mesh::Feature* feaSegm =
static_cast<Mesh::Feature*>(adoc->addObject("Mesh::Feature", "Segment"));
auto* feaSegm = adoc->addObject<Mesh::Feature>("Segment");
Mesh::MeshObject* feaMesh = feaSegm->Mesh.startEditing();
feaMesh->swap(*segment);
feaMesh->clearFacetSelection();

View File

@@ -95,8 +95,8 @@ private:
}
if (file.hasExtension("skf")) {
Sketcher::SketchObjectSF* pcFeature = static_cast<Sketcher::SketchObjectSF*>(
pcDoc->addObject("Sketcher::SketchObjectSF", file.fileNamePure().c_str()));
auto filename = file.fileNamePure().c_str();
auto* pcFeature = pcDoc->addObject<Sketcher::SketchObjectSF>(filename);
pcFeature->SketchFlatFile.setValue(EncodedName.c_str());
pcDoc->recompute();

View File

@@ -69,8 +69,8 @@ private:
{
try {
Base::FileInfo file(Name);
Spreadsheet::Sheet* pcSheet = static_cast<Spreadsheet::Sheet*>(
pcDoc->addObject("Spreadsheet::Sheet", file.fileNamePure().c_str()));
auto filename = file.fileNamePure().c_str();
auto* pcSheet = pcDoc->addObject<Spreadsheet::Sheet>(filename);
pcSheet->importFromFile(Name, '\t', '"', '\\');
pcSheet->execute();

View File

@@ -200,9 +200,8 @@ void CmdSpreadsheetImport::activated(int iMsg)
&selectedFilter);
if (!fileName.isEmpty()) {
std::string FeatName = getUniqueObjectName("Spreadsheet");
Sheet* sheet = freecad_dynamic_cast<Sheet>(
App::GetApplication().getActiveDocument()->addObject("Spreadsheet::Sheet",
FeatName.c_str()));
auto* doc = App::GetApplication().getActiveDocument();
Sheet* sheet = doc->addObject<Spreadsheet::Sheet>(FeatName.c_str());
if (sheet) {
char delim, quote, escape;
std::string errMsg = "Import";

View File

@@ -463,43 +463,39 @@ App::DocumentObject* DrawProjGroup::addProjection(const char* viewProjType)
if (checkViewProjType(viewProjType) && !hasProjection(viewProjType)) {
std::string FeatName = getDocument()->getUniqueObjectName("ProjItem");
auto docObj(getDocument()->addObject("TechDraw::DrawProjGroupItem",//add to Document
FeatName.c_str()));
view = dynamic_cast<TechDraw::DrawProjGroupItem*>(docObj);
if (!view && docObj) {
view = getDocument()->addObject<TechDraw::DrawProjGroupItem>(FeatName.c_str());
if (!view) {
//should never happen that we create a DPGI that isn't a DPGI!!
Base::Console().Error("PROBLEM - DPG::addProjection - created a non DPGI! %s / %s\n",
getNameInDocument(), viewProjType);
throw Base::TypeError("Error: new projection is not a DPGI!");
}
if (view) {//coverity CID 151722
// the label must be set before the view is added
view->Label.setValue(viewProjType);
// somewhere deep in DocumentObject, duplicate Labels have a numeric suffix applied,
// so we need to wait until that happens before building the translated label
view->translateLabel("DrawProjGroupItem", viewProjType, view->Label.getValue());
addView(view);//from DrawViewCollection
view->Source.setValues(Source.getValues());
view->XSource.setValues(XSource.getValues());
// the label must be set before the view is added
view->Label.setValue(viewProjType);
// somewhere deep in DocumentObject, duplicate Labels have a numeric suffix applied,
// so we need to wait until that happens before building the translated label
view->translateLabel("DrawProjGroupItem", viewProjType, view->Label.getValue());
addView(view);//from DrawViewCollection
view->Source.setValues(Source.getValues());
view->XSource.setValues(XSource.getValues());
// the Scale is already set by DrawView
view->Type.setValue(viewProjType);
if (strcmp(viewProjType, "Front") != 0) {//not Front!
vecs = getDirsFromFront(view);
view->Direction.setValue(vecs.first);
view->XDirection.setValue(vecs.second);
view->recomputeFeature();
}
else {//Front
Anchor.setValue(view);
Anchor.purgeTouched();
requestPaint();//make sure the group object is on the Gui page
view->LockPosition.setValue(
true);//lock "Front" position within DPG (note not Page!).
view->LockPosition.setStatus(App::Property::ReadOnly,
true);//Front should stay locked.
view->LockPosition.purgeTouched();
}
// the Scale is already set by DrawView
view->Type.setValue(viewProjType);
if (strcmp(viewProjType, "Front") != 0) {//not Front!
vecs = getDirsFromFront(view);
view->Direction.setValue(vecs.first);
view->XDirection.setValue(vecs.second);
view->recomputeFeature();
}
else {//Front
Anchor.setValue(view);
Anchor.purgeTouched();
requestPaint();//make sure the group object is on the Gui page
view->LockPosition.setValue(
true);//lock "Front" position within DPG (note not Page!).
view->LockPosition.setStatus(App::Property::ReadOnly,
true);//Front should stay locked.
view->LockPosition.purgeTouched();
}
}
return view;

View File

@@ -397,8 +397,7 @@ bool TaskSurfaceFinishSymbols::accept()
{
Gui::Command::openCommand(QT_TRANSLATE_NOOP("Command", "Surface Finish Symbols"));
App::Document *doc = Application::Instance->activeDocument()->getDocument();
App::DocumentObject *docObject = doc->addObject("TechDraw::DrawViewSymbol", "SurfaceSymbol");
TechDraw::DrawViewSymbol *surfaceSymbol = dynamic_cast<TechDraw::DrawViewSymbol*>(docObject);
auto* surfaceSymbol = doc->addObject<TechDraw::DrawViewSymbol>("SurfaceSymbol");
surfaceSymbol->Symbol.setValue(completeSymbol());
surfaceSymbol->Rotation.setValue(ui->leAngle->text().toDouble());