Search code examples
pythoncaddxf

how to quickly create large dxf from geometrical shapes


I have a 2D pattern consisting of ~10 million circles, stored in the form of a list

[[x, y, radius], [x, y, radius], ...]

I want to turn it into a DXF (a common CAD file format). I tried dxfwrite and ezdxf and they both work but very slowly. (If I extrapolate from smaller tests, dxfwrite would take ~12 hours and ezdxf ~4 hours.)

Is there any way to do this substantially faster?

(The list is in Python to start with, but I don't mind exporting to a text file and then using a different program.)


Solution

  • For just circles you can use the simple DXF R12 format with just an ENTITIES section:

      0
    SECTION
      2
    ENTITIES
      0
    CIRCLE
      8
    0
     10
    {x-coord}
     20
    {y-coord}
     40
    {radius}
      0
    ENDSEC
      0
    EOF
    

    The following Python script creates a DXF file with 10.000.000 circles in less than a minute on a CAD workstation. AutoCAD needs less than 3 minutes to open it, but AutoCAD is not responsive anymore.

    from random import random
    
    MAX_X_COORD = 1000.0
    MAX_Y_COORD = 1000.0
    MAX_R = 1.0
    CIRCLE_COUNT = 10000000
    
    def dxf_circle(x, y, r):
        return "0\nCIRCLE\n8\n0\n10\n{x:.3f}\n20\n{y:.3f}\n40\n{r:.2f}\n".format(x=x, y=y, r=r)
    
    with open("circles.dxf", 'wt') as f:
        f.write("0\nSECTION\n2\nENTITIES\n")
        for i in range(CIRCLE_COUNT):
            f.write(dxf_circle(MAX_X_COORD*random(), MAX_Y_COORD*random(), MAX_R*random()))
        f.write("0\nENDSEC\n0\nEOF\n")