Add template based Document::countObjectsOfType

Also convert code to use this new method
This commit is contained in:
Benjamin Nauck
2025-01-11 20:00:24 +01:00
parent 74f3aee9a7
commit 08fe94ef25
14 changed files with 40 additions and 40 deletions

View File

@@ -4407,6 +4407,12 @@ int Document::countObjectsOfType(const Base::Type& typeId) const
return ct;
}
int Document::countObjectsOfType(const char* typeName) const
{
Base::Type type = Base::Type::fromName(typeName);
return type.isBad() ? 0 : countObjectsOfType(type);
}
PyObject* Document::getPyObject()
{
return Py::new_reference_to(d->DocumentPythonObject);

View File

@@ -339,6 +339,9 @@ public:
template<typename T>
inline std::vector<T*> getObjectsOfType() const;
int countObjectsOfType(const Base::Type& typeId) const;
template<typename T>
inline int countObjectsOfType() const;
int countObjectsOfType(const char* typeName) const;
/// get the number of objects in the document
int countObjects() const;
//@}
@@ -651,6 +654,14 @@ inline std::vector<T*> Document::getObjectsOfType() const
return type;
}
template<typename T>
inline int Document::countObjectsOfType() const
{
static_assert(std::is_base_of<App::DocumentObject, T>::value,
"T must be derived from App::DocumentObject");
return this->countObjectsOfType(T::getClassTypeId());
}
} // namespace App

View File

@@ -99,7 +99,7 @@ void CmdInspectElement::activated(int)
bool CmdInspectElement::isActive()
{
App::Document* doc = App::GetApplication().getActiveDocument();
if (!doc || doc->countObjectsOfType(Inspection::Feature::getClassTypeId()) == 0) {
if (!doc || doc->countObjectsOfType<Inspection::Feature>() == 0) {
return false;
}

View File

@@ -64,7 +64,7 @@ void StdCmdMeasure::activated(int iMsg)
bool StdCmdMeasure::isActive()
{
App::Document* doc = App::GetApplication().getActiveDocument();
if (!doc || doc->countObjectsOfType(App::GeoFeature::getClassTypeId()) == 0) {
if (!doc || doc->countObjectsOfType<App::GeoFeature>() == 0) {
return false;
}

View File

@@ -636,7 +636,7 @@ void CmdMeshVertexCurvatureInfo::activated(int)
bool CmdMeshVertexCurvatureInfo::isActive()
{
App::Document* doc = App::GetApplication().getActiveDocument();
if (!doc || doc->countObjectsOfType(Mesh::Curvature::getClassTypeId()) == 0) {
if (!doc || doc->countObjectsOfType<Mesh::Curvature>() == 0) {
return false;
}
@@ -1070,7 +1070,7 @@ void CmdMeshEvaluation::activated(int)
bool CmdMeshEvaluation::isActive()
{
App::Document* doc = App::GetApplication().getActiveDocument();
if (!doc || doc->countObjectsOfType(Mesh::Feature::getClassTypeId()) == 0) {
if (!doc || doc->countObjectsOfType<Mesh::Feature>() == 0) {
return false;
}
return true;
@@ -1109,7 +1109,7 @@ void CmdMeshEvaluateFacet::activated(int)
bool CmdMeshEvaluateFacet::isActive()
{
App::Document* doc = App::GetApplication().getActiveDocument();
if (!doc || doc->countObjectsOfType(Mesh::Feature::getClassTypeId()) == 0) {
if (!doc || doc->countObjectsOfType<Mesh::Feature>() == 0) {
return false;
}
@@ -1152,7 +1152,7 @@ bool CmdMeshRemoveComponents::isActive()
{
// Check for the selected mesh feature (all Mesh types)
App::Document* doc = getDocument();
if (!(doc && doc->countObjectsOfType(Mesh::Feature::getClassTypeId()) > 0)) {
if (!(doc && doc->countObjectsOfType<Mesh::Feature>() > 0)) {
return false;
}
Gui::Document* viewDoc = Gui::Application::Instance->getDocument(doc);
@@ -1237,7 +1237,7 @@ void CmdMeshRemoveCompByHand::activated(int)
bool CmdMeshRemoveCompByHand::isActive()
{
App::Document* doc = App::GetApplication().getActiveDocument();
if (!doc || doc->countObjectsOfType(Mesh::Feature::getClassTypeId()) == 0) {
if (!doc || doc->countObjectsOfType<Mesh::Feature>() == 0) {
return false;
}
@@ -1596,7 +1596,7 @@ void CmdMeshFillInteractiveHole::activated(int)
bool CmdMeshFillInteractiveHole::isActive()
{
App::Document* doc = App::GetApplication().getActiveDocument();
if (!doc || doc->countObjectsOfType(Mesh::Feature::getClassTypeId()) == 0) {
if (!doc || doc->countObjectsOfType<Mesh::Feature>() == 0) {
return false;
}

View File

@@ -335,11 +335,7 @@ bool CmdMeshPartCurveOnMesh::isActive()
// Check for the selected mesh feature (all Mesh types)
App::Document* doc = App::GetApplication().getActiveDocument();
if (doc && doc->countObjectsOfType(Mesh::Feature::getClassTypeId()) > 0) {
return true;
}
return false;
return doc && doc->countObjectsOfType<Mesh::Feature>() > 0;
}

View File

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

View File

@@ -1153,8 +1153,7 @@ void CmdPartMakeSolid::activated(int iMsg)
bool CmdPartMakeSolid::isActive()
{
return Gui::Selection().countObjectsOfType
(App::DocumentObject::getClassTypeId(), nullptr, Gui::ResolveMode::FollowLink) > 0;
return Gui::Selection().countObjectsOfType<App::DocumentObject>(nullptr, Gui::ResolveMode::FollowLink) > 0;
}
//===========================================================================
@@ -1887,8 +1886,7 @@ void CmdPartThickness::activated(int iMsg)
bool CmdPartThickness::isActive()
{
Base::Type partid = Base::Type::fromName("Part::Feature");
bool objectsSelected = Gui::Selection().countObjectsOfType(partid, nullptr, Gui::ResolveMode::FollowLink) > 0;
bool objectsSelected = Gui::Selection().countObjectsOfType<Part::Feature>(nullptr, Gui::ResolveMode::FollowLink) > 0;
return (objectsSelected && !Gui::Control().activeDialog());
}
@@ -1918,7 +1916,7 @@ void CmdShapeInfo::activated(int iMsg)
bool CmdShapeInfo::isActive()
{
App::Document* doc = App::GetApplication().getActiveDocument();
if (!doc || doc->countObjectsOfType(Part::Feature::getClassTypeId()) == 0)
if (!doc || doc->countObjectsOfType<Part::Feature>() == 0)
return false;
Gui::MDIView* view = Gui::getMainWindow()->activeWindow();
@@ -2102,8 +2100,7 @@ void CmdColorPerFace::activated(int iMsg)
bool CmdColorPerFace::isActive()
{
Base::Type partid = Base::Type::fromName("Part::Feature");
bool objectSelected = Gui::Selection().countObjectsOfType(partid) == 1;
bool objectSelected = Gui::Selection().countObjectsOfType<Part::Feature>() == 1;
return (hasActiveDocument() && !Gui::Control().activeDialog() && objectSelected);
}

View File

@@ -64,6 +64,7 @@
#include <Mod/Part/App/PartFeature.h>
#include <Mod/Part/App/PrimitiveFeature.h>
#include <Mod/Part/App/DatumFeature.h>
#include <Mod/Part/App/FeatureMirroring.h>
#include <App/Datums.h>
#include "Mirroring.h"
@@ -289,7 +290,7 @@ bool Mirroring::accept()
}
Gui::WaitCursor wc;
unsigned int count = activeDoc->countObjectsOfType(Base::Type::fromName("Part::Mirroring"));
unsigned int count = activeDoc->countObjectsOfType<Part::Mirroring>();
activeDoc->openTransaction("Mirroring");
QString shape, label, selectionString;

View File

@@ -707,7 +707,7 @@ std::tuple<bool, PartDesign::Body*> SketchWorkflow::shouldCreateBody()
pdBody->Placement.setValue(xLink->Placement.getValue());
}
if (!pdBody) {
if (appdocument->countObjectsOfType(PartDesign::Body::getClassTypeId()) == 0) {
if (appdocument->countObjectsOfType<PartDesign::Body>() == 0) {
shouldMakeBody = true;
}
else {

View File

@@ -121,7 +121,7 @@ PartDesign::Body *getBody(bool messageIfNot, bool autoActivate, bool assertModer
if (activeView) {
auto doc = activeView->getAppDocument();
bool singleBodyDocument = doc->countObjectsOfType(PartDesign::Body::getClassTypeId()) == 1;
bool singleBodyDocument = doc->countObjectsOfType<PartDesign::Body>() == 1;
if (assertModern) {
activeBody = activeView->getActiveObject<PartDesign::Body*>(PDBODYKEY,topParent,subname);

View File

@@ -101,13 +101,10 @@ void Workbench::setupContextMenu(const char* recipient, Gui::MenuItem* item) con
*item << "Std_ToggleFreeze";
}
bool docHaveBodies = activeView->getAppDocument()->countObjectsOfType (
PartDesign::Body::getClassTypeId () ) > 0;
if ( docHaveBodies ) {
if (activeView->getAppDocument()->countObjectsOfType<PartDesign::Body>() > 0) {
bool addMoveFeature = true;
bool addMoveFeatureInTree = (body != nullptr);
for (auto sel: selection) {
for (auto sel : selection) {
// if at least one selected feature cannot be moved to a body
// disable the entry
if ( addMoveFeature && !PartDesign::Body::isAllowed ( sel.pObject ) ) {

View File

@@ -763,12 +763,8 @@ void CmdSketcherMapSketch::activated(int iMsg)
bool CmdSketcherMapSketch::isActive()
{
App::Document* doc = App::GetApplication().getActiveDocument();
Base::Type sketch_type = Base::Type::fromName("Part::Part2DObject");
std::vector<Gui::SelectionObject> selobjs = Gui::Selection().getSelectionEx();
if (doc && doc->countObjectsOfType(sketch_type) > 0 && !selobjs.empty())
return true;
return false;
return doc && doc->countObjectsOfType<Part::Part2DObject>() > 0 && !selobjs.empty();
}
DEF_STD_CMD_A(CmdSketcherViewSketch)

View File

@@ -201,13 +201,9 @@ bool CmdSurfaceCurveOnMesh::isActive()
}
// Check for the selected mesh feature (all Mesh types)
Base::Type meshType = Base::Type::fromName("Mesh::Feature");
App::Document* doc = App::GetApplication().getActiveDocument();
if (doc && doc->countObjectsOfType(meshType) > 0) {
return true;
}
return false;
// Use string based check to avoid linking to Mesh module
return doc && doc->countObjectsOfType("Mesh::Feature") > 0;
}
//===========================================================================