US$0.00
0

Author: Amen Jlili

SOLIDWORKS API Memory Access Violations Caused by GDI Object Count and Unreleased COM Objects

When building long-running SOLIDWORKS API tools, crashes are not always caused by high RAM usage. One of the most common causes of instability is excessive GDI object usage combined with unreleased COM objects. This often appears during batch automation jobs where SOLIDWORKS repeatedly opens documents, makes them visible, generates previews, switches windows, or captures images. The most typical symptom is the “Memory access violation” exception which tends to crash SOLIDWORKS. In many cases, the real issue is that GDI objects are slowly accumulating until the process reaches the Windows limit. What Causes GDI Object Leaks? GDI objects are Windows resources used for UI rendering. They include: In the SOLIDWORKS API, GDI count can increase rapidly if you repeatedly: At the same time, COM references to SOLIDWORKS objects can remain alive longer than expected if they are not released manually. Common SOLIDWORKS COM objects that should be released include: Opening documents silently is usually much safer than opening them visibly. When a document is made visible, SOLIDWORKS creates additional windows, previews, feature manager graphics, graphics pipeline objects, and UI resources. For example: swApp.Visible = true;var model = swApp.OpenDoc6(path, type, options, “”, ref errors, ref warnings); If this occurs repeatedly inside a large loop without proper cleanup, the GDI count can climb rapidly. This is especially common when processing: How to Monitor GDI Object Count You can monitor GDI usage directly in Windows Task Manager: Watch the SLDWORKS.exe process while your code runs. If the GDI count keeps climbing and never comes back down, you likely have a leak. Once a process gets close to around 10,000 GDI objects, crashes and instability become much more likely. Best Practices to Avoid Crashes 1. Dispose Graphics Resources Always wrap GDI-related objects in using blocks: using (Bitmap bmp = new Bitmap(width, height))using (Graphics g = Graphics.FromImage(bmp)){ g.Clear(Color.White); // Draw content here bmp.Save(outputPath);} This applies to: 2. Close Documents Immediately Never leave documents open longer than necessary: ModelDoc2 model = null;try{ model = swApp.OpenDoc6(path, type, options, “”, ref errors, ref warnings); // Process document}finally{ if (model != null) { swApp.CloseDoc(model.GetTitle()); Marshal.ReleaseComObject(model); model = null; }} 3. Release COM Objects Aggressively SOLIDWORKS API objects often remain alive even after they go out of scope. Release them manually: if (feature != null){ Marshal.ReleaseComObject(feature); feature = null;}if (component != null){ Marshal.ReleaseComObject(component); component = null;} This is especially important in loops processing hundreds or thousands of components, features, faces, or bodies. 4. Periodically Force Garbage Collection For large batch operations, periodic garbage collection can help reduce memory pressure: GC.Collect();GC.WaitForPendingFinalizers();GC.Collect(); This should not replace proper disposal and COM cleanup, but it can help stabilize long-running jobs. 5. Prefer Silent Processing Whenever possible, process documents silently instead of visibly: int options = (int)swOpenDocOptions_e.swOpenDocOptions_Silent; This reduces UI overhead and significantly lowers GDI usage. Final Thoughts If your SOLIDWORKS API application crashes after running for a long time, do not assume the issue is only RAM usage. Monitor GDI objects, dispose of graphics resources, close documents immediately, and aggressively release COM references. These small changes can make a major difference in the stability of long-running SOLIDWORKS automation tools, especially when processing large assemblies, exporting files, or generating previews. For large PDM tasks, image generation, PDF export, or batch automation jobs, careful cleanup is often the difference between a stable application and one that crashes after a few hundred files.

Read More »

How to Safely Delete a Vault in SOLIDWORKS PDM (Step-by-Step)

Deleting a vault in SOLIDWORKS PDM is a destructive and irreversible operation. It removes the vault definition from the archive server and permanently deletes the associated database. This post walks through the correct order of operations, common prompts you’ll see, and the most frequent error that blocks vault removal.

Read More »

Automating Bill of Materials Extraction in SOLIDWORKS PDM Using the PDMShell BOM Command

Ride of the Valkyries Alert 🎼 — Don’t listen if you don’t enjoy Wagner. When you are managing complex assemblies inside SOLIDWORKS PDM, generating a bill of materials (BOM) is a routine task, but i’s also one of the most repetitive. This is exactly where the PDMShell BOM command becomes invaluable, eliminating manual exports and saving your from those small tasks quitely eat away engineering hours across large teams. PDMShell solves this problem with a simple but extremely powerful tool: the BOMCommand. The BOMCommand extracts a Bill of Materials directly from a SOLIDWORKS file in the PDM vault and exports it as a clean CSV file. It removes all manual steps by providing a scriptable way to generate BOMs using batch jobs, scheduled tasks, or command-line workflows. If you’ve ever needed fully automated downstream manufacturing data, ERP integrations, or repeatable export pipelines, this command is designed for exactly that.

Read More »

PowerShell PDMShell Search Command: Complete Guide for SOLIDWORKS PDM Administrators

PDMShell gives SOLIDWORKS PDM administrators the ability to run precise, scriptable, and automated operations across large sets of files, and the PowerShell PDMShell search command is at the center of that capability. One of its most powerful features is the unified search syntax unlocks a completely different level of control over your PDM vault. Consider the following search command: At first glance, it may appear complicated, but every element serves a clear purpose and is easy to understand once broken down. Name=%.sldprt This filter targets all part files (SLDPRT) using a wildcard pattern.The use of % allows flexible pattern matching, returning any file that ends with .sldprt regardless of prefix or naming convention. Recursive=true This instructs PDMShell to search not only the current folder, but all subfolders beneath it.This is especially important for large vaults, deep project structures, or multi-level assemblies. @Document Number~143 This uses a variable-based search.PDMShell looks up the PDM variable named “Document Number” and applies a contains (~) comparison against the value 143. Variable search supports all standard comparison operators, including equals, greater than, less than, contains, and more. What makes this system so powerful is that the same search filter can be passed into a wide range of PDMShell commands. You write the search once, and then apply it to whatever action you need to perform. Below are the PDMShell commands that fully support the -search parameter, along with direct documentation links: Version Control frogleap (increment, decrement, or manage versions)https://pdmshell.com/src/FROGLEAP.html Bulk Variable Updates setvar (update PDM variables in bulk)https://pdmshell.com/src/SETVAR.html File Removal delete (soft-delete matching files)https://pdmshell.com/src/DELETE.htmldestroy (permanently remove matching files)https://pdmshell.com/src/DESTROY.html Local Cache Retrieval get (retrieve files to local cache)https://pdmshell.com/src/GET.html Publishing and Exporting export (PDF, DXF, STEP, and custom publishing)https://pdmshell.com/src/export.html SOLIDWORKS Automation runswmacro (open SOLIDWORKS and execute a macro on each file)https://pdmshell.com/src/runswmacro.html File State Management checkouthttps://pdmshell.com/src/CHECKOUT.htmlcheckinhttps://pdmshell.com/src/CHECKIN.html If you would like more examples, additional documentation, or help building automated workflows, reach out anytime.

Read More »

How to Export Flat Patterns with PDMPublisher

With PDMPublisher, exporting flat patterns from a top-level SOLIDWORKS assembly is completely automated and requires no manual steps.If you have ever wondered how to export flat patterns with PDMPublisher in a reliable, hands-off way, this workflow makes the entire process effortless. Step 1: Set the Export Location In this example, the export location is defined as the DXF subfolder inside the C:\Export directory. By enabling “Export file references individually,” both the main assembly and its referenced parts will be processed automatically. Step 2: Enable Flat Pattern Export Next, check the option “Export sheet metal part to 1:1 flat pattern DXF.”This ensures that only sheet metal components are exported as clean, accurate flat patterns that are ideal for manufacturing. Step 3: Define the Naming Convention File names are automatically built using the part number (or item name) followed by the revision.This keeps every exported file organized and traceable within your vault or output folder. Step 4: Apply Filters (Optional) To avoid processing every component, you can apply filename filters.In this case, the export was limited to parts named base_shape and control_panel, allowing targeted processing for selected models. Step 5: Publish Finally, click Publish.PDM Publisher will begin processing the top-level assembly and its references, loading them into memory, extracting each flat pattern, and saving them to the target folder automatically. Step 6: Review the Results Once complete, your DXF flat pattern files are ready for fabrication, organized, clean, and consistent.

Read More »

SolidWorks Macro to Rebuild Referenced Models in All Drawing Sheets

Description This SolidWorks macro to rebuild referenced models in all drawing sheets works by rebuilding all referenced models for each sheet in an active SOLIDWORKS drawing document. It validates the active document, iterates through every sheet, and for each one, ensures the referenced model views are refreshed. After completing the rebuild process, the macro closes the models to free up memory efficiently. System Requirements Pre-Conditions Post-Conditions VBA Macro Code Macro You can download the macro from here. Customization Need to modify the macro to meet specific requirements or integrate it with other processes? We provide custom macro development tailored to your needs. Contact us

Read More »
0
    0
    Your Cart
    Your cart is emptyReturn to Shop
    0
      0
      Your Cart
      Your cart is emptyReturn to Shop