# Can't write polydata to file...

**URL:** <https://discourse.vtk.org/t/cant-write-polydata-to-file/12836>\
**Category:** Development\
**Tags:** code, file-formats\
**Created:** [December 4, 2023, 7:34pm UTC](https://discourse.vtk.org/t/cant-write-polydata-to-file/12836 "2023-12-04T19:34:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafael\_Scatena](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rafael_scatena/32/7820_2.png) [@Rafael\_Scatena](https://discourse.vtk.org/u/Rafael_Scatena)\
**Post date:** [December 4, 2023, 7:34pm UTC](https://discourse.vtk.org/t/cant-write-polydata-to-file/12836/1 "2023-12-04T19:34:32Z")

</div>

Hi there. Can’t make my VTK module write polydata. Any help welcome. Thanks.

/usr/bin/ld: CMakeFiles/createmesh.dir/MeshWriter.cxx.o: undefined reference to symbol “\_ZN9vtkWriter12SetInputDataEP13vtkDataObject”  
/usr/bin/ld: /home/3484681/Area de Trabalho/vtk-master/build/lib/libvtkIOCore-9.3.so.1: error adding symbols: DSO missing from command line  
collect2: error: ld returned 1 exit status  
make[2]: \*\*\* [CMakeFiles/createmesh.dir/build.make:347: createmesh] Error 1  
make[1]: \*\*\* [CMakeFiles/Makefile2:83: CMakeFiles/createmesh.dir/all] Error 2  
make: \*\*\* [Makefile:91: all] Error 2

#include “MeshWriter.h” // Replace ‘path/to/’ with the actual path  
#include \<vtkSmartPointer.h\>  
#include \<vtkPolyData.h\>  
#include \<vtkPLYWriter.h\>  
#include \<vtkSTLWriter.h\>  
#include \<vtkOBJWriter.h\>  
#include \<vtkXMLPolyDataWriter.h\>  
#include \<vtkPolyDataWriter.h\>

#include   
#include

#include \<vtkPolyDataReader.h\>  
#include \<vtkXMLPolyDataReader.h\>  
#include \<vtkSTLReader.h\>  
#include \<vtkOBJReader.h\>  
#include   
#include   
#include \<vtkAlgorithmOutput.h\>  
#include \<vtkDataReader.h\>  
#include \<vtkPolyDataReader.h\>

std::string GetFileFormat(const std::string& filename) {  
// Find the position of the last dot in the filename.  
size\_t dotPos = filename.find\_last\_of(‘.’);

```
  // Check if a dot was found and extract the format if found.
  if (dotPos != std::string::npos) {
      // Use substr to get the characters following the last dot.
      return filename.substr(dotPos + 1);
  }
  
  // If no dot is found, return an empty string to indicate no format.
  return "";

```

}

MeshWriter::MeshWriter() {  
// Constructor implementation, if needed  
}

MeshWriter::~MeshWriter() {  
// Destructor implementation, if needed  
}

bool MeshWriter::WriteVTK(const vtkSmartPointer& polydata, const std::string& fileName) {  
vtkSmartPointer writer = vtkSmartPointer::New();  
writer-\>SetFileName(fileName.c\_str());  
writer-\>SetInputData(polydata);  
return writer-\>Write();  
}

bool MeshWriter::WriteVTP(const vtkSmartPointer& polydata, const std::string& fileName) {  
vtkSmartPointer writer = vtkSmartPointer::New();  
writer-\>SetFileName(fileName.c\_str());  
writer-\>SetInputData(polydata);  
return writer-\>Write();  
}

bool MeshWriter::WriteSTL(const vtkSmartPointer& polydata, const std::string& fileName) {  
vtkSmartPointer writer = vtkSmartPointer::New();  
writer-\>SetFileName(fileName.c\_str());  
writer-\>SetInputData(polydata);  
return writer-\>Write();  
}

bool MeshWriter::WriteOBJ(const vtkSmartPointer& polydata, const std::string& fileName) {  
vtkSmartPointer writer = vtkSmartPointer::New();  
writer-\>SetFileName(fileName.c\_str());  
writer-\>SetInputData(polydata);  
return writer-\>Write();  
}

bool MeshWriter::WritePLY(const vtkSmartPointer& polydata, const std::string& fileName) {  
vtkSmartPointer writer = vtkSmartPointer::New();  
writer-\>SetFileName(fileName.c\_str());  
writer-\>SetInputData(polydata);  
return writer-\>Write();  
}

bool MeshWriter::WriteMesh(const vtkSmartPointer& polydata, const std::string& fileName) {  
vtkSmartPointer writer;

```
  std::string format ;
  format= GetFileFormat(fileName);
  
  if (format == "vtk") {
      return WriteVTK(polydata, fileName);
  } else if (format == "vtp") {
      return WriteVTP(polydata, fileName);
  } else if (format == "stl") {
      return WriteSTL(polydata, fileName);
  } else if (format == "obj") {
      return WriteOBJ(polydata, fileName);
  } else {
      return WritePLY(polydata, fileName);
  }

```

}

[MeshWriter.cxx](https://discourse.vtk.org/uploads/short-url/50vbjz8DdJN5sQFD42FhqKCL9W5.cxx) (5.9 KB)  
[MeshWriter.h](https://discourse.vtk.org/uploads/short-url/fmM3pwkctTyOUms9bOW8u1QRC9.h) (1.5 KB)  
[CMakeLists.txt](https://discourse.vtk.org/uploads/short-url/6gqWfhweKC9wHVklpBajDDPQUrh.txt) (2.6 KB)

---

<div class="post-metadata">

**Author:** ![Rafael\_Scatena](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rafael_scatena/32/7820_2.png) [@Rafael\_Scatena](https://discourse.vtk.org/u/Rafael_Scatena)\
**Post date:** [December 4, 2023, 8:49pm UTC](https://discourse.vtk.org/t/cant-write-polydata-to-file/12836/2 "2023-12-04T20:49:16Z")

</div>

It compiled with these changes to the cmaklist file:

#PRISMAMESH CMAKELISTS FILE

cmake\_minimum\_required(VERSION 3.12 FATAL\_ERROR)  
project(createmesh)  
set(CMAKE\_CXX\_STANDARD 14)  
set(CMAKE\_CXX\_STANDARD\_REQUIRED ON)  
set(CMAKE\_CXX\_ENTENSIONS OFF)  
set(CMAKE\_BUILD\_TYPE Release)

set(CMAKE\_CXX\_FLAGS “”)  
set(CMAKE\_CXX\_FLAGS\_DEBUG “-g”)  
set(CMAKE\_CXX\_FLAGS\_RELEASE “-O3”)

# Find ITK.

find\_package(ITK REQUIRED)  
include({ITK\_USE\_FILE}) if (ITKVtkGlue\_LOADED) find\_package(VTK REQUIRED) if(VTK\_VERSION VERSION\_LESS "8.90") include({VTK\_USE\_FILE})  
endif()  
endif()

# Try to find VTK, but don’t use the default paths.

find\_package(VTK QUIET NO\_DEFAULT\_PATH)

# If VTK is not found, set the directories manually.

if(NOT VTK\_FOUND)  
set(VTK\_DIR “/home/3484681/Área de Trabalho/vtk-master/vtk-9.3”)  
set(VTK\_INCLUDE\_DIRS “/home/3484681/Área de Trabalho/vtk-master/vtk-9.3/include/vtk-9.3”)  
endif()

# FIND VTK PACKAGE

find\_package(VTK REQUIRED  
COMPONENTS  
${VTK\_LIBRARIES}  
IOCore  
CommonColor  
CommonCore  
CommonDataModel  
IOImage  
IOXML  
IOXMLParser  
InteractionStyle  
RenderingContextOpenGL2  
RenderingCore  
RenderingFreeType  
RenderingGL2PSOpenGL2  
RenderingOpenGL2  
IOGeometry  
CommonExecutionModel  
vtksys  
CommonSystem  
CommonMisc  
IOPLY

#FILTERING  
FiltersCore  
FiltersGeneral  
FiltersGeometry  
ImagingStatistics  
)

set(MPI\_FOUND FALSE)

if (NOT VTK\_FOUND)  
message(FATAL\_ERROR “PRISMAMESH: Unable to find the VTK build folder.”)  
endif()

#FIND MPI  
find\_package(MPI REQUIRED)  
if(MPI\_FOUND)  
message(“MPI found.”)  
else()  
message(“MPI not found.”)  
endif()

# MANUALLY ENABLE/DISENABLE MPI SUPPORT

option(ENABLE\_MPI “Enable MPI support” OFF)  
set(ENABLE\_MPI OFF)

if(ENABLE\_MPI)  
message(“MPI support is enabled”)  
else()  
message(“MPI support is not enabled”)  
endif()

if(VTK\_VERSION VERSION\_LESS “8.90”)  
include(${VTK\_USE\_FILE})  
endif()

add\_executable(createmesh createmesh.cxx segmentation\_io.cxx visualize\_polidata.cxx MeshWriter.cxx)

# Use the find\_package command to search for Boost

find\_package(Boost REQUIRED)

# Specify the Boost library path

set(BOOST\_LIBRARY\_DIRS “/home/3484681/miniconda3/envs/vtkenv/include/boost”)

# Check if Boost is found and include the library path

if(Boost\_FOUND)  
target\_include\_directories(createmesh PRIVATE ${Boost\_INCLUDE\_DIRS})

```
# Specify the Boost library path
target_link_directories(createmesh PRIVATE ${BOOST_LIBRARY_DIRS})

# Link against Boost libraries
target_link_libraries(createmesh PRIVATE ${Boost_LIBRARIES})

```

endif()

target\_include\_directories(createmesh PRIVATE {VTK\_INCLUDE\_DIRS} {CMAKE\_SOURCE\_DIR}/include)  
target\_link\_libraries(createmesh PRIVATE {VTK\_LIBRARIES} {ITK\_LIBRARIES})

if(ENABLE\_MPI)  
target\_include\_directories(createmesh PRIVATE {MPI\_CXX\_INCLUDE\_PATH} SYSTEM {MPI\_INCLUDE\_PATH})  
target\_link\_libraries(createmesh PRIVATE ${MPI\_LIBRARIES})  
endif()

if(ENABLE\_MPI AND MPI\_FOUND)  
#Define the WriteParallelMesh function  
target\_sources(createmesh PRIVATE write\_parallel.cxx)  
#Add the MPI\_ENABLED definition to your compile flags  
add\_compile\_definitions(MPI\_ENABLED)  
endif()

# vtk\_module\_autoinit is needed

vtk\_module\_autoinit(  
TARGETS createmesh  
MODULES ${VTK\_LIBRARIES}  
)  
[CMakeLists.txt](https://discourse.vtk.org/uploads/short-url/a91gOewpkCxzFTxZaBXczhMmiJ8.txt) (3.3 KB)
