简体   繁体   English

使用ezdxf将尺寸线移动到新图层

[英]Move Dimension Line to New Layer using ezdxf

I used ezdxf to change the layer name and try to move the entity, but only the dimension line remains on the old layer. 我使用ezdxf更改了图层名称并尝试移动实体,但是只有尺寸线保留在旧图层上。

I wrote code to move an entity to a new layer name, but it didn't move. 我编写了将实体移动到新图层名称的代码,但没有移动。

import ezdxf
import glob
from pathlib import Path
from typing import TYPE_CHECKING, TextIO, Iterable

file_list=glob.glob('./input/*.dxf')
for filename in file_list:
    dwg = ezdxf.readfile(filename,encoding='auto',legacy_mode=False)
    msp = dwg.modelspace()

old_layername1 = 'ABC'
new_layername1 = 'DEF'

try:
    layer = dwg.layers.get(old_layername)
except ValueError:
    print('Layer {} not found.'.format(old_layername))
else:
    layer.dxf.name = new_layername

# move entities in model space to new layer
all_entities_on_old_layer = dwg.modelspace().query('*[layer=="%s"]' % old_layername)
for entity in all_entities_on_old_layer:
    entity.dxf.layer = new_layername  # this assigns the new layer

Only the dimension line remains in the old layer name. 仅尺寸线保留在旧图层名称中。

I want to know the solution. 我想知道解决方案。

Renaming layers is not implemented in ezdxf. ezdxf中未实现重命名图层。 The usage of layers is not bound to the table entries, this means if you rename a layer table entry, all entities of this layer still have the old layer name stored as DXF attribute, remember ezdxf is an interface to DXF file format not a CAD application. 图层的使用不受表条目的约束,这意味着如果重命名图层表条目,则该图层的所有实体仍然具有旧图层名称存储为DXF属性,请记住ezdxf是DXF文件格式的接口,而不是CAD应用。 In your special case the DIMENSION entity has also a graphical representation as anonymous BLOCK, all entities of this BLOCK may still refer to the old layer name, also some entities of other BLOCKS and paperspace layouts. 在您的特殊情况下,DIMENSION实体还具有作为匿名BLOCK的图形表示形式,此BLOCK的所有实体可能仍引用旧层名称,还包含其他BLOCKS和图纸空间布局的某些实体。 This is the code to rename a layer and all entities referring to this layer, tested with beta version v0.10: 这是重命名层以及引用此层的所有实体的代码,并经过beta版本v0.10测试:

import ezdxf

def rename_layer(doc, old, new):
    """ 
    Works only for layers with an entry in the layer table, 
    layers can be used without such an entry. 
    """
    if old not in doc.layers:
        raise ValueError('Old layer "{}" does not exist.'.format(old))
    if new in doc.layers:
        raise ValueError('New layer "{}" does already exist.'.format(new))

    def rename_layer_table_entry():
        layer = doc.layers.get(old)
        layer.dxf.name = new
        # this is an internal API call, renaming table entries isn't implemented (yet)
        doc.layers.replace(old, layer)

    def rename_entities_layer_attribute():
        # layer names are case insensitive
        old_lower = old.lower()
        # iterate over all entities of modelspace, paperspace layouts
        # and block definitions
        for e in doc.chain_layouts_and_blocks():
            if e.get_dxf_attrib('layer', '0').lower() == old_lower:
                e.dxf.layer = new


    rename_layer_table_entry()
    rename_entities_layer_attribute()

# TESTING

def add_entities(layout):
    layout.add_text('A', dxfattribs={'layer': 'text'})
    layout.add_line((0, 0), (1, 1), dxfattribs={'layer': 'line'})


doc = ezdxf.new('R2010')
doc.layers.new('text')
doc.layers.new('line')

msp = doc.modelspace()
add_entities(msp)

blk = doc.blocks.new('Test')
add_entities(blk)

rename_layer(doc, 'text', 'text1')
rename_layer(doc, 'line', 'line1')

assert msp[0].dxf.layer == 'text1'
assert msp[1].dxf.layer == 'line1'

assert blk[0].dxf.layer == 'text1'
assert blk[1].dxf.layer == 'line1'

assert 'text1' in doc.layers
assert 'line1' in doc.layers

This function does not care about existing layer filters, which may be broken or frozen layers in viewports and all the things I still don't know. 此功能不关心现有的图层过滤器,这些图层过滤器可能会在视口中损坏或冻结图层,以及我仍然不知道的所有内容。 Overall renaming layers is not a good idea and may produce unreadable DXF files. 整体重命名层不是一个好主意,并且可能会产生无法读取的DXF文件。

Update: 更新:

Added rename method to Layer , will be released with v0.10b4: Layer添加了rename方法,将随v0.10b4一起发布:

layer = doc.layers.get(old_name)
layer.rename(new_name)

Renames all known layer references, but not for LAYER_INDEX and LAYER_FILTER, both entities are just preserved as a bunch of tags by ezdxf, so be careful - renaming layers with ezdxf may damage DXF files. 重命名所有已知的图层引用,但不重命名LAYER_INDEX和LAYER_FILTER,这两个实体都被ezdxf保存为一堆标签,因此请小心-用ezdxf重命名图层可能会损坏DXF文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM