You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

开发3D打印软件:如何获取DXF、STL格式3D模型的XYZ坐标?

Hey there! Let's break this down into two parts—handling DXF files and STL files, since they store 3D data in totally different ways. I've tackled this exact problem for a 3D printing workflow before, so here's what worked for me:

Extracting 3D Coordinates from DXF Files

The issue with basic DXF parsers is that they often only handle 2D elements by default. Most 3D objects in DXF are stored as 3D polylines, meshes, or 3D faces—you need to target these specific entity types explicitly. My go-to library for this is ezdxf, which has excellent support for 3D DXF entities.

First, install the library:

pip install ezdxf

Then use this script to extract coordinates:

import ezdxf

def get_dxf_3d_coords(file_path):
    # Load the DXF document
    doc = ezdxf.readfile(file_path)
    model_space = doc.modelspace()
    coords = []

    # Extract vertices from 3D polylines
    for polyline in model_space.query('POLYLINE[is_3d_polyline==True]'):
        for vertex in polyline.vertices:
            coords.append((vertex.dxf.x, vertex.dxf.y, vertex.dxf.z))
    
    # Extract vertices from mesh entities
    for mesh in model_space.query('MESH'):
        for vertex in mesh.vertices:
            coords.append((vertex.x, vertex.y, vertex.z))
    
    # Extract vertices from 3D faces
    for face in model_space.query('3DFACE'):
        # Add all four vertices (some 3DFaces only use 3)
        coords.append((face.dxf.vtx0.x, face.dxf.vtx0.y, face.dxf.vtx0.z))
        coords.append((face.dxf.vtx1.x, face.dxf.vtx1.y, face.dxf.vtx1.z))
        coords.append((face.dxf.vtx2.x, face.dxf.vtx2.y, face.dxf.vtx2.z))
        if face.dxf.vtx3 is not None:
            coords.append((face.dxf.vtx3.x, face.dxf.vtx3.y, face.dxf.vtx3.z))

    return coords

# Example usage
dxf_points = get_dxf_3d_coords("your_3d_object.dxf")
print(f"Extracted {len(dxf_points)} 3D coordinate points")

Key note: If your DXF uses ACIS solid entities (like complex 3D parts), you might need additional processing, but most 3D printing-ready DXFs use the simpler entities targeted above.

Extracting 3D Coordinates from STL Files

STL files are much simpler—they're just collections of triangular faces, each with 3 vertices. For this, numpy-stl is a lightweight, reliable option.

Install the library first:

pip install numpy-stl

Use this script to pull out coordinates:

from stl import mesh

def get_stl_3d_coords(file_path):
    # Load the STL mesh
    stl_mesh = mesh.Mesh.from_file(file_path)
    coords = []

    # Each face in the mesh has 3 vertices
    for face in stl_mesh.vectors:
        for vertex in face:
            coords.append((vertex[0], vertex[1], vertex[2]))
    
    # Optional: Remove duplicate vertices (since adjacent faces share points)
    # import numpy as np
    # unique_coords = np.unique(coords, axis=0).tolist()
    # return unique_coords

    return coords

# Example usage
stl_points = get_stl_3d_coords("your_3d_object.stl")
print(f"Extracted {len(stl_points)} 3D coordinate points")

Quick tip: STL files don't store unit information, so make sure you confirm the units (mm, inches, etc.) match your 3D printing software's requirements.

内容的提问来源于stack exchange,提问作者Keke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:17:27