SolidWorks Drawing Automation: The Hard Parts
Practical tips for the most frustrating parts of drawing automation — view transforms, annotation positioning, scale handling, and dimension placement gotchas.
Why Drawing Automation Is Hard
Drawing automation is widely considered the most frustrating area of the SolidWorks API. The core challenges:
- Views have their own coordinate system (view space) that's different from both the model and the drawing sheet
- Annotations are positioned in sheet space, but reference geometry in model space
- Scale affects everything — view scale, sheet scale, and model units all interact
- Many operations require precise entity selection that's hard to automate reliably
This guide covers the pain points and their solutions.
Understanding View Transforms
Every drawing view has a MathTransform that converts model coordinates to sheet coordinates. This transform accounts for:
- The view's position on the sheet (X, Y offset)
- The view's orientation (front, top, isometric, etc.)
- The view's scale
To convert a point from model space to sheet space:
var view = (View)drawDoc.GetFirstView(); // sheet view
view = (View)view.GetNextView(); // first model view
var transform = view.ModelToViewTransform;
var sheetPoint = modelPoint.MultiplyTransform(transform);Inserting Standard Views
Use IDrawingDoc.CreateDrawViewFromModelView3 to insert views with specific orientations:
drawDoc.CreateDrawViewFromModelView3(
modelPath,
"*Front", // view name: *Front, *Top, *Right, *Isometric, etc.
0.15, 0.20, // X, Y position on sheet (meters!)
0); // optionsThe asterisk prefix (*Front) indicates a standard view. Without it, SolidWorks looks for a named view.
Adding Dimensions
Programmatic dimension placement is tricky because you need to select the correct sketch entities within the view:
- Activate the view: drawDoc.ActivateView(viewName)
- Select edges/vertices in the view using SelectByID2 with view context
- Call AddDimension2 to create the dimension
The position you pass to AddDimension2 is the text location, not the measured entities. The selected entities determine what's measured.
Sheet Scale vs View Scale
- Sheet scale: Set on the sheet properties. Affects all annotations, title blocks, and notes on the sheet
- View scale: Set per view. Affects the visual size of the model geometry in that view
- Annotation positions: Always in sheet space, always in meters, always unaffected by view scale
When calculating annotation positions, work in sheet coordinates (meters). Don't multiply by view scale — that's already handled by the view transform.
Notes and Balloons
To insert a note at a specific position:
var note = (Note)model.InsertNote(
"<FONT size=12>Part: $PRP:PartNo</FONT>");
note.SetTextPoint2(0.05, 0.28, 0); // sheet coordinates, metersNotes support rich text formatting with HTML-like tags and can reference custom properties with $PRP:PropertyName syntax.
BOM Tables
BOM automation is common but has quirks:
- Insert with IDrawingDoc.InsertBomTable4 — specify the anchor point, BOM type, and template
- Read cells with IBomTableAnnotation.Text[row, col]
- BOM row order may not match the visual order — use GetSortedItemNumbers for the display sequence
- To link a BOM to a specific view, select the view before inserting
Exporting Individual Sheets to PDF
To export specific sheets (not all sheets) to separate PDFs:
- Get the sheet names: drawDoc.GetSheetNames()
- For each target sheet: drawDoc.ActivateSheet(sheetName)
- Set the ExportPdfData to include only the active sheet
- Call SaveAs3 with the .pdf extension
Common Gotchas
- All coordinates are in meters on the drawing sheet, regardless of the document's unit system
- The first view returned by GetFirstView() is the sheet itself, not the first model view. Call GetNextView() to get actual model views
- Selection context matters: When selecting entities in a specific view, you must activate that view first or include the view name in the SelectByID2 call
- Title block custom properties: These pull from the referenced model's properties, not the drawing file's properties. Confusing when the values don't update
- RebuildDrawing is not the same as EditRebuild3 — use ForceRebuild3(false) for drawings to update all views and annotations