From de37e94f32a7dd47e0d7784d57e4c5bcf32e4721 Mon Sep 17 00:00:00 2001 From: Roy-043 Date: Sat, 27 Apr 2024 23:12:01 +0200 Subject: [PATCH] Draft: legacy DXF importer fix group and layer behavior Fixes #13621. Currently the legacy DXF importer creates a main group with the DXF name if a DXF is imported into an existing FreeCAD document. For each DXF layer it then creates a sub-group (or Draft Layer) inside that main group. Objects are nested in the sub-groups (or Draft Layers). This does not make sense for Draft Layers as they should be nested in the LayerContainer, which should be in the root of the FreeCAD document. Both for Draft Layers and sub-groups there is the issue that if mutliple DXF files are imported in the same FreeCAD document, exising Draft Layers and sub-groups are not taken into account. For each DXF a new "0" group/layer is created (with a Label that is not "0" and therefore not recognized), and if there are objects on that layer a new group/layer is created for each object (labelled "0001", "0002", "0003", etc). To solve this the main group with the DXF name is no longer created in the revised code and the layers variable is initialized with the groups/layers that already exist in the FreeCAD document. Additionally the formatObject function should not format objects that are in Draft Layers. Its action conflicts with the layer mechanism resulting in the last imported object having an incorrect color. --- src/Mod/Draft/importDXF.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Mod/Draft/importDXF.py b/src/Mod/Draft/importDXF.py index f8eeedcc72..0cd56ec1d1 100644 --- a/src/Mod/Draft/importDXF.py +++ b/src/Mod/Draft/importDXF.py @@ -626,7 +626,10 @@ def getColor(): def formatObject(obj, dxfobj=None): """Apply text and line color to an object from a DXF object. - This function only works when the graphical user interface is loaded + If `dxfUseDraftVisGroups` is `True` the function returns immediately. + The color of the object then depends on the Draft Layer the object is in. + + Else this function only works if the graphical user interface is loaded as it needs access to the `ViewObject` attribute of the objects. If `dxfobj` and the global variable `dxfGetColors` exist @@ -652,6 +655,9 @@ def formatObject(obj, dxfobj=None): ----- Use local variables, not global variables. """ + if dxfUseDraftVisGroups: + return + if dxfGetColors and dxfobj and hasattr(dxfobj, "color_index"): if hasattr(obj.ViewObject, "TextColor"): if dxfobj.color_index == 256: @@ -2208,7 +2214,8 @@ def processdxf(document, filename, getShapes=False, reComputeFlag=True): global resolvedScale resolvedScale = getScaleFromDXF(drawing.header) * dxfScaling global layers - layers = [] + typ = "Layer" if dxfUseDraftVisGroups else "App::DocumentObjectGroup" + layers = [o for o in FreeCAD.ActiveDocument.Objects if Draft.getType(o) == typ] global doc doc = document global blockshapes @@ -2838,11 +2845,7 @@ def insert(filename, docname): if dxfUseLegacyImporter: getDXFlibs() if dxfReader: - groupname = os.path.splitext(os.path.basename(filename))[0] - importgroup = doc.addObject("App::DocumentObjectGroup", groupname) processdxf(doc, filename) - for l in layers: - importgroup.addObject(l) else: errorDXFLib(gui) else: