from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeWire
from OCC.Core.gp import gp_Pnt
from OCC.Core.TopExp import TopExp_Explorer
from OCC.Core.TopAbs import TopAbs_EDGE
from OCC.Core.BRep import BRep_Tool
from OCC.Core.Geom import Geom_Line
import ezdxf
def shape_to_dxf(shape, filename):
"""
Export the edges to dxf
"""
doc = ezdxf.new(dxfversion='R2010')
msp = doc.modelspace()
explorer = TopExp_Explorer(shape, TopAbs_EDGE)
while explorer.More():
edge = explorer.Current()
curve_handle, first, last = BRep_Tool.Curve(edge)
# Check if there is a line
if isinstance(curve_handle, Geom_Line):
# Extrage punctele
start_point = curve_handle.Value(first)
end_point = curve_handle.Value(last)
# Add the line in DXF
msp.add_line((start_point.X(), start_point.Y()), (end_point.X(), end_point.Y()))
explorer.Next()
doc.saveas(filename)
# Create a 2D shape
p1 = gp_Pnt(0, 0, 0)
p2 = gp_Pnt(10, 0, 0)
p3 = gp_Pnt(10, 10, 0)
p4 = gp_Pnt(0, 10, 0)
edge1 = BRepBuilderAPI_MakeEdge(p1, p2).Edge()
edge2 = BRepBuilderAPI_MakeEdge(p2, p3).Edge()
edge3 = BRepBuilderAPI_MakeEdge(p3, p4).Edge()
edge4 = BRepBuilderAPI_MakeEdge(p4, p1).Edge()
wire = BRepBuilderAPI_MakeWire(edge1, edge2, edge3, edge4).Wire()
def draw_rectangle_in_dxf(a, b, c, d, filename):
# Create a new DXF file
doc = ezdxf.new(dxfversion='R2010')
msp = doc.modelspace()
# Add lines to the DXF file
msp.add_line((p1.X(), p1.Y()), (p2.X(), p2.Y()))
msp.add_line((p2.X(), p2.Y()), (p3.X(), p3.Y()))
msp.add_line((p3.X(), p3.Y()), (p4.X(), p4.Y()))
msp.add_line((p4.X(), p4.Y()), (p1.X(), p1.Y()))
# Save DXF
doc.saveas(filename)
# Give a name to the DXF file
filename = "rectangle.dxf"
draw_rectangle_in_dxf(p1, p2, p3, p4, filename)
print(f"Fișierul DXF cu dreptunghiul a fost creat: {filename}")
#dxf line to pythonocc algorithm
import ezdxf
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge
from OCC.Core.gp import gp_Pnt
from OCC.Display.SimpleGui import init_display
# Funcție pentru a citi o linie DXF și a o converti într-o muchie pythonOCC
def dxf_line_to_occ_edge(dxf_filename):
# Citim fișierul DXF
doc = ezdxf.readfile(dxf_filename)
msp = doc.modelspace()
# Căutăm prima entitate de tip linie
line = None
for entity in msp:
if entity.dxftype() == 'LINE':
line = entity
break
if line is None:
raise ValueError("Nu s-a găsit nicio linie în fișierul DXF")
# Obținem coordonatele punctelor de început și sfârșit ale liniei
start_point = gp_Pnt(line.dxf.start.x, line.dxf.start.y, line.dxf.start.z)
end_point = gp_Pnt(line.dxf.end.x, line.dxf.end.y, line.dxf.end.z)
# Creăm muchia folosind pythonOCC
edge = BRepBuilderAPI_MakeEdge(start_point, end_point).Edge()
return edge
# Initialize the display
display, start_display, add_menu, add_function_to_menu = init_display()
# Convertim linia DXF într-o muchie pythonOCC
dxf_filename = "test.dxf"
edge = dxf_line_to_occ_edge(dxf_filename)
# Afișăm muchia în viewer-ul pythonOCC
display.DisplayShape(edge, update=True)
# Pornim afișarea
start_display()
Urmatorul cod citeste vectorii poliliniilor inchise (dreptunghiuri, triunghiuri samd)
import ezdxf
doc = ezdxf.readfile("test.dxf")
msp = doc.modelspace()
for line in msp.query("LWPOLYLINE"):
with line.points("xyseb") as points:
# points is a standard Python list
# existing points are 5-tuples, but new points can be
# set as (x, y, [start_width, [end_width, [bulge]]]) tuple
# set start_width, end_width to 0 to be ignored (x, y, 0, 0, bulge).
print(points)
Aici citeste cam tot ce este format din linii drepte. Cercurile nu functioneaza.
import ezdxf
from OCC.Core.AIS import AIS_Shape
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeWire
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakePrism
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeFace
from OCC.Core.gp import gp_Pnt, gp_Vec, gp_Dir, gp_Ax2, gp_Circ
from OCC.Display.SimpleGui import init_display
def read_dxf(file_path):
doc = ezdxf.readfile(file_path)
shapes = []
for entity in doc.modelspace().query('LINE CIRCLE ARC LWPOLYLINE POLYLINE'):
if entity.dxftype() == 'LINE':
start = entity.dxf.start
end = entity.dxf.end
p1 = gp_Pnt(start.x, start.y, start.z)
p2 = gp_Pnt(end.x, end.y, end.z)
edge = BRepBuilderAPI_MakeEdge(p1, p2).Edge()
shapes.append(edge)
elif entity.dxftype() == 'CIRCLE':
center = entity.dxf.center
radius = entity.dxf.radius
gp_circle = gp_Circ(gp_Ax2(gp_Pnt(center.x, center.y, center.z), gp_Dir(0, 0, 1)), radius)
edge = BRepBuilderAPI_MakeEdge(gp_circle).Edge()
shapes.append(edge)
elif entity.dxftype() == 'ARC':
center = entity.dxf.center
radius = entity.dxf.radius
start_angle = entity.dxf.start_angle
end_angle = entity.dxf.end_angle
gp_circle = gp_Circ(gp_Ax2(gp_Pnt(center.x, center.y, center.z), gp_Dir(0, 0, 1)), radius)
edge = BRepBuilderAPI_MakeEdge(gp_circle, start_angle, end_angle).Edge()
shapes.append(edge)
elif entity.dxftype() == 'LWPOLYLINE':
points = [gp_Pnt(point[0], point[1], 0) for point in entity]
wire_builder = BRepBuilderAPI_MakeWire()
for i in range(len(points) - 1):
try:
edge = BRepBuilderAPI_MakeEdge(points[i], points[i + 1]).Edge()
wire_builder.Add(edge)
except RuntimeError:
print(f"Could not create edge between points {points[i]} and {points[i+1]}")
if entity.is_closed or points[0].IsEqual(points[-1], 1e-7):
try:
edge = BRepBuilderAPI_MakeEdge(points[-1], points[0]).Edge()
wire_builder.Add(edge)
wire = wire_builder.Wire()
if wire.Closed():
face = BRepBuilderAPI_MakeFace(wire).Face()
prism = BRepPrimAPI_MakePrism(face, gp_Vec(0, 0, 300)).Shape()
shapes.append(prism)
except RuntimeError:
print(f"Could not create closing edge between points {points[-1]} and {points[0]}")
else:
shapes.append(wire_builder.Wire())
elif entity.dxftype() == 'POLYLINE':
points = [gp_Pnt(vertex.dxf.location.x, vertex.dxf.location.y, vertex.dxf.location.z) for vertex in entity.vertices]
wire_builder = BRepBuilderAPI_MakeWire()
for i in range(len(points) - 1):
try:
edge = BRepBuilderAPI_MakeEdge(points[i], points[i + 1]).Edge()
wire_builder.Add(edge)
except RuntimeError:
print(f"Could not create edge between points {points[i]} and {points[i+1]}")
if entity.is_closed or points[0].IsEqual(points[-1], 1e-7):
try:
edge = BRepBuilderAPI_MakeEdge(points[-1], points[0]).Edge()
wire_builder.Add(edge)
wire = wire_builder.Wire()
if wire.Closed():
face = BRepBuilderAPI_MakeFace(wire).Face()
prism = BRepPrimAPI_MakePrism(face, gp_Vec(0, 0, 300)).Shape()
shapes.append(prism)
except RuntimeError:
print(f"Could not create closing edge between points {points[-1]} and {points[0]}")
else:
shapes.append(wire_builder.Wire())
return shapes
def display_shapes(shapes):
display, start_display, add_menu, add_function_to_menu = init_display()
for shape in shapes:
ais_shape = AIS_Shape(shape)
display.Context.Display(ais_shape, True)
start_display()
if __name__ == "__main__":
dxf_file_path = 'test.dxf'
shapes = read_dxf(dxf_file_path)
display_shapes(shapes)