File size: 1,956 Bytes
fc7d689
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""Geometry export functions for structural form-finding results.

Exports mesh geometry and force density data to standard formats
importable by CAD and structural analysis software.
"""

from __future__ import annotations

import numpy as np


def export_dxf(
    path: str,
    xyz: np.ndarray,
    edges: np.ndarray,
) -> None:
    """Export centerline wireframe as DXF.

    Each structural member is written as a LINE entity on layer STRUCTURE.
    Importable in AutoCAD, ETABS, SAP2000, Robot Structural, Rhino.

    Parameters
    ----------
    path : str
        Output file path (.dxf).
    xyz : ndarray (N, 3)
        Vertex positions.
    edges : ndarray (E, 2)
        Edge connectivity.
    """
    try:
        import ezdxf
    except ImportError:
        raise ImportError(
            "ezdxf is required for DXF export. "
            "Install with: pip install ezdxf"
        )

    pts = np.asarray(xyz).reshape(-1, 3)
    doc = ezdxf.new("R2010")
    msp = doc.modelspace()

    doc.layers.new("STRUCTURE", dxfattribs={"color": 7})

    for e in edges:
        i, j = int(e[0]), int(e[1])
        p1 = tuple(float(v) for v in pts[i])
        p2 = tuple(float(v) for v in pts[j])
        msp.add_line(p1, p2, dxfattribs={"layer": "STRUCTURE"})

    doc.saveas(path)


def export_obj(
    path: str,
    xyz: np.ndarray,
    faces: np.ndarray,
) -> None:
    """Export mesh geometry as Wavefront OBJ.

    Parameters
    ----------
    path : str
        Output file path (.obj).
    xyz : ndarray (N, 3)
        Vertex positions.
    faces : ndarray (F, 3 or 4)
        Face vertex indices (0-based).
    """
    pts = np.asarray(xyz).reshape(-1, 3)
    with open(path, "w") as f:
        f.write("# VAE-FDM exported mesh\n")
        for v in pts:
            f.write(f"v {v[0]:.6f} {v[1]:.6f} {v[2]:.6f}\n")
        for face in faces:
            indices = " ".join(str(int(i) + 1) for i in face)
            f.write(f"f {indices}\n")