0

Trying to create a script that will create individual DXF's via ezdxf from an openpyxl import. I am using set x and y coordinates for testing purposes. The issue is that the variables "name" and "qty" seem to retain all previous assignments when used by ezdxf. This results in each DXF generated by a loop placing all previous loops variable data on it.

My issue appears to be with misunderstanding how to use the msp.add_text() in a loop whos variable is updated with each loop.

# user selected file from GUI
xl = values["-xl_file-"]

wb = load_workbook(filename= xl)
sr = wb['Trim']

    # vairables for testing
    rowcount = 12
    x = 100
    y = 10
    rc = 1
    
    for row in sr.iter_rows(min_row=12, min_col=3, max_row=16, max_col=3, values_only=True):
        for value in row:
            # skip blank rows
            if value is None:
                break
            qty = sr.cell(row = rowcount, column = 2).value
            name = sr.cell(row = rowcount, column = 3).value
            points = [(0, 0), (x, 0), (x, y), (0, y), (0, 0)]
            msp.add_lwpolyline(points, dxfattribs={"layer": "3"})
            msp.add_text(name, dxfattribs={"layer": "2"}).set_pos((50, 3), align='MIDDLE_RIGHT')
            print(rc, name)
            msp.add_text(f"Part: {name}", dxfattribs={"layer": "2"}).set_pos((0, -5), align='MIDDLE_LEFT')
            msp.add_text(f"Qty: {qty}", dxfattribs={"layer": "2"}).set_pos((0, -10), align='MIDDLE_LEFT')
            doc.saveas(f"{xl[:-13]}{rowcount}.dxf")
        rowcount = rowcount + 1
        rc = rc +1

Adding a print statement to the name variable appears to produce the correct output. Each loop assigns that rows value to the variable skipping the blank row.

Excel:excel

Terminal:print output

The problem is each iteration of the loop adds that loops variable as well as each previous loops variable. By the time it loops for the 5th time all four of the looped variables are drawn on top of one another.

First loop:enter image description here

Fifth loop:enter image description here

Rand0mdude
  • 27
  • 1
  • 10
  • It's not really clear what the problem is. As you are assigning the values for each row, there's no need to set them to `None`. I'd suggest you debug using print statements first and work on the DXF stuff once that is giving you what you want. – Charlie Clark Aug 24 '22 at 09:15

1 Answers1

0

The solution was that I needed to delete all attributes from ezdxf at the completion of each loop.

for row in sr.iter_rows(min_row=12, min_col=3, max_row=16, max_col=3, values_only=True):
for value in row:
    # skip blank rows
    if value is None:
        break
    qty = sr.cell(row = rowcount, column = 2).value
    name = sr.cell(row = rowcount, column = 3).value
    points = [(0, 0), (x, 0), (x, y), (0, y), (0, 0)]
    msp.add_lwpolyline(points, dxfattribs={"layer": "3"})
    msp.add_text(name, dxfattribs={"layer": "2"}).set_pos((50, 3), align='MIDDLE_RIGHT')
    print(rc, name)
    msp.add_text(f"Part: {name}", dxfattribs={"layer": "2"}).set_pos((0, -5), align='MIDDLE_LEFT')
    msp.add_text(f"Qty: {qty}", dxfattribs={"layer": "2"}).set_pos((0, -10), align='MIDDLE_LEFT')
    doc.saveas(f"{xl[:-13]}{rowcount}.dxf")
rowcount = rowcount + 1
msp.delete_all_entities()
msp.purge()
Rand0mdude
  • 27
  • 1
  • 10