Monday, October 16, 2023

Python + AutoCAD, create Table and Hittest

 

In this example, I’ll be importing AxApp24, ActiveX wrappers that are a part of PyRx

Using ActiveX is the easy way to create Tables. Table’s in ARX and .NET have gone through some function changes over the years that can sometimes make it a hassle, at least from a scripting language point of view. Also in this example, I show one method of getting access to a Table’s cell without having to manually select the table first. 

 


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

import AxApp24 as Ax
import AxAppUtils24 as AxUt


def PyRxCmd_makeTable():
    try:
        axApp = Ax.getApp()
        axDoc = axApp.ActiveDocument

        # pick a point for the table location
        tablePnt = axDoc.Utility.GetPoint("\nTable location: ")

        # point, rows, columns, row Height, row width
        axTable = axDoc.ModelSpace.AddTable(tablePnt, 5, 5, 10, 30)
       
        #:)
        axTable.SetTextString(0, 0, 1, "My Py Table")

    except Exception as err:
        traceback.print_exception(err)


def PyRxCmd_hitTest():
    try:
        axApp = Ax.getApp()
        axDoc = axApp.ActiveDocument

        # view vec, just use a Z vector
        hitVec = (0, 0, 1)
        hitPnt = axDoc.Utility.GetPoint("\nSelect cell: ")

        # combine the hitPnt with vsmax to make a fence
        fence = hitPnt + axDoc.GetVariable("VSMAX")

        axSs = axDoc.SelectionSets.Add("AXTBLSS")
        axSs.SelectByPolygon(Ax.constants.acSelectionSetFence,
                             fence, [0], ["ACAD_TABLE"])

        #do the hit test
        for axEnt in axSs:
            axTable = Ax.IAcadTable(axEnt)
            hit = axTable.HitTest(hitPnt, hitVec)
            if hit[0]:
                cellstr = "Cell={},{}".format(hit[1], hit[2])
                axTable.SetTextString(hit[1], hit[2], 1, cellstr)
                return

    except Exception as err:
        traceback.print_exception(err)

    finally:
        axSs.Delete()



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...