def test_write_r12(filename): with r12writer(filename) as dxf: dxf.add_line((0, 0), (17, 23)) dxf.add_arc((0, 0), radius=3, start=0, end=175) dxf.add_solid([(0, 0), (1, 0), (0, 1), (1, 1)]) dxf.add_point((1.5, 1.5)) dxf.add_polyline_2d([(5, 5), (7, 3), (7, 6)]) # 2d polyline dxf.add_polyline_2d([(5, 5), (7, 3), (7, 6)], closed=True) # closed 2d polyline dxf.add_polyline_2d( [(5, 5), (7, 3, 0.5), (7, 6)], format="xyb", start_width=0.1, end_width=0.2, ) # 2d polyline with bulge value dxf.add_polyline([(4, 3), (8, 5), (2, 4)]) # 2d as 3d polyline dxf.add_polyline([(4, 3, 2), (8, 5, 0), (2, 4, 9)], closed=True) # closed 3d polyline dxf.add_text("test the text entity", align="MIDDLE_CENTER") for i in range(CIRCLE_COUNT): dxf.add_circle((MAX_X_COORD * random(), MAX_Y_COORD * random()), radius=2) assert os.path.exists(filename)
def menger_sponge(filename, level=1, kind=0): t0 = perf_counter() sponge = MengerSponge(level=level, kind=kind).mesh() t1 = perf_counter() print(f'Build menger sponge <{kind}> in {t1 - t0:.5f}s.') with r12writer(filename) as r12: r12.add_polyface(sponge.vertices, sponge.faces, color=1) print(f'saved as "{filename}".')
def test_context_manager(filename): with pytest.raises(ValueError): with r12writer(filename) as dxf: dxf.add_line((0, 0), (17, 23)) raise ValueError() dwg = ezdxf.readfile(filename) entities = list(dwg.modelspace()) assert len(entities) == 1 assert entities[0].dxftype() == "LINE"
def perlin_mesh(filename, size=(10, 10), heigth=1, scale=1): m, n = size # rows, cols vertices = [] dx = 1. / m * scale dy = 1. / n * scale for x in range(m): # rows second for y in range(n): # cols first vertices.append((x, y, snoise2(x * dx, y * dy) * heigth)) with r12writer(filename) as r12: r12.add_polymesh(vertices, size=size, color=1) print(f'saved as "{filename}".')
def create_r12(filename: str): height = 20 with r12writer(filename) as r12: for i in range(STACK_SIZE): noise.randomize() print(f'Writing mesh #{i} as {TYPE}') generator = perlin_3dfaces if TYPE == '3DFACE' else perlin_mesh generator(r12, size=(GRID_SIZE, GRID_SIZE), heigth=height, offset=height * i * 1.5, color=(i % 254) + 1)
def test_write_and_read_binary_dxf(tmpdir_factory): filename = str(tmpdir_factory.getbasetemp().join("bin.dxf")) with r12writer(filename, fmt="bin") as dxf: dxf.add_line((0, 0), (17, 23)) doc = ezdxf.readfile(filename) line = doc.modelspace()[0] assert line.dxftype() == "LINE" assert line.dxf.start == (0, 0, 0) assert line.dxf.end == (17, 23, 0) if os.path.exists(filename): os.remove(filename)
def polymesh(filename, size=(10, 10), heigth=1): m, n = size # rows, cols dx = math.pi / m * 2 dy = math.pi / n * 2 vertices = [] for x in range(m): # rows second z1 = math.sin(dx * x) for y in range(n): # cols first z2 = math.sin(dy * y) z = z1 * z2 * heigth vertices.append((x, y, z)) with r12writer(filename) as r12: r12.add_polymesh(vertices, size=size, color=1) print(f'saved as "{filename}".')
def polylines(filename): with r12writer(filename) as r12: r12.add_polyline_2d(circle(8), color=1, closed=False) r12.add_polyline_2d( translate(circle(8), vec=(3, 0)), color=3, closed=True ) r12.add_polyline_2d( [(0, 4), (4, 4, 1), (8, 4, 0, 0.2, 0.000001), (12, 4)], format="xybse", start_width=0.1, end_width=0.1, color=5, ) print(f'saved as "{filename}".')
def test_write_r12(filename): with r12writer(filename) as dxf: dxf.add_line((0, 0), (17, 23)) dxf.add_arc((0, 0), radius=3, start=0, end=175) dxf.add_solid([(0, 0), (1, 0), (0, 1), (1, 1)]) dxf.add_point((1.5, 1.5)) dxf.add_polyline([(5, 5), (7, 3), (7, 6)]) # 2d polyline dxf.add_polyline([(4, 3, 2), (8, 5, 0), (2, 4, 9)]) # 3d polyline dxf.add_text("test the text entity", align="MIDDLE_CENTER") for i in range(CIRCLE_COUNT): dxf.add_circle((MAX_X_COORD * random(), MAX_Y_COORD * random()), radius=2) assert os.path.exists(filename)
def create_r12(filename: str, gridsize: int): with r12writer(filename) as r12: megagrid(r12, size=(gridsize, gridsize), heigth=20, scale=3, color=1)
# Copyright (c) 2018 Manfred Moitzi # License: MIT License from random import random from ezdxf.addons import r12writer MAX_X_COORD = 1000.0 MAX_Y_COORD = 1000.0 CIRCLE_COUNT = 10000 with r12writer("quick_and_dirty_dxf_r12.dxf") as dxf: dxf.add_line((0, 0), (17, 23)) dxf.add_circle((0, 0), radius=2) dxf.add_arc((0, 0), radius=3, start=0, end=175) dxf.add_solid([(0, 0), (1, 0), (0, 1), (1, 1)]) dxf.add_point((1.5, 1.5)) dxf.add_polyline([(5, 5), (7, 3), (7, 6)]) # 2d polyline dxf.add_polyline([(4, 3, 2), (8, 5, 0), (2, 4, 9)]) # 3d polyline dxf.add_text("test the text entity", align="MIDDLE_CENTER") with r12writer("many_circles.dxf") as dxf: for i in range(CIRCLE_COUNT): dxf.add_circle((MAX_X_COORD * random(), MAX_Y_COORD * random()), radius=2) LINETYPES = [ 'CONTINUOUS', 'CENTER', 'CENTERX2', 'CENTER2', 'DASHED', 'DASHEDX2', 'DASHED2',
contour_image = numpy.ones((img_height, img_width, 3), numpy.uint8) cv2.drawContours(contour_image, contours, -1, (255, 0, 0), 3) cv2.imshow("EdgeDetect Contours", contour_image) return contours #request path to folder containg input images input_path = input("Input path: ") or os.path.dirname( os.path.abspath(__file__)) #loop through the given folder exploring all subdirectories for subdir, dirs, files in os.walk(input_path): #for each file... for file in files: #create a dxf document to write to with r12writer(file + ".dxf") as doc: #read image from input file print(os.path.join(subdir, file)) raw_img = cv2.imread(os.path.join(subdir, file), cv2.IMREAD_GRAYSCALE) #check if input file is valid image and display if raw_img is None: continue cv2.imshow("Raw", raw_img) #apply the two contour detection methods threshold_contours = threshold(raw_img) edgedetect_contours = edgedetect(raw_img) #plot threshold contour points onto layer 0 of the dxf for thresh_contour in threshold_contours: points_list = [] for points in thresh_contour:
# Copyright (c) 2018-2020 Manfred Moitzi # License: MIT License from random import random from pathlib import Path from ezdxf.addons import r12writer OUTBOX = Path("~/Desktop/Outbox").expanduser() MAX_X_COORD = 1000.0 MAX_Y_COORD = 1000.0 CIRCLE_COUNT = 10000 with r12writer(OUTBOX / "quick_and_dirty_dxf_r12.dxf") as dxf: dxf.add_line((0, 0), (17, 23)) dxf.add_circle((0, 0), radius=2) dxf.add_arc((0, 0), radius=3, start=0, end=175) dxf.add_solid([(0, 0), (1, 0), (0, 1), (1, 1)]) dxf.add_point((1.5, 1.5)) dxf.add_polyline([(5, 5), (7, 3), (7, 6)]) # 2d polyline dxf.add_polyline([(4, 3, 2), (8, 5, 0), (2, 4, 9)]) # 3d polyline dxf.add_text("test the text entity", align="MIDDLE_CENTER") with r12writer(OUTBOX / "many_circles.dxf") as dxf: for i in range(CIRCLE_COUNT): dxf.add_circle((MAX_X_COORD * random(), MAX_Y_COORD * random()), radius=2) with r12writer(OUTBOX / "many_circles_bin.dxf", fmt="bin") as dxf: for i in range(CIRCLE_COUNT): dxf.add_circle((MAX_X_COORD * random(), MAX_Y_COORD * random()), radius=2)
from pathlib import Path import random import ezdxf from ezdxf.addons import r12writer from ezdxf.tools.standards import setup_dimstyle DIR = Path("~/Desktop/Outbox").expanduser() XSIZE, YSIZE = (100, 100) COUNT = 1000 POINTS = [(random.random() * XSIZE, random.random() * YSIZE) for _ in range(COUNT)] XREF = "points.dxf" # scale 1:1000; text size H = 2.5mm on paper; 1 drawing unit = 1m DIMSTYLE = "USR_M_1000_H25_M" with r12writer(str(DIR / XREF)) as r12: for point in POINTS: r12.add_point(point, layer="Points") doc = ezdxf.new("R2000", setup=True) doc.add_xref_def(filename=".\\" + XREF, name="points_xref") msp = doc.modelspace() msp.add_blockref(name="points_xref", insert=(0, 0)) setup_dimstyle(doc, fmt=DIMSTYLE, style=ezdxf.options.default_dimension_text_style) msp.add_aligned_dim(p1=(0, 0), p2=(0, 100), distance=5, dimstyle=DIMSTYLE) msp.add_aligned_dim(p1=(0, 100), p2=(100, 100), distance=5, dimstyle=DIMSTYLE) doc.set_modelspace_vport(height=YSIZE * 1.25, center=(XSIZE / 2, YSIZE / 2)) doc.saveas(DIR / "host.dxf")
def polyface_sphere(filename): mesh = sphere(128, 64, quads=True) with r12writer(filename) as r12: r12.add_polyface(mesh.vertices, mesh.faces, color=1) print(f'saved as "{filename}".')