Commit 8429bf04 authored by John Tourtellott's avatar John Tourtellott Committed by Kitware Robot

Merge topic 'project-second-model'

f6fa614e Add second model as an option when specifying new projects
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: !1507
parents c92c6e4c f6fa614e
Pipeline #134329 running with stage
## Add second model as an option when specifying new projects
The project manager was updated to accept a second geometry
file when initializing new projects. In this implementation,
only the primary geometry is linked to the simulation
attributes resource when the project is initialized.
(Resource links can be added or removed by application code,
of course.)
......@@ -2,7 +2,7 @@
<!-- Description of the project "NewProject" Operation -->
<SMTK_AttributeResource Version="3">
<Definitions>
<AttDef Type="new-project" Label="New Project" BaseType="">
<AttDef Type="new-project" Label="New Project" BaseType="" Version="1">
<BriefDescription>
Create and initialize a CMB project.
</BriefDescription>
......@@ -20,28 +20,35 @@
</Directory>
<String Name="project-folder" Label="Name (Subdirectory)" NumberOfRequiredValues="1">
<BriefDescription>Project files will be stored in a workspace folder with this name.</BriefDescription>
<DefaultValue>NewProject</DefaultValue>
</String>
<File Name="simulation-template" Label="Simulation Template File" NumberOfRequiredValues="1"
ShouldExist="true" Optional="true" IsEnabledByDefault="true"
FileFilters="CMB Template Files (*.sbt);;All Files (*)">
<BriefDescription>The CMB template file (*.sbt) specifying the simulation</BriefDescription>
</File>
<File Name="model-file" Label="Input Geometry" NumberOfRequiredValues="1" ShouldExist="true"
Optional="true" IsEnabledByDefault="true"
FileFilters="Exodus Files (*.ex? *.gen);;All Files (*)">
<BriefDescription>The model or mesh file to import into the project.</BriefDescription>
<DetailedDescription>The current implementation only supports Exodus file.</DetailedDescription>
</File>
<Void Name="copy-model-file" Label="Copy Input Geometry"
Optional="true" IsEnabledByDefault="true">
<BriefDescription>If enabled, store a copy of the input geometry file in the project directory.</BriefDescription>
</Void>
<String Name="model-file-identifier" Label="Model File Label" NumberOfRequiredValues="1" AdvanceLevel="1">
<BriefDescription>A text label that can be used to identify this model</BriefDescription>
<DefaultValue>default</DefaultValue>
</String>
<Group Name="model-group" Label="Input Geometry" Optional="true" IsEnabledByDefault="true">
<ItemDefinitions>
<File Name="model-file" Label="Geometry File" NumberOfRequiredValues="1" ShouldExist="true"
FileFilters="Exodus Files (*.ex? *.gen);;NetCDF Files (*.ncdf);;All Files (*)">
<BriefDescription>The model or mesh file to import into the project.</BriefDescription>
<DetailedDescription>The current implementation only supports Exodus and NetCDF files.</DetailedDescription>
</File>
<File Name="second-model-file" Label="Second Geometry File" NumberOfRequiredValues="1" ShouldExist="true"
Optional="true" IsEnabledByDefault="false"
FileFilters="Exodus Files (*.ex? *.gen);;NetCDF Files (*.ncdf);;All Files (*)">
<BriefDescription>A second model or mesh file to import into the project.</BriefDescription>
<DetailedDescription>The current implementation only supports Exodus and NetCDF files.</DetailedDescription>
</File>
<Void Name="copy-file" Label="Copy Geometry File(s) Into Project Folder" Optional="true" IsEnabledByDefault="true">
<BriefDescription>If enabled, store a copy of the file in the project directory.</BriefDescription>
</Void>
</ItemDefinitions>
</Group>
<!--
The "use-vtk-session" option is set TRUE by default, as a temporary workaround for
loading exodus files, which otherwise would use the mesh session. The vtk session is
......
......@@ -13,7 +13,9 @@
#include "smtk/attribute/ComponentItem.h"
#include "smtk/attribute/DirectoryItem.h"
#include "smtk/attribute/FileItem.h"
#include "smtk/attribute/GroupItem.h"
#include "smtk/attribute/IntItem.h"
#include "smtk/attribute/Item.h"
#include "smtk/attribute/Resource.h"
#include "smtk/attribute/ResourceItem.h"
#include "smtk/attribute/StringItem.h"
......@@ -179,21 +181,20 @@ bool Project::build(smtk::attribute::AttributePtr specification, smtk::io::Logge
} // if (projectPath exists)
boost::filesystem::create_directory(projectPath);
// Import the model resource
// Import model resources
if (!this->importModels(specification, logger))
{
return false;
}
// Initialize modelDescriptor to the primary model.
// At this point, only models have been loaded, and the default model is
// loaded first.
ResourceDescriptor modelDescriptor;
auto modelFileItem = specification->findFile("model-file");
if (modelFileItem->isEnabled())
{
// Check use-vtk-session option
std::string modelPath = modelFileItem->value(0);
bool copyNativeModel = specification->findVoid("copy-model-file")->isEnabled();
bool useVTKSession = specification->findVoid("use-vtk-session")->isEnabled();
if (!this->importModel(modelPath, copyNativeModel, modelDescriptor, useVTKSession, logger))
{
return false;
}
m_resourceDescriptors.push_back(modelDescriptor);
} // if (modelFileItem emabled)
if (!m_resourceDescriptors.empty())
{
modelDescriptor = m_resourceDescriptors[0];
}
// Import the attribute template
ResourceDescriptor attDescriptor;
......@@ -362,6 +363,52 @@ bool Project::open(const std::string& location, smtk::io::Logger& logger)
return this->loadResources(directoryPath.string(), logger);
} // open()
bool Project::importModels(
const smtk::attribute::AttributePtr specification, smtk::io::Logger& logger)
{
bool useVTKSession = specification->findVoid("use-vtk-session")->isEnabled();
auto modelGroupItem = specification->findGroup("model-group");
bool copyFile = modelGroupItem->find("copy-file")->isEnabled();
for (int i = 0; i < 2; ++i)
{
smtk::attribute::ItemPtr baseItem;
std::string identifier;
switch (i)
{
case 0:
baseItem = modelGroupItem->find("model-file");
identifier = "default";
break;
case 1:
baseItem = modelGroupItem->find("second-model-file");
identifier = "second";
break;
default:
assert(false);
} // switch
assert(!!baseItem);
if (baseItem->isEnabled())
{
auto fileItem = smtk::dynamic_pointer_cast<smtk::attribute::FileItem>(baseItem);
std::string modelPath = fileItem->value(0);
ResourceDescriptor modelDescriptor;
if (!this->importModel(modelPath, copyFile, modelDescriptor, useVTKSession, logger))
{
return false;
}
modelDescriptor.m_identifier = identifier;
m_resourceDescriptors.push_back(modelDescriptor);
}
} // for (i)
return true;
}
bool Project::importModel(const std::string& importPath, bool copyNativeModel,
ResourceDescriptor& descriptor, bool useVTKSession, smtk::io::Logger& logger)
{
......
......@@ -80,6 +80,8 @@ protected:
// Remaining calls are for internal use
bool importModels(const smtk::attribute::AttributePtr specification, smtk::io::Logger& logger);
bool importModel(const std::string& location, bool copyNativeFile, ResourceDescriptor& descriptor,
bool useVtkSession, smtk::io::Logger& logger = smtk::io::Logger::instance());
......
#=============================================================================
# =============================================================================
#
# Copyright (c) Kitware, Inc.
# All rights reserved.
......@@ -8,7 +8,7 @@
# the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the above copyright notice for more information.
#
#=============================================================================
# =============================================================================
from __future__ import print_function
import os
......@@ -25,6 +25,7 @@ import smtk.session.mesh
import smtk.testing
PROJECT1 = 'project1'
NUMBER_OF_RESOURCES = 3
class TestProjectManager(unittest.TestCase):
......@@ -84,9 +85,18 @@ class TestProjectManager(unittest.TestCase):
smtk.testing.DATA_DIR, 'simulation-workflows', 'ACE3P', 'ACE3P.sbt')
spec.findFile('simulation-template').setValue(0, sim_template)
model_group_item = spec.findGroup('model-group')
model_group_item.setIsEnabled(True)
model_file = os.path.join(
smtk.testing.DATA_DIR, 'model', '3d', 'genesis', 'gun-1fourth.gen')
spec.findFile('model-file').setValue(0, model_file)
model_group_item.find('model-file').setValue(0, model_file)
second_model_file = os.path.join(
smtk.testing.DATA_DIR, 'model', '3d', 'exodus', 'disk_out_ref.ex2')
model_group_item.find('second-model-file').setIsEnabled(True)
model_group_item.find(
'second-model-file').setValue(0, second_model_file)
# Make sure vtk-session option is off
spec.findVoid('use-vtk-session').setIsEnabled(False)
......@@ -103,16 +113,18 @@ class TestProjectManager(unittest.TestCase):
self.project = project
# Verify that 2 resources were created
self.assertEqual(len(self.project.resources()), 2)
self.assertEqual(len(self.project.resources()), NUMBER_OF_RESOURCES)
after_count = len(self.rm.resources())
self.assertEqual(after_count - before_count, 2)
self.assertEqual(after_count - before_count, NUMBER_OF_RESOURCES)
# Verify that folders & files were created
project_folder = os.path.join(smtk.testing.TEMP_DIR, project_name)
self.assertTrue(os.path.exists(project_folder))
filenames = [
'.smtkproject', 'sbi.default.smtk', 'gun-1fourth.gen', 'gun-1fourth.gen.h5m', 'gun-1fourth.gen.smtk']
'.smtkproject', 'sbi.default.smtk',
'gun-1fourth.gen', 'gun-1fourth.gen.h5m', 'gun-1fourth.gen.smtk',
'disk_out_ref.ex2', 'disk_out_ref.ex2.h5m', 'disk_out_ref.ex2.smtk']
for f in filenames:
path = os.path.join(project_folder, f)
self.assertTrue(os.path.exists(path), '{}'.format(path))
......@@ -148,7 +160,7 @@ class TestProjectManager(unittest.TestCase):
type_item = att.findString('NonlinearMaterial').setValue(0, 'AL6061')
after_count = len(att_resource.attributes())
self.assertEqual(after_count - before_count, 3)
self.assertEqual(after_count - before_count, NUMBER_OF_RESOURCES)
self.attribute_count = after_count
def save_project(self):
......@@ -168,7 +180,7 @@ class TestProjectManager(unittest.TestCase):
self.assertEqual(project.directory(), path)
resources = project.resources()
self.assertEqual(len(resources), 2)
self.assertEqual(len(resources), NUMBER_OF_RESOURCES)
for res in resources:
if isinstance(res, smtk.attribute.Resource):
......@@ -184,7 +196,7 @@ class TestProjectManager(unittest.TestCase):
self.assertTrue(success, msg=logger.convertToString())
after_count = len(self.rm.resources())
self.assertEqual(before_count - after_count, 2)
self.assertEqual(before_count - after_count, NUMBER_OF_RESOURCES)
project = self.pm.getCurrentProject()
self.assertIsNone(project)
......@@ -213,6 +225,7 @@ class TestProjectManager(unittest.TestCase):
if os.path.exists(folder):
shutil.rmtree(folder)
if __name__ == '__main__':
smtk.testing.process_arguments()
unittest.main()
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