From f84ed59a43a4ce67ef6728c0deb056ef963ab85f Mon Sep 17 00:00:00 2001 From: Furgo <148809153+furgo16@users.noreply.github.com> Date: Fri, 13 Jun 2025 04:07:39 +0200 Subject: [PATCH] Import: DXF backend, frontent; add time measurement --- src/Mod/Draft/importDXF.py | 27 ++++++++++++++++++++------- src/Mod/Import/App/AppImportPy.cpp | 8 +++++++- src/Mod/Import/App/dxf/dxf.h | 4 ++++ src/Mod/Import/Gui/AppImportGuiPy.cpp | 7 +++++++ 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/Mod/Draft/importDXF.py b/src/Mod/Draft/importDXF.py index 84e1c8ed33..edde131cdb 100644 --- a/src/Mod/Draft/importDXF.py +++ b/src/Mod/Draft/importDXF.py @@ -54,6 +54,7 @@ import sys import os import math import re +import time import FreeCAD import Part import Draft @@ -2810,6 +2811,8 @@ def open(filename): Use local variables, not global variables. """ readPreferences() + total_start_time = time.perf_counter() + if dxfUseLegacyImporter: getDXFlibs() if dxfReader: @@ -2825,6 +2828,7 @@ def open(filename): doc = FreeCAD.newDocument(docname) doc.Label = docname FreeCAD.setActiveDocument(doc.Name) + stats = None if gui: import ImportGui stats = ImportGui.readDXF(filename) @@ -2832,8 +2836,9 @@ def open(filename): import Import stats = Import.readDXF(filename) + total_end_time = time.perf_counter() if stats: - reporter = DxfImportReporter(stats) + reporter = DxfImportReporter(stats, total_end_time - total_start_time) reporter.report_to_console() Draft.convert_draft_texts() # convert annotations to Draft texts @@ -2860,6 +2865,7 @@ def insert(filename, docname): Use local variables, not global variables. """ readPreferences() + total_start_time = time.perf_counter() try: doc = FreeCAD.getDocument(docname) except NameError: @@ -2872,6 +2878,7 @@ def insert(filename, docname): else: errorDXFLib(gui) else: + stats = None if gui: import ImportGui stats = ImportGui.readDXF(filename) @@ -2879,8 +2886,9 @@ def insert(filename, docname): import Import stats = Import.readDXF(filename) + total_end_time = time.perf_counter() if stats: - reporter = DxfImportReporter(stats) + reporter = DxfImportReporter(stats, total_end_time - total_start_time) reporter.report_to_console() Draft.convert_draft_texts() # convert annotations to Draft texts @@ -4211,8 +4219,9 @@ def readPreferences(): class DxfImportReporter: """Formats and reports statistics from a DXF import process.""" - def __init__(self, stats_dict): + def __init__(self, stats_dict, total_time=0.0): self.stats = stats_dict + self.total_time = total_time def to_console_string(self): """ @@ -4223,11 +4232,11 @@ class DxfImportReporter: lines = ["\n--- DXF Import Summary ---"] - # General Info + # General info lines.append(f"DXF Version: {self.stats.get('dxfVersion', 'Unknown')}") lines.append(f"File Encoding: {self.stats.get('dxfEncoding', 'Unknown')}") - # Scaling Info + # Scaling info file_units = self.stats.get('fileUnits', 'Not specified') source = self.stats.get('scalingSource', '') if source: @@ -4240,9 +4249,13 @@ class DxfImportReporter: final_scaling = self.stats.get('finalScalingFactor', 1.0) lines.append(f"Final Scaling: 1 DXF unit = {final_scaling:.4f} mm") + lines.append("") - import_time = self.stats.get('importTimeSeconds', 0.0) - lines.append(f"Import Time: {import_time:.4f} seconds") + # Timing + lines.append("Performance:") + cpp_time = self.stats.get('importTimeSeconds', 0.0) + lines.append(f" - C++ Import Time: {cpp_time:.4f} seconds") + lines.append(f" - Total Import Time: {self.total_time:.4f} seconds") lines.append("") # Settings diff --git a/src/Mod/Import/App/AppImportPy.cpp b/src/Mod/Import/App/AppImportPy.cpp index 91512a4a2e..362700b83c 100644 --- a/src/Mod/Import/App/AppImportPy.cpp +++ b/src/Mod/Import/App/AppImportPy.cpp @@ -48,6 +48,7 @@ #endif #endif +#include #include "dxf/ImpExpDxf.h" #include "SketchExportHelper.h" #include @@ -413,7 +414,13 @@ private: ImpExpDxfRead dxf_file(EncodedName, pcDoc); dxf_file.setOptionSource(defaultOptions); dxf_file.setOptions(); + + auto startTime = std::chrono::high_resolution_clock::now(); dxf_file.DoRead(IgnoreErrors); + auto endTime = std::chrono::high_resolution_clock::now(); + std::chrono::duration elapsed = endTime - startTime; + dxf_file.setImportTime(elapsed.count()); + pcDoc->recompute(); return dxf_file.getStatsAsPyObject(); } @@ -423,7 +430,6 @@ private: catch (const Base::Exception& e) { throw Py::RuntimeError(e.what()); } - return Py::None(); } diff --git a/src/Mod/Import/App/dxf/dxf.h b/src/Mod/Import/App/dxf/dxf.h index b6a04a1cad..8bd97937d4 100644 --- a/src/Mod/Import/App/dxf/dxf.h +++ b/src/Mod/Import/App/dxf/dxf.h @@ -862,6 +862,10 @@ public: { return m_fail; } + void setImportTime(double seconds) + { + m_stats.importTimeSeconds = seconds; + } void DoRead(bool ignore_errors = false); // this reads the file and calls the following functions virtual void StartImport() diff --git a/src/Mod/Import/Gui/AppImportGuiPy.cpp b/src/Mod/Import/Gui/AppImportGuiPy.cpp index 700f439736..0e4dc03c11 100644 --- a/src/Mod/Import/Gui/AppImportGuiPy.cpp +++ b/src/Mod/Import/Gui/AppImportGuiPy.cpp @@ -46,6 +46,7 @@ #endif #endif +#include #include "ExportOCAFGui.h" #include "ImportOCAFGui.h" #include "OCAFBrowser.h" @@ -401,7 +402,13 @@ private: ImpExpDxfReadGui dxf_file(EncodedName, pcDoc); dxf_file.setOptionSource(defaultOptions); dxf_file.setOptions(); + + auto startTime = std::chrono::high_resolution_clock::now(); dxf_file.DoRead(IgnoreErrors); + auto endTime = std::chrono::high_resolution_clock::now(); + std::chrono::duration elapsed = endTime - startTime; + dxf_file.setImportTime(elapsed.count()); + pcDoc->recompute(); return dxf_file.getStatsAsPyObject(); }