C# .NET Managed API

.NET Cookbook 08: Headless Multi-File DWG Batch Processing Using ReadDwgFile Side Databases

Process hundreds of DWG files in the background without opening them in the CAD editor by instantiating `new Database(false, true)` and calling `ReadDwgFile()` in C# .NET.

1. Headless Side-Database Processing (`new Database(false, true)` & `ReadDwgFile`) for Batch Processing 1,000+ DWG Files

Opening 1,000 DWG files sequentially into the visible GstarCAD MDI editor window (`DocumentCollection.Open`) forces full graphics viewport regeneration, ribbon UI updates, and layout switching—taking 45+ minutes and risking out-of-memory crashes. By instantiating an invisible in-memory **Side Database** via `new Database(buildDefaultDrawing: false, noDocument: true)` and calling `db.ReadDwgFile()`, .NET plugins process thousands of drawings in the background at 20x speed.

Critical Kernel & Memory Safety Rule: When executing transactions inside a headless Side Database, **never** access `Application.DocumentManager.MdiActiveDocument.Database`. instead, start transactions directly on the side database instance (`using (Transaction tr = sideDb.TransactionManager.StartTransaction())`) and temporarily switch `HostApplicationServices.WorkingDatabase = sideDb` around any API call that implicitly queries the current working database, restoring the original working database inside a `finally` block.

2. Managed C# .NET Implementation (new Database(false)

Reference GsMgd.dll and GsDbMgd.dll (with Copy Local = False) in your C# class library project to execute .NET Cookbook 08: Headless Multi-File DWG Batch Processing Using ReadDwgFile Side Databases inside the GstarCAD 2027 CLR runtime:

// GstarCAD 2027 C# .NET API — Headless Side-Database Batch Processor
using Gssoft.GstarCAD.DatabaseServices;

public class SideDbBatchProcessor {
    public void ProcessExternalDrawing(string dwgPath) {
        using (Database sideDb = new Database(false, true)) {
            sideDb.ReadDwgFile(dwgPath, FileOpenMode.OpenForReadAndWriteNoShare, true, null);
            sideDb.CloseInput(true);
            using (Transaction tr = sideDb.TransactionManager.StartTransaction()) {
                // Modify layers, title blocks, or purge RegApps in memory
                tr.Commit();
            }
            sideDb.SaveAs(dwgPath, DwgVersion.Current);
        }
    }
}

3. ObjectARX-to-GRX & .NET API Porting Matrix

When dual-targeting plugins for both AutoCAD and GstarCAD 2027 from a unified Git repository, reference the following symbol, header, and assembly mapping table:

SDK Dimension AutoCAD® ObjectARX / .NET GstarCAD 2027 GRX / .NET Equivalent
Target API Classes new Database(false, true), ReadDwgFile, SaveAs, DwgVersion.Current, HostApplicationServices.WorkingDatabase Identical class & method signatures via compat headers
C++ Binary Extension *.arx / *.dbx *.grx / *.tx (Loaded via APPLOAD or Registry)
Managed .NET Assemblies acmgd.dll, acdbmgd.dll, accoremgd.dll GsMgd.dll, GsDbMgd.dll (Set Copy Local = False)

4. Memory Management, Thread Safety & QA Checklist

  • Deterministic Object Closure & Transaction Disposal: In C++ GRX, every acdbOpenObject() call that returns Acad::eOk must be paired with an explicit pObj->close() before starting a command or returning control to the editor. In C# .NET, always wrap StartTransaction() inside a using block and explicitly call tr.Commit().
  • Document Locking from Modeless Contexts: Whenever modifying the active DWG database from a modeless WPF PaletteSet, WinForms dialog, or external COM event, acquire a write lock via doc.LockDocument() to prevent eLockViolation exceptions.
  • Multi-Core Fiber & Main-Thread Discipline: The DWG database (`AcDbDatabase`) is not thread-safe for concurrent writes. Offload heavy geometric calculations or SQL/REST I/O to background worker threads, then marshal database mutations back to the main CAD UI thread.
Back to GstarCAD Developer Hub → Download GstarCAD 2027 & GRX SDK Test Your CAD API Mastery

Production Compilation, Packaging & Enterprise Deployment Guide

When deploying this developer recipe into commercial enterprise production, development leads should configure automated CI/CD build agents (GitHub Actions or Azure DevOps) using MSBuild command-line flags. Ensure that all dependent assemblies (such as GsMgd.dll and GsDbMgd.dll) have their reference property set to Copy Local = False (Private=False) to prevent deployment conflicts with the host GstarCAD 2027 application folder:

  • Target Framework Alignment: For C# .NET plugins, target .NET Framework 4.8 or .NET 8.0 Windows Desktop runtime depending on your GstarCAD host version. For C++ GRX extensions, compile using Visual Studio 2022 (MSVC v143) with the platform toolset set to 64-bit (x64) and Character Set set to Unicode.
  • Registry-Free Autoloading (.bundle): Package your compiled binary and menu CUIX files into an Autodesk/GstarCAD-compatible Application Package bundle folder (e.g., EnterprisePlugin.bundle/PackageContents.xml) and deploy directly to %APPDATA%\Autodesk\ApplicationPlugins or %PROGRAMDATA%\Gstarsoft\ApplicationPlugins for automatic silent loading without requiring administrative Windows registry rights.
  • Automated Exception Trapping & Telemetry: Always wrap top-level command methods and event handlers inside structured try-catch or AutoLISP vl-catch-all-apply blocks to log diagnostic stack traces to corporate log repositories rather than letting unhandled exceptions crash the host drawing session.
Build Artifact Production Location Deployment Verification Gate
C++ GRX Binary bin/x64/Release/*.grx Verify zero unresolved DLL imports via Dependency Walker / dumpbin /dependents.
Managed .NET DLL bin/Release/net48/*.dll Confirm assemblies are strong-name signed and pass NETLOAD in GstarCAD 2027.
AutoLISP / VLX build/dist/*.vlx or *.lsp Verify clean loading in Startup Suite (APPLOAD) with zero console syntax errors.