0

In OS WIn 7 using Autocad 2007 I try to select items then do stuff Problem is that there are no ITEMS in the selection set ssetObj - not sure why!

Code: works in vba but not standalone 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
Parrish Husband
  • 3,148
  • 18
  • 40
  • Please describe what exactly happens, and how that differs from what you expect. Is there an exception? If so, copy and paste the exception information. – Owen Wengerd Dec 30 '13 at 21:07
  • @Locke, phrases like "not working" and "not indexing properly" have no place on a technical site where precise description is encouraged. If you have a more precise description of the problem than the OP was able to express, then please correct the post instead of mocking me for pointing out its deficiency. – Owen Wengerd Dec 31 '13 at 14:11
  • @OwenWengerd, I'm really having a hard time understanding why this was such a difficult question to follow. I agreed with your assessment on the previous AutoCAD question you commented on, however you clearly showed no effort on your own part in looking at what the OP was asking. "Not indexing properly" actually is stupid, good call. – Parrish Husband Dec 31 '13 at 14:38
  • @OwenWengerd There are many terrible questions on stack that show up every day, this one isn't that bad. The OP posted his work, pointed out where it was breaking, and explained in plain English what he was trying to do. If only all questions on Stack were at least this good, we'd be better off. Holding BRAND NEW users (who show effort) to your own level of technical development knowledge is not only elitist, it's just plain silly. – Parrish Husband Dec 31 '13 at 14:54
  • @Locke, you're entitled to your opinion, but I disagree, and I regard the opinion of the OP as more important than either yours or mine. I don't think high standards are elitist at all. I think it's helpful to take the time to explain to new (and old) users how they can get faster and better responses. In this case, it's not a matter of "understanding the question", but of "understanding the problem". The problem is not clearly explained, so responders are left to guess. That's not helpful for the guessers or the OP. – Owen Wengerd Dec 31 '13 at 17:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44249/discussion-between-locke-and-owen-wengerd) – Parrish Husband Dec 31 '13 at 18:09

1 Answers1

1

Edit: Further investigation shows that you should be calling ssetObj(i) if you want to get indexed items of your selection set.

I'd not worry about trying to get the count of the selection set anyway if you plan on iterating through it. A For Each should suffice to walk though it. One of the problems with going from VBA/VB6 to VB.NET is the temptation to use the same methodology, when it can sometimes be invalid (at times it can be excellent, but .NET is very capable). Here's my entire class that I tested your problem with, just to show how I'm connecting to AutoCAD and interfacing with it.

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

External COM methods like this are going to be slower than you're used to seeing via VBA. Therefore it's definitely worth diving into the in-process AutoCAD .NET stuff to see great performance.

Parrish Husband
  • 3,148
  • 18
  • 40
  • Got the following error running the sample code:"Exception from HRESULT: 0x8002802B (TYPE_E_ELEMENTNOTFOUND)". I use the COM's; Autocad 2007 Type Library and Autodesk.Autocad.interop.common.dll as references. I get no warnings - just the failure to find items in the selectionset after successfully selecting entities on the screen. What references should be used here? – user1611961 Jan 01 '14 at 17:48
  • Those are the correct references. However it is very possible that 2007 doesn't has limited capabilities in comparison to future versions. In your code, how does treating the selection set as an object collection work? ( ssetObj(i) ) – Parrish Husband Jan 02 '14 at 14:19
  • Added: ssobjColl = mydoc.SelectionSets; and obtained the count of selectionsets properly but no 'Items method. I am using Visual Basic 2010 express with Win 7 - everything works in other projects EXCEPT obtaining selection set items. I can communicate with Autocad but can't create a selection set with Items exposed for looping. I am concerned that the Type Library is not registered - but I get no warnings. I checked registered Dll's and do not see the Dll's registered - but if I can grab Autocad the dll's must be working - I think. - Help. – user1611961 Jan 02 '14 at 15:31
  • The type library is definitely registered. The items are directly indexed under the class, rather than put into an items sublist like VBA does. Verify this by pulling up the ObjectBrowser in visual studio and looking at the AcadSelectionSet class. So get to the 'ith' item of the selection set, you would call it like this: ssetObj(i). – Parrish Husband Jan 02 '14 at 17:08