1

I am developing a program, and one of the requirements is to take DXF as input. The input is limited to 2D case only. The program itself is in C++/Qt, but to test it I need some sample DXF input. The spline import is already implemented, the next step is polyline with spline fit points or control points added. I decided to use Python/ezdxf to generate such polyline, as I don't have Autocad.

My first approach was to create a spline from fit points utilizing add_spline_control_frame, then convert it to polyline. The problem is there turned out to be no conversion from spline to polyline (although I think I saw it in the docs, but cannot find it anymore).

The current approach is to make polyline by add_polyline2d(points), making each point to be with DXF flag field equal 8 (spline vertex created by spline-fitting). The problem is points need to be of type DXFVertex (docs state Vertex, but it is absent), and that type is private for ezdxf.

Please share your approaches either to the problems I've faced with ezdxf, or to the initial problem.

P.S. I tried to use LibreCAD to generate such a polyline, but it's hardly possible to make a closed polyline from spline fit points there.

4

1 回答 1

2

在 DXF R2000 中添加 SPLINE 实体之前,AutoCAD 使用了通过 POLYLINE 实体创建 B 样条线的功能。Autodesk 没有记录此功能的使用,ezdxf 也没有以任何方式推广。

如果可以,请使用 SPLINE 实体,但如果您必须使用 DXF R12 - ezdxf 中有一个帮助类来创建此类样条线,并在此处ezdxf.render.R12Spline提供使用示例。

但是你会失望 BricsCAD 和 AutoCAD 显示一个非常明显的多边形结构: 在此处输入图像描述

因为不仅控制点,而且近似曲线点都必须存储为折线点,为了获得更平滑的曲线,您必须使用许多近似点,但您也可以使用常规 POLYLINE 作为近似点。我假设控制点仅被存储以保持样条线可编辑。

我对这个主题的所有了解都记录在r12spline.py文件中。如果您找到一种更好的方法来为 DXF R12 创建平滑的 B 样条曲线,且近似点更少,请告诉我。

将 SPLINE 实体近似spline为点的示例,可以由 POLYLINE 实体使用:

bspline = spline.construction_tool()
msp.add_polyline3d(bpline.approximate(segments=20))

SPLINE 实体是一个 3D 实体,如果要将样条线压缩到 xy 平面,请移除 z 轴:

xy_pts = [p.xy for p in bpline.approximate(segments=20)]
msp.add_polyline2d(xy_pts)

# or as LWPOLYLINE entity:
msp.add_lwpolyline(xy_pts, format='xy')

于 2020-09-01T14:54:26.623 回答