.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to select all entities of a layer?

2 REPLIES 2
Reply
Message 1 of 3
AakashChopra
4469 Views, 2 Replies

How to select all entities of a layer?

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
        Else
            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)
    Else
        Call oSS.SelectByPolygon(mode, pointsList, groupCode, dataCode)
    End If
    
    Set GetEntities = oSS

End Function

 Layer collection will contains the various layer name

2 REPLIES 2
Message 2 of 3
_gile
in reply to: AakashChopra

Hi,

 

You can use a SelectionFilter:

http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer%27s%20Guide/files/WS1a9193826455f5f...



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 3
Hallex
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)
            Else
                Return
            End If
            Using tr As Transaction = db.TransactionManager.StartTransaction()
                Try
                    
                    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

                    Next

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

        End Sub

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost