pythondxfezdxf

MESH created with ezdxf crashed Autodesk Viewer


Here is my code:

import ezdxf
import numpy as np

x,y = np.meshgrid(range(-3,3),range(-3,3))
z = x**2 + y**2

doc = ezdxf.new('R2010', setup=True)

# Add a mesh entity
msp = doc.modelspace()

rows, cols = z.shape

# Add a MESH entity
mesh = msp.add_mesh()

msp.doc.layers.add("3d_surface")
mesh = msp.add_mesh(dxfattribs={"layer": "3d_surface"})

with mesh.edit_data() as mesh_data:
    # Create vertices from X, Y, and dem_data (Z)
    for i in range(rows):
        for j in range(cols):
            mesh_data.vertices.append((x[i, j], y[i, j], z[i, j]))

    # Create faces (each grid cell becomes a quadrilateral face)
    for i in range(rows - 1):
        for j in range(cols - 1):
            # Vertex indices for the four corners of the grid cell
            v0 = i * cols + j          # Top-left
            v1 = v0 + 1                # Top-right
            v2 = v0 + cols             # Bottom-left
            v3 = v2 + 1                # Bottom-right

            # Add a quadrilateral face
            mesh_data.faces.append((v0, v1, v3, v2))
    
dxf_file_path = 'example.dxf'
doc.saveas(dxf_file_path)

The created file can be read correctly by edrawings and on sharecad.org but not by freecad or autodesk.

Autodesk viewer gives the following:

AutoCAD-InvalidFile Sorry, the drawing file is invalid and cannot be viewed. - Please try to recover the file in AutoCAD, and upload it again to view.

TranslationWorker-InternalFailure Unrecoverable exit code from extractor: -1073741831

Am I doing something wrong?


Solution

  • Just to earn (maybe) some reputation points 😁:

    As already said in issue 1219 in the ezdxf repo:

    The mesh entity created first does not have any vertices and faces and such empty MESH entities are invalid for AutoCAD and BricsCAD.

    The next version of ezdxf will not export such empty MESH entities.