Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

namespace
{
constexpr int32 k_FailedFindPipelineError = -15;
constexpr nx::core::int32 k_NoExportPathError = -1;
constexpr nx::core::int32 k_FailedFindPipelineError = -15;
} // namespace

// -----------------------------------------------------------------------------
Expand All @@ -29,51 +30,93 @@
// -----------------------------------------------------------------------------
WriteDREAM3D::~WriteDREAM3D() noexcept = default;

// -----------------------------------------------------------------------------
Result<> WriteDREAM3D::operator()()
/**
* @brief Extracts the preceding Pipeline from the PipelineFilter.
* This method returns an empty Pipeline if the PipelineFilter is null.
* @param pipelineNode The PipelineFilter to extract the Pipeline from.
* @return Result<Pipeline> The target Pipeline or an error message if the process failed to complete.
*/
Result<Pipeline> ExtractPipeline(const PipelineFilter* pipelineNode)
{
auto atomicFileResult = AtomicFile::Create(m_InputValues->ExportFilePath);
if(atomicFileResult.invalid())
Pipeline pipeline;
if(pipelineNode != nullptr)
{
return ConvertResult(std::move(atomicFileResult));
auto pipelinePtr = pipelineNode->getPrecedingPipeline();
if(pipelinePtr == nullptr)
{
return MakeErrorResult<Pipeline>(k_FailedFindPipelineError, "Failed to retrieve pipeline.");
}

pipeline = *pipelinePtr;
}
AtomicFile atomicFile = std::move(atomicFileResult.value());
return {pipeline};
}

/**
* @brief Writes the DREAM3D file to the temp file, commits changes, and then writes the XDMF file if requested.
* @param atomicFile Temp file for writing the DREAM3D file
* @param dataStructure DataStructure to be written to file.
* @param pipeline Pipeline to write to file.
* @param writeXdmfFile
* @return Result<>
*/
Result<> WriteDREAM3DFile(AtomicFile& atomicFile, const DataStructure& dataStructure, const Pipeline& pipeline, bool writeXdmfFile)
{
auto exportFilePath = atomicFile.tempFilePath();
auto writeXdmf = m_InputValues->WriteXdmfFile;

Pipeline pipeline;
auto results = DREAM3D::WriteFile(exportFilePath, dataStructure, pipeline, writeXdmfFile);
if(results.invalid())
{
return results;
}

if(m_InputValues->PipelineNode != nullptr)
// Commit changes to the temp file. Return an invalid Result if errors occured.
if(auto commitResult = atomicFile.commit(); commitResult.invalid())
{
auto pipelinePtr = m_InputValues->PipelineNode->getPrecedingPipeline();
if(pipelinePtr == nullptr)
return commitResult;
}

// Write the XDMF file if specified
if(writeXdmfFile)
{
// TODO: Double check this
fs::path xdmfFilePath = exportFilePath.replace_extension(".xdmf");
std::error_code errorCode;
fs::rename(xdmfFilePath, fs::path(exportFilePath).replace_extension(".xdmf"), errorCode);

Check failure on line 85 in src/Plugins/SimplnxCore/src/SimplnxCore/Filters/Algorithms/WriteDREAM3D.cpp

View workflow job for this annotation

GitHub Actions / clang_format_pr

code should be clang-formatted [-Wclang-format-violations]

// If the XDMF file failed to be renamed, return an invalid Result
if(errorCode)
{
return MakeErrorResult(k_FailedFindPipelineError, "Failed to retrieve pipeline.");
std::string ss = fmt::format("Failed to rename xdmf file with error: '{}'", errorCode.message());
return MakeErrorResult(errorCode.value(), ss);
}
}

pipeline = *pipelinePtr;
return {};
}

// -----------------------------------------------------------------------------
Result<> WriteDREAM3D::operator()()
{
// Create AtomicFile to write.
auto atomicFileResult = AtomicFile::Create(m_InputValues->ExportFilePath);
if(atomicFileResult.invalid())
{
return ConvertResult(std::move(atomicFileResult));
}
AtomicFile atomicFile = std::move(atomicFileResult.value());

auto results = DREAM3D::WriteFile(exportFilePath, m_DataStructure, pipeline, writeXdmf);
if(results.valid())
// Extract Preceding Pipeline
Pipeline pipeline;
if(auto result = ExtractPipeline(m_InputValues->PipelineNode); result.invalid())
{
Result<> commitResult = atomicFile.commit();
if(commitResult.invalid())
{
return commitResult;
}
if(writeXdmf)
{
fs::path xdmfFilePath = exportFilePath.replace_extension(".xdmf");
std::error_code errorCode;
fs::rename(xdmfFilePath, m_InputValues->ExportFilePath.parent_path() / m_InputValues->ExportFilePath.stem().concat(".xdmf"), errorCode);
if(errorCode)
{
std::string ss = fmt::format("Failed to rename xdmf file with error: '{}'", errorCode.message());
return MakeErrorResult(errorCode.value(), ss);
}
}
return ConvertResult(std::move(result));
}
return results;
else
{
pipeline = std::move(result.value());
}

// Write DREAM.3D file
return WriteDREAM3DFile(atomicFile, m_DataStructure, pipeline, m_InputValues->WriteXdmfFile);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

#include "SimplnxCore/Filters/Algorithms/WriteDREAM3D.hpp"

#include "simplnx/Parameters/BoolParameter.hpp"

Check failure on line 5 in src/Plugins/SimplnxCore/src/SimplnxCore/Filters/WriteDREAM3DFilter.cpp

View workflow job for this annotation

GitHub Actions / clang_format_pr

code should be clang-formatted [-Wclang-format-violations]
#include "simplnx/Parameters/FileSystemPathParameter.hpp"
#include "simplnx/Utilities/SIMPLConversion.hpp"
#include "SimplnxCore/Filters/Algorithms/WriteDREAM3D.hpp"

namespace
{
Expand Down
Loading