11 Posts
0 Kudos
Registered: ‎10-07-2012
Post 1 of 3

How to select all entities of a layer?

1027 Views, 2 Replies
10-08-2012 09:27 AM

How to select all entites of a layer?

I want to migrate my VB code to VB.NET code, can anyone help me to migrate the below code?


Private Function GetEntities(oLayerCollection As Collection, sSelectionSetName As String, _
                    Optional pointsList As Variant = Empty, _
                    Optional mode As AcSelect = acSelectionSetCrossingPolygon) As AcadSelectionSet
    Dim oSS As AcadSelectionSet
    Dim Point1(0 To 2) As Double, point2(0 To 2) As Double
    Dim gpCode(0 To 0) As Integer
    Dim dataValue(0 To 0) As Variant
    Dim groupCode As Variant, dataCode As Variant
    Dim vLayer As Variant
    Dim oCurrentDocument As AcadDocument
    ' dummy points
    Point1(0) = 0: Point1(1) = 0: Point1(2) = 0
    point2(0) = 0: point2(1) = 0: point2(2) = 0
    Set oCurrentDocument = m_oApplication.ActiveDocument
    On Error Resume Next
    Set oSS = oCurrentDocument.SelectionSets.Add(sSelectionSetName)
    On Error GoTo 0
    Set oSS = oCurrentDocument.SelectionSets(sSelectionSetName)
    Call oSS.Clear

    gpCode(0) = 8
    For Each vLayer In oLayerCollection
        If dataValue(0) = "" Then
            dataValue(0) = vLayer
            dataValue(0) = dataValue(0) & "," & vLayer
        End If
    Next vLayer
    groupCode = gpCode
    dataCode = dataValue
    If VarType(pointsList) = vbEmpty Then
         Call oSS.Select(acSelectionSetAll, Point1, point2, groupCode, dataCode)
        Call oSS.SelectByPolygon(mode, pointsList, groupCode, dataCode)
    End If
    Set GetEntities = oSS

End Function

 Layer collection will contains the various layer name

*Expert Elite*
2,140 Posts
253 Kudos
Registered: ‎04-29-2006
Post 2 of 3

Re : How to select all entities of a layer?

10-08-2012 11:38 AM in reply to: AakashChopra
*Expert Elite*
1,569 Posts
171 Kudos
Registered: ‎10-08-2008
Post 3 of 3

Re: How to select all entities of a layer?

10-14-2012 12:16 PM in reply to: AakashChopra

Here is extended version of this example:

    <CommandMethod("CSW")> _
        Public Sub TestSelectWindow()
            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

            Dim db As Database = doc.Database

            Dim ed As Editor = doc.Editor
            '' dummy object types for test:
            Dim objects() As String = New String() {"INSERT", "LINE", "LWPOLYLINE", "CIRCLE"}
            '' dummy layers for test:
            Dim layers() As String = New String() {"DIM", "DWGRU", "ANNO-TLB", "Defpoints"}

            Dim layerfilt As String = String.Join(",", layers)

            Dim objfilt As String = String.Join(",", objects)

            '' Create a TypedValue array to define the filter by object types (dxf code 0) and layers (dxf code 8)
            Dim tvs() As TypedValue = New TypedValue() {New TypedValue(0, objfilt), New TypedValue(8, layerfilt)}

            '' Assign the filter criteria to a SelectionFilter object
            Dim filter As SelectionFilter = New SelectionFilter(tvs)

            Dim ppo As PromptPointOptions = New PromptPointOptions(vbLf & "Specify lower legt corner of window: ")

            Dim pres As PromptPointResult = ed.GetPoint(ppo)

            If pres.Status <> PromptStatus.OK Then Return

            Dim p1 As Point3d = pres.Value

            Dim pco As PromptCornerOptions = New PromptCornerOptions(vbLf & "Opposite corner point: ", p1)

            pres = ed.GetCorner(pco)

            If pres.Status <> PromptStatus.OK Then Return

            Dim p2 As Point3d = pres.Value

            '' Request for objects to be selected in the drawing area
            Dim res As PromptSelectionResult
            res = ed.SelectCrossingWindow(p1, p2, filter)

            '' If the prompt status is OK, objects were selected
            'If res.Status = PromptStatus.OK Then
            Dim sset As SelectionSet = res.Value
            If sset.Count > 0 Then
                Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Number of objects selected: " & sset.Count.ToString)
            End If
            Using tr As Transaction = db.TransactionManager.StartTransaction()
                    For Each sobj As SelectedObject In sset
                        Dim ent As Entity = DirectCast(tr.GetObject(sobj.ObjectId, OpenMode.ForRead, False), Entity)
                        Dim blk As BlockReference = TryCast(ent, BlockReference)
                        If blk IsNot Nothing Then
                            ed.WriteMessage(vbLf & "Block Name: {0}", blk.Name)
                        End If
                        Dim circ As Circle = TryCast(ent, Circle)
                        If circ IsNot Nothing Then
                            ed.WriteMessage(vbLf & "Radius: {0}", circ.Radius.ToString())
                        End If
                        Dim pline As Polyline = TryCast(ent, Polyline)
                        If pline IsNot Nothing Then
                            ed.WriteMessage(vbLf & "Number Of Vertices: {0}", pline.NumberOfVertices.ToString())
                        End If
                        Dim ln As Line = TryCast(ent, Line)
                        If ln IsNot Nothing Then
                            ed.WriteMessage(vbLf & "Start Point: {0}", ln.StartPoint.ToString())
                        End If


                Catch ex As System.Exception
                    ed.WriteMessage(ex.StackTrace & vbLf & ex.StackTrace)
                End Try
            End Using

        End Sub



Post to the Community

Have questions about Autodesk products? Ask the community.

New Post