Jump to content

Recommended Posts

Posted (edited)
import traceback
from pyrx import Rx, Ge, Gi, Gs, Db, Ap, Ed, Ax

@Ap.Command()
def mynewcommand():
    try:
        
        print("\nhello world!")
        
    except Exception as err:
        traceback.print_exception(err)

 

So, you’ve decided to take the red pill.

 

from pyrx import Rx, Ge, Gi, Gs, Db, Ap, Ed, Ax

You can think of these as similar to .NET references, or name spaces. Ge = AcGe from ObjectArx, Ax = ActiveX

 

@Ap.Command()

Tells the system to register a new command, you can also register lisp functions, so you can use python to help extend lisp (more on that later)

 

Body of the function.

Command functions should have a try catch to prevent python exceptions from escaping into C++, it’s not the end of the world if it happens, but it’s advised. You don’t need to have try catches in your subroutines.

 

print("\nhello world!")

Since Python is now running in CAD’s process, I have redirected Python’s std_out to AutoCAD’s command line. 

Yay! Stuff like this is forbidden lol!

Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage

Just print will do

 

Edited by Danielm103
Posted (edited)

Next, choose your style, ARX vs AX

 

Ax is ActiveX, the coding style most Lisp or VB(A) users are used to. You don’t need to manage an open close state for the objects. Perfect for quick scripts

 

ARX style, you pass around collections of ObjectIds, in which you open objects with. In this style you may have to manage open close state. Python will help you manage this along the way. Unlike .NET,  you don’t need to use transactions, you don’t need to use a transaction to open objects then cast, there's no commit, Just pass the id and the open state. Python will do all the closing for you

 

import traceback
from pyrx import Rx, Ge, Gi, Gs, Db, Ap, Ed, Ax

@Ap.Command()
def py_arx_vs_ax():
    try:
        
        # for VBA users
        axApp = Ap.Application.acadApplication()
        axDoc = axApp.activeDocument()
        axSpace = axDoc.modelSpace()
        for ent in axSpace:
            ent.setLayer("0")
        
 
        # ARX users
        cdoc = Ap.curDoc()
        db = Db.curDb()
        space = Db.BlockTableRecord(db.modelSpaceId(), Db.OpenMode.kForRead)
        for id in space:
            ent = Db.Entity(id, Db.OpenMode.kForWrite)
            ent.setLayer("0")
        
    except Exception as err:
        traceback.print_exception(err)

 

Edited by Danielm103
Posted (edited)

In Python, ent is a smart pointer, almost exactly like its C++ counterpart

ent = Db.Entity(id, Db.OpenMode.kForWrite)

 

AcDbEntityPointer pEnt(eid, AcDb::OpenMode::kForWrite);

 

ent is closed when it goes out of scope

 

An actual wrapper

 

PyDbEntity::PyDbEntity(const PyDbObjectId& id)
    : PyDbObject(openAcDbObject<AcDbEntity>(id, AcDb::OpenMode::kForRead), false)
{
}

PyDbEntity::PyDbEntity(const PyDbObjectId& id, AcDb::OpenMode mode)
    : PyDbObject(openAcDbObject<AcDbEntity>(id, mode), false)
{
}

PyDbEntity::PyDbEntity(const PyDbObjectId& id, AcDb::OpenMode mode, bool erased)
    : PyDbObject(openAcDbObject<AcDbEntity>(id, mode, erased), false)
{
}

 

Edited by Danielm103
more info

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...