diff --git a/Modules/Simulation/mitkSimulationReader.cpp b/Modules/Simulation/mitkSimulationReader.cpp index 644b504317..be87f5893a 100644 --- a/Modules/Simulation/mitkSimulationReader.cpp +++ b/Modules/Simulation/mitkSimulationReader.cpp @@ -1,133 +1,159 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkGetSimulationService.h" #include "mitkISimulationService.h" #include "mitkSimulation.h" #include "mitkSimulationReader.h" #include #include #include bool mitk::SimulationReader::CanReadFile(const std::string& filename, const std::string&, const std::string&) { std::string::size_type length = filename.length(); if (length < 5) return false; std::string ext = filename.substr(length - 4); std::transform(ext.begin(), ext.end(), ext.begin(), tolower); if (ext == ".scn" || ext == ".xml") return true; return false; } mitk::SimulationReader::SimulationReader() { mitk::Simulation::Pointer output = mitk::Simulation::New(); this->SetNumberOfRequiredOutputs(1); this->SetNthOutput(0, output.GetPointer()); } mitk::SimulationReader::~SimulationReader() { } void mitk::SimulationReader::GenerateData() { Simulation::Pointer simulation = static_cast(this->GetOutput()); sofa::simulation::Simulation::SPtr sofaSimulation = simulation->GetSimulation(); ISimulationService* simulationService = GetSimulationService(); Simulation::Pointer currentSimulation = simulationService->GetSimulation(); simulationService->SetSimulation(simulation); + std::ifstream scnFile(m_FileName.c_str()); + std::string content = std::string((std::istreambuf_iterator(scnFile)), std::istreambuf_iterator()); + scnFile.close(); + + std::istringstream stream(content); + std::string firstLine; + + if (!std::getline(stream, firstLine).good()) + mitkThrow() << "Could not load '" << m_FileName << "'!"; + + std::string originalPath; + + if (firstLine.size() > 21 && firstLine.substr(0, 21) == "\n"; + std::ofstream scnFile(m_FileName.c_str()); - scnFile << simulation->GetProperty("Scene File")->GetValueAsString(); + scnFile << originalPath << simulation->GetProperty("Scene File")->GetValueAsString(); } mitk::Simulation* mitk::SimulationWriter::GetInput() { if (this->GetNumberOfInputs() == 0) return NULL; return dynamic_cast(this->GetInput(0)); } void mitk::SimulationWriter::SetInput(Simulation *simulation) { this->SetNthInput(0, simulation); } const char* mitk::SimulationWriter::GetFileName() const { return m_FileName.c_str(); } void mitk::SimulationWriter::SetFileName(const char* fileName) { m_FileName = fileName; } const char* mitk::SimulationWriter::GetFilePrefix() const { return m_FilePrefix.c_str(); } void mitk::SimulationWriter::SetFilePrefix(const char* filePrefix) { m_FilePrefix = filePrefix; } const char* mitk::SimulationWriter::GetFilePattern() const { return m_FilePattern.c_str(); } void mitk::SimulationWriter::SetFilePattern(const char* filePattern) { m_FilePattern = filePattern; } std::string mitk::SimulationWriter::GetFileExtension() { return m_FileExtension; } std::vector mitk::SimulationWriter::GetPossibleFileExtensions() { std::vector possibleFileExtensions; possibleFileExtensions.push_back(m_FileExtension); return possibleFileExtensions; } std::string mitk::SimulationWriter::GetWritenMIMEType() { return m_MIMEType; } bool mitk::SimulationWriter::CanWriteDataType(DataNode* dataNode) { if (dataNode == NULL) return false; return this->CanWriteBaseDataType(dataNode->GetData()); } const char* mitk::SimulationWriter::GetDefaultFilename() { std::string defaultFilename = "Simulation" + m_FileExtension; return defaultFilename.c_str(); } const char* mitk::SimulationWriter::GetFileDialogPattern() { std::string fileDialogPattern = "SOFA Scene Files (*" + m_FileExtension + ")"; return fileDialogPattern.c_str(); } const char* mitk::SimulationWriter::GetDefaultExtension() { return m_FileExtension.c_str(); } bool mitk::SimulationWriter::CanWriteBaseDataType(BaseData::Pointer data) { if (data.IsNull()) return false; return dynamic_cast(data.GetPointer()) != NULL; } void mitk::SimulationWriter::DoWrite(BaseData::Pointer data) { if (this->CanWriteBaseDataType(data)) { this->SetNthInput(0, dynamic_cast(data.GetPointer())); this->Update(); } }