GstarCAD 2027 Python Programming Guide

Contents

7691132d282d554d618cf53e

Introduction

b2281f7220bbe12e88521151

Introduction

GstarCAD continuously enhance the integrity and compatibility of GRX, .NET, and LISP, ensuring that users' secondary development programs run smoothly after migration, thereby reducing the cost of secondary development migration.

GstarCAD GRXSDK is a set of programming interfaces developed by GstarCAD that combines GRX and Python. It supports users in extending the functionality of the GstarCAD platform software using Python. With over 790 commonly used Python interfaces, we cover daily usage scenarios, such as creating custom commands, custom graphics and entities, accessing and modifying various drawing data, and implementing custom drawing and analysis algorithms. Users can leverage Python's powerful features for custom development and automation, significantly expanding GRX functionality. This script-based invocation of GRX interfaces supports cross-platform development and lowers the learning cost for new developers.

System Environment Requirements

  • Python 3.11.8

  • Windows 10 and above

Install Python

Download the Python 3.11.8 installation package and install it as administrator, configure the options as below:

ca94e47397a400e010f00d40 900f01feb1e83d3a6378251c 7322215cbe4d60b6d810a897

Then finish the installation.

Import Interface

Use the Import command in the Python file to import the secondary development package:

    from pygcad.core import *
    from pygcad.pygrx import *

pygcad.core includes Python-specific core interfaces such as the @command decorator;

pygcad.pygrx contains various types and methods corresponding to GRX interfaces.

Register Custom CAD Commands

During development, when a user-defined function is decorated with @command, this function is automatically registered as a command in GstarCAD, with the function name as the default command name. For example:

0f3cad4c814547e43d9444a0

The function pyDrawLine() is decorated with @command(), automatically registering it as the GCAD command PYDRAWLINE.

If you don't want to use the function name as the command name, you can specify it in the decorator function command(). The @command decorator is defined as follows:

    def command(local_name="", global_name="", group_name="", cmd_flags=0):

When local_name is empty, the name of the decorated function is automatically used as local_name. If global_name is empty, it automatically uses local_name as global_name. If group_name is empty, it selects global_name as group_name. For example:

5a471e10becc1ed900a837c4

This defines a custom command: PY_MY_CMD.

Load in GstarCAD

For example, we used the secondary development interface to develop our own PYMKENTS command. After running the command, the program will draw a line, a circle with a red linetype, and create a layer named ASDK_MYLAYER.

The Python file content for PYMKENTS command is:

from pygcad.core import *
from pygcad.pygrx import *
#import pysnooper
def createLine():
    startPt = GcGePoint3d(4.0, 2.0, 0.0)
    entPt = GcGePoint3d(10, 7.0, 0.0)
    pLine = GcDbLine(startPt, entPt)
    es, pBlockTable = gcdbHostApplicationServices(
    ).workingDatabase().getBlockTable(GcDb.kForRead)
    es, pBlockTableRecord = pBlockTable.getAt(GCDB_MODEL_SPACE, GcDb.kForWrite)
    pBlockTable.close()
    es, lineId = pBlockTableRecord.appendGcDbEntity(pLine)
    pBlockTableRecord.close()
    pLine.close()
    return lineId
def createCircle():
    center = GcGePoint3d(9.0, 3.0, 0.0)
    normal = GcGeVector3d(0.0, 0.0, 1.0)
    pCirc = GcDbCircle(center, normal, 2.0)
    es, pBlockTable = gcdbHostApplicationServices(
    ).workingDatabase().getBlockTable(GcDb.kForRead)
    es, pBlockTableRecord = pBlockTable.getAt(GCDB_MODEL_SPACE, GcDb.kForWrite)
    pBlockTable.close()
    es, circleId = pBlockTableRecord.appendGcDbEntity(pCirc)
    pBlockTableRecord.close()
    pCirc.close()
    return circleId
def createNewLayer():
    es, pLayerTable = gcdbHostApplicationServices(
    ).workingDatabase().getLayerTable(GcDb.kForWrite)
    pLayerTableRecord = GcDbLayerTableRecord()
    pLayerTableRecord.setName("ASDK_MYLAYER")
    pLayerTable.add(pLayerTableRecord)
    pLayerTable.close()
    pLayerTableRecord.close()
def createGroup(objIds=[], pGroupName=""):
    pGroup = GcDbGroup(pGroupName)
    es, pGroupDict = gcdbHostApplicationServices(
    ).workingDatabase().getGroupDictionary(GcDb.kForWrite)
    es, pGroupId = pGroupDict.setAt(pGroupName, pGroup)
    pGroupDict.close()
    for i in range(len(objIds)):
        pGroup.append(objIds[i])
    pGroup.close()
def changeColor(entId: GcDbObjectId, newColor: int):
    es, pObj = gcdbOpenObject(entId, GcDb.kForWrite)
    if pObj.isKindOf(GcDbEntity.desc()):
        pEntity = GcDbEntity.cast(pObj)
    pEntity.setColorIndex(newColor)
    pEntity.close()
    return Gcad.eOk
def runIt():
    createNewLayer()
    idArr = []
    idArr.append(createLine())
    idArr.append(createCircle())
    changeColor(idArr[-1], 1)
    createGroup(idArr, "ASDK_TEST_GROUP")
@command()
def pyMKENTS():
    runIt()
  • Open GstarCAD 2027, enter the APPLOAD command, the Load/Unload Applications dialog box will pop up:
e2d3a63b44ce44c6cd589178
  • Click Load button and select the ents.py file, it will register the command automatically: PYMKENTS
f3dce4fa66dac816e3600a31
  • Close the dialog box and enter command PYMKENTS, the program automatically runs:
7856db268cebe8e43f06551e b69eaf5aa624fed8cfbdb642