<commit_before>#  Copyright (c) 2022, <NAME>
#  License: MIT License
import pathlib
import ezdxf
from ezdxf.addons import Importer
from ezdxf.query import EntityQuery

CWD = pathlib.Path("~/Desktop/Outbox").expanduser()
if not CWD.exists():
    CWD = pathlib.Path(".")

SOURCE_DXF = "test.dxf"

# ------------------------------------------------------------------------------
# This example shows how to separate entities by their layer attribute
#
# The example imports all entities from the modelspace into a new DXF document.
# The Importer add-on is required to also import the required resources used by
# the imported entities.
#
# Important advice:
# Keep things simple, more complex DXF documents may fail or at least the entities
# will not look the same as in the source file! Read the docs!
#
# Basic concept of layers : https://ezdxf.mozman.at/docs/concepts/layers.html
# Importer add-on: https://ezdxf.mozman.at/docs/addons/index.html
# ------------------------------------------------------------------------------


def main():
    source_doc = ezdxf.readfile(CWD / SOURCE_DXF)
    source_msp = source_doc.modelspace()
    # create an EntityQuery container with all entities from the modelspace
    source_entities = source_msp.query()
    # get all layer names from entities in the modelspace:
    all_layer_names = [e.dxf.layer for e in source_entities]
    # remove unwanted layers if needed

    for layer_name in all_layer_names:
        # create a new document for each layer with the same DXF version as the
        # source file
        layer_doc = ezdxf.new(dxfversion=source_doc.dxfversion)
        layer_msp = layer_doc.modelspace()
        importer = Importer(source_doc, layer_doc)
        # select all entities from modelspace from this layer (extended query
        # feature of class EntityQuery)
        entities: EntityQuery = source_entities.layer == layer_name  # type: ignore
        if len(entities):
            importer.import_entities(entities, layer_msp)
            # create required resources
            importer.finalize()
            layer_doc.saveas(CWD / f"{layer_name}.dxf")


if __name__ == "__main__":
    main()<commit_msg>refactor example separate_layers.py<commit_after>#  Copyright (c) 2022, <NAME>
#  License: MIT License
import pathlib
import ezdxf
from ezdxf.addons import Importer
from ezdxf.query import EntityQuery

CWD = pathlib.Path("~/Desktop/Outbox").expanduser()
if not CWD.exists():
    CWD = pathlib.Path(".")

# ------------------------------------------------------------------------------
# This example shows how to separate entities by their layer attribute
#
# The example imports all entities from the modelspace into a new DXF document.
# The Importer add-on is required to also import the required resources used by
# the imported entities.
#
# Important advice:
# Keep things simple, more complex DXF documents may fail or at least the entities
# will not look the same as in the source file! Read the docs!
#
# Basic concept of layers : https://ezdxf.mozman.at/docs/concepts/layers.html
# Importer add-on: https://ezdxf.mozman.at/docs/addons/index.html
# ------------------------------------------------------------------------------


def main(filename: str):
    source_doc = ezdxf.readfile(CWD / filename)
    source_msp = source_doc.modelspace()
    # create an EntityQuery container with all entities from the modelspace
    source_entities = source_msp.query()
    # get all layer names from entities in the modelspace:
    all_layer_names = [e.dxf.layer for e in source_entities]
    # remove unwanted layers if needed

    for layer_name in all_layer_names:
        # create a new document for each layer with the same DXF version as the
        # source file
        layer_doc = ezdxf.new(dxfversion=source_doc.dxfversion)
        layer_msp = layer_doc.modelspace()
        importer = Importer(source_doc, layer_doc)
        # select all entities from modelspace from this layer (extended query
        # feature of class EntityQuery)
        entities: EntityQuery = source_entities.layer == layer_name  # type: ignore
        if len(entities):
            importer.import_entities(entities, layer_msp)
            # create required resources
            importer.finalize()
            layer_doc.saveas(CWD / f"{layer_name}.dxf")


if __name__ == "__main__":
    main("test.dxf")
{'fraction_non_alphanumeric': 0.14583333333333334, 'fraction_numerical': 0.0018315018315018315, 'mean_word_length': 2.5262308313155772, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3366905', 'n_tokens_mistral': 1216, 'n_tokens_neox': 1207, 'n_words': 412}