ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
qcc_io.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "../casters.h"
9 
10 #include <FileIO.h>
11 #include <FileIOFilter.h>
12 
13 #include <QFileInfo>
14 #include <QWidget>
15 
16 namespace py = pybind11;
17 using namespace pybind11::literals;
19 {
20  switch (error)
21  {
22  case CC_FERR_NO_ERROR:
23  break;
25  throw std::runtime_error("Bad argument");
27  throw std::runtime_error("Unknown file");
29  throw std::runtime_error("Wrong file type");
30  case CC_FERR_WRITING:
31  throw std::runtime_error("Error when writing");
32  case CC_FERR_READING:
33  throw std::runtime_error("Error when reading");
34  case CC_FERR_NO_SAVE:
35  throw std::runtime_error("Nothing to save");
36  case CC_FERR_NO_LOAD:
37  throw std::runtime_error("Nothing to load");
39  throw std::runtime_error("Bad entity type");
41  throw std::runtime_error("Canceled by user");
43  throw std::runtime_error("Not enough memory");
45  throw std::runtime_error("Malformed File");
47  throw std::runtime_error("The error has been logged in the console");
49  throw std::runtime_error("Broken dependency");
51  throw std::runtime_error("File was written by unknown plugin");
53  throw std::runtime_error("Third party lib failure");
55  throw std::runtime_error("Third party lib exception");
56  case CC_FERR_INTERNAL:
57  throw std::runtime_error("Internal error");
59  throw std::runtime_error("Not implemented");
60  }
61 }
62 
63 void define_qcc_io(py::module &m)
64 {
65  py::class_<ecvGlobalShiftManager> PyccGlobalShiftManager(m, "ccGlobalShiftManager");
66 
67  py::native_enum<ecvGlobalShiftManager::Mode>(
68  PyccGlobalShiftManager, "Mode", "enum.Enum", "ecvGlobalShiftManager::Mode.")
69  .value("NO_DIALOG", ecvGlobalShiftManager::Mode::NO_DIALOG)
70  .value("NO_DIALOG_AUTO_SHIFT", ecvGlobalShiftManager::Mode::NO_DIALOG_AUTO_SHIFT)
71  .value("DIALOG_IF_NECESSARY", ecvGlobalShiftManager::Mode::DIALOG_IF_NECESSARY)
72  .value("ALWAYS_DISPLAY_DIALOG", ecvGlobalShiftManager::Mode::ALWAYS_DISPLAY_DIALOG)
73  .export_values()
74  .finalize();
75 
76  py::class_<FileIOFilter> PyFileIOFilter(m, "FileIOFilter");
77  PyFileIOFilter
78  .def_static(
79  "LoadFromFile",
80  [](const QString &filename, FileIOFilter::LoadParameters &parameters)
81  {
83  ccHObject *newGroup = FileIOFilter::LoadFromFile(filename, parameters, result);
85  return newGroup;
86  },
87  py::return_value_policy::take_ownership)
88  .def_static(
89  "SaveToFile",
90  [](ccHObject *entities,
91  const QString &filename,
92  const FileIOFilter::SaveParameters &parameters,
93  const QString filterName = QString())
94  {
95  const QString requestedFilterName =
96  filterName.isEmpty() ? QFileInfo(filename).suffix() : filterName;
97 
98  const FileIOFilter::FilterContainer &availableFilters = FileIOFilter::GetFilters();
99  for (const FileIOFilter::Shared &filter : availableFilters)
100  {
101  const QStringList filters = filter->getFileFilters(false /* onImport */);
102  const auto it =
103  std::find_if(filters.begin(),
104  filters.end(),
105  [&requestedFilterName](const QString &filterString)
106  { return filterString.contains(requestedFilterName); });
107  if (it != filters.end())
108  {
110  FileIOFilter::SaveToFile(entities, filename, parameters, filter);
112  return;
113  }
114  }
115  throw std::runtime_error(std::string("Unable to find FileFilter for ") +
116  requestedFilterName.toStdString());
117  },
118  "entities"_a,
119  "filename"_a,
120  "parameters"_a,
121  "fileFileter"_a = QString());
122 
123  py::class_<FileIOFilter::LoadParameters>(PyFileIOFilter, "LoadParameters")
124  .def(py::init<>())
125  .def_readwrite("shiftHandlingMode", &FileIOFilter::LoadParameters::shiftHandlingMode)
126  .def_readwrite("alwaysDisplayLoadDialog",
128  .def_readwrite("coordinatesShiftEnabled",
130  .def_readwrite("coordinatesShift",
132  py::return_value_policy::reference)
133  .def_readwrite("preserveShiftOnSave", &FileIOFilter::LoadParameters::preserveShiftOnSave)
134  .def_readwrite("autoComputeNormals", &FileIOFilter::LoadParameters::autoComputeNormals)
135  .def_readwrite("parentWidget",
137  py::return_value_policy::reference)
138  .def_readwrite("sessionStart", &FileIOFilter::LoadParameters::sessionStart);
139 
140  py::class_<FileIOFilter::SaveParameters>(PyFileIOFilter, "SaveParameters")
141  .def(py::init<>())
142  .def_readwrite("alwaysDisplaySaveDialog",
144  .def_readwrite("parentWidget",
146  py::return_value_policy::reference);
147 }
std::string filename
CC_FILE_ERROR
Typical I/O filter errors.
Definition: FileIOFilter.h:20
@ CC_FERR_CONSOLE_ERROR
Definition: FileIOFilter.h:33
@ CC_FERR_CANCELED_BY_USER
Definition: FileIOFilter.h:30
@ CC_FERR_THIRD_PARTY_LIB_FAILURE
Definition: FileIOFilter.h:36
@ CC_FERR_NO_LOAD
Definition: FileIOFilter.h:28
@ CC_FERR_WRITING
Definition: FileIOFilter.h:25
@ CC_FERR_THIRD_PARTY_LIB_EXCEPTION
Definition: FileIOFilter.h:37
@ CC_FERR_MALFORMED_FILE
Definition: FileIOFilter.h:32
@ CC_FERR_BAD_ARGUMENT
Definition: FileIOFilter.h:22
@ CC_FERR_UNKNOWN_FILE
Definition: FileIOFilter.h:23
@ CC_FERR_NO_SAVE
Definition: FileIOFilter.h:27
@ CC_FERR_NO_ERROR
Definition: FileIOFilter.h:21
@ CC_FERR_NOT_IMPLEMENTED
Definition: FileIOFilter.h:38
@ CC_FERR_BROKEN_DEPENDENCY_ERROR
Definition: FileIOFilter.h:34
@ CC_FERR_BAD_ENTITY_TYPE
Definition: FileIOFilter.h:29
@ CC_FERR_INTERNAL
Definition: FileIOFilter.h:39
@ CC_FERR_READING
Definition: FileIOFilter.h:26
@ CC_FERR_FILE_WAS_WRITTEN_BY_UNKNOWN_PLUGIN
Definition: FileIOFilter.h:35
@ CC_FERR_NOT_ENOUGH_MEMORY
Definition: FileIOFilter.h:31
@ CC_FERR_WRONG_FILE_TYPE
Definition: FileIOFilter.h:24
core::Tensor result
Definition: VtkUtils.cpp:76
static CC_FILE_ERROR SaveToFile(ccHObject *entities, const QString &filename, const SaveParameters &parameters, Shared filter)
static const FilterContainer & GetFilters()
Returns the set of all registered filters.
QSharedPointer< FileIOFilter > Shared
Shared type.
Definition: FileIOFilter.h:97
static ccHObject * LoadFromFile(const QString &filename, LoadParameters &parameters, Shared filter, CC_FILE_ERROR &result)
Loads one or more entities from a file with a known filter.
std::vector< FileIOFilter::Shared > FilterContainer
Type of a I/O filters container.
Definition: FileIOFilter.h:284
Hierarchical CLOUDVIEWER Object.
Definition: ecvHObject.h:25
static void error(char *msg)
Definition: lsd.c:159
void define_qcc_io(py::module &m)
Definition: qcc_io.cpp:63
static void ThrowForFileError(CC_FILE_ERROR error)
Definition: qcc_io.cpp:18
Generic loading parameters.
Definition: FileIOFilter.h:51
CCVector3d * coordinatesShift
If applicable, applied shift on load (optional)
Definition: FileIOFilter.h:71
ecvGlobalShiftManager::Mode shiftHandlingMode
How to handle big coordinates.
Definition: FileIOFilter.h:64
QWidget * parentWidget
Parent widget (if any)
Definition: FileIOFilter.h:78
bool sessionStart
Session start (whether the load action is the first of a session)
Definition: FileIOFilter.h:80
bool preserveShiftOnSave
If applicable, whether shift should be preserved or not (optional)
Definition: FileIOFilter.h:73
bool * coordinatesShiftEnabled
Whether shift on load has been applied after loading (optional)
Definition: FileIOFilter.h:69
Generic saving parameters.
Definition: FileIOFilter.h:84
QWidget * parentWidget
Parent widget (if any)
Definition: FileIOFilter.h:93