Jan 26, 2016

SOLIDWORKS Document Manager API Toolkit


The SOLIDWORKS Document Manager is a powerful tool used for reporting content and manipulating SOLIDWORKS parts, assemblies and drawings.  I’ve used it to export custom properties from thousands of SOLIDWORKS files in seconds!  It can be used to read bill of materials and other tables from drawings as well as update references when files are renamed.

But, just like any other project, it can take time to get started and get familiar with the calls needed.  To make that all easier, I’ve put together a Visual Studio 2010 project in VB.NET I’m calling the Document Manager Toolkit.

I’ll be presenting the toolkit along with a general discussion of the SOLIDWORKS Document Manager at SOLIDWORKS World 2016 in Dallas, TX next week.  Hope to see you there!

“What’s in the toolkit?” you ask?  Download the project here for a sample implementation and read up on the general functionality below.

Document Manager Toolkit Download
Document Manager Presentation PDF (SOLIDWORKS World 2016)

MyDocMan Class


The MyDocMan class is the basis for the toolkit.  It takes care of license validation and a plethora of common operations shared below.  The functions and procedures are documented in the project to make it easier to use in your own tools.

Instantiating the class will connect to the SOLIDWORKS Document Manager and is built to read your Document Manager license from a text file named “DocMgrLicense.txt” from C:\.  Edit the GetDocManApp procedure to insert and compile your license, redirect the license file location, or point to license information saved in My.Settings.

Common Operations



GetDocument

Summary:
From a full file path, return an opened SwDMDocument.

Parameters:
FilePath: full path to the desired SOLIDWORKS file
forReadOnly: True to open the file read-only, False to open with write access.

Return Values:
Returns an SwDMDocument interface if successful.

GetAllProperties

Summary:
Function to read all file and configuration specific custom properties from a document.

Parameters:
dmDoc: Document Manager document

Return Values:
A tab-delimited string containing all configuration names, properties and values. An empty string in the configuration column indicates a file property.

Remarks:
Each line in the string will terminate in a carriage return character.

GetDelimitedTableText

Summary:
Get all text from a named table with the desired delimiter.

Parameters:
tableName: Name of the table in the SOLIDWORKS file
dmDoc: the SwDMDocument10 interface containing the table
delimiter: the desired column delimiter

Return Values:
A string representing the text of the entire table, including headers. Return characters are applied between rows.

GetBOMTableNames

Summary:
Gets all table names from a document.

Parameters:
dmDoc: The desired document containing tables.

Return Values:
An array of strings in an Object.

Remarks:
Use this function before calling GetDelimitedTableText to retrieve a valid BOM table name.

GetMissingReferences

Summary:
Recursive routine to report all missing file references.

Parameters:
dmDoc: Parent document of reported children, typically an assembly or drawing.

Remarks:
Reports back a string of missing reference files separated by a return character.

File Operations


BrowseForFile
GetFilesFromPath

BrowseForFile

Summary:
Browse for a SOLIDWORKS file using the Windows OpenFileDialog

Return Values:
Returns the full path to the selected file.

GetFilesFromPath

Summary:
Get all file paths from a user-selected directory.

Parameters:
extensionFilter: a file extension to filter. "*.sld*" would retrieve all SOLIDWORKS files.
includeSubFolders: Set to True to get files from all sub folders

Return Values:
Returns an array of full file paths.

Remarks:
Temp files, ~$Part1.sldprt, should be filtered from your results before processing.

10 comments:

  1. Hi, nice stuff, thanks. Made a modified version to read couple props that we needed to compare. Is there a good way to read only parts and assemblies with getfilesfrompath? "*.sld*" is not perfect, because there might be other SW-files that docmanager can't read and code fails. Now I made one exe for parts and other for assemblies with different filters :-)

    ReplyDelete
  2. Hi Markku, glad the toolkit has been helpful! The way the tool is currently developed, you could repeat the process for each file type. For example, get all part properties, then get all assembly properties. Use something like this...

    files() = dm.GetFilesFromPath("*.sldprt", True)
    ...
    For Each File As String In files
    ...
    Next

    files() = dm.GetFilesFromPath("*.sldasm", True)
    ...
    For Each file As String in files
    ...
    Next

    If you want to trick out GetFilesFromPath to handle multiple extensions alone, check out the following post.
    http://www.vbforums.com/showthread.php?664831-Multiple-file-extensions-with-GetFiles()

    ReplyDelete
  3. Thanks for the always useful information. This is great information to help garage type SEO people like me.aSolidWorks 2016_ 2017

    ReplyDelete
  4. Thanks for the always useful information. This is great information to help garage type SEO people like me.SOLIDWORKS 2017

    ReplyDelete
  5. Hi Mike,
    I'm pretty familiar with the SW & the DM API, but I was just wondering why I can't run my application on someone's computer who does not have SolidWorks installed on their PC.
    Is there anything I need to do so that the Document Manager DLL gets registered on someone else's PC?

    ReplyDelete
  6. Yes, you can. Either install SolidWorks Explorer first, or put your tool into an installer package that includes the Document Manager dll and registers it. The SolidWorks Explorer installation registers the dll too.

    ReplyDelete
  7. This comment has been removed by a blog administrator.

    ReplyDelete
  8. =========================================================================
    Solidworks Custom Document Renamer Log File - Time:5/8/2018 2:53:17 PM
    =========================================================================
    +++ Unable to fetch Document Manager License
    +++Tool Exception: System.NullReferenceException: Object reference not set to an instance of an object.
    at SWDocumentRenamer.SWDocumentRenamer.btnRenamer_Click(Object sender, EventArgs e)


    I am getting above error please suggest how to overcome it

    ReplyDelete
  9. I'm getting a runtime error "Could not load file or assembly 'Interop.SwDocumentMgr, Version=26.0.0.0". the local version of this file in the zip is 24.0.0.0. Any idea why its looking for 26.0.0.0 ?

    ReplyDelete