Scan Assembly Dependencies Without Opening Files
Use GetDocumentDependencies2 to discover all referenced parts, sub-assemblies, and drawings without opening the file. Scans 500+ component assemblies in under a second.
using SolidWorks.Interop.sldworks;
public List<string> ScanDependencies(ISldWorks swApp, string assemblyPath)
{
var uniquePaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
// No file open required — reads metadata only!
object? result = swApp.GetDocumentDependencies2(
assemblyPath, true, true, false);
if (result == null) return new List<string>();
var depArray = (object[])result;
// Result is [name, path, name, path, ...] — stride by 2
for (int i = 0; i < depArray.Length; i += 2)
{
if (i + 1 >= depArray.Length) break;
string path = depArray[i + 1]?.ToString() ?? "";
if (string.IsNullOrEmpty(path)) continue;
string normalized = Path.GetFullPath(path);
uniquePaths.Add(normalized);
}
// Find associated drawings (not in dependency list)
var drawings = new List<string>();
foreach (var dep in uniquePaths)
{
string drwPath = Path.ChangeExtension(dep, ".SLDDRW");
if (File.Exists(drwPath) && !uniquePaths.Contains(drwPath))
drawings.Add(drwPath);
}
var all = uniquePaths.ToList();
all.AddRange(drawings);
return all;
}How It Works
GetDocumentDependencies2 reads assembly metadata without opening the file. It takes 4 parameters:
| Parameter | Value | Meaning |
|---|---|---|
| Path | assembly path | Full path to the .SLDASM file |
| Traverse | true | Recurse into sub-assemblies |
| SearchFlag | true | Search referenced folders |
| AddReadOnlyInfo | false | Skip read-only metadata (faster) |
The return value is an object[] array of alternating name/path pairs: [name1, path1, name2, path2, ...]. Stride by 2 to extract each dependency.
Why No File-Open
SolidWorks stores dependency information in the file header — a small metadata block at the start of the file. GetDocumentDependencies2 reads only this header, never loading the 3D geometry. This makes it dramatically faster:
- Opening a 500-component assembly: 30-60 seconds
- Scanning dependencies: under 1 second
This is critical for PDM tools, file management utilities, and pre-flight checks.
Finding Drawings
Drawings reference parts/assemblies (a drawing *depends on* a part), but not vice versa. So GetDocumentDependencies2 on an assembly will NOT return its drawings.
The workaround: for each dependency, check if a .SLDDRW file with the same name exists in the same folder. This works because SolidWorks convention is to keep drawings alongside their referenced models.
Fallback Strategy
If SolidWorks is not installed on the machine (common for servers or CI/CD pipelines), fall back to a folder scan:
var files = Directory.GetFiles(folder, "*.SLD*",
SearchOption.AllDirectories);Warning: This returns ALL SolidWorks files in the folder, not just actual dependencies. Always warn the user that results may include unrelated files.
Performance Tip
When scanning multiple assemblies, reuse the same ISldWorks instance. Creating a new SolidWorks COM connection takes 3-5 seconds; reusing an existing one is instant.