Commit 6e2b2a2d authored by John Tourtellott's avatar John Tourtellott Committed by Kitware Robot

Merge topic 'project-find-resource'

fb9ea983 Add Project methods to find resources by type and name (identifier)
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Acked-by: T.J. Corona's avatarT.J. Corona <tj.corona@kitware.com>
Merge-request: !1524
parents f5b62da0 fb9ea983
Pipeline #134715 running with stage
......@@ -23,6 +23,7 @@
#include "smtk/common/TypeName.h"
#include "smtk/io/AttributeReader.h"
#include "smtk/io/AttributeWriter.h"
#include "smtk/mesh/core/Resource.h"
#include "smtk/model/EntityTypeBits.h"
#include "smtk/model/Resource.h"
#include "smtk/operation/Manager.h"
......@@ -30,7 +31,6 @@
#include "smtk/operation/operators/ReadResource.h"
#include "smtk/operation/operators/WriteResource.h"
#include "smtk/project/json/jsonProjectDescriptor.h"
#include "smtk/resource/Manager.h"
#ifdef SMTK_PYTHON_ENABLED
#include "smtk/operation/operators/ImportPythonOperation.h"
......
......@@ -19,6 +19,7 @@
#include "smtk/io/Logger.h"
#include "smtk/project/ProjectDescriptor.h"
#include "smtk/project/ResourceDescriptor.h"
#include "smtk/resource/Manager.h"
#include <string>
#include <vector>
......@@ -58,6 +59,10 @@ public:
/// string might be empty (unknown).
std::string importLocation(smtk::resource::ResourcePtr res) const;
/// Return resource of specified type and identifier
template <typename ResourceType>
smtk::shared_ptr<ResourceType> findResource(const std::string& identifier) const;
// Future:
// * methods to add/remove project resources
protected:
......@@ -134,6 +139,32 @@ private:
Project();
}; // class smtk::project::Project
template <typename ResourceType>
smtk::shared_ptr<ResourceType> Project::findResource(const std::string& identifier) const
{
auto resManager = m_resourceManager.lock();
if (!resManager)
{
return nullptr;
}
// Traverse resource descriptors
for (const auto& rd : m_resourceDescriptors)
{
if (rd.m_identifier == identifier)
{
smtk::resource::ResourcePtr resource = resManager->get(rd.m_uuid);
if ((resource != nullptr) && (resource->isOfType<ResourceType>()))
{
return smtk::static_pointer_cast<ResourceType>(resource);
}
} // if (identifier match)
} // for (resource descriptors)
// (Resource was not found)
return nullptr;
}
} // namespace project
} // namespace smtk
......
......@@ -16,6 +16,11 @@
#include "smtk/project/Project.h"
#include "smtk/attribute/Resource.h"
#include "smtk/mesh/core/Resource.h"
#include "smtk/model/Resource.h"
#include "smtk/resource/Resource.h"
namespace py = pybind11;
PySharedPtrClass<smtk::project::Project> pybind11_init_smtk_project_Project(py::module& m)
......@@ -27,6 +32,16 @@ PySharedPtrClass<smtk::project::Project> pybind11_init_smtk_project_Project(py::
.def("directory", &smtk::project::Project::directory)
.def("resources", &smtk::project::Project::resources)
.def("importLocation", &smtk::project::Project::importLocation)
.def("findAttributeResource", [](smtk::project::Project& prj, const std::string& identifier) {
return prj.findResource<smtk::attribute::Resource>(identifier);
})
.def("findMeshResource", [](smtk::project::Project& prj, const std::string& identifier) {
return prj.findResource<smtk::mesh::Resource>(identifier);
})
.def("findModelResource", [](smtk::project::Project& prj, const std::string& identifier) {
return prj.findResource<smtk::model::Resource>(identifier);
})
;
return instance;
}
......
......@@ -11,5 +11,6 @@
#=============================================================================
import smtk.common
import smtk.io
import smtk.resource
from ._smtkPybindProject import *
......@@ -19,6 +19,7 @@ import unittest
import smtk
import smtk.attribute
import smtk.io
import smtk.mesh
import smtk.operation
import smtk.project
import smtk.session.mesh
......@@ -189,6 +190,18 @@ class TestProjectManager(unittest.TestCase):
else:
self.assertTrue(isinstance(res, smtk.session.mesh.Resource))
# Make sure we can find resources by identifer
att_res = project.findAttributeResource('default')
self.assertIsNotNone(att_res)
model_res = project.findModelResource('default')
self.assertIsNotNone(model_res)
# And that we don't find things that aren't there
nomesh_res = project.findMeshResource('default')
self.assertIsNone(nomesh_res)
noatt_res = project.findAttributeResource('second')
self.assertIsNone(noatt_res)
def close_project(self):
before_count = len(self.rm.resources())
logger = smtk.io.Logger.instance()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment