如何将此 AutoCAD VBA 代码转换为 Python?

问题描述:

我正在尝试使用 Python 在 AutoCAD 中自动化一些绘图,并且我使用 SelectOnScreen 方法.这是 VBA 中的代码:

I'm trying to automate some drawing in AutoCAD using Python and I work with SelectOnScreen method. Here is a code in VBA:

Dim FilterType(0) As Integer
Dim FilterData(0) As Variant
FilterType(0) = 0
FilterData(0) = "TEXT"
selection.SelectOnScreen FilterType, FilterData

在 Python 中,我将其用作:

In Python I use it as:

FilterType = win32com.client.VARIANT(VT_ARRAY|VT_I2, [0]) 
FilterData = win32com.client.VARIANT(VT_ARRAY|VT_VARIANT, ['TEXT'])
selection.SelectOnScreen(FilterType, FilterData)

它可以在 AutoCAD 中运行.但是,我想选择不同类型的对象(文本和多行文本),并且我在 VBA 中有一个代码示例.那么如何将下面的VBA代码翻译成Python呢?

and it works in AutoCAD. However I want to select different types of object (texts and mtexts) and I have an example of code in VBA. So how to translate the following VBA code into Python?

Dim FilterType(1) As Integer
Dim FilterData(1) As Variant
FilterType(0) = 0
FilterData(0) = "Text"
FilterType(1) = 0
FilterData(1) = "MText"
selection.SelectOnScreen FilterType, FilterData

这是我尝试过的 Python 代码,但它在 AutoCAD 中不起作用:

Here is my attempt of Python code I've tried, but it doesn't work in AutoCAD:

FilterType = win32com.client.VARIANT(VT_ARRAY|VT_I2, [0, 0]) 
FilterData = win32com.client.VARIANT(VT_ARRAY|VT_VARIANT, ["MTEXT", "TEXT"])
selection.SelectOnScreen(FilterType, FilterData)

当我尝试使用它时没有任何选择.

Nothing selects when I try to use it.

你的代码没有选择任何东西的原因是因为选择过滤器有一个隐含的AND逻辑,因此一个对象不能同时是TEXT and MTEXT.

The reason that your code fails to select anything is because the selection filter has an implicit AND logic, hence an object cannot be both TEXT and MTEXT.

由于选择过滤器将允许通配符匹配,您可以使用以下内容:

Since the selection filter will permit a wildcard match, you can use the following:

FilterType = win32com.client.VARIANT(VT_ARRAY|VT_I2, [0]) 
FilterData = win32com.client.VARIANT(VT_ARRAY|VT_VARIANT, ['TEXT,MTEXT'])
selection.SelectOnScreen(FilterType, FilterData)

或者,如果您不担心选择 RTEXT 的可能性:

Or, if you're not fussed about the possibility of selecting RTEXT:

FilterType = win32com.client.VARIANT(VT_ARRAY|VT_I2, [0]) 
FilterData = win32com.client.VARIANT(VT_ARRAY|VT_VARIANT, ['*TEXT'])
selection.SelectOnScreen(FilterType, FilterData)

您也可以将逻辑运算符 <OROR> 与组代码 -4 结合使用:

You can alternatively use the logical operators <OR and OR> in conjunction with the group code -4:

FilterType = win32com.client.VARIANT(VT_ARRAY|VT_I2, [-4, 0, 0, -4]) 
FilterData = win32com.client.VARIANT(VT_ARRAY|VT_VARIANT, ['<OR', 'TEXT', 'MTEXT', 'OR>'])
selection.SelectOnScreen(FilterType, FilterData)