Let Python do the job for you: AutoCAD drawings printing bot
This article is originally published at http://firsttimeprogrammer.blogspot.com/
Recently I’ve been getting familiar with AutoCAD and at the same time I’m trying to improve my Python skills. Odd mix, huh?!
While trying to improve my Python skills I thought I could exercise myself on automating boring tasks. I remembered that a few years ago I was given a boring job which involved printing a lot of drawings directly in PDF format from Autocad. I was too lazy to print the drawings one by one and I knew the process could be automated. At the time I put together a Python script that did the job fine, but it was a bit messy. I thought I could make an improved version.
When nice APIs are not available, such as in the case of AutoCAD (at least that was the case a few years ago, nowdays things may have changed), using Pyautogui may help in the task of automating boring tasks.
The logic of the program is simple:
- Look for all the .dwg files in a directory given by the user.
- For each file, open the file using the default program, then emulate the sequence of keys the user would need to press in order to print out the file in .pdf format.
- Check if file has been printed.
- Save log at all the steps in a log file.
Other than the fail safe provided by Pyautogui, some other checks have been added.
A small disadvantage of this program is that it assumes that the drawings are ready to be printed and that the selected printer can be chosen by pressing “am” on the printing menu. These assumptions come basically from the choice of automating the process using Pyautogui and making the program do every single step as a human would do using the computer’s GUI. On the other hand, by going through the printing procedure on your machine and by editing the Pyautogui steps on the code, you can easily fit the code to your needs.
If anything, this is another example of how Python can help in everyday tasks too. The program is shown in a gist below.
Thanks for reading this post.
Thanks for visiting r-craft.org
This article is originally published at http://firsttimeprogrammer.blogspot.com/
Please visit source website for post related comments.