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

    AutoCAD Map 3D Developer

    Reply
    Contributor
    khushboo.singh
    Posts: 20
    Registered: ‎11-09-2011

    Delete Polyline for Selected Objects

    10-18-2012 06:38 AM in reply to: khushboo.singh

    Hi

     

    I have a requirement which says, I have to open a dwg file, where I have to select some few polylines and then the selected polylines should be deleted.

    I am able to select objects in the dwg file, but not able to delete it.

    I am using AutoCAD Map 3D 2012 and VB.NET.

    Here I am pasting part of the code that I am having as of now.

     

    Dim acDocMgr As DocumentCollection = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager
                Dim acDoc As Document = acDocMgr.MdiActiveDocument

    'Get the selected objects in the drawing area
                Dim acSSPrompt As PromptSelectionResult = acDoc.Editor.GetSelection()
                MsgBox(acSSPrompt.Status)
                'If the prompt Status is OK, the objects are selected
                If acSSPrompt.Status = PromptStatus.OK Then
                    MsgBox("OK")
                    'Selection Set for the selected objects
                    Dim acSSet As SelectionSet = acSSPrompt.Value

                    For Each acSSObj As SelectedObject In acSSet

                    Dim myMap As New Autodesk.AutoCAD.DatabaseServices.IdMapping

                    Dim myObjs As New Autodesk.AutoCAD.DatabaseServices.ObjectIdCollection

                            If Not IsDBNull(acSSObj) Then
                            'Open the selected object for READ
                            Using trans As Transaction = acDoc.Database.TransactionManager.StartTransaction()
                                Using lock As DocumentLock = acDoc.LockDocument
                                    Dim acEnt As Entity = DirectCast(trans.GetObject(acSSObj.ObjectId, OpenMode.ForWrite), Entity)
                                    acEnt.UpgradeOpen()
                                    acEnt.Erase()
                                End Using
                            End Using
                        End If
                    Next

    Please use plain text.
    *Expert Elite*
    Posts: 6,428
    Registered: ‎06-29-2007

    Re: Delete Polyline for Selected Objects

    10-18-2012 06:44 AM in reply to: khushboo.singh

    Hi,

     

    why do you add your new question to an existing thread? For new questions please create new threads.

     

    And to your code: you haven't commited the transaction :smileywink:

     

    - alfred -

    -------------------------------------------------------------------------
    Alfred NESWADBA
    Ingenieur Studio HOLLAUS ... www.hollaus.at
    -------------------------------------------------------------------------
    Please use plain text.
    Contributor
    khushboo.singh
    Posts: 20
    Registered: ‎11-09-2011

    Re: Delete Polyline for Selected Objects

    10-18-2012 06:47 AM in reply to: alfred.neswadba

    Sorry for maintaining the same thread
    I will open a new one
    And I have committed the transaction, here I put just a part of the code.

    Please use plain text.
    Contributor
    khushboo.singh
    Posts: 20
    Registered: ‎11-09-2011

    Delete Polyline for Selected Objects in the dwg

    10-18-2012 06:50 AM in reply to: khushboo.singh

    Hi

     

    I have a requirement which says, I have to open a dwg file, where I have to select some few polylines and then the selected polylines should be deleted.

    I am able to select objects in the dwg file, but not able to delete it.

    I am using AutoCAD Map 3D 2012 and VB.NET.

    Here I am pasting part of the code that I am having as of now.

     

    Dim acDocMgr As DocumentCollection = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager
                Dim acDoc As Document = acDocMgr.MdiActiveDocument

    'Get the selected objects in the drawing area
                Dim acSSPrompt As PromptSelectionResult = acDoc.Editor.GetSelection()
                MsgBox(acSSPrompt.Status)
                'If the prompt Status is OK, the objects are selected
                If acSSPrompt.Status = PromptStatus.OK Then
                    MsgBox("OK")
                    'Selection Set for the selected objects
                    Dim acSSet As SelectionSet = acSSPrompt.Value

                    For Each acSSObj As SelectedObject In acSSet

                    Dim myMap As New Autodesk.AutoCAD.DatabaseServices.IdMapping

                    Dim myObjs As New Autodesk.AutoCAD.DatabaseServices.ObjectIdCollection

                            If Not IsDBNull(acSSObj) Then
                            'Open the selected object for READ
                            Using trans As Transaction = acDoc.Database.TransactionManager.StartTransaction()
                                Using lock As DocumentLock = acDoc.LockDocument
                                    Dim acEnt As Entity = DirectCast(trans.GetObject(acSSObj.ObjectId, OpenMode.ForWrite), Entity)
                                    acEnt.UpgradeOpen()
                                    acEnt.Erase()
                                End Using
                            End Using

                         trans.Commit()
                        End If
                    Next

    Please use plain text.
    *Expert Elite*
    Posts: 6,428
    Registered: ‎06-29-2007

    Re: Delete Polyline for Selected Objects

    10-18-2012 06:51 AM in reply to: khushboo.singh

    Hi,

     

    >> And I have committed the transaction, here I put just a part of the code.

    No, with End Using you disposed this transaction, and after acEnt.Erase and before End Using I see not trans.Commit

    It does not depend then on any lines after that snippet you showed here.

     

    One more tip: if you opened an object with .ForWrite you don't need the .UpgradeOpen.

     

    - alfred -

    -------------------------------------------------------------------------
    Alfred NESWADBA
    Ingenieur Studio HOLLAUS ... www.hollaus.at
    -------------------------------------------------------------------------
    Please use plain text.
    *Expert Elite*
    Posts: 6,428
    Registered: ‎06-29-2007

    Re: Delete Polyline for Selected Objects in the dwg

    10-18-2012 07:04 AM in reply to: khushboo.singh

    Hi,

     

    now you are commiting the transaction when it is already disposed ... sorry, but when you run this code you should get exceptions (hope you watch them in VS).

     

    - alfred -

    -------------------------------------------------------------------------
    Alfred NESWADBA
    Ingenieur Studio HOLLAUS ... www.hollaus.at
    -------------------------------------------------------------------------
    Please use plain text.
    Contributor
    khushboo.singh
    Posts: 20
    Registered: ‎11-09-2011

    Re: Delete Polyline for Selected Objects

    10-18-2012 07:14 AM in reply to: khushboo.singh

     

    I am confused whether I should commit the transaction or not, as I have already disposed the entity.

     

     

     

    Edited by
    Discussion_Admin

     

    I also merged the two topics into one

     

     

    Please use plain text.
    *Expert Elite*
    Posts: 6,428
    Registered: ‎06-29-2007

    Re: Delete Polyline for Selected Objects

    10-18-2012 10:05 AM in reply to: khushboo.singh

    Hi,

     

    >> I am confused whether I should commit the transaction or not, as I have already disposed the entity.

    You have first to commit the transaction and then dispose it.

     

       Using trans As Transaction = acDoc.Database.TransactionManager.StartTransaction()

           '...do your work, modify your object

           trans.Commit()    'now save the modifications to the database
       End Using     'this disposes the transaction / free up it's ressources

     

    HTH, - alfred -

    -------------------------------------------------------------------------
    Alfred NESWADBA
    Ingenieur Studio HOLLAUS ... www.hollaus.at
    -------------------------------------------------------------------------
    Please use plain text.
    Contributor
    khushboo.singh
    Posts: 20
    Registered: ‎11-09-2011

    Re: Delete Polyline for Selected Objects

    10-18-2012 07:16 PM in reply to: alfred.neswadba

    I modified the code for transaction.

    I tried to use Light Weight Polyline for deleting the polylines fromt the dwg file.

    I have kept that code in bold letter below.

    But it gives an error that there is no object as AcadLWPolyline for AutoCAD Map 3D 2012.

    I am not not able to proceed on how to delete the polyline.

    Please help me out.

      

    Dim acDocMgr As DocumentCollection =Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager
                Dim acDoc As Document = acDocMgr.MdiActiveDocument

                'Get the selected objects in the drawing area
                Dim acSSPrompt As PromptSelectionResult = acDoc.Editor.GetSelection()
                'If the prompt Status is OK, the objects are selected
                If acSSPrompt.Status = PromptStatus.OK Then

                    'Selection Set for the selected objects
                    Dim acSSet As SelectionSet = acSSPrompt.Value
                 
                    'Step through the objects in the Selection Set
                    For Each acSSObj As SelectedObject In acSSet
                        Dim myMap As New Autodesk.AutoCAD.DatabaseServices.IdMapping
                        Dim myObjs As New Autodesk.AutoCAD.DatabaseServices.ObjectIdCollection
                        'Check to make sure a valid object was returned
                        If Not IsDBNull(acSSObj) Then
                            'Open the selected object for READ
                            Using trans As Transaction = acDoc.Database.TransactionManager.StartTransaction()
                                Using lock As DocumentLock = acDoc.LockDocument
                                    'Dim acEnt As Entity = DirectCast(trans.GetObject(acSSObj.ObjectId, OpenMode.ForWrite), Entity)
                                    Dim entities As New ObjectIdCollection(acSSPrompt.Value.GetObjectIds())
                                    For Each oid As ObjectId In entities
                                        If oid.ObjectClass.Name.Equals("AcDbPolyline") Then
                                            Dim pl As Polyline = TryCast(trans.GetObject(oid, OpenMode.ForWrite), Polyline)
                                            Dim acadPl As AcadLWPolyline = TryCast(pl.AcadObject, AcadLWPolyline)
                                            acadPl.Erase()
                                        End If
                                    Next
                                    trans.Commit() --- Here I commited the transaction & later it was disposed
                                End Using
                            End Using
                        End If
                    Next

    Please use plain text.
    *Expert Elite*
    Posts: 6,428
    Registered: ‎06-29-2007

    Re: Delete Polyline for Selected Objects

    10-19-2012 01:41 AM in reply to: khushboo.singh

    Hi,

     

    you can't cast a Polyline to a AcadLwPolyline, that's the same as trying to cast a Circle to a Line, they are totally different objects.

    If you only want to delete specific objects then use the filter-methods during the selection of objects or at least verify the objecttype of the object to delete.

     

    - alfred -

    -------------------------------------------------------------------------
    Alfred NESWADBA
    Ingenieur Studio HOLLAUS ... www.hollaus.at
    -------------------------------------------------------------------------
    Please use plain text.