<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: vb.net - delete object by ObjectId - get layerinformation in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/vb-net-delete-object-by-objectid-get-layerinformation/m-p/7367466#M29743</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/3357387"&gt;@jan_tappenbeck&lt;/a&gt; wrote:&lt;BR /&gt;&lt;P&gt;hi!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i&amp;nbsp;want to delete&amp;nbsp;entities by ObjectId.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;question 1&lt;/P&gt;&lt;P&gt;how is it possible to delete entities by the ObjectId?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;question 2&lt;/P&gt;&lt;P&gt;how is it possible to get the layername of this entities by question 1?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;regards Jan&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Here's yet another way:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;   ObjectId myObjectId = .......
&lt;BR /&gt;   // Get the entity's Layer name:&lt;BR /&gt;
   string layerName = ((dynamic)myObjectId).Layer;
&lt;BR /&gt;   // Erase the entity:&lt;BR /&gt;
   ((dynamic)myObjectId).Erase();

&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 10 Sep 2017 01:19:47 GMT</pubDate>
    <dc:creator>ActivistInvestor</dc:creator>
    <dc:date>2017-09-10T01:19:47Z</dc:date>
    <item>
      <title>vb.net - delete object by ObjectId - get layerinformation</title>
      <link>https://forums.autodesk.com/t5/net-forum/vb-net-delete-object-by-objectid-get-layerinformation/m-p/7364091#M29740</link>
      <description>&lt;P&gt;hi!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i&amp;nbsp;want to delete&amp;nbsp;entities by ObjectId.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;question 1&lt;/P&gt;&lt;P&gt;how is it possible to delete entities by the ObjectId?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;question 2&lt;/P&gt;&lt;P&gt;how is it possible to get the layername of this entities by question 1?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;regards Jan&lt;/P&gt;</description>
      <pubDate>Fri, 08 Sep 2017 08:59:55 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/vb-net-delete-object-by-objectid-get-layerinformation/m-p/7364091#M29740</guid>
      <dc:creator>jan_tappenbeck</dc:creator>
      <dc:date>2017-09-08T08:59:55Z</dc:date>
    </item>
    <item>
      <title>Re: vb.net - delete object by ObjectId - get layerinformation</title>
      <link>https://forums.autodesk.com/t5/net-forum/vb-net-delete-object-by-objectid-get-layerinformation/m-p/7364149#M29741</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An ObjectId is mainly an identifier for database resident objects.&lt;/P&gt;
&lt;P&gt;The ObjectId type itself exposes only few members so you cannot directly erase an ObjectId, even less get its layer name (only entities have a Layer property, not other database resident objects as SymbolTable, SymbolTableRecord, DBDictionary).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyway, you can check if the ObjectId refers to an Entity (using ObjectId.ObjectClass), then open it as Entity, and get its layer and/or erase it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See the topics in &lt;A href="http://help.autodesk.com/view/OARX/2018/FRA/?guid=GUID-774B7F11-E374-450B-9E2E-CAE02F4AADFE" target="_blank"&gt;&lt;STRONG&gt;this section&lt;/STRONG&gt;&lt;/A&gt; of the AutoCAD .NET Developer's Guide.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Sep 2017 09:33:09 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/vb-net-delete-object-by-objectid-get-layerinformation/m-p/7364149#M29741</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2017-09-08T09:33:09Z</dc:date>
    </item>
    <item>
      <title>Re: vb.net - delete object by ObjectId - get layerinformation</title>
      <link>https://forums.autodesk.com/t5/net-forum/vb-net-delete-object-by-objectid-get-layerinformation/m-p/7365276#M29742</link>
      <description>&lt;P&gt;Here's a little example using different ways to open an object from its ObjectId.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;C#&lt;/P&gt;
&lt;PRE&gt;        [CommandMethod("TEST")]
        public void Test()
        {
            var ed = AcAp.DocumentManager.MdiActiveDocument.Editor;
            var per = ed.GetEntity("\nSelect object: ");
            if (per.Status == PromptStatus.OK)
                if(TryGetLayer(per.ObjectId, out string layerName))
                {
                    try
                    {
                        Erase(per.ObjectId);
                        ed.WriteMessage($"\nErased an entity on layer: {layerName}");
                    }
                    catch(System.Exception ex)
                    {
                        ed.WriteMessage($"\nError: {ex.Message}");
                    }
                }
        }

        /// &amp;lt;summary&amp;gt;
        /// Tries to get the entiy layer.
        /// Does not care if a transaction is active and uses an OpenCloseTransaction to open the entity.
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name="id"&amp;gt;Entity idenfier.&amp;lt;/param&amp;gt;
        /// &amp;lt;param name="layerName"&amp;gt;Layer name (ouput)&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;true if the objectId is bound to an Entity, false otherwise.&amp;lt;/returns&amp;gt;
        public bool TryGetLayer(ObjectId id, out string layerName)
        {
            if (id.ObjectClass.IsDerivedFrom(RXObject.GetClass(typeof(Entity))))
                using (var tr = id.Database.TransactionManager.StartOpenCloseTransaction())
                {
                    layerName = ((Entity)tr.GetObject(id, OpenMode.ForRead)).Layer;
                    return true;
                }
            layerName = null;
            return false;
        }

        /// &amp;lt;summary&amp;gt;
        /// Erases the DBObject.
        /// Opens the object using the active transaction if any, the Open() method otherwise.
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name="id"&amp;gt;DBObject identifier&amp;lt;/param&amp;gt;
        public void Erase(ObjectId id)
        {
            if (id.Database.TransactionManager.TopTransaction != null)
                id.GetObject(OpenMode.ForWrite).Erase();
            else
#pragma warning disable CS0618
                using (var obj = id.Open(OpenMode.ForWrite))
                    obj.Erase();
#pragma warning restore CS0618
        }&lt;/PRE&gt;
&lt;P&gt;VB&lt;/P&gt;
&lt;PRE&gt;        &amp;lt;CommandMethod("Test")&amp;gt;
        Public Sub Test()
            Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
            Dim per As PromptEntityResult = ed.GetEntity(vbLf + "Select object: ")
            If per.Status = PromptStatus.OK Then
                Dim layerName As String = Nothing
                If TryGetLayer(per.ObjectId, layerName) Then
                    Try
                        Delete(per.ObjectId)
                        ed.WriteMessage(vbLf + $"Erased an entity on layer: {layerName}")
                    Catch ex As System.Exception
                        ed.WriteMessage(vbLf + $"Error: {ex.Message}")
                    End Try
                End If
            End If
        End Sub

        ''' &amp;lt;summary&amp;gt;
        ''' Tries to get the entiy layer.
        ''' Does not care if a transaction is active and uses an OpenCloseTransaction to open the entity.
        ''' &amp;lt;/summary&amp;gt;
        ''' &amp;lt;param name="id"&amp;gt;Entity identifier.&amp;lt;/param&amp;gt;
        ''' &amp;lt;param name="layer"&amp;gt;Layer name (output).&amp;lt;/param&amp;gt;
        ''' &amp;lt;returns&amp;gt;True if the objectId is bound to an Entity, False otherwise.&amp;lt;/returns&amp;gt;
        Public Function TryGetLayer(ByVal id As ObjectId, ByRef layer As String) As Boolean
            If id.ObjectClass.IsDerivedFrom(RXObject.GetClass(GetType(Entity))) Then
                Using tr As OpenCloseTransaction = id.Database.TransactionManager.StartOpenCloseTransaction()
                    layer = DirectCast(tr.GetObject(id, OpenMode.ForRead), Entity).Layer
                    Return True
                End Using
            End If
            Return False
        End Function

        ''' &amp;lt;summary&amp;gt;
        ''' Erases the DBObject.
        ''' Opens the object using the active transaction if any, the Open() method otherwise.
        ''' &amp;lt;/summary&amp;gt;
        ''' &amp;lt;param name="id"&amp;gt;DBObject identifier&amp;lt;/param&amp;gt;
        Public Sub Delete(ByVal id As ObjectId)
            If id.Database.TransactionManager.TopTransaction IsNot Nothing Then
                id.GetObject(OpenMode.ForWrite).Erase()
            Else
#Disable Warning BC40000 ' disable the 'Obsolete' warning
                Using obj = id.Open(OpenMode.ForWrite)
                    obj.Erase()
                End Using
#Enable Warning BC40000
            End If
        End Sub&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Sep 2017 16:19:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/vb-net-delete-object-by-objectid-get-layerinformation/m-p/7365276#M29742</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2017-09-08T16:19:50Z</dc:date>
    </item>
    <item>
      <title>Re: vb.net - delete object by ObjectId - get layerinformation</title>
      <link>https://forums.autodesk.com/t5/net-forum/vb-net-delete-object-by-objectid-get-layerinformation/m-p/7367466#M29743</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/3357387"&gt;@jan_tappenbeck&lt;/a&gt; wrote:&lt;BR /&gt;&lt;P&gt;hi!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i&amp;nbsp;want to delete&amp;nbsp;entities by ObjectId.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;question 1&lt;/P&gt;&lt;P&gt;how is it possible to delete entities by the ObjectId?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;question 2&lt;/P&gt;&lt;P&gt;how is it possible to get the layername of this entities by question 1?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;regards Jan&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Here's yet another way:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;   ObjectId myObjectId = .......
&lt;BR /&gt;   // Get the entity's Layer name:&lt;BR /&gt;
   string layerName = ((dynamic)myObjectId).Layer;
&lt;BR /&gt;   // Erase the entity:&lt;BR /&gt;
   ((dynamic)myObjectId).Erase();

&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 10 Sep 2017 01:19:47 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/vb-net-delete-object-by-objectid-get-layerinformation/m-p/7367466#M29743</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2017-09-10T01:19:47Z</dc:date>
    </item>
    <item>
      <title>Re: vb.net - delete object by ObjectId - get layerinformation</title>
      <link>https://forums.autodesk.com/t5/net-forum/vb-net-delete-object-by-objectid-get-layerinformation/m-p/7367631#M29744</link>
      <description>&lt;P&gt;Yes, using the &lt;A href="https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2016/ENU/AutoCAD-NET/files/GUID-F2A96EC2-B693-498E-8425-258A0CE3653F-htm.html" target="_blank"&gt;DLR&lt;/A&gt; is another option.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With VB, you have to set the 'Option Strict' to off so that you can use late binding.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;' Get the ObjectId as Object:
Dim myObject As Object = myObjectId

' Get the layer name:
Dim layerName As String = myObject.Layer

' Erase the entity:
myObject.Erase()&lt;/PRE&gt;</description>
      <pubDate>Sun, 10 Sep 2017 07:05:27 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/vb-net-delete-object-by-objectid-get-layerinformation/m-p/7367631#M29744</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2017-09-10T07:05:27Z</dc:date>
    </item>
  </channel>
</rss>

