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

ItemDetail
TypeCLI (Windows executable)
LocationC:\git\PLC\xlsxwork\DwgToPdf.exe
SourceC:\git\PLC\xlsxwork\DwgToPdf.cs (454 lines, C#)
Interop DLLC:\git\PLC\xlsxwork\eDrawings.Interop.EModelViewControl.dll
StatusWorking — proven on 40/40 files

Prerequisites

RequirementDetail
eDrawings 2025Must be installed and COM-registered. Free viewer from Dassault/SolidWorks.
Microsoft Print to PDFWindows 10/11 built-in virtual printer (enabled by default)
.NET Framework 4.xAny version supporting WinForms
Desktop sessionCOM 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

ArgumentRequiredDescription
<input>YesSingle DWG file path or input directory
<output>YesOutput PDF file path or output directory
[filter]NoWildcard pattern for directory mode (default: *.DWG)

Exit Codes

CodeMeaning
0Success
1Invalid arguments, input not found, or no matching files
2Fatal 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

PriorityMethodHow
1Print5()Late binding via Type.InvokeMember — has printToFileName parameter for direct file output
2Save()Native eDrawings save — may support PDF export in newer versions
3Print2()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):

SettingValueNotes
Timeout60 seconds/fileSkips stuck operations, continues batch
OrientationLandscapeStandard for PSI electrical schematics
Paper size11×17 TabloidSize code 17, 4318×2794 tenths-of-mm
Margins0, 0, 0, 0Full-bleed output
PrinterMicrosoft Print to PDFWindows built-in
ScaleScale-to-FitFits drawing to page
File system delay500msPost-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:

FlagPurpose
eMVEnableSilentModeSuppress all popups and dialogs
eMVSuppressSavePromptDon’t prompt to save on close
eMVReadOnlyOpen files read-only
eMVDisableMenuSaveDisable save menu items
eMVDisableSNLCheckoutDon’t check out SolidNetWork license

Use with Digital Thread Workflow

To convert drawings for any PSI project:

  1. 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.

  2. Run the batch conversion using the drawing number prefix:

    DwgToPdf.exe "X:\DWGFiles" "C:\temp\dwg_pdf\{proj}" "{prefix}*.DWG"
    
  3. 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:

VersionScriptApproachKey Finding
1dwg2pdf.ps1COM object explorationDiscovered EModelViewControl ActiveX
2dwg2pdf_edrawings.ps1Event-based with flag pollingFirst working conversion, fragile in PS
3dwg2pdf_v2.ps1EDrawingOfficeAutomator APIExplored alternate COM APIs
4dwg2pdf_v3.ps1EModelViewApp.25 APIApp-level API with SaveActiveDocumentAs
5dwg2pdf_v4.ps1Polling + wait logicTimer-based instead of event-based
6dwg2pdf_v5.ps1WinForms hidden windowHidden form for stable COM hosting
7dwg2pdf_v6.ps1Corrected Init(0)Init(0) = inCollaborate flag, not HWND
FinalDwgToPdf.csC# AxHost + Print5 late bindingProduction 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

LimitationDetail
Requires desktop sessionCannot run as a headless service or scheduled task without an interactive Windows session
Sequential processingOne file at a time (COM single-threading). ~40 files takes ~10 minutes
eDrawings version-specificCLSID is hardcoded for eDrawings 2025. Different versions need recompilation
No sheet selectionPrints all sheets — cannot select specific sheets from a multi-sheet DWG
Tabloid paper onlyHardcoded to 11×17. Letter-size drawings print with excess whitespace
No rendering validationVerifies 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


Last updated: February 2026