I'm trying to retrieve 2 output Arrays from an XRecord in AutoCAD 2016 using python 2.7, with comtypes imported, the first array is an array of integers (DXF Group Codes) and the second array is an array of variants (the values of XRecord).
The opposite way of what this question seeks to
The method of interest is GetXRecordData
, which (according to AutoCAD's documentation) if successful returns None, and only accepts 2 output arguments.
when I try to retrieve it with code like
DxfGrCd = []
vals = []
an_XRecord.GetXRecordData(DxfGrCd, vals)
and see the values of DxfGrCd
and vals
I found no change happened to them, both of them still equal to []
, the same is also with
DxfGrCd = {}
vals = {}
anXRecord.GetXRecordData(DxfGrCd, vals)
also no change is applied on them, both of them still equal to {}
, even though dictionaries and lists are mutable.
Is there any way to deal with that kind of methods in python?
Well, I haven't figured out any way to do so from python, however, since the data stored in XRecords are just numbers and strings (in my application), stored in the XRecord as variants, I've used MS Excel as a middle man to pass me data.
Note: All numbers I've got were retrieved but as
floats
.And all strings were retrieved but their type is
unicode
. (you can convert them tostring
easily with the built-in functionstr()
)Here's how I've done that.
First: Creation of The Facilitator Workbook (Our Middle Man)
1-Normally as a regular windows user, open Excel, then open Visual Basic Editor, one way to do that is to go to Developer tab and click on Visual Basic Editor.
2-From the Editor, insert a module (one way is from the menu bar: insert>Module), then left-double click on its default name and type "mod_facilitate", then hit Enter.
3-Left-double click on its icon at the project viewer.
4- A window will appear, copy the following code to it.
5- From Tools>References Select these reference names, leaving the others at their previous states
Then click on OK, then hit Ctrl+s to save.
6- Save the file and name it "facilitator", save it within the same directory of your python file. Save it of type Excel Macro-Enabled Workbook (has the extension .xlsm)
7- At your python file, define the function to retrieve XRecord's data as following, I'll tell what are its arguments for:
Second: What to Insure
Note: All the following steps must be written before the definition of
XRecord_return
1- AutoCAD must be instantiated from python using a line like
autocad = CreateObject("AutoCAD.Application.20",dynamic=True)
orautocad = comtypes.client.CreateObject("AutoCAD.Application.20",dynamic=True)
depending on the scope of importing and importing form [import comtypes.client
orfrom comtypes.client import CreateObject
], here, importing scope is the python file's module scope.2-instantiate Excel using
xl = CreateObject("Excel.Application")
and open the facilitator file with3- You have to know how many elements are stored in the XRecord (excluding the number of associated DXF group codes), this number of elements is what you'll supply to
XRecord_return
as itssize
argument.e.g. An XRecord that stores
3.0 "abc" 5
and have correspondent DXF group codes1 2 3
is of size 3, not 6.Third: Supplying Data to The Facilitator Workbook
We need only its first worksheet, you must provide the following data:-
1- The drawing's full path/directory to cell "A1".
To get the drawing's full path if you have its
Document
object you can get it from the propertyFullName
. This value is what you'll supply toXRecord_return
as itsnamefile
argument.To assign, for instance:
xl.Range["A1"].Values[xlRangeValueDefault] = filepath
2-The XRecord's handle value to cell "A2", you can get it from the property
Handle
of the XRecord. This value is what you'll supply toXRecord_return
as its 'handle' argument.To assign, for instance:
xl.Range["A1"].Values[xlRangeValueDefault] = handlevalue
3- After that, wherever you need to get the XRecords data, call the
XRecord_return
function, likeThe outputs are lists that contain the correspondent data.
Last, But not Least
When you finish using Excel for retrieving data from as many XRecords as you need, close the facilitator workbook using
xlwb.Close(SaveChanges=0)