如何将此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 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)