Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ax.AcadBlock.insertBlock inappropriate error type when block does not exist #219

Closed
gswifort opened this issue Mar 18, 2025 · 2 comments
Closed

Comments

@gswifort
Copy link
Contributor

When the block exists everything is ok, if not - I get this message.

msp.insertBlock(Ge.Point3d(0.0, 0.0, 0.0), name, Ge.Scale3d(1.0), 0.0)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9c in position 19: invalid start byte

Image

@CEXT-Dan
Copy link
Owner

I assume, if the block does not exist, then ‘name’ should be the full path for the .dwg file to insert
To create a new block , one would use the block collection. I still need to work out the error messages, they’re a bit unreliable

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

@command
def xdoit():
    axApp = Ap.Application.acadApplication()
    axDoc = axApp.activeDocument()

    newBlockName = "WOOHOO"
    axBlocks = axDoc.blocks()

    names = [blk.name() for blk in axBlocks]
    if not newBlockName in names:
        btr = axBlocks.add(Ge.Point3d(0, 0, 0), newBlockName)
        btr.addCircle(Ge.Point3d(0, 0, 0), 5)

    axSpace = axDoc.modelSpace()
    bref = axSpace.insertBlock(Ge.Point3d(0, 0, 0), newBlockName, Ge.Scale3d(1.0), 0.0)


@command
def xdoit2():
    axApp = Ap.Application.acadApplication()
    axDoc = axApp.activeDocument()
    axSpace = axDoc.modelSpace()
    pth = "M:\\Dev\\Projects\\PyRxGit\\tests\\media\\18X36RP.dwg"
    bref = axSpace.insertBlock(Ge.Point3d(0, 0, 0), pth, Ge.Scale3d(1.0), 0.0)

@CEXT-Dan
Copy link
Owner

added AcadBlocks.has('name') so there's no need to iterate the table

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

@command
def xdoit():
    axApp = Ap.Application.acadApplication()
    axDoc = axApp.activeDocument()

    newBlockName = "WOOHOO"
    axBlocks = axDoc.blocks()
    if not axBlocks.has(newBlockName): # has
        btr = axBlocks.add(Ge.Point3d(0, 0, 0), newBlockName)
        btr.addCircle(Ge.Point3d(0, 0, 0), 5)

    axSpace = axDoc.modelSpace()
    bref = axSpace.insertBlock(Ge.Point3d(0, 0, 0), newBlockName, Ge.Scale3d(1.0), 0.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants