Skip to content

Commit 4643978

Browse files
authored
Merge pull request #215 from CRIStAL-PADR/pr_add_getCategories_Methode
[SofaRuntime] Add getCategories method
2 parents 1641256 + 8312bad commit 4643978

File tree

3 files changed

+42
-64
lines changed

3 files changed

+42
-64
lines changed

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_BaseObject.cpp

Lines changed: 6 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@
5151
#include <sofa/core/collision/Pipeline.h>
5252
#include <sofa/core/collision/Intersection.h>
5353
#include <sofa/core/objectmodel/ConfigurationSetting.h>
54-
5554
#include <sofa/core/ExecParams.h>
55+
#include <sofa/core/CategoryLibrary.h>
56+
#include <pybind11/stl.h>
5657

5758
/// Makes an alias for the pybind11 namespace to increase readability.
5859
namespace py { using namespace pybind11; }
@@ -133,69 +134,11 @@ py::object getTarget(BaseObject *self)
133134

134135
py::object getCategories(BaseObject *self)
135136
{
136-
const sofa::core::objectmodel::BaseClass* mclass=self->getClass();
137137
std::vector<std::string> categories;
138-
if (mclass->hasParent(sofa::core::objectmodel::ContextObject::GetClass()))
139-
categories.push_back("ContextObject");
140-
if (mclass->hasParent(sofa::core::visual::VisualModel::GetClass()))
141-
categories.push_back("VisualModel");
142-
if (mclass->hasParent(sofa::core::BehaviorModel::GetClass()))
143-
categories.push_back("BehaviorModel");
144-
if (mclass->hasParent(sofa::core::CollisionModel::GetClass()))
145-
categories.push_back("CollisionModel");
146-
if (mclass->hasParent(sofa::core::behavior::BaseMechanicalState::GetClass()))
147-
categories.push_back("MechanicalState");
148-
// A Mass is a technically a ForceField, but we don't want it to appear in the ForceField category
149-
if (mclass->hasParent(sofa::core::behavior::BaseForceField::GetClass()) && !mclass->hasParent(sofa::core::behavior::BaseMass::GetClass()))
150-
categories.push_back("ForceField");
151-
if (mclass->hasParent(sofa::core::behavior::BaseInteractionForceField::GetClass()))
152-
categories.push_back("InteractionForceField");
153-
if (mclass->hasParent(sofa::core::behavior::BaseProjectiveConstraintSet::GetClass()))
154-
categories.push_back("ProjectiveConstraintSet");
155-
if (mclass->hasParent(sofa::core::behavior::BaseConstraintSet::GetClass()))
156-
categories.push_back("ConstraintSet");
157-
if (mclass->hasParent(sofa::core::BaseMapping::GetClass()))
158-
categories.push_back("Mapping");
159-
if (mclass->hasParent(sofa::core::DataEngine::GetClass()))
160-
categories.push_back("Engine");
161-
if (mclass->hasParent(sofa::core::topology::TopologicalMapping::GetClass()))
162-
categories.push_back("TopologicalMapping");
163-
if (mclass->hasParent(sofa::core::behavior::BaseMass::GetClass()))
164-
categories.push_back("Mass");
165-
if (mclass->hasParent(sofa::core::behavior::OdeSolver::GetClass()))
166-
categories.push_back("OdeSolver");
167-
if (mclass->hasParent(sofa::core::behavior::ConstraintSolver::GetClass()))
168-
categories.push_back("ConstraintSolver");
169-
if (mclass->hasParent(sofa::core::behavior::BaseConstraintCorrection::GetClass()))
170-
categories.push_back("ConstraintSolver");
171-
if (mclass->hasParent(sofa::core::behavior::LinearSolver::GetClass()))
172-
categories.push_back("LinearSolver");
173-
if (mclass->hasParent(sofa::core::behavior::BaseAnimationLoop::GetClass()))
174-
categories.push_back("AnimationLoop");
175-
// Just like Mass and ForceField, we don't want TopologyObject to appear in the Topology category
176-
if (mclass->hasParent(sofa::core::topology::Topology::GetClass()) && !mclass->hasParent(sofa::core::topology::BaseTopologyObject::GetClass()))
177-
categories.push_back("Topology");
178-
if (mclass->hasParent(sofa::core::topology::BaseTopologyObject::GetClass()))
179-
categories.push_back("TopologyObject");
180-
if (mclass->hasParent(sofa::core::behavior::BaseController::GetClass()))
181-
categories.push_back("Controller");
182-
if (mclass->hasParent(sofa::core::loader::BaseLoader::GetClass()))
183-
categories.push_back("Loader");
184-
if (mclass->hasParent(sofa::core::collision::CollisionAlgorithm::GetClass()))
185-
categories.push_back("CollisionAlgorithm");
186-
if (mclass->hasParent(sofa::core::collision::Pipeline::GetClass()))
187-
categories.push_back("CollisionAlgorithm");
188-
if (mclass->hasParent(sofa::core::collision::Intersection::GetClass()))
189-
categories.push_back("CollisionAlgorithm");
190-
if (mclass->hasParent(sofa::core::objectmodel::ConfigurationSetting::GetClass()))
191-
categories.push_back("ConfigurationSetting");
192-
if (categories.empty())
193-
categories.push_back("Miscellaneous");
194-
195-
py::list list;
196-
for (unsigned int i=0; i<categories.size(); ++i)
197-
list.append(py::cast(categories[i].c_str())) ;
198-
return list;
138+
const sofa::core::objectmodel::BaseClass* c=self->getClass();
139+
sofa::core::CategoryLibrary::getCategories(c, categories);
140+
py::list l = py::cast(categories);
141+
return std::move(l);
199142
}
200143

201144
std::string getAsACreateObjectParameter(BaseObject *self)

bindings/SofaRuntime/src/SofaPython3/SofaRuntime/Module_SofaRuntime.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ using sofa::helper::system::PluginRepository;
3838
using sofa::simulation::SceneLoaderFactory;
3939
using sofa::simulation::SceneLoader;
4040

41+
42+
#include <pybind11/stl.h>
43+
#include <sofa/core/ObjectFactory.h>
44+
using sofa::core::ObjectFactory;
45+
#include <sofa/core/CategoryLibrary.h>
46+
using sofa::core::CategoryLibrary;
47+
48+
49+
4150
#include <SofaPython3/SceneLoaderPY3.h>
4251
using sofapython3::SceneLoaderPY3;
4352

@@ -64,6 +73,7 @@ using sofa::helper::logging::MainConsoleMessageHandler;
6473
namespace sofapython3
6574
{
6675

76+
6777
class SofaInitializer
6878
{
6979
public:
@@ -81,7 +91,25 @@ class SofaInitializer
8191
}
8292
};
8393

94+
static std::vector<std::string> getCategories(const std::string& className)
95+
{
96+
std::vector<std::string> categories;
97+
ObjectFactory* factory = ObjectFactory::getInstance();
8498

99+
if (factory->hasCreator(className))
100+
{
101+
ObjectFactory::ClassEntry& entry = factory->getEntry(className);
102+
ObjectFactory::CreatorMap::iterator it = entry.creatorMap.begin();
103+
104+
if( it != entry.creatorMap.end())
105+
{
106+
ObjectFactory::Creator::SPtr c = it->second;
107+
const sofa::core::objectmodel::BaseClass* objClass = c->getClass();
108+
CategoryLibrary::getCategories(objClass,categories);
109+
}
110+
}
111+
return categories ;
112+
}
85113

86114
static SofaInitializer s;
87115

@@ -141,6 +169,7 @@ PYBIND11_MODULE(SofaRuntime, m) {
141169

142170
m.add_object("DataRepository", py::cast(&sofa::helper::system::DataRepository));
143171
m.add_object("PluginRepository", py::cast(&sofa::helper::system::PluginRepository));
172+
m.def("getCategories", &getCategories);
144173

145174
addSubmoduleTimer(m);
146175
}

bindings/SofaRuntime/tests/SofaRuntime/Base.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,11 @@
66
class Test(unittest.TestCase):
77
def test_moduleImport(self):
88
import SofaRuntime
9+
SofaRuntime.importPlugin('SofaComponentAll')
910

10-
SofaRuntime.importPlugin('SofaSparseSolver')
11+
def test_getCategoriesmethod(self):
12+
import SofaRuntime
13+
SofaRuntime.importPlugin("SofaComponentAll")
14+
t = SofaRuntime.getCategories("MeshObjLoader")
15+
self.assertEquals(len(t),1)
16+
self.assertIsInstance(t, list)

0 commit comments

Comments
 (0)