How to use the ezdxf.readfile function in ezdxf

To help you get started, we’ve selected a few ezdxf examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mozman / ezdxf / integration_tests / test_leica_disto_r12.py View on Github external
def test_leica_disto_r12(filename):
    # new entity system: legacy mode not necessary
    dwg = ezdxf.readfile(filename, legacy_mode=False)
    msp = dwg.modelspace()
    points = list(msp.query('POINT'))
    assert len(points) == 11
    assert len(points[0].dxf.location) == 3
github mozman / ezdxf / integration_tests / audit.py View on Github external
def run(start):
    if start > 0:
        start -= 1
    names = list(chain(glob.glob(DIR1), glob.glob(DIR2)))
    names = names[start:]
    count = 0
    for filename in names:
        count += 1
        print("processing: {}/{} file: {}".format(count+start, len(names)+start, filename))
        doc = ezdxf.readfile(filename, legacy_mode=LEGACY_MODE)

        auditor = Auditor(doc)
        if len(auditor):
            auditor.print_error_report(auditor.errors)
github mozman / ezdxf / integration_tests / test_open_R13_R14.py View on Github external
def test_open_R13_R14(filename, tmpdir):
    doc = ezdxf.readfile(filename)
    assert 'Model' in doc.layouts, 'Model space not found'
    assert 'Layout1' in doc.layouts, 'Paper space not found'
    assert doc.dxfversion >= 'AC1015'
    msp = doc.modelspace()
    msp.add_line((0, 0), (10, 3))
    converted = str(tmpdir.join("converted_AC1015.dxf"))
    doc.saveas(converted)
    assert os.path.exists(converted)
github mozman / ezdxf / src / ezdxf / addons / odafc.py View on Github external
version: load file as specific DXF version, by default the same version as the source file or
                 if not detectable the latest by `ezdxf` supported version.
        audit: audit source file before loading

    """
    infile = Path(filename).absolute()
    if not infile.is_file():
        raise FileNotFoundError(f"No such file: '{infile}'")
    version = _detect_version(filename) if version is None else version

    with tempfile.TemporaryDirectory(prefix='odafc_') as tmp_dir:
        args = _odafc_arguments(infile.name, infile.parent, tmp_dir, output_format='DXF', version=version, audit=audit)
        _execute_odafc(args)
        out_file = Path(tmp_dir) / infile.with_suffix('.dxf').name
        if out_file.exists():
            doc = ezdxf.readfile(str(out_file))
            doc.filename = infile.with_suffix('.dxf')
            return doc
    raise ODAFCError('Failed to convert file: Unknown Error')
github mozman / ezdxf / examples / entities / lwpolyline.py View on Github external
def tut_lwpolyline():
    doc = ezdxf.new('R2000')
    msp = doc.modelspace()

    points = [(0, 0), (3, 0), (6, 3), (6, 6)]
    msp.add_lwpolyline(points)

    doc.saveas("lwpolyline1.dxf")

    # Append points to a polyline::

    doc = ezdxf.readfile("lwpolyline1.dxf")
    msp = doc.modelspace()

    line = msp.query('LWPOLYLINE')[0]  # take first LWPolyline
    line.append_points([(8, 7), (10, 7)])

    doc.saveas("lwpolyline2.dxf")

    # Use context manager to edit polyline::

    doc = ezdxf.readfile("lwpolyline2.dxf")
    msp = doc.modelspace()

    line = msp.query('LWPOLYLINE')[0]  # take first LWPolyline

    with line.points() as points:  # points is a python standard list
        # del points[-2:]  # delete last 2 points
github meerk40t / meerk40t / DefaultModules.py View on Github external
def load(kernel, pathname, **kwargs):
        """"
        Load dxf content. Requires ezdxf which tends to also require Python 3.6 or greater.

        Dxf data has an origin point located in the lower left corner. +y -> top
        """
        kernel.setting(int, "bed_width", 320)
        kernel.setting(int, "bed_height", 220)

        import ezdxf

        basename = os.path.basename(pathname)
        dxf = ezdxf.readfile(pathname)
        elements = []
        for entity in dxf.entities:
            try:
                entity.transform_to_wcs(entity.ocs())
            except AttributeError:
                pass
            if entity.dxftype() == 'CIRCLE':
                element = Circle(center=entity.dxf.center, r=entity.dxf.radius)
            elif entity.dxftype() == 'ARC':
                circ = Circle(center=entity.dxf.center,
                              r=entity.dxf.radius)
                element = Path(circ.arc_angle(Angle.degrees(entity.dxf.start_angle),
                                              Angle.degrees(entity.dxf.end_angle)))
            elif entity.dxftype() == 'ELLIPSE':

                # TODO: needs more math, axis is vector, ratio is to minor.
github mozman / ezdxf / examples / entities / lwpolyline.py View on Github external
doc.saveas("lwpolyline1.dxf")

    # Append points to a polyline::

    doc = ezdxf.readfile("lwpolyline1.dxf")
    msp = doc.modelspace()

    line = msp.query('LWPOLYLINE')[0]  # take first LWPolyline
    line.append_points([(8, 7), (10, 7)])

    doc.saveas("lwpolyline2.dxf")

    # Use context manager to edit polyline::

    doc = ezdxf.readfile("lwpolyline2.dxf")
    msp = doc.modelspace()

    line = msp.query('LWPOLYLINE')[0]  # take first LWPolyline

    with line.points() as points:  # points is a python standard list
        # del points[-2:]  # delete last 2 points
        # points.extend([(4, 7), (0, 7)])  # adding 2 other points
        # the same as one command
        points[-2:] = [(4, 7), (0, 7)]
    # implicit call of line.set_points(points) at context manager exit

    doc.saveas("lwpolyline3.dxf")

    # Each line segment can have a different start/end width, if omitted start/end width = 0::

    doc = ezdxf.new('AC1015')
github mozman / ezdxf / experiments / copy_samples_by_import.py View on Github external
importer.import_modelspace()
    importer.import_paperspace_layouts()
    importer.finalize()


SEP_LINE = '-----------------------------------------------------------------------'

for filename in CADKIT_FILES[:2]:
    filename = DXFTEST_PATH / filename
    new_name = outname(filename)
    if not new_name.exists():
        logging.info('processing file: {}'.format(filename))
        logging.info(SEP_LINE)
        print('reading file: {}'.format(filename), end=' ')
        start = datetime.datetime.now()
        doc = ezdxf.readfile(str(filename), legacy_mode=False)
        end = datetime.datetime.now()
        print(' ... in {:.1f} sec'.format((end - start).total_seconds()))

        print('importing modelspace and paperspace layouts from: {}'.format(filename), end=' ')
        start = datetime.datetime.now()
        new_doc = ezdxf.new('R2010')
        import_msp_and_psp(doc, new_doc)
        end = datetime.datetime.now()
        print(' ... in {:.1f} sec\n'.format((end - start).total_seconds()))

        print('writing file: {}'.format(new_name), end=' ')
        start = datetime.datetime.now()
        new_doc.saveas(new_name)
        end = datetime.datetime.now()
        print(' ... in {:.1f} sec\n'.format((end - start).total_seconds()))
github awesomebytes / etherdream_tools / web_engine / translation_dxf.py View on Github external
def add_square(dwg):
    # Get base square -1000 to 1000
    square_dxf = readfile('Square.dxf')
github mozman / ezdxf / examples / addons / dxf2src.py View on Github external
import ezdxf
from ezdxf.addons.dxf2code import entities_to_code

NAME = "A_000217"
DXF_FILE = r"D:\Source\dxftest\CADKitSamples\{}.dxf".format(NAME)
# DXF_FILE = r"C:\Users\manfred\Desktop\Outbox\{}.dxf".format(NAME)
SOUCE_CODE_FILE = r"C:\Users\manfred\Desktop\Outbox\{}.py".format(NAME)

doc = ezdxf.readfile(DXF_FILE)
msp = doc.modelspace()

source = entities_to_code(msp)

print('writing ' + SOUCE_CODE_FILE)
with open(SOUCE_CODE_FILE, mode='wt') as f:
    f.write(source.import_str())
    f.write('\n\n')
    f.write(source.code_str())
    f.write('\n')

print('done.')