₹0.00
0

SOLIDWORKS Document Manager API: Read Files Without SOLIDWORKS

The SOLIDWORKS Document Manager API reads and writes properties, configurations and references in SOLIDWORKS files without SOLIDWORKS running.
SOLIDWORKS Document Manager API: Read Files Without SOLIDWORKS
Picture of Amen Jlili

Amen Jlili

Amen Jlili is the founder and technical director of Blue Byte Systems Inc., a software company in Vancouver, Canada, specializing in automating SOLIDWORKS and PDM.

The SOLIDWORKS Document Manager API is a lightweight COM library that reads and writes SOLIDWORKS files (parts, assemblies and drawings) without SOLIDWORKS running. With it you can read and update custom properties and configuration specific properties, list configurations, read and replace file references, and read some stored metadata, much faster than opening each file in SOLIDWORKS. It needs a Document Manager license key, which SOLIDWORKS subscription customers request through the SOLIDWORKS Customer Portal. This guide explains what the API can and cannot do, walks through opening a file and reading properties in C#, and shows where it fits in SOLIDWORKS PDM automation.

What the SOLIDWORKS Document Manager API is

SOLIDWORKS offers several APIs, each for a different job. We cover the full landscape in the SOLIDWORKS APIs; in short:

API Needs SOLIDWORKS running Typical use
SOLIDWORKS API (ISldWorks) Yes Modeling, drawings, export, anything geometric
SOLIDWORKS PDM API (IEdmVault5) No (needs PDM client) Vault operations: check in/out, states, variables
Document Manager API (SwDMApplication) No File metadata: properties, configurations, references

The Document Manager reads the SOLIDWORKS file structure directly. It does not load geometry, does not rebuild, and does not start a SOLIDWORKS session. That makes it the right tool for any job that touches many files but only their metadata: property audits, bulk property updates, where-used and reference reports, file renames with reference repair, and services that run unattended on servers where no one should be starting SOLIDWORKS sessions.

Getting a Document Manager license key

Every Document Manager application has to pass a license key when it starts. Without a valid key, opening a document fails with a license error.

Customers on active SOLIDWORKS subscription can request a key through the SOLIDWORKS Customer Portal (look for the API support section and the Document Manager key request). The key is tied to your company, not to a specific machine. Key problems are a common forum topic, for example Document Manager license key does not work on the SOLIDWORKS Forum.

Treat the key like a credential:

  • Keep it out of public source control. Load it from a configuration file, an environment variable or a secure store.
  • Do not share it outside your organization. Commercial products ship with their vendor’s key; your in-house tools use yours.

Setting up a project

The interop assembly is SolidWorks.Interop.swdocumentmgr.dll, which installs with SOLIDWORKS (look in the API redistributable folder of your installation). Reference it in your C# project. The underlying COM library must be registered on the machine that runs your code, which is normally the case on machines with SOLIDWORKS or the SOLIDWORKS PDM client installed.

A Document Manager library can read files saved in its own version or older. A newer file opened with an older library returns a future version error, so keep the library at least as new as the newest SOLIDWORKS version your users save in.

Opening a document: SwDMClassFactory and GetDocument

The entry point is SwDMClassFactory. Call GetApplication with your license key to get an SwDMApplication, then call GetDocument with the file path, the document type and a read only flag. GetDocument reports problems through an out parameter of type SwDmDocumentOpenError, so always check it.

using System;
using System.IO;
using SolidWorks.Interop.swdocumentmgr;

class DocManDemo
{
    static void Main(string[] args)
    {
        string key = Environment.GetEnvironmentVariable("SW_DM_KEY");
        string path = args[0];

        var factory = new SwDMClassFactory();
        SwDMApplication app = factory.GetApplication(key);

        SwDmDocumentType type = GetDocType(path);
        SwDmDocumentOpenError openError;
        SwDMDocument doc = app.GetDocument(path, type, true, out openError);

        if (doc == null || openError != SwDmDocumentOpenError.swDmDocumentOpenErrorNone)
        {
            Console.WriteLine($"Could not open {path}: {openError}");
            return;
        }

        try
        {
            PrintFileProperties(doc);
            PrintConfigurationProperties(doc);
        }
        finally
        {
            doc.CloseDoc();
        }
    }

    static SwDmDocumentType GetDocType(string path)
    {
        switch (Path.GetExtension(path).ToLowerInvariant())
        {
            case ".sldprt": return SwDmDocumentType.swDmDocumentPart;
            case ".sldasm": return SwDmDocumentType.swDmDocumentAssembly;
            case ".slddrw": return SwDmDocumentType.swDmDocumentDrawing;
            default: return SwDmDocumentType.swDmDocumentUnknown;
        }
    }
}

Open read only (true for the third argument) whenever you only read. It avoids file locks and works on files that are read only on disk, which is the normal state for files in a SOLIDWORKS PDM local cache that are not checked out.

Common open errors to handle explicitly are file not found, no license (bad or missing key), future version, and file read only when you requested write access.

Reading custom properties

File level custom properties (the Custom tab in SOLIDWORKS File Properties) are read directly from the document. GetCustomPropertyNames returns the names as an array, and GetCustomProperty returns the value as a string plus its type through an out parameter of type SwDmCustomInfoType (text, date, number or yes/no).

static void PrintFileProperties(SwDMDocument doc)
{
    var names = doc.GetCustomPropertyNames() as string[];
    if (names == null) return;

    foreach (string name in names)
    {
        SwDmCustomInfoType propType;
        string value = doc.GetCustomProperty(name, out propType);
        Console.WriteLine($"[File] {name} = {value} ({propType})");
    }
}

Writing works the same way in reverse: open the document with write access, call SetCustomProperty for existing properties or AddCustomProperty for new ones, then call Save and check the returned save error before CloseDoc.

One thing to be aware of: a property that contains a link such as "SW-Mass@Part1.SLDPRT" is an expression that SOLIDWORKS evaluates. The Document Manager reads what is stored in the file, so depending on the method and version you get either the expression or the value SOLIDWORKS last saved. Values that require a rebuild to be correct can only be as current as the last save in SOLIDWORKS.

Configurations and configuration specific properties

Configurations are reached through the configuration manager, ISwDMConfigurationMgr, from the document’s ConfigurationManager property. GetConfigurationNames lists the configurations, and GetConfigurationByName returns an SwDMConfiguration with its own GetCustomPropertyNames and GetCustomProperty methods for the Configuration Specific properties.

static void PrintConfigurationProperties(SwDMDocument doc)
{
    SwDMConfigurationMgr mgr = doc.ConfigurationManager;
    var configNames = mgr.GetConfigurationNames() as string[];
    if (configNames == null) return;

    foreach (string configName in configNames)
    {
        var config = (SwDMConfiguration)mgr.GetConfigurationByName(configName);
        var names = config.GetCustomPropertyNames() as string[];
        if (names == null) continue;

        foreach (string name in names)
        {
            SwDmCustomInfoType propType;
            string value = config.GetCustomProperty(name, out propType);
            Console.WriteLine($"[{configName}] {name} = {value}");
        }
    }
}

Drawings have file level properties but no configuration specific properties, so the configuration loop is mainly useful for parts and assemblies.

Reading and replacing references

Assemblies reference their components and drawings reference their models. The Document Manager can list these external references without opening anything in SOLIDWORKS. The family of external reference methods on the document takes a search option object (created from SwDMApplication.GetSearchOptionObject), which controls where the library looks when resolving referenced paths, such as the folder of the parent file and additional search paths you provide.

Newer revisions of the reference methods also report which references are broken and which are virtual components, which makes the Document Manager a good engine for missing reference reports.

The document interface also provides a way to replace a reference path with a new one. That is the building block for renaming or moving files outside SOLIDWORKS while keeping assemblies and drawings intact: rename the child file, then update every parent that pointed to the old path. Whether you can remove a reference entirely, rather than replace it, is a question that has come up on CADOverflow.

Tip: in a SOLIDWORKS PDM vault, do not rename or move files on disk behind the vault’s back. Use the PDM rename and move operations, which update references in the vault’s database. Document Manager reference editing is for files outside a vault, or for preparing data before a migration (see our migration virtual machine checklist).

What the Document Manager API cannot do

The Document Manager is a metadata tool. It is not a substitute for the SOLIDWORKS API when you need anything geometric or anything that requires a rebuild. If you are also scripting in SOLIDWORKS itself, our custom properties and Save As macro shows the SOLIDWORKS API side of property work. It cannot:

  • Read or modify geometry, features, sketches or dimensions.
  • Rebuild a model, so it cannot refresh values that depend on a rebuild (mass, bounding box, equations).
  • Export to PDF, DXF, STEP, Parasolid or any other format.
  • Create new SOLIDWORKS documents.
  • Evaluate configurations the way SOLIDWORKS does, beyond what is stored in the file.

If your task needs any of that, use the SOLIDWORKS API (or a tool built on it). A common and efficient pattern is to use the Document Manager to scan thousands of files and find the handful that need work, then open only those in SOLIDWORKS.

Document Manager automation without writing code

Not every property audit needs a custom application. PDMShell includes commands built on the Document Manager. docmanprops reads custom and configuration specific properties from SOLIDWORKS files in a directory into a CSV file, and the same command can push edited values from that CSV back into the files:

docmanprops -directory "Designs" -csv properties.csv -extension *.sld* -recursive
docmanprops -update -csv properties.csv

The workflow is simple: export, fix the values in Excel, update. The docman command handles the other common case, renaming SOLIDWORKS files and updating the references in the files that use them. Both are documented at pdmshell.com.

Community discussions

FAQ

Do I need SOLIDWORKS installed to use the Document Manager API?

SOLIDWORKS does not need to be running, and the library does not start it. The Document Manager COM library must be installed and registered on the machine, and you need a valid license key.

How do I get a SOLIDWORKS Document Manager license key?

Customers on active SOLIDWORKS subscription request it through the SOLIDWORKS Customer Portal. The key is issued to your company and passed to SwDMClassFactory.GetApplication.

Can the Document Manager read files from a newer SOLIDWORKS version?

No. A file saved in a newer version than the library returns a future version error. Keep the library current.

Can I update properties of files in a SOLIDWORKS PDM vault?

Only on checked out files, since cached files are read only. In most cases it is better to update PDM data card variables through the PDM API and let the vault write them to the file. Our practical guide to the SOLIDWORKS PDM API covers that route.

Need help with this?

We build Document Manager and PDM API tools for property cleanup, reference repair and data migration. Contact us to talk about your project, or try the Document Manager commands in PDMShell with a PDMShell trial.

Need this adapted to your workflow?

We build custom SOLIDWORKS macros, add-ins and PDM automation.

Picture of Amen Jlili

Amen Jlili

Amen Jlili is the founder and technical director of Blue Byte Systems Inc., a software company in Vancouver, Canada, specializing in automating SOLIDWORKS and PDM.

Leave a Comment

0
0
Your Cart
Your cart is emptyReturn to Shop