Skip to content

Commit

Permalink
Setting parent of merged implicit nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
3dJan committed Oct 14, 2024
1 parent 80ecd20 commit 57105a4
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 5 deletions.
1 change: 1 addition & 0 deletions Include/Model/Classes/NMR_ModelImplicitNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ namespace NMR
void setGraphID(GraphID id);
GraphID getGraphID() const;
CModelImplicitFunction * getParent() const;
void setParent(CModelImplicitFunction * parent);
};

using PModelImplicitNode = std::shared_ptr<CModelImplicitNode>;
Expand Down
22 changes: 21 additions & 1 deletion Source/Model/Classes/NMR_Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,11 @@ namespace NMR {
pNewImplicitFunction->setPackageResourceID(newPkgId);
pNewImplicitFunction->setModel(this);

for (auto &node : *pNewImplicitFunction->getNodes())
{
node->setParent(pNewImplicitFunction.get());
}

addResource(pNewImplicitFunction);
oldToNewMapping[pOldImplicitFunction->getPackageResourceID()->getUniqueID()] = pNewImplicitFunction->getPackageResourceID()->getUniqueID();
}
Expand Down Expand Up @@ -1145,8 +1150,23 @@ namespace NMR {
{
throw CNMRException(NMR_ERROR_RESOURCENOTFOUND);
}

node->setModelResourceID(newId->getModelResourceID());
}

// check if the resource is available
auto res = findResource(currentPath(), newId->getModelResourceID());
if (!res)
{
throw CNMRException(NMR_ERROR_RESOURCENOTFOUND);
}

auto resource = node->getResource();
if (!resource)
{
throw CNMRException(NMR_ERROR_RESOURCENOTFOUND);

}
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions Source/Model/Classes/NMR_ModelImplicitNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,8 @@ namespace NMR
return m_parent;
}

void CModelImplicitNode::setParent(CModelImplicitFunction* parent)
{
m_parent = parent;
}
} // namespace NMR
58 changes: 54 additions & 4 deletions Tests/CPP_Bindings/Source/Volumetric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1280,10 +1280,6 @@ namespace Lib3MF
reader->SetStrictModeActive(true);
reader->ReadFromFile(InFolder + "RadialRadiator.3mf");

// save the source model to a file with a different name
auto writer = sourceModel->QueryWriter("3mf");
writer->WriteToFile(Volumetric::OutFolder + "RadialRadiatorCopy.3mf");

auto sourceModelFunctionCount = sourceModel->GetFunctions()->Count();

auto const targetModel = wrapper->CreateModel();
Expand All @@ -1299,4 +1295,58 @@ namespace Lib3MF
EXPECT_EQ(targetFunctionsIter->Count(), sourceModelFunctionCount + previousTargetFunctionCount);
}

TEST_F(Volumetric,
Volumetric_Merge_FunctionsFromLoadedModelIntoLoadedTargetModelWithoutFunctions_VaildResourceReferences)
{
// load the source model
auto const sourceModel = wrapper->CreateModel();
auto reader = sourceModel->QueryReader("3mf");
reader->SetStrictModeActive(true);
reader->ReadFromFile(InFolder + "template.3mf");

auto sourceModelFunctionCount = sourceModel->GetFunctions()->Count();
auto const targetModel = wrapper->CreateModel();
auto targetReader = targetModel->QueryReader("3mf");
targetReader->SetStrictModeActive(true);

targetReader->ReadFromFile(InFolder + "Cube.3mf");
auto previousTargetFunctionCount = targetModel->GetFunctions()->Count();

EXPECT_NO_THROW(targetModel->MergeFromModel(sourceModel.get()));

auto targetFunctionsIter = targetModel->GetFunctions();
EXPECT_EQ(targetFunctionsIter->Count(), sourceModelFunctionCount + previousTargetFunctionCount);

while (targetFunctionsIter->MoveNext())
{
auto function = targetFunctionsIter->GetCurrentFunction();
ASSERT_TRUE(function);
auto implicitFunction = std::dynamic_pointer_cast<CImplicitFunction>(function);
if (implicitFunction)
{
std::cout << "Implicit function: " << implicitFunction->GetDisplayName() << " Resource ID: " << implicitFunction->GetModelResourceID() << std::endl;

auto res = targetModel->GetResourceByID(implicitFunction->GetUniqueResourceID());
ASSERT_TRUE(res);

auto nodeIterator = implicitFunction->GetNodes();
while (nodeIterator->MoveNext())
{
auto node = nodeIterator->GetCurrent();
ASSERT_TRUE(node);


if (node->GetNodeType() == Lib3MF::eImplicitNodeType::ConstResourceID)
{
auto resourceIdNode = std::dynamic_pointer_cast<CResourceIdNode>(node);
std::cout << "Resource ID node: " << resourceIdNode->GetDisplayName() << " identifier: " << resourceIdNode->GetIdentifier() << std::endl;
ASSERT_TRUE(resourceIdNode);
auto resource = resourceIdNode->GetResource();
ASSERT_TRUE(resource);
}
}
}
}
}

} // namespace Lib3MF
Binary file modified Tests/TestFiles/Volumetric/template.3mf
Binary file not shown.

0 comments on commit 57105a4

Please sign in to comment.