ravas / dxfdedup (public) (License: GPLv3) (since 2017-04-04) (hash sha1)
dxfdedup removes duplicates from DXF files. Python 3.6+ and ezdxf are required. This project is in the alpha stage.

/__init__.py (404816812322db6a6fb561bed01c33d266d59bf7) (3222 bytes) (mode 100644) (type blob)

# dxfdedup removes duplicate entities from DXF files.
#
# Copyright (C) 2017 ravas (lastauth.com/profile/ravas)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# https://www.gnu.org/licenses/gpl-3.0.en.html

# Always back-up your files before using this software!!
# Do some tests!!


import importlib
import pathlib
import time

import ezdxf

class Deduplicator(object):

    def __init__(self, entities=None, iders='dxfdedup.identifiers'):
        """
        :entities: list of entity types to deduplicate; None ? dedup all types
        """
        print('Back-up your files before using this object.')
        self.entities = entities
        self.iders = iders
        self.identifiers = []
        self.set_iders(entities, iders)

    def set_iders(self, entities=None, iders=None):
        """
        Import the indetifiers subpackage and its submodules,
        and append an instance of each submodule's
        Indetifier(Object) to self.entities.
        :entities: list of entity types to deduplicate
        :iders: name of the identifiers subpackage
        """

        entities = entities or self.entities
        iders = iders or self.iders
        
        ider_pkg = importlib.import_module(iders)

        names = []
        
        if entities is None:
            names = ider_pkg.__all__
        else:
            names = [n for n in ider_pkg.__all__ if n in entities]

        print(f'The following types will be deduplicated: {names}')

        for name in names:
            i = importlib.import_module(f'{iders}.{name}')
            self.identifiers.append(i.Identifier())

    def dedup_d(self, path):
        """
        Remove duplicates from all dxf files in path.
        :path: a directory path (string)
        """
        
        po = pathlib.Path(path).expanduser()
        
        if po.is_dir():
            start = time.time()
            for p in po.glob('**/*.dxf'):
                print(p)
                self.dedup(p)
            stop = time.time()
            print(f'Finished! Time Elapsed: {stop-start}')
        else:
            print('Invalid Directory!')

    def dedup(self, path):
        """
        Remove duplicates from path.
        :path: a dxf file path (string)
        """
        
        dxf = ezdxf.readfile(path)
        modelspace = dxf.modelspace()
        deleted = 0
        
        for i in self.identifiers:
            entities = modelspace.query(i.e_type)
            seen = set()
            for entity in entities:
                uid = i.unique_id(entity)
                if uid not in seen:
                    seen.add(uid)
                else:
                    deleted += 1
                    modelspace.delete_entity(entity)

        if deleted > 0:
            dxf.saveas(path)          
        print(f'Duplicates deleted: {deleted}')


Mode Type Size Ref File
100644 blob 35146 20d40b6bceca3a6c0237d7455ebf1820aeff3680 LICENSE
100644 blob 1602 137bb768cd8eb9030c8d4a6d5ec1f62db572d720 README.md
100644 blob 3222 404816812322db6a6fb561bed01c33d266d59bf7 __init__.py
040000 tree - b2ee66e64b32c0d71a0b551d5e3607a48207cefa identifiers
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/ravas/dxfdedup

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/ravas/dxfdedup

Clone this repository using git:
git clone git://git.rocketgit.com/user/ravas/dxfdedup

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main