【问题标题】:Can I convert a contour plot in matplotlib to a format that can be imported and edited in AutoCAD?我可以将 matplotlib 中的等高线图转换为可以在 AutoCAD 中导入和编辑的格式吗?
【发布时间】:2013-07-02 05:55:08
【问题描述】:

我在 Python 3.3 中编写了一个脚本,它从文本文件中读取位置(x,y 坐标)和风险值,并使用 matplotlib 保存生成的等高线图。我的公司需要能够在 AutoCAD 中编辑轮廓。不幸的是,我对 AutoCAD 的了解非常有限,而且我公司中了解 AutoCAD 的人员对生成等高线图知之甚少。

如何创建可在 AutoCAD 中导入的等高线图?我目前的想法是,我应该将绘图保存为 svg 文件并将其转换为 AutoCAD 可以打开的文件,或者为 AutoCAD 安装一个插件,使其能够打开 matplotlib 可以保存的格式之一。我见过this question,但这不太适合我的需要。

*编辑*

我已经尝试将绘图保存为 SVG 文件,在 Inkscape 中打开它,并将其保存为 DXF,但它不保存轮廓颜色信息,并且无论如何任务都需要自动化。保留轮廓颜色信息很重要,因为颜色表示风险的数量级。

【问题讨论】:

  • Matplotlib 可以导出为几种矢量格式,而 Autocad 似乎除了它自己的格式之外无法读取任何内容。您也许应该尝试一些可以从 EPS、PDF、SVG 生成 DXF 的转换器......
  • 谢谢,@theta。我可以使用 Inkscape 手动将 SVG 转换为 DXF 格式,但我会丢失轮廓颜色,我更愿意自动化该过程,因为我们一次可以转换数百个。
  • 恕我直言,pstoedit(如下所示)是不错的选择(PS/EPS 到 DXF)。如果不使用字体转换很容易

标签: python-3.x matplotlib autocad


【解决方案1】:

如果你可以生成 postscript 文件(matplotlib 可以创建 pdf 的对吗?)你也许可以在命令行中使用 pstoedit 将其转换为 dxf。

或者,您可以使用 Illustrator(非免费)或 Inkscape(免费)将 svg 转换为 dxf。互联网上有一些普遍的谣言说 Inkscape 有时会将贝塞尔曲线变成直线,但我还没有检查这是否仍然正确。

【讨论】:

  • +1 表示我以前从未听说过的事情。我刚试过,还没有弄明白,但也许我很快就能搞定。
【解决方案2】:

我最终让我的绘图程序创建了一个非常基本的 Autocad 脚本。我提到了this question,关于从等高线图中提取 x,y 数据以编写 Autocad 脚本。下面是相关函数:

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')

我发送 make_autocad_script 我需要的 outfilecontour 图,然后在 Autocad 中导入脚本。这会将每个轮廓绘制为随机颜色,但可以用您想要的任何颜色替换。

【讨论】:

    猜你喜欢
    • 2013-02-17
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多