Skip to content

Commit

Permalink
Refactoring: Split openscad_gui.cc from openscad.cc, slimmed down Exp…
Browse files Browse the repository at this point in the history
…ortInfo (openscad#5368)
  • Loading branch information
kintel authored Oct 24, 2024
1 parent 5259f36 commit d8cdc56
Show file tree
Hide file tree
Showing 15 changed files with 720 additions and 679 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,7 @@ set(INPUT_DRIVER_SOURCES

set(GUI_SOURCES
${GUI_SOURCES}
src/openscad_gui.cc
src/gui/AutoUpdater.cc
src/gui/CGALWorker.cc
src/gui/ViewportControl.cc
Expand Down
4 changes: 2 additions & 2 deletions src/gui/ExportPdfDialog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ ExportPdfDialog::ExportPdfDialog()
bool ExportPdfDialog::getShowScaleMsg() {
return cbScaleUsg->isChecked();
}
bool ExportPdfDialog::getShowDsnFn() {
bool ExportPdfDialog::getShowDesignFilename() {
return cbDsnFn->isChecked();
}
bool ExportPdfDialog::getShowGrid() {
Expand All @@ -127,7 +127,7 @@ ExportPdfDialog::ExportPdfDialog()
void ExportPdfDialog::setShowScaleMsg(bool state) {
cbScaleUsg->setChecked(state);
}
void ExportPdfDialog::setShowDsnFn(bool state) {
void ExportPdfDialog::setShowDesignFilename(bool state) {
cbDsnFn->setChecked(state);
}
void ExportPdfDialog::setShowGrid(bool state) {
Expand Down
4 changes: 2 additions & 2 deletions src/gui/ExportPdfDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ class ExportPdfDialog : public QDialog, public Ui::ExportPdfDialog
paperOrientations getOrientation();
bool getShowScale();
bool getShowScaleMsg();
bool getShowDsnFn();
bool getShowDesignFilename();
bool getShowGrid();

void setShowScale(bool state);
void setShowScaleMsg(bool state);
void setShowDsnFn(bool state);
void setShowDesignFilename(bool state);
void setShowGrid(bool state);

void setPaperSize(paperSizes paper);
Expand Down
77 changes: 24 additions & 53 deletions src/gui/MainWindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2147,25 +2147,6 @@ void MainWindow::action3DPrint()
#endif // ifdef ENABLE_3D_PRINTING
}

namespace {

ExportInfo createExportInfo(FileFormat format, const QString& exportFilename, const QString& sourceFilePath)
{
const QFileInfo info(sourceFilePath);

ExportInfo exportInfo;
exportInfo.format = format;
exportInfo.fileName = exportFilename.toLocal8Bit().constData();
exportInfo.displayName = exportFilename.toUtf8().toStdString();
exportInfo.sourceFilePath = sourceFilePath.toUtf8().toStdString();
exportInfo.sourceFileName = info.fileName().toUtf8().toStdString();
exportInfo.useStdOut = false;
exportInfo.options = nullptr;
return exportInfo;
}

}

void MainWindow::sendToOctoPrint()
{
#ifdef ENABLE_3D_PRINTING
Expand All @@ -2176,20 +2157,22 @@ void MainWindow::sendToOctoPrint()
return;
}

// FIXME: To make this cleaner, we could define which formats are supported by OctoPrint separately,
// then using fileformat::fromIdentifier() to convert.
const QString fileFormat = QString::fromStdString(Settings::Settings::octoPrintFileFormat.value());
FileFormat exportFileFormat{FileFormat::STL};
FileFormat exportFileFormat{FileFormat::BINARY_STL};
if (fileFormat == "OBJ") {
exportFileFormat = FileFormat::OBJ;
} else if (fileFormat == "OFF") {
exportFileFormat = FileFormat::OFF;
} else if (fileFormat == "ASCIISTL") {
exportFileFormat = FileFormat::ASCIISTL;
exportFileFormat = FileFormat::ASCII_STL;
} else if (fileFormat == "AMF") {
exportFileFormat = FileFormat::AMF;
} else if (fileFormat == "3MF") {
exportFileFormat = FileFormat::_3MF;
} else {
exportFileFormat = FileFormat::STL;
exportFileFormat = FileFormat::BINARY_STL;
}

QTemporaryFile exportFile{QDir::temp().filePath("OpenSCAD.XXXXXX." + fileFormat.toLower())};
Expand All @@ -2208,8 +2191,8 @@ void MainWindow::sendToOctoPrint()
userFileName = fileInfo.baseName() + "." + fileFormat.toLower();
}

ExportInfo exportInfo = createExportInfo(exportFileFormat, exportFileName, activeEditor->filepath);
exportFileByName(this->root_geom, exportInfo);
ExportInfo exportInfo = {.format = exportFileFormat, .sourceFilePath = activeEditor->filepath.toStdString()};
exportFileByName(this->root_geom, exportFileName.toStdString(), exportInfo);

try {
this->progresswidget = new ProgressWidget(this);
Expand Down Expand Up @@ -2239,25 +2222,13 @@ void MainWindow::sendToLocalSlicer()
#ifdef ENABLE_3D_PRINTING
const QString slicer = QString::fromStdString(Settings::Settings::localSlicerExecutable.value());

const QString fileFormat = QString::fromStdString(Settings::Settings::localSlicerFileFormat.value());
FileFormat exportFileFormat{FileFormat::STL};
if (fileFormat == "OBJ") {
exportFileFormat = FileFormat::OBJ;
} else if (fileFormat == "OFF") {
exportFileFormat = FileFormat::OFF;
} else if (fileFormat == "ASCIISTL") {
exportFileFormat = FileFormat::ASCIISTL;
} else if (fileFormat == "AMF") {
exportFileFormat = FileFormat::AMF;
} else if (fileFormat == "3MF") {
exportFileFormat = FileFormat::_3MF;
} else if (fileFormat == "POV") {
exportFileFormat = FileFormat::POV;
} else {
exportFileFormat = FileFormat::STL;
const QString fileFormat = QString::fromStdString(Settings::Settings::localSlicerFileFormat.value()).toLower();
FileFormat exportFileFormat = FileFormat::BINARY_STL;
if (!fileformat::fromIdentifier(fileFormat.toStdString(), exportFileFormat)) {
LOG("Invalid suffix %1$s. Defaulting to binary STL.", fileFormat.toStdString());
}

const auto tmpPath = QDir::temp().filePath("OpenSCAD.XXXXXX."+fileFormat.toLower());
const auto tmpPath = QDir::temp().filePath("OpenSCAD.XXXXXX."+fileFormat);
auto exportFile = std::make_unique<QTemporaryFile>(tmpPath);
if (!exportFile->open()) {
LOG(message_group::Error, "Could not open temporary file '%1$s'.", tmpPath.toStdString());
Expand All @@ -2272,11 +2243,11 @@ void MainWindow::sendToLocalSlicer()
userFileName = exportFileName;
} else {
QFileInfo fileInfo{activeEditor->filepath};
userFileName = fileInfo.baseName() + fileFormat.toLower();
userFileName = fileInfo.baseName() + fileFormat;
}

ExportInfo exportInfo = createExportInfo(exportFileFormat, exportFileName, activeEditor->filepath);
exportFileByName(this->root_geom, exportInfo);
ExportInfo exportInfo = {.format = exportFileFormat, .sourceFilePath = activeEditor->filepath.toStdString()};
exportFileByName(this->root_geom, exportFileName.toStdString(), exportInfo);

QProcess process(this);
process.setProcessChannelMode(QProcess::MergedChannels);
Expand Down Expand Up @@ -2310,8 +2281,8 @@ void MainWindow::sendToPrintService()
const QString exportFilename = exportFile.fileName();

//Render the stl to a temporary file:
ExportInfo exportInfo = createExportInfo(FileFormat::STL, exportFilename, activeEditor->filepath);
exportFileByName(this->root_geom, exportInfo);
ExportInfo exportInfo = {.format = FileFormat::BINARY_STL, .sourceFilePath = activeEditor->filepath.toStdString()};
exportFileByName(this->root_geom, exportFilename.toStdString(), exportInfo);

//Create a name that the order process will use to refer to the file. Base it off of the project name
QString userFacingName = "unsaved.stl";
Expand Down Expand Up @@ -2942,11 +2913,11 @@ void MainWindow::actionExport(FileFormat format, const char *type_name, const ch
}
this->export_paths[suffix] = exportFilename;

ExportInfo exportInfo = createExportInfo(format, exportFilename, activeEditor->filepath);
ExportInfo exportInfo = {.format = format, .sourceFilePath = activeEditor->filepath.toStdString()};
// Add options
exportInfo.options = options;

bool exportResult = exportFileByName(this->root_geom, exportInfo);
bool exportResult = exportFileByName(this->root_geom, exportFilename.toStdString(), exportInfo);

if (exportResult) fileExportedMessage(type_name, exportFilename);
clearCurrentOutput();
Expand All @@ -2955,9 +2926,9 @@ void MainWindow::actionExport(FileFormat format, const char *type_name, const ch
void MainWindow::actionExportSTL()
{
if (Settings::Settings::exportUseAsciiSTL.value()) {
actionExport(FileFormat::ASCIISTL, "ASCIISTL", ".stl", 3);
actionExport(FileFormat::ASCII_STL, "ASCIISTL", ".stl", 3);
} else {
actionExport(FileFormat::STL, "STL", ".stl", 3);
actionExport(FileFormat::BINARY_STL, "STL", ".stl", 3);
}
}

Expand Down Expand Up @@ -3016,7 +2987,7 @@ void MainWindow::actionExportPDF()
QString::fromStdString(paperSizeStrings[static_cast<int>(exportPdfOptions.paperSize)])).toString())); // enum map
exportPdfDialog->setOrientation(orientationsString2Enum(settings.value("exportPdfOpts/orientation",
QString::fromStdString(paperOrientationsStrings[static_cast<int>(exportPdfOptions.Orientation)])).toString())); // enum map
exportPdfDialog->setShowDsnFn(settings.value("exportPdfOpts/showDsgnFN", exportPdfOptions.showDsgnFN).toBool());
exportPdfDialog->setShowDesignFilename(settings.value("exportPdfOpts/showDsgnFN", exportPdfOptions.showDesignFilename).toBool());
exportPdfDialog->setShowScale(settings.value("exportPdfOpts/showScale", exportPdfOptions.showScale).toBool());
exportPdfDialog->setShowScaleMsg(settings.value("exportPdfOpts/showScaleMsg", exportPdfOptions.showScaleMsg).toBool());
exportPdfDialog->setShowGrid(settings.value("exportPdfOpts/showGrid", exportPdfOptions.showGrid).toBool());
Expand All @@ -3029,15 +3000,15 @@ void MainWindow::actionExportPDF()

exportPdfOptions.paperSize = exportPdfDialog->getPaperSize();
exportPdfOptions.Orientation = exportPdfDialog->getOrientation();
exportPdfOptions.showDsgnFN = exportPdfDialog->getShowDsnFn();
exportPdfOptions.showDesignFilename = exportPdfDialog->getShowDesignFilename();
exportPdfOptions.showScale = exportPdfDialog->getShowScale();
exportPdfOptions.showScaleMsg = exportPdfDialog->getShowScaleMsg();
exportPdfOptions.showGrid = exportPdfDialog->getShowGrid();
exportPdfOptions.gridSize = exportPdfDialog->getGridSize();

settings.setValue("exportPdfOpts/paperSize", QString::fromStdString(paperSizeStrings[static_cast<int>(exportPdfDialog->getPaperSize())]));
settings.setValue("exportPdfOpts/orientation", QString::fromStdString(paperOrientationsStrings[static_cast<int>(exportPdfDialog->getOrientation())]));
settings.setValue("exportPdfOpts/showDsgnFN", exportPdfDialog->getShowDsnFn());
settings.setValue("exportPdfOpts/showDsgnFN", exportPdfDialog->getShowDesignFilename());
settings.setValue("exportPdfOpts/showScale", exportPdfDialog->getShowScale());
settings.setValue("exportPdfOpts/showScaleMsg", exportPdfDialog->getShowScaleMsg());
settings.setValue("exportPdfOpts/showGrid", exportPdfDialog->getShowGrid());
Expand Down
108 changes: 88 additions & 20 deletions src/io/export.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@
#include <functional>
#include <cassert>
#include <map>
#include <iostream>
#include <cstdint>
#include <memory>
#include <cstddef>
#include <fstream>
#include <vector>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/fstream.hpp>
#include <iostream>

#ifdef _WIN32
#include <io.h>
Expand All @@ -48,6 +50,78 @@
#define QUOTE(x__) # x__
#define QUOTED(x__) QUOTE(x__)

namespace {

struct FileFormatInfo {
FileFormat format;
std::string identifier;
std::string suffix;
std::string description;
};

std::unordered_map<std::string, FileFormatInfo> &identifierToInfo() {
static auto identifierToInfo = std::make_unique<std::unordered_map<std::string, FileFormatInfo>>();
return *identifierToInfo;
}

std::unordered_map<FileFormat, FileFormatInfo> &fileFormatToInfo() {
static auto fileFormatToInfo = std::make_unique<std::unordered_map<FileFormat, FileFormatInfo>>();
return *fileFormatToInfo;
}

void add_item(const FileFormatInfo& info) {

identifierToInfo()[info.identifier] = info;
fileFormatToInfo()[info.format] = info;
}

bool initialized = false;
bool initialize() {
add_item({FileFormat::ASCII_STL, "asciistl", "stl", "STL (ascii)"});
add_item({FileFormat::BINARY_STL, "binstl", "stl", "STL (binary)"});
add_item({FileFormat::OBJ, "obj", "obj", "OBJ"});
add_item({FileFormat::OFF, "off", "off", "OFF"});
add_item({FileFormat::WRL, "wrl", "wrl", "VRML"});
add_item({FileFormat::AMF, "amf", "amf", "AMF"});
add_item({FileFormat::_3MF, "3mf", "3mf", "3MF"});
add_item({FileFormat::DXF, "dxf", "dxf", "DXF"});
add_item({FileFormat::SVG, "svg", "svg", "SVG"});
add_item({FileFormat::NEFDBG, "nefdbg", "nefdbg", "nefdbg"});
add_item({FileFormat::NEF3, "nef3", "nef3", "nef3"});
add_item({FileFormat::CSG, "csg", "csg", "CSG"});
add_item({FileFormat::PARAM, "param", "param", "param"});
add_item({FileFormat::AST, "ast", "ast", "AST"});
add_item({FileFormat::TERM, "term", "term", "term"});
add_item({FileFormat::ECHO, "echo", "echo", "echo"});
add_item({FileFormat::PNG, "png", "png", "PNG"});
add_item({FileFormat::PDF, "pdf", "pdf", "PDF"});
add_item({FileFormat::POV, "pov", "pov", "POV"});

// Alias
identifierToInfo()["stl"] = identifierToInfo()["asciistl"];

return true;
}

} // namespace

namespace fileformat {

bool fromIdentifier(const std::string& suffix, FileFormat& format)
{
if (!initialized) initialized = initialize();
auto it = identifierToInfo().find(suffix);
if (it == identifierToInfo().end()) return false;
format = it->second.format;
return true;
}

const std::string& toSuffix(FileFormat& format)
{
if (!initialized) initialized = initialize();
return fileFormatToInfo()[format].suffix;
}

bool canPreview(const FileFormat format) {
return (format == FileFormat::AST ||
format == FileFormat::CSG ||
Expand All @@ -58,8 +132,8 @@ bool canPreview(const FileFormat format) {
}

bool is3D(const FileFormat format) {
return format == FileFormat::ASCIISTL ||
format == FileFormat::STL ||
return format == FileFormat::ASCII_STL ||
format == FileFormat::BINARY_STL ||
format == FileFormat::OBJ ||
format == FileFormat::OFF ||
format == FileFormat::WRL ||
Expand All @@ -76,13 +150,15 @@ bool is2D(const FileFormat format) {
format == FileFormat::PDF;
}

} // namespace FileFormat

void exportFile(const std::shared_ptr<const Geometry>& root_geom, std::ostream& output, const ExportInfo& exportInfo)
{
switch (exportInfo.format) {
case FileFormat::ASCIISTL:
case FileFormat::ASCII_STL:
export_stl(root_geom, output, false);
break;
case FileFormat::STL:
case FileFormat::BINARY_STL:
export_stl(root_geom, output, true);
break;
case FileFormat::OBJ:
Expand Down Expand Up @@ -125,7 +201,7 @@ void exportFile(const std::shared_ptr<const Geometry>& root_geom, std::ostream&
}
}

bool exportFileByNameStdout(const std::shared_ptr<const Geometry>& root_geom, const ExportInfo& exportInfo)
bool exportFileStdOut(const std::shared_ptr<const Geometry>& root_geom, const ExportInfo& exportInfo)
{
#ifdef _WIN32
_setmode(_fileno(stdout), _O_BINARY);
Expand All @@ -134,15 +210,16 @@ bool exportFileByNameStdout(const std::shared_ptr<const Geometry>& root_geom, co
return true;
}

bool exportFileByNameStream(const std::shared_ptr<const Geometry>& root_geom, const ExportInfo& exportInfo)
bool exportFileByName(const std::shared_ptr<const Geometry>& root_geom, const std::string& filename, const ExportInfo& exportInfo)
{
std::ios::openmode mode = std::ios::out | std::ios::trunc;
if (exportInfo.format == FileFormat::_3MF || exportInfo.format == FileFormat::STL || exportInfo.format == FileFormat::PDF) {
if (exportInfo.format == FileFormat::_3MF || exportInfo.format == FileFormat::BINARY_STL || exportInfo.format == FileFormat::PDF) {
mode |= std::ios::binary;
}
std::ofstream fstream(exportInfo.fileName, mode);
const boost::filesystem::path path(filename);
boost::filesystem::ofstream fstream(path, mode);
if (!fstream.is_open()) {
LOG(_("Can't open file \"%1$s\" for export"), exportInfo.displayName);
LOG(_("Can't open file \"%1$s\" for export"), filename);
return false;
} else {
bool onerror = false;
Expand All @@ -158,21 +235,12 @@ bool exportFileByNameStream(const std::shared_ptr<const Geometry>& root_geom, co
onerror = true;
}
if (onerror) {
LOG(message_group::Error, _("\"%1$s\" write error. (Disk full?)"), exportInfo.displayName);
LOG(message_group::Error, _("\"%1$s\" write error. (Disk full?)"), filename);
}
return !onerror;
}
}

bool exportFileByName(const std::shared_ptr<const Geometry>& root_geom, const ExportInfo& exportInfo)
{
if (exportInfo.useStdOut) {
return exportFileByNameStdout(root_geom, exportInfo);
} else {
return exportFileByNameStream(root_geom, exportInfo);
}
}

namespace {

double remove_negative_zero(double x) {
Expand Down
Loading

0 comments on commit d8cdc56

Please sign in to comment.