# 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:
1. Opens the DWG in AutoCAD (COM automation).
2. Uses the selected Master DWG as an XREF source (name: XREF1), except when processing the master itself.
3. Ensures the target template layout exists by importing from the selected Template DWG if needed.
4. Activates that layout and runs viewport zoom using mode-specific coordinates.
5. 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.
6. Deletes Layout1 and Layout2.
7. Saves and closes the DWG.
8. Writes progress to export_progress.json for resume support.
9. Deletes matching .bak right after each successful DWG.
10. 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

1. Open PowerShell in the project folder.
2. 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:
1. Master DWG (XREF source)
2. Template DWG (layout source)
3. Drawing files to process
4. 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:
  1. Check python path:
     where python
  2. 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.
