1

I have written a script in Python 3.3 which reads position (x, y coordinate) and risk values from a text file and saves the resulting contour plot using matplotlib. My company needs to be able to edit the contour in AutoCAD. Unfortunately, my knowledge about AutoCAD is severely limited, and the people at my company who know AutoCAD know very little about generating contour plots.

How can I create a contour plot that can be imported in AutoCAD? My current thinking is that I should save the plot as an svg file and convert it to something AutoCAD can open, or install a plugin for AutoCAD that will allow it to open one of the formats that matplotlib can save. I have seen this question but that does not quite suit my needs.

*Edit*

I have tried saving the plot as an SVG file, opening it in Inkscape, and saving it as DXF, but it does not save the contour color information, and the task needs to be automated anyway. The contour color information is important to preserve because the color indicates the order of magnitude of risk.

Community
  • 1
  • 1
Cody Piersall
  • 8,312
  • 2
  • 43
  • 57
  • Matplotlib can export to couple of vector formats, while Autocad seems unable to read anything but it's own format. You should perhaps try some converter that can produce DXF from EPS, PDF, SVG... – theta Apr 16 '13 at 19:57
  • Thanks, @theta. I can manually convert SVG to a DXF format using [Inkscape](http://inkscape.org/), but I lose the contour colors, and I would prefer to automate the process because we could have hundreds of these to convert at a time. – Cody Piersall Apr 16 '13 at 20:02
  • IMHO, `pstoedit` (as suggested below) is great option (PS/EPS to DXF). If you don't use fonts conversion is very easy – theta Apr 16 '13 at 21:12

2 Answers2

2

If you can generate a postscript file (matplotlib can create pdf's right?) you might be able to use pstoedit from the command line to convert it to a dxf.

Alternatively, you can use Illustrator(not free) or Inkscape(free) to convert the svg to a dxf. There are some general rumblings on the internet that Inkscape will sometime turn bezier curves into straight lines, but I haven't check to see if this is still true or not.

miah
  • 10,093
  • 3
  • 21
  • 32
0

I ended up having my plotting program create a very basic Autocad script. I referred to this question about extracting x,y data from a contour plot to write the Autocad script. Here is the relevant function:

def make_autocad_script(outfile_name, contour):
    ''' 
    Creates an Autocad script which contains polylines for each contour.
    Args
    outfile_name: the name of the Autocad script file.
    contour: the contour plot that needs to be exported to Autocad.
    '''

    with open(outfile_name, 'w', newline='') as outfile:
        writer = csv.writer(outfile, delimiter=',', )
        # each collection is associated with a contour level    
        for collection in contour.collections:

            # If the contour level is never reached, then the collection will be an empty list.
            if collection:
                # Set color for contour level
                outfile.write('COLOR {}\n'.format(random.randint(1,100)))
                # Each continuous contour line in a collection is a path.
                for path in collection.get_paths():

                    vertices = path.vertices

                    # pline is an autocad command for polyline.  It interprets
                    # the next (x,y) pairs as coordinates of a line until
                    # it sees a blank line.
                    outfile.write('pline\n')
                    writer.writerows(vertices)
                    outfile.write('\n')

I send make_autocad_script the outfile and contour plot that I need, and in Autocad I import the script. This plots each contour as a random color, but that could be replaced with whatever color you wanted.

Community
  • 1
  • 1
Cody Piersall
  • 8,312
  • 2
  • 43
  • 57