grdwyer / Robot-Assisted-Manufacturing

Apache License 2.0
0 stars 0 forks source link

DXF importer #38

Open grdwyer opened 2 years ago

grdwyer commented 2 years ago

Look into creating a DXF importer for the toolpaths, this would allow integration with most cad software and enable more complex toolpaths to be created.

Currently looked into ezdxf which seems quick and stable enough for use.

Alternatives

this uses dxf files, potential alternatives would be svg or dwg, neither seem to have good libraries.

Potential issues

circles and arcs are described parameterised (radius, center point, etc.) these will need to be interpolated into points to be usable.

grdwyer commented 2 years ago

initial test function with some dxfs generated from onshape.
Seems to be good interface only issue is the entities do not seem to be ordered along the path, so the path will need to be determined on loading the dxf entities.

import sys
import ezdxf

# helper function
def print_line(e):
    print("LINE on layer: %s\n" % e.dxf.layer)
    print("start point: %s\n" % e.dxf.start)
    print("end point: %s\n" % e.dxf.end)

def print_circle(e):
    print("CIRCLE on layer: {}]\nCentre point: {}\nRadius: {}".format(e.dxf.layer, e.dxf.center, e.dxf.radius))

def print_arc(e):
    print("CIRCLE on layer: {}]\nCentre point: {}\nRadius: {}\nStart angle: {}\nEnd angle: {}\nStart Point: {}"
          "\nEnd Point: {}"
          .format(e.dxf.layer, e.dxf.center, e.dxf.radius, e.dxf.start_angle, e.dxf.end_angle, e.start_point,
                  e.end_point))

try:
    doc = ezdxf.readfile("/home/george/Downloads/two_line_and_arc.dxf")
    # iterate over all entities in modelspace
    msp = doc.modelspace()
    for e in msp:
        print("Entity type found: {}".format(e.dxftype()))

        if e.dxftype() == 'LINE':
            print_line(e)
        elif e.dxftype() == 'CIRCLE':
            print_circle(e)
        elif e.dxftype() == 'ARC':
            print_arc(e)

    # # entity query for all LINE entities in modelspace
    # for e in msp.query('LINE'):
    #     print_entity(e)
except IOError:
    print(f'Not a DXF file or a generic I/O error.')
    sys.exit(1)
except ezdxf.DXFStructureError:
    print(f'Invalid or corrupted DXF file.')
    sys.exit(2)