From 37e6c19a4b08733fbd665d55e498d86a6dbf2dac Mon Sep 17 00:00:00 2001 From: Kevin Martin Date: Fri, 24 Nov 2023 11:51:05 -0500 Subject: [PATCH] Add hooks so Gui classes can be seen by DXF importer --- src/Mod/Draft/importDXF.py | 16 ++++-- src/Mod/Import/App/AppImportPy.cpp | 7 +++ src/Mod/Import/Gui/AppImportGuiPy.cpp | 69 ++++++++++++++++++++++++ src/Mod/Import/Gui/CMakeLists.txt | 2 + src/Mod/Import/Gui/dxf/ImpExpDxfGui.cpp | 71 +++++++++++++++++++++++++ src/Mod/Import/Gui/dxf/ImpExpDxfGui.h | 43 +++++++++++++++ 6 files changed, 204 insertions(+), 4 deletions(-) create mode 100644 src/Mod/Import/Gui/dxf/ImpExpDxfGui.cpp create mode 100644 src/Mod/Import/Gui/dxf/ImpExpDxfGui.h diff --git a/src/Mod/Draft/importDXF.py b/src/Mod/Draft/importDXF.py index e1fbff154f..476c97a267 100644 --- a/src/Mod/Draft/importDXF.py +++ b/src/Mod/Draft/importDXF.py @@ -2800,8 +2800,12 @@ def open(filename): doc = FreeCAD.newDocument(docname) doc.Label = docname FreeCAD.setActiveDocument(doc.Name) - import Import - Import.readDXF(filename) + try: + import ImportGui + ImportGui.readDXF(filename) + except Exception: + import Import + Import.readDXF(filename) Draft.convert_draft_texts() # convert annotations to Draft texts doc.recompute() @@ -2842,8 +2846,12 @@ def insert(filename, docname): else: errorDXFLib(gui) else: - import Import - Import.readDXF(filename) + try: + import ImportGui + ImportGui.readDXF(filename) + except Exception: + import Import + Import.readDXF(filename) Draft.convert_draft_texts() # convert annotations to Draft texts doc.recompute() diff --git a/src/Mod/Import/App/AppImportPy.cpp b/src/Mod/Import/App/AppImportPy.cpp index 57ce0be34a..797f916871 100644 --- a/src/Mod/Import/App/AppImportPy.cpp +++ b/src/Mod/Import/App/AppImportPy.cpp @@ -363,6 +363,13 @@ private: return Py::None(); } + // This readDXF method is an almost exact duplicate of the one in ImportGui::Module. + // The only difference is the CDxfRead class derivation that is created. + // It would seem desirable to have most of this code in just one place, passing it + // e.g. a pointer to a function that does the 4 lines during the lifetime of the + // CDxfRead object, but right now Import::Module and ImportGui::Module cannot see + // each other's functions so this shared code would need some place to live where + // both places could include a declaration. Py::Object readDXF(const Py::Tuple& args) { char* Name = nullptr; diff --git a/src/Mod/Import/Gui/AppImportGuiPy.cpp b/src/Mod/Import/Gui/AppImportGuiPy.cpp index 3043938f41..106cb0bd3b 100644 --- a/src/Mod/Import/Gui/AppImportGuiPy.cpp +++ b/src/Mod/Import/Gui/AppImportGuiPy.cpp @@ -51,6 +51,7 @@ #include "ImportOCAFGui.h" #include "OCAFBrowser.h" +#include "dxf/ImpExpDxfGui.h" #include #include #include @@ -93,6 +94,10 @@ public: add_keyword_method("insert", &Module::insert, "insert(string,string) -- Insert the file into the given document."); + add_varargs_method("readDXF", + &Module::readDXF, + "readDXF(filename,[document,ignore_errors,option_source]): Imports a " + "DXF file into the given document. ignore_errors is True by default."); add_varargs_method("exportOptions", &Module::exportOptions, "exportOptions(string) -- Return the export options of a file type."); @@ -250,6 +255,70 @@ private: return {}; } + // This readDXF method is an almost exact duplicate of the one in Import::Module. + // The only difference is the CDxfRead class derivation that is created. + // It would seem desirable to have most of this code in just one place, passing it + // e.g. a pointer to a function that does the 4 lines during the lifetime of the + // CDxfRead object, but right now Import::Module and ImportGui::Module cannot see + // each other's functions so this shared code would need some place to live where + // both places could include a declaration. + Py::Object readDXF(const Py::Tuple& args) + { + char* Name = nullptr; + const char* DocName = nullptr; + const char* optionSource = nullptr; + std::string defaultOptions = "User parameter:BaseApp/Preferences/Mod/Draft"; + bool IgnoreErrors = true; + if (!PyArg_ParseTuple(args.ptr(), + "et|sbs", + "utf-8", + &Name, + &DocName, + &IgnoreErrors, + &optionSource)) { + throw Py::Exception(); + } + + std::string EncodedName = std::string(Name); + PyMem_Free(Name); + + Base::FileInfo file(EncodedName.c_str()); + if (!file.exists()) { + throw Py::RuntimeError("File doesn't exist"); + } + + if (optionSource) { + defaultOptions = optionSource; + } + + App::Document* pcDoc = nullptr; + if (DocName) { + pcDoc = App::GetApplication().getDocument(DocName); + } + else { + pcDoc = App::GetApplication().getActiveDocument(); + } + if (!pcDoc) { + pcDoc = App::GetApplication().newDocument(DocName); + } + + try { + // read the DXF file + ImpExpDxfReadGui dxf_file(EncodedName, pcDoc); + dxf_file.setOptionSource(defaultOptions); + dxf_file.setOptions(); + dxf_file.DoRead(IgnoreErrors); + pcDoc->recompute(); + } + catch (const Standard_Failure& e) { + throw Py::RuntimeError(e.GetMessageString()); + } + catch (const Base::Exception& e) { + throw Py::RuntimeError(e.what()); + } + return Py::None(); + } + Py::Object exportOptions(const Py::Tuple& args) { char* Name; diff --git a/src/Mod/Import/Gui/CMakeLists.txt b/src/Mod/Import/Gui/CMakeLists.txt index c5ea01a5f2..4809d9ea0a 100644 --- a/src/Mod/Import/Gui/CMakeLists.txt +++ b/src/Mod/Import/Gui/CMakeLists.txt @@ -25,6 +25,8 @@ SET(ImportGui_SRCS Command.cpp ExportOCAFGui.cpp ExportOCAFGui.h + dxf/ImpExpDxfGui.cpp + dxf/ImpExpDxfGui.h ImportOCAFGui.cpp ImportOCAFGui.h OCAFBrowser.cpp diff --git a/src/Mod/Import/Gui/dxf/ImpExpDxfGui.cpp b/src/Mod/Import/Gui/dxf/ImpExpDxfGui.cpp new file mode 100644 index 0000000000..5f46012b23 --- /dev/null +++ b/src/Mod/Import/Gui/dxf/ImpExpDxfGui.cpp @@ -0,0 +1,71 @@ +/*************************************************************************** + * Copyright (c) 2015 Yorik van Havre (yorik@uncreated.net) * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + +#ifndef _PreComp_ +#include +#if OCC_VERSION_HEX < 0x070600 +#include +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif + +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include + +#include "ImpExpDxfGui.h" + +using namespace ImportGui; + +ImpExpDxfReadGui::ImpExpDxfReadGui(std::string filepath, App::Document* pcDoc) + : ImpExpDxfRead(filepath, pcDoc) +{} + diff --git a/src/Mod/Import/Gui/dxf/ImpExpDxfGui.h b/src/Mod/Import/Gui/dxf/ImpExpDxfGui.h new file mode 100644 index 0000000000..5b8808e723 --- /dev/null +++ b/src/Mod/Import/Gui/dxf/ImpExpDxfGui.h @@ -0,0 +1,43 @@ +/*************************************************************************** + * Copyright (c) 2015 Yorik van Havre (yorik@uncreated.net) * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + +#ifndef IMPEXPDXFGUI_H +#define IMPEXPDXFGUI_H + +#include + +#include +#include + +#include + + +namespace ImportGui +{ +class ImpExpDxfReadGui: public Import::ImpExpDxfRead +{ +public: + ImpExpDxfReadGui(std::string filepath, App::Document* pcDoc); +}; +} // namespace ImportGui + +#endif // IMPEXPDXFGUI_H