从 python 中的 AutoCAD API 方法检索输出参数 [英] Retrieve output parameters from an AutoCAD API method in python

查看:20
本文介绍了从 python 中的 AutoCAD API 方法检索输出参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 python 2.7 从 AutoCAD 2016 中的 XRecord 中检索 2 个输出数组,使用 comtypes 导入,第一个数组是整数数组(DXF Group Codes),第二个数组是变体数组(XRecord 的值).

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

相反的方式 这个问题旨在

感兴趣的方法是GetXRecordData,它(根据AutoCAD的文档)如果成功返回None,并且只接受2个输出参数.

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)

并查看DxfGrCdvals的值我发现它们没有发生任何变化,它们仍然等于[]

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.

python有没有办法处理这种方法?

Is there any way to deal with that kind of methods in python?

推荐答案

好吧,我还没有想出任何方法可以从 python 中做到这一点,但是,因为存储在 XRecords 中的数据只是数字和字符串(在我的应用程序中)),作为变体存储在 XRecord 中,我使用 MS Excel 作为中间人向我传递数据.

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.

注意:我得到的所有数字都被检索了,但都是 floats.

Note: All numbers I've got were retrieved but as floats.

并且所有字符串都被检索,但它们的类型是unicode.(您可以使用内置函数 str() 轻松将它们转换为 string)

And all strings were retrieved but their type is unicode. (you can convert them to string easily with the built-in function str())

我就是这样做的.

1-通常作为普通 Windows 用户,打开 Excel,然后打开 Visual Basic Editor,一种方法是转到 Developer 选项卡并单击 Visual Basic 编辑器.

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-从编辑器中插入一个模块(一种方法是从菜单栏:insert>Module),然后左键双击它的 默认名称 并输入mod_facilitate",然后按 Enter.

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 双击其图标.

3-Left-double click on its icon at the project viewer.

4- 将出现一个窗口,将以下代码复制到其中.

4- A window will appear, copy the following code to it.

Sub getxrecord()
'get running AutoCAD object
Dim mycad As AcadApplication, mydoc As AcadDocument, filepath As String
Set mycad = GetObject(, "AutoCAD.Application.20")
'get the selected drawing, provided from python code
With Sheet1
    filepath = .Range(.Cells(1, 1), .Cells(1, 1)).Value
End With

Dim iCount As Integer, i As Integer, j As Integer, CompName As String
iCount = mycad.Documents.Count
For i = 0 To iCount - 1
    CompName = mycad.Documents.Item(i).FullName
    If CompName Like filepath Then
        j = i
        Exit For
    End If
Next i
Set mydoc = mycad.Documents.Item(j)
Dim name2 As String

'get the object from its provided handle

With Sheet1
    handler = .Range(.Cells(2, 1), .Cells(2, 1)).Value
End With
Dim myXRecord As AcadXRecord
Set myXRecord = mydoc.HandleToObject(handler)

Dim DxfGrcd As Variant, Val As Variant
DxfGrcd = Array()
Val = Array()
myXRecord.GetXRecordData DxfGrcd, Val

Dim UB As Integer
UB = UBound(DxfGrcd)
For i = 0 To UB
    With 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 With
Next i

End Sub

5- 从 Tools>References 选择这些参考名称,让其他的保持之前的状态

5- From Tools>References Select these reference names, leaving the others at their previous states

AcSmComponents20 1.0 Type Library
AutoCAD 2016 Type Library
CAO 1.0 Type Library

然后单击确定,然后按 Ctrl+s 保存.

Then click on OK, then hit Ctrl+s to save.

6- 保存文件并将其命名为facilitator",将其保存在 Python 文件的同一目录中.将其保存为 Excel 启用宏的工作簿(扩展名为 .xlsm)

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- 在您的 python 文件中,定义检索 XRecord 数据的函数如下,我将说明它的参数是什么:

7- At your python file, define the function to retrieve XRecord's data as following, I'll tell what are its 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 = []
    for i 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

第二:投保什么

注意:以下所有步骤必须写在XRecord_return的定义之前

1- AutoCAD 必须使用类似 autocad = CreateObject("AutoCAD.Application.20",dynamic=True)autocad = comtypes.client.CreateObject("AutoCAD.Application.20",dynamic=True) 取决于导入和导入表单的范围 [ import comtypes.clientfrom comtypes.client import CreateObject> ],这里导入的作用域是python文件的模块作用域.

1- AutoCAD must be instantiated from python using a line like autocad = CreateObject("AutoCAD.Application.20",dynamic=True) or autocad = comtypes.client.CreateObject("AutoCAD.Application.20",dynamic=True) depending on the scope of importing and importing form [ import comtypes.client or from comtypes.client import CreateObject ], here, importing scope is the python file's module scope.

使用 xl = CreateObject("Excel.Application") 2 实例化 Excel 并使用

2-instantiate Excel using xl = CreateObject("Excel.Application") and open the facilitator file with

xlpath = os.getcwd()
xlpath += '\facilitator.xlsm'
xl = CreateObject("Excel.Application")
from comtypes.gen.Excel import xlRangeValueDefault
xlwb = xl.Workbooks.Open(Filename=xlpath,ReadOnly=0)

3- 您必须知道 XRecord 中存储了多少个元素(不包括关联的 DXF 组代码的数量),这个元素数量就是您将提供给 XRecord_return 作为其 <代码>大小参数.

3- 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 its size argument.

例如存储 3.0 "abc" 5 并具有相应 DXF 组代码 1 2 3 的 XRecord 的大小为 3,而不是 6.

e.g. An XRecord that stores 3.0 "abc" 5 and have correspondent DXF group codes 1 2 3 is of size 3, not 6.

我们只需要它的第一个工作表,您必须提供以下数据:-

We need only its first worksheet, you must provide the following data:-

1- 绘图到单元格A1"的完整路径/目录.

1- The drawing's full path/directory to cell "A1".

要获取绘图的完整路径(如果您有它的 Document 对象),您可以从属性 FullName 中获取它.该值是您将作为 namefile 参数提供给 XRecord_return 的值.

To get the drawing's full path if you have its Document object you can get it from the property FullName. This value is what you'll supply to XRecord_return as its namefile argument.

赋值,例如:xl.Range["A1"].Values[xlRangeValueDefault] = filepath

2-单元格A2"的XRecord的句柄值,可以从XRecord的属性Handle获取.该值是您将提供给 XRecord_return 作为其句柄"参数的值.

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 to XRecord_return as its 'handle' argument.

赋值,例如:xl.Range["A1"].Values[xlRangeValueDefault] = handlevalue

3- 之后,无论您需要在何处获取 XRecords 数据,都调用 XRecord_return 函数,例如

3- After that, wherever you need to get the XRecords data, call the XRecord_return function, like

DxfGrCd,vals = XRecord_return(filepath,handlevalue,size_of_XRecord)

输出是包含相应数据的列表.

The outputs are lists that contain the correspondent data.

当您完成使用 Excel 从所需数量的 XRecord 中检索数据后,使用 xlwb.Close(SaveChanges=0)

When you finish using Excel for retrieving data from as many XRecords as you need, close the facilitator workbook using xlwb.Close(SaveChanges=0)

这篇关于从 python 中的 AutoCAD API 方法检索输出参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆