DwgToPdf
Batch command-line tool for converting AutoCAD DWG files to PDF using the eDrawings 2025 ActiveX control. Built during the Project 2399 digital thread pilot.
Overview
PSI stores all engineering drawings as DWG files on the X:\DWGFiles\ share. These are binary AutoCAD format — they cannot be read, searched, or analyzed programmatically. DwgToPdf bridges that gap by hosting the eDrawings 2025 ActiveX control in a hidden WinForms window and printing each DWG to PDF via the Windows built-in “Microsoft Print to PDF” printer.
The tool supports both single-file and batch-directory modes with wildcard filtering, making it practical to convert an entire project’s drawing set in one command.
First use: Project 2399 — converted all 40 electrical and pneumatic schematics (drawings 359900–359939, 359970–359979) with 100% success rate.
Quick Reference
| Item | Detail |
|---|---|
| Type | CLI (Windows executable) |
| Location | C:\git\PLC\xlsxwork\DwgToPdf.exe |
| Source | C:\git\PLC\xlsxwork\DwgToPdf.cs (454 lines, C#) |
| Interop DLL | C:\git\PLC\xlsxwork\eDrawings.Interop.EModelViewControl.dll |
| Status | Working — proven on 40/40 files |
Prerequisites
| Requirement | Detail |
|---|---|
| eDrawings 2025 | Must be installed and COM-registered. Free viewer from Dassault/SolidWorks. |
| Microsoft Print to PDF | Windows 10/11 built-in virtual printer (enabled by default) |
| .NET Framework 4.x | Any version supporting WinForms |
| Desktop session | COM control needs a Windows message pump — requires interactive logon or RDP session |
Usage
Single file conversion:
DwgToPdf.exe "X:\DWGFiles\359920.DWG" "C:\temp\dwg_pdf\359920.pdf"
Batch directory conversion:
DwgToPdf.exe "X:\DWGFiles" "C:\temp\dwg_pdf" "35992*.DWG"
Convert all drawings for a project (example — Project 2399):
DwgToPdf.exe "X:\DWGFiles" "C:\temp\dwg_pdf" "3599*.DWG"
Arguments
| Argument | Required | Description |
|---|---|---|
<input> | Yes | Single DWG file path or input directory |
<output> | Yes | Output PDF file path or output directory |
[filter] | No | Wildcard pattern for directory mode (default: *.DWG) |
Exit Codes
| Code | Meaning |
|---|---|
0 | Success |
1 | Invalid arguments, input not found, or no matching files |
2 | Fatal error during conversion |
How It Works
The tool is a C# WinForms application that hosts the eDrawings ActiveX control in a hidden window (minimized, opacity=0). The conversion pipeline is event-driven:
1. Parse arguments → build job queue
2. Create hidden Form → host eDrawings AxHost wrapper
3. Configure silent mode (suppress all UI, skip license checkout)
4. For each file in queue:
a. OpenDoc(inputPath) → wait for OnFinishedLoadingDocument
b. SetPageSetupOptions(landscape, 11x17, "Microsoft Print to PDF")
c. Print5(outputPath) via late binding → wait for OnFinishedPrintingDocument
d. Verify output PDF exists with non-zero size
e. If Print5 fails → fallback to Save() → fallback to Print2()
5. Report: N succeeded, M failed out of T total
Export Fallback Chain
| Priority | Method | How |
|---|---|---|
| 1 | Print5() | Late binding via Type.InvokeMember — has printToFileName parameter for direct file output |
| 2 | Save() | Native eDrawings save — may support PDF export in newer versions |
| 3 | Print2() | Standard print — relies on pre-configured PDF printer |
Print5 is not exposed in the eDrawings interop assembly, so it must be called via reflection. This technique was discovered by examining the ProPrintServer / ProViewer source code at C:\git\PSI.All\PSI.ProViewer\.
Configuration
All settings are compiled into the executable (no external config file):
| Setting | Value | Notes |
|---|---|---|
| Timeout | 60 seconds/file | Skips stuck operations, continues batch |
| Orientation | Landscape | Standard for PSI electrical schematics |
| Paper size | 11×17 Tabloid | Size code 17, 4318×2794 tenths-of-mm |
| Margins | 0, 0, 0, 0 | Full-bleed output |
| Printer | Microsoft Print to PDF | Windows built-in |
| Scale | Scale-to-Fit | Fits drawing to page |
| File system delay | 500ms | Post-print wait for file write to flush |
| eDrawings CLSID | {965A90A2-D575-42A8-9495-94321271AF88} | eDrawings 2025 specific |
Silent Mode Flags
These flags are set on the eDrawings control to prevent any UI interaction during batch processing:
| Flag | Purpose |
|---|---|
eMVEnableSilentMode | Suppress all popups and dialogs |
eMVSuppressSavePrompt | Don’t prompt to save on close |
eMVReadOnly | Open files read-only |
eMVDisableMenuSave | Disable save menu items |
eMVDisableSNLCheckout | Don’t check out SolidNetWork license |
Use with Digital Thread Workflow
To convert drawings for any PSI project:
-
Find the drawing range. Open the WDP project file at
X:\Controls\{proj}\{proj}.wdp— it’s a text-readable index that lists all drawing numbers for the project. -
Run the batch conversion using the drawing number prefix:
DwgToPdf.exe "X:\DWGFiles" "C:\temp\dwg_pdf\{proj}" "{prefix}*.DWG" -
Output lands in the specified directory — one PDF per DWG, named by drawing number.
The batch DWG converter script batch_dwg_convert.ps1 in C:\git\PLC\xlsxwork\ wraps this tool and auto-detects the drawing range from the BOM API.
See Digital Thread Methodology for the full machine analysis workflow.
Development History
The tool evolved through 7 PowerShell prototypes before arriving at the final C# implementation:
| Version | Script | Approach | Key Finding |
|---|---|---|---|
| 1 | dwg2pdf.ps1 | COM object exploration | Discovered EModelViewControl ActiveX |
| 2 | dwg2pdf_edrawings.ps1 | Event-based with flag polling | First working conversion, fragile in PS |
| 3 | dwg2pdf_v2.ps1 | EDrawingOfficeAutomator API | Explored alternate COM APIs |
| 4 | dwg2pdf_v3.ps1 | EModelViewApp.25 API | App-level API with SaveActiveDocumentAs |
| 5 | dwg2pdf_v4.ps1 | Polling + wait logic | Timer-based instead of event-based |
| 6 | dwg2pdf_v5.ps1 | WinForms hidden window | Hidden form for stable COM hosting |
| 7 | dwg2pdf_v6.ps1 | Corrected Init(0) | Init(0) = inCollaborate flag, not HWND |
| Final | DwgToPdf.cs | C# AxHost + Print5 late binding | Production build combining all learnings |
Key insight: The eDrawings ActiveX control requires a Windows message pump and a valid container window to function. PowerShell’s COM interop could not reliably provide this. The C# AxHost wrapper gives the control a proper WinForms container while keeping the window invisible (minimized, opacity=0).
All prototype scripts are preserved at C:\git\PLC\xlsxwork\dwg2pdf*.ps1 for reference.
Known Limitations
| Limitation | Detail |
|---|---|
| Requires desktop session | Cannot run as a headless service or scheduled task without an interactive Windows session |
| Sequential processing | One file at a time (COM single-threading). ~40 files takes ~10 minutes |
| eDrawings version-specific | CLSID is hardcoded for eDrawings 2025. Different versions need recompilation |
| No sheet selection | Prints all sheets — cannot select specific sheets from a multi-sheet DWG |
| Tabloid paper only | Hardcoded to 11×17. Letter-size drawings print with excess whitespace |
| No rendering validation | Verifies PDF exists with non-zero size, but doesn’t validate visual quality |
Compilation
To recompile from source (e.g., after an eDrawings version upgrade):
csc /target:exe /platform:x86 /reference:eDrawings.Interop.EModelViewControl.dll DwgToPdf.cs
The interop DLL can be regenerated from the installed eDrawings COM registration using tlbimp or by adding a COM reference in Visual Studio.
To find the CLSID for a different eDrawings version, search the registry:
reg query "HKCR\EModelView.EModelViewControl" /s
Related Pages
- Digital Thread Methodology — Full machine analysis workflow that uses this tool
- ProPrintServer — PSI’s SolidWorks-to-PDF conversion service (inspired the Print5 approach)
- PSI Export CLI — Another PSI CLI tool for data export
Last updated: February 2026