只扫描CAD DXF文件的选定区域

您所在的位置:网站首页 cad的公众号 只扫描CAD DXF文件的选定区域

只扫描CAD DXF文件的选定区域

2023-05-12 06:30| 来源: 网络整理| 查看: 265

此解决方案以4块形式呈现DXF文件,包括过滤呈现区域外的DXF实体。但是,包围框的计算也很昂贵,并且重叠区域中的实体被多次渲染,这意味着这种解决方案作为一次遍历渲染所需的时间更长。但它显示了这个概念。这些图像完美地结合在一起,留下的空间表明这是4幅图像:

import matplotlib.pyplot as plt import random import ezdxf from ezdxf.addons.drawing import RenderContext, Frontend from ezdxf.addons.drawing.matplotlib import MatplotlibBackend from ezdxf import bbox from ezdxf.math import BoundingBox2d COLORS = list(range(1, 7)) DPI = 300 WIDTH = 400 HEIGHT = 200 LEFT = 0 BOTTOM = 0 doc = ezdxf.new() msp = doc.modelspace() def random_points(count): for _ in range(count): yield WIDTH * random.random(), HEIGHT * random.random() for s, e in zip(random_points(100), random_points(100)): msp.add_line(s, e, dxfattribs={"color": random.choice(COLORS)}) # detecting the drawing extents by ezdxf can take along time for big files! cache = bbox.Cache() # reuse bounding boxes for entity filtering rect = bbox.extents(msp, cache=cache) WIDTH = rect.size.x HEIGHT = rect.size.y LEFT = rect.extmin.x BOTTOM = rect.extmin.y VIEWPORT_X = [LEFT, LEFT + WIDTH / 2, LEFT, LEFT + WIDTH / 2] VIEWPORT_Y = [BOTTOM, BOTTOM, BOTTOM + HEIGHT / 2, BOTTOM + HEIGHT / 2] ctx = RenderContext(doc) for quarter in [0, 1, 2, 3]: # setup drawing add-on: fig = plt.figure(dpi=300) ax = fig.add_axes([0, 0, 1, 1]) out = MatplotlibBackend(ax) # calculate and set render borders: left = VIEWPORT_X[quarter] bottom = VIEWPORT_Y[quarter] ax.set_xlim(left, left + WIDTH / 2) ax.set_ylim(bottom, bottom + HEIGHT / 2) # set entities outside of the rendering area invisible: # Bounding box calculation can be very costly, especially for deep nested # block references! If you did the extents calculation and reuse the cache # you already have paid the price: render_area = BoundingBox2d( [(left, bottom), (left + WIDTH / 2, bottom + HEIGHT / 2)]) for entity in msp: entity_bbox = bbox.extents([entity], cache=cache) if render_area.intersect(entity_bbox): entity.dxf.invisible = 0 else: entity.dxf.invisible = 1 # finalizing invokes auto-scaling! Frontend(ctx, out).draw_layout(msp, finalize=False) # set output size in inches # width = 6 in x 300 dpi = 1800 px # height = 3 in x 300 dpi = 900 px fig.set_size_inches(6, 3, forward=True) filename = f"lines{quarter}.png" print(f'saving to "{filename}"') fig.savefig(filename, dpi=300) plt.close(fig)

draw_layout()方法有一个参数filter_func来指定一个函数,该函数接受一个DXF实体作为参数,并返回True或False来呈现或忽略这个实体。这将是在不改变DXF内容的情况下过滤呈现区域以外的实体的另一种选择。

更新:在github上可以找到一个精化的示例



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3