Posts: 11
Registered: ‎10-07-2012
Message 1 of 3 (712 Views)

How to select all entities of a layer?

712 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*
Posts: 1,981
Registered: ‎04-29-2006
Message 2 of 3 (689 Views)

Re : How to select all entities of a layer?

10-08-2012 11:38 AM in reply to: AakashChopra
*Expert Elite*
Posts: 1,555
Registered: ‎10-08-2008
Message 3 of 3 (633 Views)

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




You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Welcome to the new Autodesk Community!
If this is your first visit, click here to get started and make the most of the Community. Let us know what you think of the new experience in the Community Feedback Forum.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community