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

@@ -24,10 +24,8 @@ protected:
{
_docName = App::GetApplication().getUniqueDocumentName("test");
auto _doc = App::GetApplication().newDocument(_docName.c_str(), "testUser");
_assemblyObj =
static_cast<Assembly::AssemblyObject*>(_doc->addObject("Assembly::AssemblyObject"));
_jointGroupObj = static_cast<Assembly::JointGroup*>(
_assemblyObj->addObject("Assembly::JointGroup", "jointGroupTest"));
_assemblyObj = _doc->addObject<Assembly::AssemblyObject>();
_jointGroupObj = _assemblyObj->addObject<Assembly::JointGroup>("jointGroupTest");
}
void TearDown() override

View File

@@ -48,13 +48,12 @@ private:
TEST_F(MeasureDistance, testCircleCircle)
{
App::Document* doc = getDocument();
auto p1 = dynamic_cast<Part::Feature*>(doc->addObject("Part::Feature", "Shape1"));
auto p1 = doc->addObject<Part::Feature>("Shape1");
p1->Shape.setValue(makeCircle(gp_Pnt(0.0, 0.0, 0.0)));
auto p2 = dynamic_cast<Part::Feature*>(doc->addObject("Part::Feature", "Shape2"));
auto p2 = doc->addObject<Part::Feature>("Shape2");
p2->Shape.setValue(makeCircle(gp_Pnt(3.0, 4.0, 0.0)));
auto md = dynamic_cast<Measure::MeasureDistance*>(
doc->addObject("Measure::MeasureDistance", "Distance"));
auto md = doc->addObject<Measure::MeasureDistance>("Distance");
md->Element1.setValue(p1, {"Edge1"});
md->Element2.setValue(p2, {"Edge1"});

View File

@@ -22,11 +22,11 @@ protected:
fileInfo.setFile(Base::FileInfo::getTempFileName() + ".stl");
const double value = 10.0;
cube1 = dynamic_cast<Mesh::Cube*>(document->addObject("Mesh::Cube", "Cube1"));
cube1 = document->addObject<Mesh::Cube>("Cube1");
cube1->Length.setValue(value);
cube1->Width.setValue(value);
cube1->Height.setValue(value);
cube2 = dynamic_cast<Mesh::Cube*>(document->addObject("Mesh::Cube", "Cube2"));
cube2 = document->addObject<Mesh::Cube>("Cube2");
cube2->Length.setValue(value);
cube2->Width.setValue(value);
cube2->Height.setValue(value);
@@ -130,7 +130,7 @@ TEST_F(ExporterTest, TestMeshesInPart)
// add extra scope because the file will be written when destroying the exporter
{
auto part = dynamic_cast<App::Part*>(getDocument()->addObject("App::Part", "Part"));
auto part = getDocument()->addObject<App::Part>("Part");
part->placement().setValue(plm);
part->addObjects(getObjects());
Mesh::MergeExporter exporter(getFileName(), MeshCore::MeshIO::Format::STL);

View File

@@ -38,8 +38,8 @@ private:
TEST_F(AttachExtensionTest, testPlanePlane)
{
auto plane1 = dynamic_cast<Part::Plane*>(getDocument()->addObject("Part::Plane", "Plane1"));
auto plane2 = dynamic_cast<Part::Plane*>(getDocument()->addObject("Part::Plane", "Plane2"));
auto plane1 = getDocument()->addObject<Part::Plane>("Plane1");
auto plane2 = getDocument()->addObject<Part::Plane>("Plane2");
ASSERT_TRUE(plane1);
ASSERT_TRUE(plane2);
@@ -57,7 +57,7 @@ TEST_F(AttachExtensionTest, testPlanePlane)
TEST_F(AttachExtensionTest, testAttacherEngineType)
{
auto plane = dynamic_cast<Part::Plane*>(getDocument()->addObject("Part::Plane", "Plane"));
auto plane = getDocument()->addObject<Part::Plane>("Plane");
EXPECT_STREQ(plane->AttacherType.getValue(), "Attacher::AttachEngine3D");
EXPECT_STREQ(plane->AttacherEngine.getValueAsString(), "Engine 3D");
@@ -76,7 +76,7 @@ TEST_F(AttachExtensionTest, testAttacherEngineType)
TEST_F(AttachExtensionTest, testAttacherTypeEngine)
{
auto plane = dynamic_cast<Part::Plane*>(getDocument()->addObject("Part::Plane", "Plane"));
auto plane = getDocument()->addObject<Part::Plane>("Plane");
EXPECT_STREQ(plane->AttacherType.getValue(), "Attacher::AttachEngine3D");
EXPECT_STREQ(plane->AttacherEngine.getValueAsString(), "Engine 3D");

View File

@@ -28,11 +28,11 @@ protected:
_boxes[1]->Length.setValue(1);
_boxes[1]->Width.setValue(2);
_boxes[1]->Height.setValue(3);
_fused = dynamic_cast<Part::Fuse*>(_doc->addObject("Part::Fuse"));
_fused = _doc->addObject<Part::Fuse>();
_fused->Base.setValue(_boxes[0]);
_fused->Tool.setValue(_boxes[1]);
_fused->execute();
_chamfer = dynamic_cast<Part::Chamfer*>(_doc->addObject("Part::Chamfer"));
_chamfer = _doc->addObject<Part::Chamfer>();
}
void TearDown() override

View File

@@ -19,7 +19,7 @@ protected:
void SetUp() override
{
createTestDoc();
_compound = dynamic_cast<Part::Compound*>(_doc->addObject("Part::Compound"));
_compound = _doc->addObject<Part::Compound>();
}
void TearDown() override

View File

@@ -21,7 +21,7 @@ protected:
void SetUp() override
{
createTestDoc();
_extrusion = dynamic_cast<Part::Extrusion*>(_doc->addObject("Part::Extrusion"));
_extrusion = _doc->addObject<Part::Extrusion>();
PartTestHelpers::rectangle(len, wid, "Rect1");
_extrusion->Base.setValue(_doc->getObjects().back());
_extrusion->LengthFwd.setValue(ext1);
@@ -252,7 +252,7 @@ TEST_F(FeatureExtrusionTest, testExecuteEdge)
const double ang = 30;
const double tangent = tan(ang / 180.0 * M_PI);
BRepBuilderAPI_MakeEdge e1(gp_Pnt(0, 0, 0), gp_Pnt(ext1, ext1, ext1));
auto edge = dynamic_cast<Part::Feature*>(_doc->addObject("Part::Feature", "Edge"));
auto edge = _doc->addObject<Part::Feature>("Edge");
edge->Shape.setValue(e1);
_extrusion->DirLink.setValue(edge);
_extrusion->DirMode.setValue(1);
@@ -310,7 +310,7 @@ TEST_F(FeatureExtrusionTest, testFaceWithHoles)
// newFace cleans that up and is the outside minus the internal hole.
auto face2 = newFace.getShape();
auto partFeature = dynamic_cast<Part::Feature*>(_doc->addObject("Part::Feature"));
auto partFeature = _doc->addObject<Part::Feature>();
partFeature->Shape.setValue(face2);
_extrusion->Base.setValue(_doc->getObjects().back());
_extrusion->FaceMakerClass.setValue("Part::FaceMakerCheese");

View File

@@ -28,11 +28,11 @@ protected:
_boxes[1]->Length.setValue(1);
_boxes[1]->Width.setValue(2);
_boxes[1]->Height.setValue(3);
_fused = dynamic_cast<Part::Fuse*>(_doc->addObject("Part::Fuse"));
_fused = _doc->addObject<Part::Fuse>();
_fused->Base.setValue(_boxes[0]);
_fused->Tool.setValue(_boxes[1]);
_fused->execute();
_fillet = dynamic_cast<Part::Fillet*>(_doc->addObject("Part::Fillet"));
_fillet = _doc->addObject<Part::Fillet>();
}
void TearDown() override

View File

@@ -21,7 +21,7 @@ protected:
void SetUp() override
{
createTestDoc();
_mirror = dynamic_cast<Part::Mirroring*>(_doc->addObject("Part::Mirroring"));
_mirror = _doc->addObject<Part::Mirroring>();
_mirror->Source.setValue(_boxes[0]);
_mirror->Base.setValue(1, 0, 0);
_mirror->execute();
@@ -61,7 +61,7 @@ TEST_F(FeatureMirroringTest, testYMirrorWithExistingElementMap)
{
// Arrange
Part::Fuse* _fuse = nullptr; // NOLINT Can't be private in a test framework
_fuse = dynamic_cast<Part::Fuse*>(_doc->addObject("Part::Fuse"));
_fuse = _doc->addObject<Part::Fuse>();
_fuse->Base.setValue(_boxes[0]);
_fuse->Tool.setValue(_boxes[1]);
// Act

View File

@@ -21,7 +21,7 @@ protected:
void SetUp() override
{
createTestDoc();
_offset = dynamic_cast<Part::Offset*>(_doc->addObject("Part::Offset"));
_offset = _doc->addObject<Part::Offset>();
_offset->Source.setValue(_boxes[0]);
_offset->Value.setValue(1);
_offset->Join.setValue((int)JoinType::intersection);
@@ -52,7 +52,7 @@ TEST_F(FeatureOffsetTest, testOffset3DWithExistingElementMap)
{
// Arrange
Part::Fuse* _fuse = nullptr; // NOLINT Can't be private in a test framework
_fuse = dynamic_cast<Part::Fuse*>(_doc->addObject("Part::Fuse"));
_fuse = _doc->addObject<Part::Fuse>();
_fuse->Base.setValue(_boxes[0]);
_fuse->Tool.setValue(_boxes[1]);
_fuse->Refine.setValue(true);
@@ -76,8 +76,8 @@ TEST_F(FeatureOffsetTest, testOffset3DWithExistingElementMap)
TEST_F(FeatureOffsetTest, testOffset2D)
{
// Arrange
Part::Offset2D* _offset2 = dynamic_cast<Part::Offset2D*>(_doc->addObject("Part::Offset2D"));
Part::Plane* _pln = dynamic_cast<Part::Plane*>(_doc->addObject("Part::Plane"));
Part::Offset2D* _offset2 = _doc->addObject<Part::Offset2D>();
Part::Plane* _pln = _doc->addObject<Part::Plane>();
_pln->Length.setValue(2);
_pln->Width.setValue(3);
_offset2->Source.setValue(_pln);

View File

@@ -19,7 +19,7 @@ protected:
void SetUp() override
{
// createTestDoc();
// _boolean = dynamic_cast<Part::Boolean*>(_doc->addObject("Part::Boolean"));
// _boolean = _doc->addObject<Part::Boolean>();
}
void TearDown() override

View File

@@ -19,7 +19,7 @@ protected:
void SetUp() override
{
createTestDoc();
_common = dynamic_cast<Part::Common*>(_doc->addObject("Part::Common"));
_common = _doc->addObject<Part::Common>();
}
void TearDown() override

View File

@@ -18,7 +18,7 @@ protected:
void SetUp() override
{
createTestDoc();
_cut = dynamic_cast<Part::Cut*>(_doc->addObject("Part::Cut"));
_cut = _doc->addObject<Part::Cut>();
}
void TearDown() override

View File

@@ -20,8 +20,8 @@ protected:
void SetUp() override
{
createTestDoc();
_fuse = dynamic_cast<Part::Fuse*>(_doc->addObject("Part::Fuse"));
_multiFuse = dynamic_cast<Part::MultiFuse*>(_doc->addObject("Part::MultiFuse"));
_fuse = _doc->addObject<Part::Fuse>();
_multiFuse = _doc->addObject<Part::MultiFuse>();
}
void TearDown() override
@@ -58,7 +58,7 @@ TEST_F(FeaturePartFuseTest, testCompound)
{
// Arrange
Part::Compound* _compound = nullptr;
_compound = dynamic_cast<Part::Compound*>(_doc->addObject("Part::Compound"));
_compound = _doc->addObject<Part::Compound>();
_compound->Links.setValues({_boxes[0], _boxes[1]});
_multiFuse->Shapes.setValues({_compound});
@@ -85,7 +85,7 @@ TEST_F(FeaturePartFuseTest, testRecursiveCompound)
Part::Compound* _compound[3] = {nullptr};
int t;
for (t = 0; t < 3; t++) {
_compound[t] = dynamic_cast<Part::Compound*>(_doc->addObject("Part::Compound"));
_compound[t] = _doc->addObject<Part::Compound>();
}
_compound[0]->Links.setValues({_boxes[0], _boxes[1]});
_compound[1]->Links.setValues({_compound[0]});

View File

@@ -25,7 +25,7 @@ protected:
void SetUp() override
{
createTestDoc();
_revolution = dynamic_cast<Part::Revolution*>(_doc->addObject("Part::Revolution"));
_revolution = _doc->addObject<Part::Revolution>();
PartTestHelpers::rectangle(len, wid, "Rect1");
_revolution->Source.setValue(_doc->getObjects().back());
_revolution->Axis.setValue(0, 1, 0);
@@ -95,7 +95,7 @@ TEST_F(FeatureRevolutionTest, testAxisLink)
{
// Arrange
BRepBuilderAPI_MakeEdge e1(gp_Pnt(0, 0, 0), gp_Pnt(0, 0, ext1));
auto edge = dynamic_cast<Part::Feature*>(_doc->addObject("Part::Feature", "Edge"));
auto edge = _doc->addObject<Part::Feature>("Edge");
edge->Shape.setValue(e1);
_revolution->AxisLink.setValue(edge);
// double puckVolume = wid * wid * M_PI * len; // Area is PIr2; apply height

View File

@@ -22,10 +22,10 @@ protected:
{
createTestDoc();
std::string testPath = App::Application::getHomePath() + "/tests/brepfiles/";
_fuse = dynamic_cast<Part::Fuse*>(_doc->addObject("Part::Fuse"));
_cylinder1 = dynamic_cast<Part::ImportBrep*>(_doc->addObject("Part::ImportBrep"));
_fuse = _doc->addObject<Part::Fuse>();
_cylinder1 = _doc->addObject<Part::ImportBrep>();
_cylinder1->FileName.setValue(testPath + "cylinder1.brep");
_helix1 = dynamic_cast<Part::ImportBrep*>(_doc->addObject("Part::ImportBrep"));
_helix1 = _doc->addObject<Part::ImportBrep>();
_helix1->FileName.setValue(testPath + "helix1.brep");
// Load

View File

@@ -24,7 +24,7 @@ protected:
void SetUp() override
{
createTestDoc();
_common = dynamic_cast<Common*>(_doc->addObject("Part::Common"));
_common = _doc->addObject<Common>();
}
void TearDown() override

View File

@@ -30,8 +30,8 @@ protected:
TEST_F(PartFeaturesTest, testRuledSurface)
{
// Arrange
auto _edge1 = dynamic_cast<Line*>(_doc->addObject("Part::Line"));
auto _edge2 = dynamic_cast<Line*>(_doc->addObject("Part::Line"));
auto _edge1 = _doc->addObject<Line>();
auto _edge2 = _doc->addObject<Line>();
_edge1->X1.setValue(0);
_edge1->Y1.setValue(0);
_edge1->Z1.setValue(0);
@@ -44,7 +44,7 @@ TEST_F(PartFeaturesTest, testRuledSurface)
_edge2->X2.setValue(2);
_edge2->Y2.setValue(2);
_edge2->Z2.setValue(0);
auto _ruled = dynamic_cast<RuledSurface*>(_doc->addObject("Part::RuledSurface"));
auto _ruled = _doc->addObject<RuledSurface>();
_ruled->Curve1.setValue(_edge1);
_ruled->Curve2.setValue(_edge2);
// Act
@@ -65,14 +65,14 @@ TEST_F(PartFeaturesTest, testRuledSurface)
TEST_F(PartFeaturesTest, testLoft)
{
// Arrange
auto _plane1 = dynamic_cast<Plane*>(_doc->addObject("Part::Plane"));
auto _plane1 = _doc->addObject<Plane>();
_plane1->Length.setValue(4);
_plane1->Width.setValue(4);
auto _plane2 = dynamic_cast<Plane*>(_doc->addObject("Part::Plane"));
auto _plane2 = _doc->addObject<Plane>();
_plane2->Length.setValue(4);
_plane2->Width.setValue(4);
_plane2->Placement.setValue(Base::Placement(Base::Vector3d(0, 0, 2), Base::Rotation()));
auto _loft = dynamic_cast<Loft*>(_doc->addObject("Part::Loft"));
auto _loft = _doc->addObject<Loft>();
_loft->Sections.setValues({_plane1, _plane2});
_loft->Solid.setValue((true));
// Act
@@ -93,17 +93,17 @@ TEST_F(PartFeaturesTest, testLoft)
TEST_F(PartFeaturesTest, testSweep)
{
// Arrange
auto _edge1 = dynamic_cast<Line*>(_doc->addObject("Part::Line"));
auto _edge1 = _doc->addObject<Line>();
_edge1->X1.setValue(0);
_edge1->Y1.setValue(0);
_edge1->Z1.setValue(0);
_edge1->X2.setValue(0);
_edge1->Y2.setValue(0);
_edge1->Z2.setValue(3);
auto _plane1 = dynamic_cast<Plane*>(_doc->addObject("Part::Plane"));
auto _plane1 = _doc->addObject<Plane>();
_plane1->Length.setValue(4);
_plane1->Width.setValue(4);
auto _sweep = dynamic_cast<Sweep*>(_doc->addObject("Part::Sweep"));
auto _sweep = _doc->addObject<Sweep>();
_sweep->Sections.setValues({_plane1});
_sweep->Spine.setValue(_edge1);
// Act
@@ -124,7 +124,7 @@ TEST_F(PartFeaturesTest, testSweep)
TEST_F(PartFeaturesTest, testThickness)
{
// Arrange
auto _thickness = dynamic_cast<Thickness*>(_doc->addObject("Part::Thickness"));
auto _thickness = _doc->addObject<Thickness>();
_thickness->Faces.setValue(_boxes[0], {"Face1"});
_thickness->Value.setValue(0.25);
_thickness->Join.setValue("Intersection");
@@ -146,12 +146,12 @@ TEST_F(PartFeaturesTest, testThickness)
TEST_F(PartFeaturesTest, testRefine)
{
// Arrange
auto _fuse = dynamic_cast<Part::Fuse*>(_doc->addObject("Part::Fuse"));
auto _fuse = _doc->addObject<Part::Fuse>();
_fuse->Base.setValue(_boxes[0]);
_fuse->Tool.setValue(_boxes[3]);
_fuse->execute();
Part::TopoShape fusedts = _fuse->Shape.getShape();
auto _refine = dynamic_cast<Refine*>(_doc->addObject("Part::Refine"));
auto _refine = _doc->addObject<Refine>();
_refine->Source.setValue(_fuse);
// Act
_refine->execute();
@@ -175,7 +175,7 @@ TEST_F(PartFeaturesTest, testRefine)
TEST_F(PartFeaturesTest, testReverse)
{
// Arrange
auto _reverse = dynamic_cast<Reverse*>(_doc->addObject("Part::Reverse"));
auto _reverse = _doc->addObject<Reverse>();
_reverse->Source.setValue(_boxes[0]);
// Act
_reverse->execute();

View File

@@ -44,7 +44,7 @@ void PartTestHelperClass::createTestDoc()
Base::Vector3d(0, 2 - minimalDistance, 0)};
for (unsigned i = 0; i < _boxes.size(); i++) {
auto box = _boxes[i] = dynamic_cast<Part::Box*>(_doc->addObject("Part::Box")); // NOLINT
auto box = _boxes[i] = _doc->addObject<Part::Box>(); // NOLINT
box->Length.setValue(1);
box->Width.setValue(2);
box->Height.setValue(3);

View File

@@ -23,7 +23,7 @@ protected:
void SetUp() override
{
createTestDoc();
_common = dynamic_cast<Common*>(_doc->addObject("Part::Common"));
_common = _doc->addObject<Common>();
_common->Base.setValue(_boxes[0]);
_common->Tool.setValue(_boxes[1]);
_common->execute(); // We should now have an elementMap with 26 entries.

View File

@@ -29,7 +29,7 @@ TEST_F(FeaturePartMakeElementRefineTest, makeElementRefineBoxes)
{
// Arrange
auto _doc = App::GetApplication().getActiveDocument();
auto _fuse = dynamic_cast<Part::Fuse*>(_doc->addObject("Part::Fuse"));
auto _fuse = _doc->addObject<Part::Fuse>();
_fuse->Base.setValue(_boxes[0]);
_fuse->Tool.setValue(_boxes[3]);
// Act

View File

@@ -22,7 +22,7 @@ protected:
{
_docName = App::GetApplication().getUniqueDocumentName("test");
_doc = App::GetApplication().newDocument(_docName.c_str(), "testUser");
_body = dynamic_cast<PartDesign::Body*>(_doc->addObject("PartDesign::Body"));
_body = _doc->addObject<PartDesign::Body>();
}
void TearDown() override
@@ -48,7 +48,7 @@ private:
TEST_F(DatumPlaneTest, attachDatumPlane)
{
auto datumPlane = getDocument()->addObject("PartDesign::Plane", "Plane");
auto datumPlane = getDocument()->addObject<PartDesign::Plane>("Plane");
ASSERT_TRUE(datumPlane);
getBody()->addObject(datumPlane);
auto origin = getBody()->getOrigin();

View File

@@ -23,9 +23,8 @@ protected:
void SetUp() override
{
_doc = App::GetApplication().newDocument("Pad_test", "testUser");
_body = dynamic_cast<PartDesign::Body*>(_doc->addObject("PartDesign::Body"));
_sketch = dynamic_cast<Sketcher::SketchObject*>(
_doc->addObject("Sketcher::SketchObject", "Sketch"));
_body = _doc->addObject<PartDesign::Body>();
_sketch = _doc->addObject<Sketcher::SketchObject>("Sketch");
_body->addObject(_sketch);
_sketch->AttachmentSupport.setValue(_doc->getObject("XY_Plane"), "");
@@ -69,7 +68,7 @@ TEST_F(PadTest, TestMidPlaneTwoLength)
doc->recompute();
auto pad = dynamic_cast<PartDesign::Pad*>(doc->addObject("PartDesign::Pad", "Pad"));
auto pad = doc->addObject<PartDesign::Pad>("Pad");
body->addObject(pad);
pad->Profile.setValue(sketch, {""});
pad->Direction.setValue(0.0, 0.0, 1.0);

View File

@@ -23,8 +23,8 @@ protected:
{
_docName = App::GetApplication().getUniqueDocumentName("test");
_doc = App::GetApplication().newDocument(_docName.c_str(), "testUser");
_body = dynamic_cast<PartDesign::Body*>(_doc->addObject("PartDesign::Body")); // NOLINT
_box = dynamic_cast<Part::Box*>(_doc->addObject("Part::Box")); // NOLINT
_body = _doc->addObject<PartDesign::Body>();
_box = _doc->addObject<Part::Box>();
_box->Length.setValue(1);
_box->Width.setValue(2);
_box->Height.setValue(3);
@@ -32,10 +32,8 @@ protected:
Base::Placement(Base::Vector3d(), Base::Rotation(), Base::Vector3d())); // NOLINT
// _body->addObject(_box); // Invalid, Part::Features can't go in a PartDesign::Body,
// but we can bind them.
_binder = dynamic_cast<PartDesign::ShapeBinder*>(
_doc->addObject("PartDesign::ShapeBinder", "ShapeBinderFoo")); // NOLINT
_subbinder = dynamic_cast<PartDesign::SubShapeBinder*>(
_doc->addObject("PartDesign::SubShapeBinder", "SubShapeBinderBar")); // NOLINT
_binder = _doc->addObject<PartDesign::ShapeBinder>("ShapeBinderFoo");
_subbinder = _doc->addObject<PartDesign::SubShapeBinder>("SubShapeBinderBar");
_binder->Shape.setValue(_box->Shape.getShape());
_subbinder->setLinks({{_box, {"Face1", "Face2"}}}, false);
_body->addObject(_binder);

View File

@@ -12,6 +12,7 @@
using namespace SketcherTestHelpers;
TEST_F(SketchObjectTest, createSketchObject) // NOLINT
{
// Arrange