Skip to content

Corrputed spline with two identical control points causes ZeroDivisionError: float division by zero #831

@jsimomaa

Description

@jsimomaa

Describe the bug
Not sure if this is a bug but I have a DXF that contains a SPLINE that has two identical control points thus resulting in division by zero. When the file is opened in QCAD the corrupted (?) SPLINE gets removed. See attached DXF files.

Here is the trace:

$ ezdxf draw splinetest.dxf 
loading file "splinetest.dxf"...
Traceback (most recent call last):
  File "/home/vscode/.local/bin/ezdxf", line 8, in <module>
    sys.exit(main())
  File "/home/vscode/.local/lib/python3.10/site-packages/ezdxf/__main__.py", line 98, in main
    run(args)
  File "/home/vscode/.local/lib/python3.10/site-packages/ezdxf/commands.py", line 390, in run
    frontend.draw_layout(layout, finalize=True)
  File "/home/vscode/.local/lib/python3.10/site-packages/ezdxf/addons/drawing/frontend.py", line 242, in draw_layout
    self.draw_entities(
  File "/home/vscode/.local/lib/python3.10/site-packages/ezdxf/addons/drawing/frontend.py", line 259, in draw_entities
    _draw_entities(self, self.ctx, entities, filter_func=filter_func)
  File "/home/vscode/.local/lib/python3.10/site-packages/ezdxf/addons/drawing/frontend.py", line 1030, in _draw_entities
    frontend.draw_entity(entity, properties)
  File "/home/vscode/.local/lib/python3.10/site-packages/ezdxf/addons/drawing/frontend.py", line 278, in draw_entity
    draw_method(entity, properties)
  File "/home/vscode/.local/lib/python3.10/site-packages/ezdxf/addons/drawing/frontend.py", line 378, in draw_curve_entity
    path = make_path(entity)
  File "/usr/local/lib/python3.10/functools.py", line 889, in wrapper
    return dispatch(args[0].__class__)(*args, **kw)
  File "/home/vscode/.local/lib/python3.10/site-packages/ezdxf/path/converter.py", line 164, in _from_spline
    tools.add_spline(path, spline.construction_tool(), level=level, reset=True)
  File "/home/vscode/.local/lib/python3.10/site-packages/ezdxf/path/tools.py", line 731, in add_spline
    curves = spline.cubic_bezier_approximation(level=level)
  File "/home/vscode/.local/lib/python3.10/site-packages/ezdxf/math/bspline.py", line 1309, in cubic_bezier_approximation
    points = list(self.points(self.approximation_params(level)))
  File "/home/vscode/.local/lib/python3.10/site-packages/ezdxf/math/bspline.py", line 1326, in approximation_params
    params = list(create_t_vector(self._control_points, "chord"))
  File "/home/vscode/.local/lib/python3.10/site-packages/ezdxf/math/parametrize.py", line 41, in distance_t_vector
    yield from _normalize_distances(list(linear_distances(fit_points)))
  File "/home/vscode/.local/lib/python3.10/site-packages/ezdxf/math/parametrize.py", line 57, in _normalize_distances
    yield s / total_length
ZeroDivisionError: float division by zero

To Reproduce

$ ezdxf draw splinetest.dxf

Version info:

$ cat /etc/os-release 
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
$ python -VV
Python 3.10.9 (main, Jan 23 2023, 22:32:48) [GCC 10.2.1 20210110]
$ pip show ezdxf
Name: ezdxf
Version: 1.0.1
Summary: A Python package to create/manipulate DXF drawings.
Home-page: https://ezdxf.mozman.at
Author: Manfred Moitzi
Author-email: me@mozman.at
License: MIT License
Location: /home/vscode/.local/lib/python3.10/site-packages
Requires: pyparsing, typing-extensions
Required-by: 

Please find attached simplified splinetest.dxf.zip DXF that triggers the error:
splinetest.dxf.zip

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions