How to translate this AutoCAD VBA code into Python?

会有一股神秘感。 提交于 2019-12-11 05:33:47

问题


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

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)

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

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.


回答1:


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)

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)

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)


来源:https://stackoverflow.com/questions/54408992/how-to-translate-this-autocad-vba-code-into-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!