← LibrarySolidWorks Drawing Automation: Creation, Views and PrintingEngineering · ComputersLesson 9/10← PrevNext →
ArticlePublished 4 Aug 20267 min readBy Kevin JoginSolidWorks APIdrawingsprintingdrawing automation

Engineering / Computers / Part 8 of 9

Drawing Automation

Drawings are where an engineering office spends its time and where its errors become expensive. They are also highly rule-governed, which makes them unusually good candidates for automation — provided the tool understands one structural quirk about how views are organised on a sheet.

  • Part 8 · Applied
  • Templates & sheet sizes
  • Sheet is the first view
  • Print control

KL-ENG-COMP-1508 · KEVOS® Knowledge Library · Australian English

Three capabilities cover most drawing automation: creating a drawing and populating it with standard views, walking the sheets and views to audit or modify them, and printing with real control over what goes where. Each is straightforward once the object structure is clear.

1Call to place a standard set of views
1Structural quirk that catches every newcomer
2Printing routes — simple and controlled

01What is worth automating in drawings

High value

Creating the drawing

New drawing from the correct template with standard views placed, for every model in a folder. Removes both the keystrokes and the risk of the wrong template.

High value

Auditing sheets

Which drawings have views with no model, incorrect scales, missing sheets, or references to files that have moved.

High value

Batch output

Every sheet to PDF or DXF under a naming rule, or every drawing in a release printed to a defined device. Covered in Part 3 for export; printing is below.

Moderate

Title-block population

Usually better solved by linking title-block fields to custom properties (Part 7) than by writing text through the API.

Low value

Dimensioning and annotation

Placement is a judgement about legibility. Automated dimensioning generally produces drawings that need rearranging by hand.

Low value

One-off layouts

Anything drawn once is faster drawn by hand.

02Creating a drawing from a template

A drawing is created like any other document, from a template — but unlike parts and assemblies, the paper size and sheet dimension arguments matter.

1Choose the templateTake it as configuration, or read the configured template location from user preferences.
2Read its sheet sizeAsk for the template's paper size and sheet dimensions rather than assuming them.
3Create the documentPass the template with the values you just read, and keep the returned handle.
4VerifyA null return means a missing, corrupt or unreadable template — say so plainly.

Reading the size back from the template rather than passing constants is what makes the tool work across a company that uses more than one sheet size. The call reports the paper-size enumerator together with the sheet width and height; where the size is a custom one, the dimensions carry the information the enumerator cannot.

Template paths are per installation

An absolute template path embedded in a tool is the most common reason it works for its author and nobody else. Read the configured template folder from user preferences, or take the path as a setting stored beside the tool.

03Placing standard views

A single call places a standard set of views of a named model onto the drawing, in first-angle or third-angle arrangement.

C#create views, and check the result
IDrawingDoc swDrawing = (IDrawingDoc)swModel;

bool placed = swDrawing.Create3rdAngleViews2(modelPath);
if (!placed)
{
    log.Add("no views placed for " + modelPath);
    return;
}

The choice between first and third angle is a drawing-standard decision, not a preference. Where a business works to a single standard, fix it in the tool and say so in the tool's documentation. Where it works to more than one — different customers, different territories — make it an input and record which was used on each drawing.

Verify before assuming

A false return means no views were placed — a missing model, a path that has moved, or a model that cannot be resolved. Continuing past it produces an empty drawing that looks like a template problem. Test the return, log the model path, and stop.

04Walking sheets and views

This is where the structural quirk lives, and it is the single most useful thing to know about drawing automation.

The first view is the sheet

Asking a drawing for its first view does not return the first drawing view. It returns an object representing the sheet itself. The actual views are that object's successors. A loop that starts processing at the first returned object will report one view too many, and the extra one will have properties that make no sense for a view.

C#the correct sheet-and-view loop
foreach (string sheetName in (string[])swDrawing.GetSheetNames())
{
    swDrawing.ActivateSheet(sheetName);

    // The first object returned represents the sheet; step past it.
    IView view = (IView)swDrawing.GetFirstView();
    view = (IView)view.GetNextView();

    int viewCount = 0;
    while (view != null)
    {
        viewCount++;
        Record(sheetName, view.GetName2(), view.Type, view.ScaleRatio);
        view = (IView)view.GetNextView();
    }

    log.Add($"{sheetName}: {viewCount} views");
}
What a view audit can establish
CheckFinds
View count per sheetEmpty sheets, and sheets carrying far more than the standard expects
Referenced model of each viewViews pointing at files that have moved, been renamed or been superseded
Scale of each viewScales outside the permitted set, and detail views inconsistent with their parent
View typeSection and detail views that have lost their parent
Sheet count and namesNaming that departs from the convention downstream systems expect

05Printing with control

There are two routes. One is a single call with no options; the other gives you everything a print dialogue would.

The simple route

A direct print call sends the document to the default device with current settings. It is one line, and it is genuinely useful for "print what is open". It offers no control over range, copies, collation or device.

The controlled route

The document extension provides a print call taking a page range, a copy count, a collation flag, a named printer and an optional output file. This is the one to use for anything unattended.

The page range argument

Ranges are supplied as an array of integers read in pairs, each pair being a start and an end. A pair of one and five prints five pages; adding a further pair of seven and eight prints two more. A single page is expressed as a pair with the same value twice — supplying an odd number of entries is a defect that produces unpredictable output.

Collation

With collation on, multiple copies emerge as complete sets. With it off, all copies of each page emerge together. For a multi-sheet drawing issued to several recipients, collation on is almost always what is wanted, and it is worth setting explicitly rather than relying on a device default.

The printer name

The printer is identified by its exact name as the operating system knows it. Supplying nothing uses the default device. Two consequences follow: a tool that hard-codes a printer name will fail on any machine where that device is named differently, and a tool that relies on the default will print wherever the user last printed. Present the list of installed devices and let the user choose, storing the choice.

Print to file is not export

The output-file argument writes printer data to a file, not a portable document. To produce a PDF, export using the save operation with a PDF extension as in Part 3, or print to a device that is itself a PDF writer. Confusing the two produces files nobody can open and a difficult diagnosis.

06Export or print

Choosing between exporting and printing
RequirementUseWhy
Issue a drawing pack electronicallyExport to PDFReproducible, archivable, and independent of any installed device
Feed a cutting or nesting systemExport to DXFGeometry is required, not a rendering. See Part 3
Physical copies for a workshopControlled printRange, copies, collation and device all matter
Print what is on screenDirect printOne line; no configuration to get wrong
Archive of what was issuedExport, and keep the fileA printed sheet leaves no record; an exported file is the record
Issue control

Where output feeds manufacturing, the tool should record what it produced: source file, revision, configuration, sheet, timestamp and destination. That log is the evidence connecting a physical part to the drawing that authorised it, and it costs a few lines to write while the tool is running anyway.

07Quick reference

ISldWorks::GetTemplateSizes
Reports a template's paper size and sheet dimensions. Read them rather than assuming.
ISldWorks::NewDocument
Creates the drawing from a template with those size values.
IDrawingDoc::Create3rdAngleViews2
Places a standard third-angle view set of a named model. A first-angle equivalent exists.
IDrawingDoc::GetSheetNames
Every sheet name, as an array to cast.
IDrawingDoc::ActivateSheet
Makes a sheet current. Restore the original before exiting.
IDrawingDoc::GetFirstView
Returns the sheet, not the first view. Step to the next object before processing.
IView::GetNextView
Next view on the sheet; null ends the loop.
IView::GetName2
View name, for reporting and for locating a view again later.
IModelDoc2::PrintDirect
Prints to the default device with current settings. No options.
IModelDocExtension::PrintOut2
Controlled printing — page-range pairs, copies, collation, named printer, output file. Later numbered variants exist.
Page range pairs
Integers read two at a time as start and end. A single page is a pair of identical values.

Continue learning

SolidWorks Custom Properties and Configurations Through the APIArticle · ComputersNEXT LESSON →SolidWorks Add-ins: Structure, Registration and DeploymentArticle · ComputersTraversing SolidWorks Assemblies and Feature TreesArticle · ComputersSolidWorks PropertyManager Pages: Building Native User InterfaceArticle · Computers