Thanks for your help, and sorry for the bump/delayed response. I figured I would update this in case someone needs it later.
I tried a few different approaches in a122, including your suggestions, but couldn't get it working. However, I recently updated to a130 and have been able to do a lot using the reference operator (&).
The only caveat I've found so far is that "optional" arguments from the AutoCAD API can be left blank but are, in fact, required. So expect your calls to look like "acObj.Method(x, y,,,,)" or similar.
The following script works in a130:
Code: Select all
#SingleInstance Force
;;Get a reference to the active CAD instance using the appropriate version
ACAD_VERSION := 23
ACAD := ComObjActive("AutoCAD.Application." . ACAD_VERSION)
;;For use with <AcadSelectionSet>.Select()
class acSelectionSet {
static Window[] => 0
static Crossing[] => 1
static Previous[] => 3
static Last[] => 4
static All[] => 5
}
;; Delete the selection set named 'ALL' if it exists
;; If it doesn't exist, fail quietly (dangerous game, I know) and continue
try {
ACAD.ActiveDocument.SelectionSets("ALL").Delete()
}
;;Now create the set named 'ALL'
allEnts := ACAD.ActiveDocument.SelectionSets.Add("ALL")
;;Select all mtext on layer 0 in model space
;;VBA Int type is VT_I2 (which evaluates to 2)
dxfType := ComObjArray(VT_I2:=2,3)
dxfType[0] := 410
dxfType[1] := 0
dxfType[2] := 8
;;Strings will need a variant -> VT_VARIANT evaluates to 12
dxfValue := ComObjArray(VT_VARIANT:=12,3)
dxfValue[0]:= "Model"
dxfValue[1]:= "MTEXT"
dxfValue[2]:= "0"
allEnts.Select(acSelectionSet.All,,,dxfType,dxfValue)
;;Loop through the selected ents and set (then get) the xdata
for e in allEnts {
dataType := ComObjArray(VT_I2, 4)
dataValue := ComObjArray(VT_VARIANT, 4)
;;This bracket structure mirrors the way the 'xdata' express tool command attaches xdata from within autocad
dataType[0] := 1001
dataType[1] := 1002
dataType[2] := 1000
dataType[3] := 1002
dataValue[0] := "TEST_APP"
dataValue[1] := "{"
dataValue[2] := "Hello world."
dataValue[3] := "}"
;;Set the XDATA for a TEST_APP application
e.setXData(dataType, dataValue)
;;Predeclare our output variables.
;;Not necessary, just personal preference.
retDataType := ""
retDataValue := ""
;;Get the XDATA and msgbox it
e.getXData("TEST_APP", &retDataType, &retDataValue)
msgbox(retDataValue[2], "XData says...")
;;If you want to delete the data associated with TEST_APP, call setXData with only the app name
;;Set this flag to TRUE to see it in action. Use the XDLIST command to check the results.
flag_deleteXData := FALSE
if(flag_deleteXData){
dataType := ComObjArray(VT_I2, 1)
dataType[0] := 1001
dataValue := ComObjArray(VT_VARIANT, 1)
dataValue[0] := "TEST_APP"
e.setXData(dataType, dataValue)
}
}
Figured I would put this out in the wild, as I've seen very little AutoCAD COM with anything other than VBA or VL.
Thanks again for the help, lexikos. V2 is looking better every day.