vb.net autocad 2007 选择集没有项目

问题描述 投票:0回答:2

在使用 Autocad 2007 的 OS WIn 7 中,我尝试选择项目然后执行操作 问题是选择集中 ssetObj 中没有 ITEMS - 不知道为什么!

代码:适用于 vba,但不适用于独立的 vb.net

Private Sub CommandButton1_Click()

Dim myapp As AcadApplication
Dim mydoc As AcadDocument
Dim ssetObj As AcadSelectionSet
Dim ent As AcadObject
Dim numVertices As Long

On Error GoTo err:


Set myapp = GetObject(, "AutoCAD.Application.17")
Set mydoc = myapp.ActiveDocument

    If mydoc.SelectionSets.Count > 0 Then
        mydoc.SelectionSets(0).Delete
    End If

Set ssetObj = mydoc.SelectionSets.Add("ss")

list1.Clear


Me.Hide

AppActivate ("Autocad")

ssetObj.SelectOnScreen:'WORKS TO SELECT

Dim numpls As Integer

numpls = ssetObj.Count:'WORKS TO GET COUNT

Dim i As Integer
For i = 0 To numpls - 1


    Set ent = ssetObj.Item(i)':PROBLEM HERE**THERE ARE NO ITEMS THOUGH COUNT IS CORRECT



    If ent.ObjectName = "AcDbLWPolyline" Or ent.ObjectName = "AcDbPolyline" Then

    numVertices = (UBound(ent.Coordinates) + 1) / 2

        list1.AddItem Str(ent.ObjectID) + "\" + Str(numVertices) + " Vertices"

    End If

Next i

Me.Show


Exit Sub

err:

MsgBox err.Description


End Sub
vb.net selection autocad
2个回答
1
投票

编辑:进一步调查表明,如果您想获取选择集的索引项,您应该调用 ssetObj(i)。

如果您打算迭代它,我无论如何都不担心尝试获取选择集的计数。 A For Each 应该足以遍历它。 从 VBA/VB6 转向 VB.NET 的问题之一是使用相同方法的诱惑,但有时它可能无效(有时它可能非常好,但 .NET 非常强大)。 这是我测试您的问题的整个课程,只是为了展示我如何连接到 AutoCAD 并与其交互。

Public Class frmMain

    Private acApp As AcadApplication
    Private polyList As List(Of String)
    Const acProgId As String = "AutoCAD.Application.17"

    <DllImport("User32.dll")> _
    Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Boolean
    End Function

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        Try
            acApp = DirectCast(Marshal.GetActiveObject(acProgId), AcadApplication)
        Catch
            Try
                Dim acType = Type.GetTypeFromProgID(acProgId)
                acApp = DirectCast(Activator.CreateInstance(acType), AcadApplication)
            Catch ex As Exception
                MsgBox("Unable to create AutoCAD application of type: " & acProgId)
            End Try
        End Try
    End Sub

    Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
        If acApp Is Nothing Then Return
        acApp.Visible = True

        Dim acDoc As AcadDocument = acApp.ActiveDocument

        ' Kill all existing selection sets
        While (acDoc.SelectionSets.Count > 0)
            acDoc.SelectionSets(0).Delete()
        End While

        Dim mySS As AcadSelectionSet = acDoc.SelectionSets.Add("ss")
        SetForegroundWindow(acApp.HWND)

        mySS.SelectOnScreen()
        polyList = New List(Of String)
        Dim numVertices As Integer
        For Each ent As AcadEntity In mySS
            If ent.ObjectName = "AcDbLWPolyline" Or
            ent.ObjectName = "AcDbPolyline" Then
                numVertices = (ent.Coordinates.Length) / 2
                polyList.Add(String.Format("{0} \ {1} Vertices", ent.ObjectID, numVertices))
            End If
        Next

    End Sub
End Class

像这样的外部 COM 方法将比您通过 VBA 看到的要慢。 因此,绝对值得深入研究进程内的 AutoCAD .NET 内容以了解出色的性能。


0
投票

我刚刚学习使用 VBA for AutoCAD,遇到了同样的问题。我不知道为什么,但我们不能直接将元素添加到选择集中。为了添加元素,您必须创建一个 AcadEntity 数组(至少有两个元素,否则它也不起作用)并将其添加到集合中

Option Base 1
Sub Test()
On Error Resume Next 'use it so that when an error occurs, the sets are deleted and there is no need to change their names for the next run
Dim sset As AcadSelectionSet
Dim sset2 As AcadSelectionSet
Dim entry As AcadEntity
Dim earr(2) As AcadEntity 'at least 2 elements in the array are needed

Set sset = ThisDrawing.SelectionSets.Add("SS5")
Set sset2 = ThisDrawing.SelectionSets.Add("SS6")

AppActivate ThisDrawing.Application.Caption
sset.SelectOnScreen

'sort objects by properties and add to a new set
For Each entry In sset
    If entry.ObjectName = "AcDbMText" Then
        Set earr(1) = entry 'i use Option Base 1, so first index = 1
        sset2.AddItems earr
    End If
Next entry

'here we get a set sset2 with the selected elements
'and we can do with it what we want

'delete the sets to use the same names next time
sset.Delete
sset2.Delete

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.