Operating environments
CAD Platform | ZWCAD 2021 or later
IDE | Visual studio 2015 or later
.NET Framework | .NET Framework 4.7
.NET Library files | ZwManaged.dll ZwDatabaseMgd.dll ZcWindows.dll ZdWindows.dll
Development Language | VB.NET or C# etc.


Create a project and set properties

Create a project

Start Visual studio, create new project with template “Class Library (.NET Framework)”, specify the .Net Framework version as 4.7.

Add reference

Now, at the Solution Explorer window, right click the project’s References item and select AddReference….

Select “Browse” tab and click Browse button, add reference to ZwManaged.dll and ZwDatabaseMgd.dll at the installation folder of ZWCAD. The result should be like in the following image.

Additionally, as we will use some COM features with our project, add reference to ZWCAD.exe in the same folder. The result should be like in the following screenshot.

Finally, change the property “Copy Local” of ZwDatabaseMgd and ZwManaged to False.


Run the DLL file

After building a project, a dll file will be generated, then we could run NETLOAD command in ZWCAD to load the DLL file. And then you can run commands which are registered in the code.


Sample code

The following example program is defining the AddCircle command in the command line, will create a red circle with radius of 10 in the drawing and center of the circle is (2, 3, 0).

Steps:

Follow the steps above to create a project and add reference. And then copy andpaste the bellow codes to the Class1.cs file.Overwrite the original code.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using ZwSoft.ZwCAD.ApplicationServices;
using ZwSoft.ZwCAD.Runtime;
using ZwSoft.ZwCAD.DatabaseServices;
using ZwSoft.ZwCAD.Geometry;
namespace TEST
{
    public class SHMain
    {
        [CommandMethod("AddCircle")]
        public void AddCircle()
        {
            Document zcDoc = Application.DocumentManager.MdiActiveDocument;
            Database zcDB = zcDoc.Database;
            Transaction ZcTran = zcDoc.TransactionManager.StartTransaction();
            using (ZcTran)
            {
                BlockTable zcBLT = (BlockTable)ZcTran.GetObject(zcDB.BlockTableId,OpenMode.ForRead);
                BlockTableRecord zcBLTR = (BlockTableRecord)ZcTran.GetObject(zcBLT[BlockTableRecord.ModelSpace],OpenMode.ForWrite);
                Circle zcCircle = new Circle();
                zcCircle.Center = new Point3d(2, 3, 0);
                zcCircle.Radius = 10;
                zcCircle.ColorIndex = 1;
                zcBLTR.AppendEntity(zcCircle);
                ZcTran.AddNewlyCreatedDBObject(zcCircle, true);
                ZcTran.Commit();
            }
            zcDoc.SendStringToExecute("_ZOOM E ", false, false, false);
        }
    }
}

For your convenience, here is VB code:

Imports ZwSoft.ZwCAD.ApplicationServices
Imports ZwSoft.ZwCAD.Runtime
Imports ZwSoft.ZwCAD.DatabaseServices
Imports ZwSoft.ZwCAD.Geometry

Namespace TEST
    Public Class SHMain
        <CommandMethod("AddCircle")> _
        Public Sub AddCircle()
            Dim zcDoc As Document = Application.DocumentManager.MdiActiveDocument
            Dim zcDB As Database = zcDoc.Database
            Using zcTran As Transaction = zcDB.TransactionManager.StartTransaction
                Dim zcBLT As BlockTable
                zcBLT = zcTran.GetObject(zcDB.BlockTableId, OpenMode.ForRead)
                Dim zcBLTR As BlockTableRecord
                zcBLTR = zcTran.GetObject(zcBLT(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
                Dim zcCircle As Circle = New Circle
                zcCircle.Center = New Point3d(2, 3, 0)
                zcCircle.Radius = 10
                zcCircle.ColorIndex = 1
                zcBLTR.AppendEntity(zcCircle)
                zcTran.AddNewlyCreatedDBObject(zcCircle, True)
                zcTran.Commit()
            End Using
            zcDoc.SendStringToExecute("_ZOOM E ", False, False, False)
        End Sub
    End Class
End Namespace