Monday, October 2, 2023

Using PyAutoCAD with PyRx

 

You can use your existing PyAutoCAD code inside PyRx with a few small changes. 

1, PyAutoCAD has it’s own main and is run out of process, where as with PyRx, the Python interpreter is running inside the ARX module, in AutoCAD, so it’s inside the process. As a result your PyAutoCAD code should run an order of magnitude faster!

2, Since there’s no main, you must execute your code in a command context, E.g. define a command, then run the command from inside AutoCAD

That’s it! This example shows how to run the example posted on PyAutoCAD PyPi page, followed up by an example in PyRx

 

import PyRx as Rx
import PyGe as Ge
import PyGi as Gi
import PyDb as Db
import PyAp as Ap
import PyEd as Ed
import traceback

from pyautocad import Autocad, APoint


# pyautoacad from a command context
def PyRxCmd_pyautoacad():
    try:
        acad = Autocad()

        # acad.prompt has an issue because the code calls print
        # and prompt so it shows on the command line twice,
        # stdio is redirected to to command line with PyRx
        # so just use print
        acad.prompt("Hello, Autocad from Python")
        print(acad.doc.Name)

        #COM API
        p1 = APoint(0, 0)
        p2 = APoint(50, 25)
 
        for i in range(5):
            text = acad.model.AddText('Hi %s!' % i, p1, 2.5)
            acad.model.AddLine(p1, p2)
            acad.model.AddCircle(p1, 10)
            p1.y += 10

    except Exception as err:
        traceback.print_exception(err)

# define pyrx command
def PyRxCmd_pyrx():
    try:
        # calls acutPrintf
        print("Hello, Autocad from Python\n")
        print(Ap.curDoc().docTitle())

        # get the working database and open model space for write
        db = Db.HostApplicationServices().workingDatabase()
        model = Db.BlockTableRecord(db.modelSpaceId(), Db.OpenMode.kForWrite)

        p1 = Ge.Point3d(0, 0, 0)
        p2 = Ge.Point3d(50, 25, 0)

        # move to the right so we don't overlap pyautoacad
        p1 += (Ge.Vector3d.kXAxis * 100)
        p2 += (Ge.Vector3d.kXAxis * 100)

        for i in range(5):
         
            # create a MText object and add it to the database
            mt = Db.MText()
            mt.setDatabaseDefaults()
            mt.setLocation(p1)
            mt.setContents('PyHi %s!' % i)
            mt.setTextHeight(2.5)
            model.appendAcDbEntity(mt)
           
            # create a line and add it to the database
            line = Db.Line(p1, p2)
            model.appendAcDbEntity(line)

            # create a circle and add it to the database
            circle = Db.Circle(p1, Ge.Vector3d.kZAxis, 10)
            model.appendAcDbEntity(circle)
            p1.y += 10

    except Exception as err:
        traceback.print_exception(err)


No comments:

Post a Comment

TraceBoundary sample in Python for AutoCAD

    import traceback from pyrx_imp import Rx from pyrx_imp import Ge from pyrx_imp import Gi from pyrx_imp import Db from pyrx_imp...