From 980303b6186c8997b0a9385cb74ae7d4e80a4331 Mon Sep 17 00:00:00 2001 From: Kevin Martin Date: Mon, 23 Sep 2024 05:37:34 -0400 Subject: [PATCH] DXF: Remove O(n^2) time on DXF import with the legacy importer (#16611) * Remove O(n^2) time on DXF import Accumulates the contents of each layer in a local Python list, then at the end assignes all the objects at once into the layer. This avoids a very slow process of traversing the objects so far and (re-)updating their properties each time a new object is added. Fixes #16604 * Correct last change to not damage existing layer contents. The layer could already have contents if the DXF file is being Imported rather than Opened. The original change would lose the previous layer contents. --- src/Mod/Draft/importDXF.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Mod/Draft/importDXF.py b/src/Mod/Draft/importDXF.py index 2222f20e1d..8baee72332 100644 --- a/src/Mod/Draft/importDXF.py +++ b/src/Mod/Draft/importDXF.py @@ -1927,11 +1927,24 @@ def addObject(shape, name="Shape", layer=None): if layer: lay = locateLayer(layer) # For old style layers, which are just groups - if hasattr(lay, "addObject"): - lay.addObject(newob) + if hasattr(lay, "Group"): + pass # For new Draft Layers - elif hasattr(lay, "Proxy") and hasattr(lay.Proxy, "addObject"): - lay.Proxy.addObject(lay, newob) + elif hasattr(lay, "Proxy") and hasattr(lay.Proxy, "Group"): + lay = lay.Proxy + else: + lay = None + + if lay != None: + if lay not in layerObjects: + l = [] + layerObjects[lay] = l + else: + l = layerObjects[lay] + l.append(newob) + + + formatObject(newob) return newob @@ -2227,6 +2240,8 @@ def processdxf(document, filename, getShapes=False, reComputeFlag=True): badobjects = [] global layerBlocks layerBlocks = {} + global layerObjects + layerObjects = {} sketch = None shapes = [] @@ -2724,6 +2739,10 @@ def processdxf(document, filename, getShapes=False, reComputeFlag=True): formatObject(newob, insert) num += 1 + # Move layer contents to layers + for (l, contents) in layerObjects.items(): + l.Group += contents + # Make blocks, if any if dxfMakeBlocks: print("creating layerblocks...")