3.7 KiB
CAD Layout Batch Export Tool
This project automates DWG layout preparation in AutoCAD.
It opens selected drawings, attaches a master drawing as XREF, imports a template layout if missing, applies viewport zoom positioning, updates title block attributes, removes default layouts, saves, and closes each file.
The app has two modes:
- IO
- NETWORK
The script is in export_layouts_ui.py.
What This Tool Does
For each drawing in the selected batch, the tool:
- Opens the DWG in AutoCAD (COM automation).
- Uses the selected Master DWG as an XREF source (name: XREF1), except when processing the master itself.
- Ensures the target template layout exists by importing from the selected Template DWG if needed.
- Activates that layout and runs viewport zoom using mode-specific coordinates.
- Updates title block attributes:
- IO mode: uses grouped names from the names file.
- NETWORK mode: uses one name per sheet and transforms naming for network labels.
- Deletes Layout1 and Layout2.
- Saves and closes the DWG.
- Writes progress to export_progress.json for resume support.
- Deletes matching .bak right after each successful DWG.
- On full completion of all files, performs a final recursive cleanup of remaining .bak files.
Requirements
System:
- Windows
- AutoCAD installed
- AutoCAD must be available through COM
Python:
- Python 3.x
- Package: pywin32
Standard library modules used:
- os
- re
- json
- threading
- time
- tkinter
Installation
- Open PowerShell in the project folder.
- Install dependency:
python -m pip install pywin32
If you use a virtual environment, activate it first and run the same command.
How To Run
Start the app:
python export_layouts_ui.py
In the UI, select:
- Master DWG (XREF source)
- Template DWG (layout source)
- Drawing files to process
- Block Names File (TXT)
Then click Run Batch.
You can also use the same DWG as both Master and Template if that drawing contains a valid paper-space layout.
Input File Notes
Block Names TXT:
- One name per line
- Empty lines are ignored
Example names file is list.txt.
Progress file:
- export_progress.json is auto-created/updated.
- It stores completed DWG file names.
- Start Over in the UI resets this progress file.
Zoom Logic Overview
The script computes viewport center per file index:
- center_x = base_x + (index * block_width)
- center_y = base_y
It then sends AutoCAD command:
- ZOOM C center_x,center_y height
IO and NETWORK modes use different base values and height.
Resume and Cleanup Behavior
- If a batch is interrupted, rerun and it resumes using export_progress.json.
- After each successful file, the related .bak is removed.
- After full completion (all files done and not canceled), remaining .bak files are deleted recursively from the current working folder.
Troubleshooting
ModuleNotFoundError: pythoncom
- Cause: pywin32 not installed in the same Python interpreter used to run the script.
- Fix:
- Check python path: where python
- Install in that interpreter: python -m pip install pywin32
AutoCAD COM errors (for example call rejected by callee)
- Keep AutoCAD open and responsive during batch processing.
- Retry the batch (the script already includes retry logic for open calls).
No layout found in template file
- Ensure the template DWG has at least one paper-space layout (not only Model).
Main Project Files
- export_layouts_ui.py: main application
- list.txt: example block names input
- export_progress.json: generated progress log during runs
Safety Notes
- This tool edits and saves DWG files in place.
- Test with a small file set first.
- Keep a separate backup of critical project drawings before first production run.