Retrieve output parameters from an AutoCAD API method in python -
i'm trying retrieve 2 output arrays xrecord in autocad 2016 using python 2.7, comtypes imported, first array array of integers (dxf group codes) , second array array of variants (the values of xrecord).
the opposite way of this question seeks to
the method of interest getxrecorddata, (according autocad's documentation) if successful returns none, , accepts 2 output arguments.
when try retrieve code like
dxfgrcd = [] vals = [] an_xrecord.getxrecorddata(dxfgrcd, vals) and see values of dxfgrcd , vals found no change happened them, both of them still equal [], same with
dxfgrcd = {} vals = {} anxrecord.getxrecorddata(dxfgrcd, vals) also no change applied on them, both of them still equal {}, though dictionaries , lists mutable.
is there way deal kind of methods in python?
well, haven't figured out way python, however, since data stored in xrecords numbers , strings (in application), stored in xrecord variants, i've used ms excel middle man pass me data.
note: numbers i've got retrieved floats.
and strings retrieved type unicode. (you can convert them string built-in function str())
here's how i've done that.
first: creation of facilitator workbook (our middle man)
1-normally regular windows user, open excel, open visual basic editor, 1 way go developer tab , click on visual basic editor.
2-from editor, insert module (one way menu bar: insert>module), left-double click on default name , type "mod_facilitate", hit enter.
3-left-double click on icon @ project viewer.
4- window appear, copy following code it.
sub getxrecord() 'get running autocad object dim mycad acadapplication, mydoc acaddocument, filepath string set mycad = getobject(, "autocad.application.20") 'get selected drawing, provided python code sheet1 filepath = .range(.cells(1, 1), .cells(1, 1)).value end dim icount integer, integer, j integer, compname string icount = mycad.documents.count = 0 icount - 1 compname = mycad.documents.item(i).fullname if compname filepath j = exit end if next set mydoc = mycad.documents.item(j) dim name2 string 'get object provided handle sheet1 handler = .range(.cells(2, 1), .cells(2, 1)).value end dim myxrecord acadxrecord set myxrecord = mydoc.handletoobject(handler) dim dxfgrcd variant, val variant dxfgrcd = array() val = array() myxrecord.getxrecorddata dxfgrcd, val dim ub integer ub = ubound(dxfgrcd) = 0 ub sheet1 .range(.cells((i + 1), 2), .cells((i + 1), 2)).value = dxfgrcd(i) .range(.cells((i + 1), 3), .cells((i + 1), 3)).value = val(i) end next end sub 5- tools>references select these reference names, leaving others @ previous states
acsmcomponents20 1.0 type library autocad 2016 type library cao 1.0 type library then click on ok, hit ctrl+s save.
6- save file , name "facilitator", save within same directory of python file. save of type excel macro-enabled workbook (has extension .xlsm)
7- @ python file, define function retrieve xrecord's data following, i'll tell arguments for:
def xrecord_return(namefile,handle,size): xl.range["a1"].value[xlrangevaluedefault] = namefile xl.range["a2"].value[xlrangevaluedefault] = handle xl.application.run("facilitator.xlsm!mod_facilitate.getxrecord") dxfgrcd = [] vals = [] in range(0,size): cellb = 'b' + str(i+1) cellc = 'c' + str(i+1) dxfgrcd.append(xl.range[cellb].value[xlrangevaluedefault]) vals.append(xl.range[cellc].value[xlrangevaluedefault]) return dxfgrcd,vals second: insure
note: following steps must written before definition of xrecord_return
1- autocad must instantiated python using line autocad = createobject("autocad.application.20",dynamic=true) or autocad = comtypes.client.createobject("autocad.application.20",dynamic=true) depending on scope of importing , importing form [ import comtypes.client or from comtypes.client import createobject ], here, importing scope python file's module scope.
2-instantiate excel using xl = createobject("excel.application") , open facilitator file with
xlpath = os.getcwd() xlpath += '\\facilitator.xlsm' xl = createobject("excel.application") comtypes.gen.excel import xlrangevaluedefault xlwb = xl.workbooks.open(filename=xlpath,readonly=0) 3- have know how many elements stored in xrecord (excluding number of associated dxf group codes), number of elements you'll supply xrecord_return size argument.
e.g. xrecord stores 3.0 "abc" 5 , have correspondent dxf group codes 1 2 3 of size 3, not 6.
third: supplying data facilitator workbook
we need first worksheet, must provide following data:-
1- drawing's full path/directory cell "a1".
to drawing's full path if have document object can property fullname. value you'll supply xrecord_return namefile argument.
to assign, instance: xl.range["a1"].values[xlrangevaluedefault] = filepath
2-the xrecord's handle value cell "a2", can property handle of xrecord. value you'll supply xrecord_return 'handle' argument.
to assign, instance: xl.range["a1"].values[xlrangevaluedefault] = handlevalue
3- after that, wherever need xrecords data, call xrecord_return function, like
dxfgrcd,vals = xrecord_return(filepath,handlevalue,size_of_xrecord) the outputs lists contain correspondent data.
last, not least
when finish using excel retrieving data many xrecords need, close facilitator workbook using xlwb.close(savechanges=0)
Comments
Post a Comment