def export_path(path):
    doc = ezdxf.new()
    msp = doc.modelspace()
    bbox = BoundingBox(path)
    msp.add_polyline3d(path, dxfattribs={"layer": "Path", "color": 2})
    for curve in cubic_bezier_interpolation(path):
        msp.add_polyline3d(curve.approximate(20),
                           dxfattribs={
                               "layer": "Bézier",
                               "color": 1
                           })
    doc.set_modelspace_vport(center=bbox.center, height=bbox.size[1])
    doc.saveas(DIR / "path1.dxf")
Esempio n. 2
0
def test_vertex_interpolation():
    points = [(0, 0), (3, 1), (5, 3), (0, 8)]
    result = list(cubic_bezier_interpolation(points))
    assert len(result) == 3
    c1, c2, c3 = result
    p = c1.control_points
    assert p[0].isclose((0, 0))
    assert p[1].isclose((0.9333333333333331, 0.3111111111111111))
    assert p[2].isclose((1.8666666666666663, 0.6222222222222222))
    assert p[3].isclose((3, 1))

    p = c2.control_points
    assert p[0].isclose((3, 1))
    assert p[1].isclose((4.133333333333334, 1.3777777777777778))
    assert p[2].isclose((5.466666666666667, 1.822222222222222))
    assert p[3].isclose((5, 3))

    p = c3.control_points
    assert p[0].isclose((5, 3))
    assert p[1].isclose((4.533333333333333, 4.177777777777778))
    assert p[2].isclose((2.2666666666666666, 6.088888888888889))
    assert p[3].isclose((0, 8))
#
# http://help.autodesk.com/view/OARX/2018/ENU/?guid=OREF-AcDbSpline__setFitData_AcGePoint3dArray__AcGeVector3d__AcGeVector3d__AcGe__KnotParameterization_int_double
# Remark in the AutoCAD ObjectARX reference for AcDbSpline about construction
# of a B-spline from fit points:
# degree has no effect. A spline with degree=3 is always constructed when
# interpolating a series of fit points.
# Sadly this works only for short simple splines.

doc, msp = setup()
msp.add_spline(points,
               degree=2,
               dxfattribs={
                   'layer': 'BricsCAD B-spline',
                   'color': 2
               })
bezier_curves = cubic_bezier_interpolation(points)
s = bezier_to_bspline(bezier_curves)
msp.add_spline(dxfattribs={
    'color': 6,
    'layer': 'Cubic Bezier Curve Interpolation'
}).apply_construction_tool(s)

zoom.extents(msp)
doc.saveas(DIR / 'concept-4-cubic-bezier-curves.dxf')

# ----------------------------------------------------------------------------
# A better way to create a SPLINE defined by control vertices from fit points
# without given end tangents for SHORT B-splines:
# ----------------------------------------------------------------------------
doc, msp = setup()
def profile_bezier_interpolation(count, path):
    for _ in range(count):
        curves = list(cubic_bezier_interpolation(path))