• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    *Expert Elite*
    Hallex
    Posts: 1,332
    Registered: ‎10-08-2008

    Re: Insert Block on Multiple Selected Points

    04-26-2012 09:06 AM in reply to: bkenyon13

    Please, explain a bit more

    Do you want to select the objects inside the circles, ellipses, splines?

     

    ~'J'~

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Active Contributor
    Posts: 45
    Registered: ‎04-20-2012

    Re: Insert Block on Multiple Selected Points

    04-27-2012 09:18 AM in reply to: Hallex

    Sorry if it is hard to follow I have trouble explaining things easily.

     

    Basically I would like to be able to do is place blocks at the end points of polylines and lines as well as on nodes on point entities.

     

    I don't want to be restricted to only polylines.

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,332
    Registered: ‎10-08-2008

    Re: Insert Block on Multiple Selected Points

    04-27-2012 10:48 AM in reply to: bkenyon13

    You can retrieve startpoint and endpoint of entities easily in this case,

    sorry, i can't help right now, coz i'm a bit busy yet

     

    ~'J'~

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Active Contributor
    Posts: 45
    Registered: ‎04-20-2012

    Re: Insert Block on Multiple Selected Points

    04-30-2012 08:46 AM in reply to: Hallex

    I understand and thank you for all your help to this point Hallex.

     

    I have worked on some code to try and get it to also select the start and end points of a line and also select point entities, but it is not working right and I am not sure that I have the code correct.

     

    I have attach a txt file with all my code it it, if anyone can look at it and let me know what I am doing wrong.

     

    if someone could easily point out what I have wrong and tell me what it is I should be able to get it fixed, but I am a little lost at this point.

     

    Thanks to all you can assist.

    Please use plain text.
    Active Contributor
    Posts: 45
    Registered: ‎04-20-2012

    Re: Insert Block on Multiple Selected Points

    04-30-2012 09:58 AM in reply to: bkenyon13

    ok, I have got the code workign for polylines as well as lines.

    I am still working on the point entities part of it.

    I have split the code into two seperate commands one for polylines and the other for lines, for some reason the way I was trying to have them in the same command was not working.

     

    however I have ran into a small problem with the one for lines and that if you have two lines connected it will add two blocks to that location as it is both a start point and end point of a line.

     

    would I add an if statement in the shared function for the line or the sub for the line to fix that problem?

    the other thing is how would I tell it to only insert one block at the location?

     

    below is all my code that I have so far and help will be

    Imports Autodesk.AutoCAD.Runtime
    Imports Autodesk.AutoCAD.ApplicationServices
    Imports Autodesk.AutoCAD.DatabaseServices
    Imports Autodesk.AutoCAD.EditorInput
    Imports Autodesk.AutoCAD.Geometry
    Imports Autodesk.AutoCAD.Windows
    
    Public Class Class1
        'Gets the coordinates for the selected polylines
        Public Shared Function GetPolylineCoordinates(ByVal ent As Polyline) As Point3dCollection
            Dim pts As Point3dCollection = New Point3dCollection()
            Dim coord As Point3d
            Dim i As Integer = 0
            For i = 0 To ent.NumberOfVertices - 1
                coord = ent.GetPoint3dAt(i)
                pts.Add(coord)
            Next
            Return pts
        End Function
        'Specifies the command that is used for polylines
        <CommandMethod("plblk")> _
        Public Sub PolylineBlocks()
            'Selects the dwg file to be used as the block that is inserted
            Dim ofd As New OpenFileDialog(title:="Block Selection", defaultName:="", dialogName:="Block Selection", extension:="dwg", flags:=OpenFileDialog.OpenFileDialogFlags.NoFtpSites)
            ofd.ShowDialog()
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim ed As Editor = doc.Editor
            'Specifies the selection area
            Dim ppo As New PromptPointOptions(vbLf & "First Corner:")
            Dim ppr As PromptPointResult = ed.GetPoint(ppo)
            Dim pco As New PromptCornerOptions(vbLf & "Second Corner:", ppr.Value)
            Dim pcr As PromptPointResult = ed.GetCorner(pco)
            'Gets the start and end points of the selected polylines
            Dim p1 As Point3d = ppr.Value
            Dim p2 As Point3d = pcr.Value
            Dim pts As New Point3dCollection()
            pts.Add(p1)
            pts.Add(New Point3d(p2.X, p1.Y, 0))
            pts.Add(p2)
            pts.Add(New Point3d(p1.X, p2.Y, 0))
            'Creates a selection filter
            Dim tv As TypedValue() = New TypedValue() {New TypedValue(0, "LWPOLYLINE")}
            Dim flt As New SelectionFilter(tv)
            Dim res As PromptSelectionResult = ed.SelectCrossingPolygon(pts, flt)
            Using doclock As DocumentLock = doc.LockDocument
                Using tr As Transaction = db.TransactionManager.StartTransaction()
                    Try
                        'Creates the specified block within the dwg
                        Dim bt As BlockTable
                        bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
                        Dim blkid As ObjectId
                        Dim ndb As Database = New Database(False, True)
                        ndb.ReadDwgFile(ofd.Filename, FileOpenMode.OpenForReadAndReadShare, True, Nothing)
                        Dim name As String = SymbolUtilityServices.GetBlockNameFromInsertPathName(ofd.Filename)
                        Dim btr As BlockTableRecord = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite)
                        blkid = db.Insert(name, ndb, True)
                        For Each selobj As SelectedObject In res.Value
                            Dim ent As Entity = tr.GetObject(selobj.ObjectId, OpenMode.ForRead)
                            'Gets the selected polylines
                            Dim pline As Polyline
                            pline = TryCast(ent, Polyline)
                            If pline IsNot Nothing Then
                                'Runs the shared function to get the coordinates of the polylines
                                Dim vertices As Point3dCollection = GetPolylineCoordinates(pline)
                                For i As Integer = 0 To vertices.Count - 1
                                    'Adds the block to the polylines
                                    Dim br As BlockReference = New BlockReference(vertices(i), blkid)
                                    br.SetDatabaseDefaults()
                                    btr.AppendEntity(br)
                                    tr.AddNewlyCreatedDBObject(br, True)
                                Next
                            End If
                        Next
                        'Commits to the changes made
                        tr.Commit()
                    Catch ex As Exception
                        ed.WriteMessage(ex.Message + vbLf + ex.StackTrace)
                    End Try
                End Using
            End Using
        End Sub
        'Get the Coordinates for the selected lines
        Public Shared Function GetLineCoordinates(ByVal ent3 As Line) As Point3dCollection
            Dim pts3 As Point3dCollection = New Point3dCollection()
            Dim spt As Point3d
            Dim ept As Point3d
            spt = ent3.StartPoint
            ept = ent3.EndPoint
            pts3.Add(spt)
            pts3.Add(ept)
            Return pts3
        End Function
        'Specifies the command that is used for lines
        <CommandMethod("lnblk")> _
        Public Sub LineBlocks()
            'Selects the dwg file to be used as the block that is inserted
            Dim ofd As New OpenFileDialog(title:="Block Selection", defaultName:="", dialogName:="Block Selection", extension:="dwg", flags:=OpenFileDialog.OpenFileDialogFlags.NoFtpSites)
            ofd.ShowDialog()
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim ed As Editor = doc.Editor
            'Specifies the selection area
            Dim ppo As New PromptPointOptions(vbLf & "First Corner:")
            Dim ppr As PromptPointResult = ed.GetPoint(ppo)
            Dim pco As New PromptCornerOptions(vbLf & "Second Corner:", ppr.Value)
            Dim pcr As PromptPointResult = ed.GetCorner(pco)
            'Gets the start and end points of the selected polylines
            Dim p1 As Point3d = ppr.Value
            Dim p2 As Point3d = pcr.Value
            Dim pts As New Point3dCollection()
            pts.Add(p1)
            pts.Add(New Point3d(p2.X, p1.Y, 0))
            pts.Add(p2)
            pts.Add(New Point3d(p1.X, p2.Y, 0))
            'Creates a selection filter
            Dim tv As TypedValue() = New TypedValue() {New TypedValue(0, "LINE")}
            Dim flt As New SelectionFilter(tv)
            Dim res As PromptSelectionResult = ed.SelectCrossingPolygon(pts, flt)
            Using doclock As DocumentLock = doc.LockDocument
                Using tr As Transaction = db.TransactionManager.StartTransaction()
                    Try
                        'Creates the specified block within the dwg
                        Dim bt As BlockTable
                        bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
                        Dim blkid As ObjectId
                        Dim ndb As Database = New Database(False, True)
                        ndb.ReadDwgFile(ofd.Filename, FileOpenMode.OpenForReadAndReadShare, True, Nothing)
                        Dim name As String = SymbolUtilityServices.GetBlockNameFromInsertPathName(ofd.Filename)
                        Dim btr As BlockTableRecord = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite)
                        blkid = db.Insert(name, ndb, True)
                        For Each selobj As SelectedObject In res.Value
                            Dim ent As Entity = tr.GetObject(selobj.ObjectId, OpenMode.ForRead)
                            'Gets the selected lines
                            Dim ln As Line
                            ln = TryCast(ent, Line)
                            If ln IsNot Nothing Then
                                'Run the shared function to get the coordinates of the lines
                                Dim vts As Point3dCollection = GetLineCoordinates(ln)
                                For i As Integer = 0 To vts.Count - 1
                                    'Adds the block to the lines
                                    Dim br3 As BlockReference = New BlockReference(vts(i), blkid)
                                    br3.SetDatabaseDefaults()
                                    btr.AppendEntity(br3)
                                    tr.AddNewlyCreatedDBObject(br3, True)
                                Next
                            End If
                        Next
                        'Commits to the changes made
                        tr.Commit()
                    Catch ex As Exception
                        ed.WriteMessage(ex.Message + vbLf + ex.StackTrace)
                    End Try
                End Using
            End Using
        End Sub
    End Class

     

    greatly appreciated

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,332
    Registered: ‎10-08-2008

    Re: Insert Block on Multiple Selected Points

    04-30-2012 09:59 AM in reply to: bkenyon13

    Your subroutines is completely wrong, so I've removed them from code

    See my poor explanation inside the code:

           Public Shared Function GetPolylineCoordinates(ByVal ent As Polyline) As Point3dCollection
                Dim pts As Point3dCollection = New Point3dCollection()
                Dim coord As Point3d
                Dim i As Integer = 0
                For i = 0 To ent.NumberOfVertices - 1
                    coord = ent.GetPoint3dAt(i)
                    pts.Add(coord)
                Next
                Return pts
            End Function
     
            'Specifies the command that is used
            <CommandMethod("instblk")> _
            Public Sub InsertBlocks()
                'Selects the dwg file to be used as the block that is inserted
                Dim ofd As New OpenFileDialog(title:="Block Selection", defaultName:="", dialogName:="Block Selection", extension:="dwg", flags:=OpenFileDialog.OpenFileDialogFlags.NoFtpSites)
                ofd.ShowDialog()
                Dim doc As Document = Application.DocumentManager.MdiActiveDocument
                Dim db As Database = doc.Database
                Dim ed As Editor = doc.Editor
                'Specifies the selection area
                Dim ppo As New PromptPointOptions(vbLf & "First Corner:")
                Dim ppr As PromptPointResult = ed.GetPoint(ppo)
                Dim pco As New PromptCornerOptions(vbLf & "Second Corner:", ppr.Value)
                Dim pcr As PromptPointResult = ed.GetCorner(pco)
                'Gets the start and end points of the selected polylines
                Dim p1 As Point3d = ppr.Value
                Dim p2 As Point3d = pcr.Value
                Dim pts As New Point3dCollection()
                pts.Add(p1)
                pts.Add(New Point3d(p2.X, p1.Y, 0))
                pts.Add(p2)
                pts.Add(New Point3d(p1.X, p2.Y, 0))
                'Creates a selection filter
                '' this line is wrong, because of value types is with 0 code (or the same DxfCode.Start)
                ''Dim tv As TypedValue() = New TypedValue() {New TypedValue(0, "LWPOLYLINE"), New TypedValue(1, "POINT"), New TypedValue(1, "LINE")}
                '' this right:
                Dim tv As TypedValue() = New TypedValue() {New TypedValue(DxfCode.Start, "LWPOLYLINE,LINE,POINT")}
                ''OR:
                ' Dim tv As TypedValue() = New TypedValue() {New TypedValue(0, "LWPOLYLINE"), New TypedValue(0, "POINT"), New TypedValue(0, "LINE")}
                Dim flt As New SelectionFilter(tv)
                Dim res As PromptSelectionResult = ed.SelectCrossingPolygon(pts, flt)
                MsgBox(res.Value.Count)
                Using doclock As DocumentLock = doc.LockDocument
                    Using tr As Transaction = db.TransactionManager.StartTransaction()
                        Try
                            'Creates the specified block within the dwg
                            Dim bt As BlockTable
                            bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
                            Dim blkid As ObjectId
                            Dim ndb As Database = New Database(False, True)
                            ndb.ReadDwgFile(ofd.FileName, FileOpenMode.OpenForReadAndReadShare, True, Nothing)
                            Dim name As String = SymbolUtilityServices.GetBlockNameFromInsertPathName(ofd.FileName)
                            Dim btr As BlockTableRecord = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite)
                            blkid = db.Insert(name, ndb, True)
    
                            For Each selobj As SelectedObject In res.Value
                                Dim ent As Entity = tr.GetObject(selobj.ObjectId, OpenMode.ForRead)
                                'Gets the selected polylines
                                Dim pline As Polyline
                                pline = TryCast(ent, Polyline)
                                'Gets the selected lines
                                Dim pt As DBPoint
                                pt = TryCast(ent, DBPoint) '<-- point entity is type of DBPoint in .NET
                                Dim ln As Line
                                ln = TryCast(ent, Line)
                                If pline IsNot Nothing Then
                                    'Runs the shared function to get the coordinates of the polylines
                                    Dim vertices As Point3dCollection = GetPolylineCoordinates(pline)
                                    For i As Integer = 0 To vertices.Count - 1
                                        'Adds the block to the polylines
                                        Dim br As BlockReference = New BlockReference(vertices(i), blkid)
                                        br.SetDatabaseDefaults()
                                        btr.AppendEntity(br)
                                        tr.AddNewlyCreatedDBObject(br, True)
                                    Next
                                Else
                                    If pt IsNot Nothing Then
                                        Dim verts As Point3d = pt.Position
                                        'Adds the block to the points
                                        Dim br2 As BlockReference = New BlockReference(verts, blkid)
                                        br2.SetDatabaseDefaults()
                                        btr.AppendEntity(br2)
                                        tr.AddNewlyCreatedDBObject(br2, True)
                                    Else
                                        If ln IsNot Nothing Then
                                            Dim vts As Point3dCollection = New Point3dCollection()
                                            vts.Add(ln.StartPoint)
                                            vts.Add(ln.EndPoint)
                                            'Adds the block to the lines
                                            Dim br3 As BlockReference = New BlockReference(vts(0), blkid)
                                            br3.SetDatabaseDefaults()
                                            btr.AppendEntity(br3)
                                            tr.AddNewlyCreatedDBObject(br3, True)
                                            br3 = New BlockReference(vts(1), blkid)
                                            br3.SetDatabaseDefaults()
                                            btr.AppendEntity(br3)
                                            tr.AddNewlyCreatedDBObject(br3, True)
                                        End If
                                    End If
                                End If
                            Next
                            'Commits to the changes made
                            tr.Commit()
                        Catch ex As System.Exception
                            ed.WriteMessage(ex.Message + vbLf + ex.StackTrace)
                        End Try
                    End Using
                End Using
            End Sub

     

    ~'J'~

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Active Contributor
    Posts: 45
    Registered: ‎04-20-2012

    Re: Insert Block on Multiple Selected Points

    04-30-2012 11:17 AM in reply to: Hallex

    Hallex I really do appreciate the time you have spend helping me with this and pretty much giving it to me even though you do not have to it is greatly appreciated.

     

    This works perfect, again thank you.

     

    the last thing that I have an issue trying to resolve with this is that as mentioned earlier when you have two lines where the end point of one meets the start point of another two blocks are added.  I think this could be resolved with an If statement but I am not sure exactly how to put it in.

     

    I am goign to assume it would look something like this:

    If ln.startpoint = ln.endpoint then

           (use only ln.startpoint or something) <---- I am not sure on this part of how to only insert one block

    End If

     

    Possibly even this:

    If ln.startpoint.x = ln.endpoint.x and ln.startpon.y = ln.endpoint.y Then

          (Do Something)

    End If

     

    If you can just let me know if I am going in the right direction or not it would be greatly appreciated.

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,332
    Registered: ‎10-08-2008

    Re: Insert Block on Multiple Selected Points

    04-30-2012 11:29 AM in reply to: bkenyon13

    Okay I understand you want to use "chain-selection" for lines

    to avoid of inserting the duplicate blocks

    In this case you need to write separate Sub for that,

    I will be try to show you that later

    And also to compare points you may use comapring for all

    coordinates with fuzz factor, e.g.:

     

    if (math.Abs( p1.X-p2.X)< 0.0001) And _

    (math.Abs( p1.Y-p2.Y)< 0.0001) And _

    (math.Abs( p1.Z-p2.Z)< 0.0001) then

    (do your job here)

    else

     

    end if

     

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,332
    Registered: ‎10-08-2008

    Re: Insert Block on Multiple Selected Points

    04-30-2012 12:09 PM in reply to: Hallex

    Here is simple code to gather line points,

    hope you could be able to include it in your project

    If not just let me know

            Public Shared Sub TestForLinePoints()
                Dim doc As Document = Application.DocumentManager.MdiActiveDocument
                Dim db As Database = doc.Database
                Dim ed As Editor = doc.Editor
                Dim points As IDictionary(Of Point3d, Integer) = New Dictionary(Of Point3d, Integer)
    
                'Specifies the selection area
                Dim ppo As New PromptPointOptions(vbLf & "First Corner:")
                Dim ppr As PromptPointResult = ed.GetPoint(ppo)
                Dim pco As New PromptCornerOptions(vbLf & "Second Corner:", ppr.Value)
                Dim pcr As PromptPointResult = ed.GetCorner(pco)
                'Gets the start and end points of the selected polylines
                Dim p1 As Point3d = ppr.Value
                Dim p2 As Point3d = pcr.Value
                Dim pts As New Point3dCollection()
                pts.Add(p1)
                pts.Add(New Point3d(p2.X, p1.Y, 0))
                pts.Add(p2)
                pts.Add(New Point3d(p1.X, p2.Y, 0))
                Dim tv As TypedValue() = New TypedValue() {New TypedValue(DxfCode.Start, "LINE")}
    
                Dim flt As New SelectionFilter(tv)
                Dim res As PromptSelectionResult = ed.SelectCrossingPolygon(pts, flt)
                Using doclock As DocumentLock = doc.LockDocument
                    Using tr As Transaction = db.TransactionManager.StartTransaction()
                        Try
                            Dim i As Integer = 1
                            For Each selobj As SelectedObject In res.Value
                                Dim ent As Entity = tr.GetObject(selobj.ObjectId, OpenMode.ForRead)
                                Dim ln As Line = TryCast(ent, Line)
                                Dim ps As Point3d = ln.StartPoint
                                Dim pe As Point3d = ln.EndPoint
                                Try
                                    points.Add(New KeyValuePair(Of Point3d, Integer)(ps, i))
                                    i += 1
                                    points.Add(New KeyValuePair(Of Point3d, Integer)(pe, i))
                                    i += 1
                                Catch
                                End Try
                            Next
                            Dim inspts As Point3dCollection = New Point3dCollection
                            For Each kvp As KeyValuePair(Of Point3d, Integer) In points
                                inspts.Add(kvp.Key)
                            Next kvp
                            MsgBox(inspts.Count)
                            tr.Commit()
                        Catch ex As System.Exception
                            ed.WriteMessage(ex.Message + vbLf + ex.StackTrace)
                        End Try
                    End Using
                End Using
            End Sub

     

    ~'J'~

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Active Contributor
    Posts: 45
    Registered: ‎04-20-2012

    Re: Insert Block on Multiple Selected Points

    05-01-2012 07:07 AM in reply to: Hallex

    Ok, so I look at the code that you gave me and tried a couple different things many of which did not work and some actually crashed AutoCAD.

     

    Anyway below is the code that I have so far for the entire project.  I am have issues with the lines still in getting the block to actually insert.

     

    I have gotten it to return a message box with values, but not insert a block on the lines.  You will probably say that how I have it was not what you intended, but I do not that it works to a point.

     

    Thank you for your assistance

     

    Imports Autodesk.AutoCAD.Runtime
    Imports Autodesk.AutoCAD.ApplicationServices
    Imports Autodesk.AutoCAD.DatabaseServices
    Imports Autodesk.AutoCAD.EditorInput
    Imports Autodesk.AutoCAD.Geometry
    Imports Autodesk.AutoCAD.Windows
    
    Public Class Class1
        'Specifies the command that is used
        <CommandMethod("instblk")> _
        Public Sub InsertBlocks()
            'Selects the dwg file to be used as the block that is inserted
            Dim ofd As New OpenFileDialog(title:="Block Selection", defaultName:="", dialogName:="Block Selection", extension:="dwg", flags:=OpenFileDialog.OpenFileDialogFlags.NoFtpSites)
            ofd.ShowDialog()
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim ed As Editor = doc.Editor
            Dim points As IDictionary(Of Point3d, Integer) = New Dictionary(Of Point3d, Integer)
            'Specifies the selection area
            Dim ppo As New PromptPointOptions(vbLf & "First Corner:")
            Dim ppr As PromptPointResult = ed.GetPoint(ppo)
            Dim pco As New PromptCornerOptions(vbLf & "Second Corner:", ppr.Value)
            Dim pcr As PromptPointResult = ed.GetCorner(pco)
            'Gets the start and end points of the selected polylines
            Dim p1 As Point3d = ppr.Value
            Dim p2 As Point3d = pcr.Value
            Dim pts As New Point3dCollection()
            pts.Add(p1)
            pts.Add(New Point3d(p2.X, p1.Y, 0))
            pts.Add(p2)
            pts.Add(New Point3d(p1.X, p2.Y, 0))
            'Creates a selection filter
            Dim tv As TypedValue() = New TypedValue() {New TypedValue(DxfCode.Start, "LWPOLYLINE,LINE,POINT")}
            Dim flt As New SelectionFilter(tv)
            Dim res As PromptSelectionResult = ed.SelectCrossingPolygon(pts, flt)
            Using doclock As DocumentLock = doc.LockDocument
                Using tr As Transaction = db.TransactionManager.StartTransaction()
                    Try
                        'Creates the specified block within the dwg
                        Dim bt As BlockTable
                        bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
                        Dim blkid As ObjectId
                        Dim ndb As Database = New Database(False, True)
                        ndb.ReadDwgFile(ofd.Filename, FileOpenMode.OpenForReadAndReadShare, True, Nothing)
                        Dim name As String = SymbolUtilityServices.GetBlockNameFromInsertPathName(ofd.Filename)
                        Dim btr As BlockTableRecord = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite)
                        blkid = db.Insert(name, ndb, True)
                        For Each selobj As SelectedObject In res.Value
                            Dim ent As Entity = tr.GetObject(selobj.ObjectId, OpenMode.ForRead)
                            'Gets the selected polylines
                            Dim pline As Polyline
                            pline = TryCast(ent, Polyline)
                            'Gets the selected points
                            Dim pt As DBPoint
                            pt = TryCast(ent, DBPoint)
                            'Gets the selected lines
                            Dim ln As Line
                            ln = TryCast(ent, Line)
                            If pline IsNot Nothing Then
                                'Runs the shared function to get the coordinates of the polylines
                                Dim vertices As Point3dCollection = New Point3dCollection()
                                For i As Integer = 0 To pline.NumberOfVertices - 1
                                    vertices.Add(pline.GetPoint3dAt(i))
                                    'Adds the block to the polylines
                                    Dim br As BlockReference = New BlockReference(vertices(i), blkid)
                                    br.SetDatabaseDefaults()
                                    btr.AppendEntity(br)
                                    tr.AddNewlyCreatedDBObject(br, True)
                                Next
                            Else
                                If pt IsNot Nothing Then
                                    Dim verts As Point3d = pt.Position
                                    'Adds the block to the points
                                    Dim br2 As BlockReference = New BlockReference(verts, blkid)
                                    br2.SetDatabaseDefaults()
                                    btr.AppendEntity(br2)
                                    tr.AddNewlyCreatedDBObject(br2, True)
                                Else
                                    If ln IsNot Nothing Then
                                        Dim i As Integer = 1
                                        Dim ps As Point3d = ln.StartPoint
                                        Dim pe As Point3d = ln.EndPoint
                                        Try
                                            points.Add(New KeyValuePair(Of Point3d, Integer)(ps, i))
                                            i += 1
                                            points.Add(New KeyValuePair(Of Point3d, Integer)(pe, i))
                                            i += 1
                                        Catch
                                        End Try
                                        Dim inspts As Point3dCollection = New Point3dCollection
                                        For Each kvp As KeyValuePair(Of Point3d, Integer) In points
                                            inspts.Add(kvp.Key)
                                        Next kvp
                                        Dim br3 As BlockReference = New BlockReference(inspts(inspts.Count), blkid)
                                        br3.SetDatabaseDefaults()
                                        btr.AppendEntity(br3)
                                        tr.AddNewlyCreatedDBObject(br3, True)
                                    End If
                                End If
                            End If
                        Next
                        'Commits to the changes made
                        tr.Commit()
                    Catch ex As Exception
                        ed.WriteMessage(ex.Message + vbLf + ex.StackTrace)
                    End Try
                End Using
            End Using
        End Sub
    End Class

     

    Please use plain text.