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.
This commit is contained in:
Kevin Martin
2024-09-23 05:37:34 -04:00
committed by GitHub
parent af9097ce87
commit 980303b618

View File

@@ -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...")