Automating PDF Export in LibreCAD

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Automating PDF Export in LibreCAD

cenaluc
Hi everyone,

I know it's a lot to ask, but does anyone know a way to use the PDF export command of LibreCAD remotely without needing a user to operate the CAD manually?

I am working with Python and ezdxf to produce simple DXF files (GA drawings) automatically, and I am using LibreCAD to generate the templates and block libraries. It is working great so far, but I would like to explore a way to generate the PDF automatically as well.

Any suggestion is very welcome!

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Automating PDF Export in LibreCAD

dxli
Have a look at dxf2pdf

https://github.com/LibreCAD/LibreCAD?tab=readme-ov-file#dxf-converter


cenaluc wrote
Hi everyone,

I know it's a lot to ask, but does anyone know a way to use the PDF export command of LibreCAD remotely without needing a user to operate the CAD manually?

I am working with Python and ezdxf to produce simple DXF files (GA drawings) automatically, and I am using LibreCAD to generate the templates and block libraries. It is working great so far, but I would like to explore a way to generate the PDF automatically as well.

Any suggestion is very welcome!

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Automating PDF Export in LibreCAD

cenaluc
Hi,

Thanks
I could not get proper output quality with dxf2pdf but I found a nice way to do it with pyautogui
See here:

import time
import os
import pyautogui


def open_data(app_instance):
    try:
        # Open the Excel file with the default application
        os.startfile(app_instance.jobfilexls)
    except Exception as e:
        print(f"An error occurred: {e}")


# def on_button_hover(app_instance, event):
#     app_instance.write_button["background"] = "#A9A9A9"
#
# def on_button_leave(app_instance, event):
#     app_instance.write_button["background"] = "#D3D3D3"


def open_cad(app_instance):
    print('ok')
    try:
        # Open the Excel file with the default application
        os.startfile(app_instance.jobfiledxf)
    except Exception as e:
        print(f"An error occurred: {e}")


def print_pdf(app_instance):
    # Get the folder and filename without extension
    folder_path = os.path.dirname(app_instance.jobfiledxf)
    file_name = os.path.basename(app_instance.jobfiledxf).replace('.dxf', '')

    # Define the path for the PDF
    pdf_path = os.path.join(folder_path, f"{file_name}.pdf")

    # Open LibreCAD
    print("Opening LibreCAD...")
    os.startfile("C:\\Program Files (x86)\\LibreCAD\\LibreCAD.exe")
    time.sleep(5)  # Wait for LibreCAD to open

    # Handle input device dialog
    print("Handling input device dialog...")
    time.sleep(2)  # Give some time for the dialog to appear
    pyautogui.press('enter')
    time.sleep(2)  # Wait for the dialog to close

    # Open the DXF file
    print("Opening DXF file...")
    pyautogui.hotkey('ctrl', 'o')
    time.sleep(2)
    pyautogui.typewrite(app_instance.jobfiledxf)
    pyautogui.press('enter')
    time.sleep(5)  # Wait for the file to open

    # Navigate to Print Preview
    print("Navigating to Print Preview...")
    pyautogui.hotkey('alt', 'f')  # Open file menu
    time.sleep(1)
    pyautogui.press('down', presses=9, interval=0.2)  # Navigate to 'Print Preview'
    pyautogui.press('enter')
    time.sleep(2)

    # Export as PDF
    print("Navigating to Export as PDF...")
    pyautogui.hotkey('alt', 'f')  # Open file menu
    time.sleep(1)
    pyautogui.press('down', presses=7, interval=0.2)  # Navigate to 'Export'
    pyautogui.press('right')
    time.sleep(1)
    pyautogui.press('down', presses=1, interval=0.2)  # Navigate to 'Export as PDF'
    pyautogui.press('enter')
    time.sleep(2)

    # Save PDF and handle overwrite prompt
    print(f"Saving PDF as {pdf_path}...")
    pyautogui.typewrite(pdf_path)
    pyautogui.press('enter')
    time.sleep(2)  # Wait for the "Save As" dialog to respond

    # Handle the overwrite prompt if it appears
    # Most dialogs will accept 'Enter' to overwrite, but this depends on LibreCAD's behavior
    pyautogui.press('enter')
    time.sleep(2)  # Wait for the save operation to complete

    # Navigate to Quit
    print("Navigating to Quit...")
    pyautogui.hotkey('alt', 'f')  # Open file menu
    time.sleep(1)
    pyautogui.press('down', presses=12, interval=0.2)  # Navigate to 'Print Preview'
    pyautogui.press('enter')
    pyautogui.press('right')
    time.sleep(1)
    pyautogui.press('enter')
    time.sleep(2)

    print("LibreCAD closed.")
Reply | Threaded
Open this post in threaded view
|

Re: Automating PDF Export in LibreCAD

flywire
cenaluc wrote
See here:
That's great.

How about putting a few files together to use as a tutorial to replace https://dokuwiki.librecad.org/doku.php/usage:tutorials#advanced? I'd be happy to tidy it up and load it.

btw, Are those block libraries publicly available?
Reply | Threaded
Open this post in threaded view
|

Re: Automating PDF Export in LibreCAD

cenaluc
Hi,

libraries are publicly available and free, especially ezdxf is really great for drawing automation.
I could help for a tutorial but I would like to wait for September when I will get the feedback from some users that are doing the beta testing. It is working well till now.