How to properly extract a face data from 3DFACE type dxfs #1027
Unanswered
Erkhembayaar
asked this question in
Q&A
Replies: 2 comments
-
The This code extracts all I don't understand the from __future__ import annotations
import dataclasses
import ezdxf
from ezdxf.math import Vec3
from ezdxf.entities import Face3d
@dataclasses.dataclass
class FaceProperties:
layer: str
entity_index: int
class Mesh:
def __init__(self):
# vertex list, vertices as list of floats
self.vertices: list[list[float]] = []
# face list, where each face is a list of indices referencing list <self.vertices>
self.faces: list[list[int]] = []
# properties of each face: layer, entity index in modelspace
self.face_properties: list[FaceProperties] = []
def add_triangle(self, triangle: list[Vec3], properties: FaceProperties) -> None:
assert len(triangle) == 3
index = len(self.vertices)
self.vertices.extend(list(v) for v in triangle)
self.faces.append([index, index + 1, index + 2])
self.face_properties.append(properties)
def main():
doc = ezdxf.readfile("your.dxf")
msp = doc.modelspace()
mesh = Mesh()
for index, face in enumerate(msp):
if not isinstance(face, Face3d):
continue
properties = FaceProperties(face.dxf.layer, index)
# get all vertices of 3DFACE
face_vertices: list[Vec3] = face.wcs_vertices()
if len(face_vertices) == 4:
# divide convex quadrilateral into 2 triangles
#
# 3---2
# | /|
# | / |
# |/ |
# 0---1
#
# first triangle: 0-1-2
mesh.add_triangle(face_vertices[:3], properties)
# second triangle: 2-3-0
mesh.add_triangle(
[face_vertices[2], face_vertices[3], face_vertices[0]], properties
)
# WARNING: this does not work for concave faces
# 2
# /|
# / |
# / 3|
# 0---1
else:
mesh.add_triangle(face_vertices, properties)
if __name__ == "__main__":
main() |
Beta Was this translation helpful? Give feedback.
0 replies
-
This is simpler solution which uses the import ezdxf
from ezdxf.render import MeshBuilder
def main():
doc = ezdxf.readfile("your.dxf")
msp = doc.modelspace()
mesh = MeshBuilder()
for face3d in msp.query("3DFACE"):
mesh.add_face(face3d.wcs_vertices())
# see docs for MeshBuilder class: https://ezdxf.mozman.at/docs/render/mesh.html
# mesh_tesselation() does a proper triangulation and also works for concave faces
# but is slower than the previous solution:
if any(len(face) > 3 for face in mesh.faces):
mesh = mesh.mesh_tessellation(max_vertex_count=3)
# mesh.vertices: list[Vec3] = [Vec3, Vec3, Vec3, ...]
# mesh.faces: list[Sequence[int]] = [(int, int, int), (int, int, int), ...]
if __name__ == "__main__":
main() |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Is there any other way of extracting actual face from 3DFACE instead of generating like shown below
Beta Was this translation helpful? Give feedback.
All reactions