4

I started learning Python (and coding all together) this week and i'm stuck on a simple task i want to do.

The functionality i'm looking for is this:

  1. Print all .dwg files in a specified directory in the console.
  2. Open an instance of AutoCAD.
  3. For every .dwg file in said directory open a drawing in a new tab. (If i have an instance of AutoCAD open it automatically opens tabs when double clicking a .dwg file)

The code i wrote now:

import os
import subprocess

autocadPath = r'C:\Program Files\Autodesk\AutoCAD 2019\acad.exe'     

for file in os.listdir("D:\openFiles"):
    if file.endswith(".dwg"):
        print(os.path.join("D:\openFiles", file))

        subprocess.Popen("%s %s" % (autocadPath, os.path.join("D:\openFiles", file))) 

What happens when i run this:

  1. Two instances of autocad open.
  2. The last printed drawing opens in both instances
  3. It tries to open the first printed file but only in the second instance of AutoCAD and it doesn't find the file.

(I only have 2 .dwg files in de directory.)

I hope someone can help me out with this.

Tonechas
  • 13,398
  • 16
  • 46
  • 80
itsme
  • 41
  • 3

1 Answers1

1

By far the easiest way to print a batch of drawings within AutoCAD is using an AutoCAD Script file (.scr), which can issue a sequence of commands and responses to command prompts to the AutoCAD command-line. I describe this process (albeit from an AutoLISP perspective) in my answer here.

Using an AutoCAD Script, you can open each drawing, issue the -PLOT command to either plot using a saved plot configuration (.pc3) or by entering the detailed configuration mode and responding to each prompt at the command-line.

I describe the syntax for an AutoCAD Script file in my tutorial here. For the -PLOT command, the Script might look something like the following:

-PLOT Y  "dwg to PDF" "ANSI full bleed B (11.00 x 17.00 Inches)" I L N E F C Y monochrome.ctb Y N N N "C:/YourFilename.pdf" N Y

You could use Python to write the Script, which can then be run from the AutoCAD command-line by supplying the SCRIPT command with the filename & filepath of the Script file created.

Lee Mac
  • 15,615
  • 6
  • 32
  • 80
  • Hey Lee, The problem isn't really the printing, this works as intended in the python script. What is not working is opening all the .dwg files in said directory in 1 instance of AutoCAD. – itsme Jan 10 '20 at 06:56
  • That is actually the main reason I suggested using an AutoCAD Script, as the script will open all of the drawings in a single AutoCAD instance. I suppose the alternative would be to use Python to issue the `OPEN` command to AutoCAD once you have established an AutoCAD session. – Lee Mac Jan 10 '20 at 19:46