<?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: effective block of anonymous block in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/effective-block-of-anonymous-block/m-p/8528427#M26383</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/932124"&gt;@ChrisBatt&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I would return the ObjectID as suggested by others, but you can't just use the dynamic block table to get it as not all anonymous blocks are dynamic which is a common misconception that people seem to have.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What you seem to be suggesting above is not true.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The DynamicBlockTableRecord property of the BlockReference for a dynamic block &lt;EM&gt;&lt;STRONG&gt;always&lt;/STRONG&gt;&lt;/EM&gt; returns the ObjectId of the BlockTableRecord representing a dynamic block's definition, whose name is the 'effective name" of the BlockReference.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If an anonymous block is not associated with a dynamic block, then &lt;EM&gt;it has no name&amp;nbsp;&lt;/EM&gt;(which is why they're called 'anonymous'), so the code you posted is not really applicable for non-dynamic anonymous blocks. For example, an associative array is also represented by an anonymous block insertion, that does not have a 'true name', and contains no handles in it's XData.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For any dynamic block reference, the code you show does exactly the same thing that the GetEffectiveName() method shown in the original post above does.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anonymous blocks serve many uses. They can be created by any application for any purpose, and may contain no XData at all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 16 Jan 2019 23:52:26 GMT</pubDate>
    <dc:creator>ActivistInvestor</dc:creator>
    <dc:date>2019-01-16T23:52:26Z</dc:date>
    <item>
      <title>effective block of anonymous block</title>
      <link>https://forums.autodesk.com/t5/net-forum/effective-block-of-anonymous-block/m-p/7915990#M26376</link>
      <description>&lt;P&gt;Hi All&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;this function&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;        public static string GetEffectiveName(BlockReference blkref)
        {
            string name;
            using (Transaction acTrans = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
            {
                BlockTableRecord btr = (BlockTableRecord)acTrans.GetObject(blkref.DynamicBlockTableRecord, OpenMode.ForRead);
                name = btr.Name;
            }
            return name;
        }&lt;/PRE&gt;&lt;P&gt;return the name of effective name of anonymous block&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;iam trying to write this code&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;        public static BlockReference GetEffectiveBlock(BlockReference blkref)
        {
            BlockReference blk;
            using (Transaction acTrans = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Database db = doc.Database;

                BlockTable bt = (BlockTable)acTrans.GetObject(db.BlockTableId, OpenMode.ForRead);
                ObjectId idBTR = bt[GetEffectiveName(blkref)];

                BlockTableRecord btr = acTrans.GetObject(idBTR, OpenMode.ForRead) as BlockTableRecord;
                using (BlockReference brf = new BlockReference(new Point3d(0, 0, 0), idBTR))
                {
                    blk = brf;
                }
           }
            return blk;
        }&lt;/PRE&gt;&lt;P&gt;to return the refrence block of this block but it's doesn't work&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;any help ?&lt;/P&gt;</description>
      <pubDate>Sun, 08 Apr 2018 10:10:45 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/effective-block-of-anonymous-block/m-p/7915990#M26376</guid>
      <dc:creator>Amremad</dc:creator>
      <dc:date>2018-04-08T10:10:45Z</dc:date>
    </item>
    <item>
      <title>Re: effective block of anonymous block</title>
      <link>https://forums.autodesk.com/t5/net-forum/effective-block-of-anonymous-block/m-p/7916053#M26377</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You should not use any DBObject outside of the scope of the transaction used to open it or create it, your GetEffectiveBlock function should return the block reference ObjectId instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That said, you neither add the newly created BlockReference to any BlockTableRecord nor to the transaction and you do not commit the transaction.&lt;/P&gt;</description>
      <pubDate>Sun, 08 Apr 2018 11:43:21 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/effective-block-of-anonymous-block/m-p/7916053#M26377</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-04-08T11:43:21Z</dc:date>
    </item>
    <item>
      <title>Re: effective block of anonymous block</title>
      <link>https://forums.autodesk.com/t5/net-forum/effective-block-of-anonymous-block/m-p/7916195#M26378</link>
      <description>&lt;P&gt;Here's an example which returns a BlockReference, it have to be called from inside the scope of the transaction used to open the BlockReference instance passed as argument.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;        public BlockReference InsertEffectiveBlock(BlockReference br)
        {
            if (br == null)
                throw new ArgumentNullException("br");

            var tr = br.Database.TransactionManager.TopTransaction;
            if (tr == null)
                throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.NoActiveTransactions);

            // get the objectId of the 'effective block table record'
            var btrId = br.IsDynamicBlock ? br.DynamicBlockTableRecord : br.BlockTableRecord;

            // get the owner (BlockTableRecord) of the block reference
            var owner = (BlockTableRecord)tr.GetObject(br.OwnerId, OpenMode.ForWrite);

            // create a new reference to the 'eefective' block definition
            var newBr = new BlockReference(Point3d.Origin, btrId);

            // insert the newly created reference an get its ObjectId
            owner.AppendEntity(newBr);
            tr.AddNewlyCreatedDBObject(newBr, true);

            // return the block reference
            return newBr;
        }&lt;/PRE&gt;
&lt;P&gt;Another way, which can be safely used independantly to any transaction is to use an ObjectId as argument, use a separated transaction and return an ObjectId.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;        public ObjectId InsertEffectiveBlock(ObjectId brId)
        {
            if (brId.IsNull)
                throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.NullObjectId);
            
            var db = brId.Database;
            if (db == null)
                throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.NotInDatabase);

            if (brId.ObjectClass != RXObject.GetClass(typeof(BlockReference)))
                throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.WrongObjectType);

            ObjectId newBrId;
            // start a transaction
            using (var tr = db.TransactionManager.StartTransaction())
            {
                // open the block reference
                var br = (BlockReference)tr.GetObject(brId, OpenMode.ForRead);
                
                // get the objectId of the 'effective block table record'
                var btrId = br.IsDynamicBlock ? br.DynamicBlockTableRecord : br.BlockTableRecord;

                // get the owner (BlockTableRecord) of the block reference
                var owner = (BlockTableRecord)tr.GetObject(br.OwnerId, OpenMode.ForWrite);

                // create a new reference to the 'eefective' block definition
                var newBr = new BlockReference(Point3d.Origin, btrId);

                // insert the newly created reference an get its ObjectId
                newBrId = owner.AppendEntity(newBr);
                tr.AddNewlyCreatedDBObject(newBr, true);

                // save changes
                tr.Commit();
            }
            // return the ObjectId of the newly created block reference
            return newBrId;
        }&lt;/PRE&gt;</description>
      <pubDate>Sun, 08 Apr 2018 14:48:57 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/effective-block-of-anonymous-block/m-p/7916195#M26378</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-04-08T14:48:57Z</dc:date>
    </item>
    <item>
      <title>Re: effective block of anonymous block</title>
      <link>https://forums.autodesk.com/t5/net-forum/effective-block-of-anonymous-block/m-p/7916376#M26379</link>
      <description>&lt;P&gt;Adding to&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;'s advice, there isn't any need for a conditional here:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;// get the objectId of the 'effective block table record'
var btrId = br.IsDynamicBlock ? br.DynamicBlockTableRecord : br.BlockTableRecord;

&lt;/PRE&gt;&lt;P&gt;You can just use the DynamicBlockTableRecord property, as it returns the correct value in all cases.&lt;/P&gt;</description>
      <pubDate>Sun, 08 Apr 2018 18:44:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/effective-block-of-anonymous-block/m-p/7916376#M26379</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2018-04-08T18:44:38Z</dc:date>
    </item>
    <item>
      <title>Re: effective block of anonymous block</title>
      <link>https://forums.autodesk.com/t5/net-forum/effective-block-of-anonymous-block/m-p/7918338#M26380</link>
      <description>&lt;P&gt;i don't know how explain hat i need but i will try to discuss if i can&amp;nbsp;&lt;/P&gt;&lt;P&gt;in first i have dynamic block (A) may be has many&amp;nbsp;anonymous names of some instants of the dynamic block (A)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;so your code if i use it i must take a new instant of dynamic block (A) and add it to database as a new entity.&lt;/P&gt;&lt;P&gt;so what i need to do&amp;nbsp;&lt;/P&gt;&lt;P&gt;create function just return the parent of block it self just something like pointer it at TableRecord.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for exmple :&lt;/P&gt;&lt;P&gt;i have dynamic block called "Dynamic" has&amp;nbsp;some&amp;nbsp; anonymous blocks name *U99 ,...., *U50&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;now when i pick the block *U99 just my function return the pure parent object which is called dynamic without add it in database ,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i hope u understand me &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Apr 2018 15:15:21 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/effective-block-of-anonymous-block/m-p/7918338#M26380</guid>
      <dc:creator>Amremad</dc:creator>
      <dc:date>2018-04-09T15:15:21Z</dc:date>
    </item>
    <item>
      <title>Re: effective block of anonymous block</title>
      <link>https://forums.autodesk.com/t5/net-forum/effective-block-of-anonymous-block/m-p/7918403#M26381</link>
      <description>&lt;P&gt;As said upper the DynamicBlockTableRecord property of any block reference returns the ObjectId of the source block definition (BlockTableRecord) of dynamic blocs.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Apr 2018 15:36:59 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/effective-block-of-anonymous-block/m-p/7918403#M26381</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-04-09T15:36:59Z</dc:date>
    </item>
    <item>
      <title>Re: effective block of anonymous block</title>
      <link>https://forums.autodesk.com/t5/net-forum/effective-block-of-anonymous-block/m-p/8526214#M26382</link>
      <description>&lt;P&gt;I would return the ObjectID as suggested by others, but you can't just use the dynamic block table to get it as not all anonymous blocks are dynamic which is a common misconception that people seem to have.&amp;nbsp; This is code that I have to get the true name of a block.&amp;nbsp; You can modify this to get the ObjectID from the effective block.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;                //Get the name of the block
                sRetName = blk.Name;
                BlockTableRecord btr = tr.GetObject(blk.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
                //check to see if this block is dynamic, which then means that the correct name is in the dynamic block table and not the block name
                if (btr.IsDynamicBlock)
                {
                    sRetName = ((BlockTableRecord)blk.DynamicBlockTableRecord.GetObject(OpenMode.ForRead)).Name;
                }
                //if the block is anonymous but not dynamic, then the name is hidden even deeper and need to get the true name as it still has the *U name
                //to do this we need to go through the XData for the block as it links back to the true original block
                else if (btr.IsAnonymous)
                {
                    //Get the XData to search through
                    var xData = btr.XData.AsArray();
                    for (int i = 0; i &amp;lt; xData.Length; i++)
                    {
                        //Check to see if this is the handle to the original block
                        var tv = xData[i];
                        if (tv.TypeCode == (int)DxfCode.ExtendedDataHandle)
                        {
                            long ln;
                            Handle hn;
                            ObjectId id;
                            // Convert the hexadecimal handle string to 64-bit integer so that we can get the objectID and get the real block which we want the name from
                            ln = Convert.ToInt64(tv.Value.ToString(), 16);
                            // Now create a Handle from the long integer
                            hn = new Handle(ln);
                            // And attempt to get an ObjectId for the Handle
                            id = doc.Database.GetObjectId(false, hn, 0);
                            //Then get the original entity which has the real name for the block
                            BlockTableRecord ent = (BlockTableRecord)tr.GetObject(id, OpenMode.ForRead);
                            sRetName = ent.Name;
                        }
                    }
                }&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jan 2019 12:02:09 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/effective-block-of-anonymous-block/m-p/8526214#M26382</guid>
      <dc:creator>ChrisBatt</dc:creator>
      <dc:date>2019-01-16T12:02:09Z</dc:date>
    </item>
    <item>
      <title>Re: effective block of anonymous block</title>
      <link>https://forums.autodesk.com/t5/net-forum/effective-block-of-anonymous-block/m-p/8528427#M26383</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/932124"&gt;@ChrisBatt&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I would return the ObjectID as suggested by others, but you can't just use the dynamic block table to get it as not all anonymous blocks are dynamic which is a common misconception that people seem to have.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What you seem to be suggesting above is not true.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The DynamicBlockTableRecord property of the BlockReference for a dynamic block &lt;EM&gt;&lt;STRONG&gt;always&lt;/STRONG&gt;&lt;/EM&gt; returns the ObjectId of the BlockTableRecord representing a dynamic block's definition, whose name is the 'effective name" of the BlockReference.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If an anonymous block is not associated with a dynamic block, then &lt;EM&gt;it has no name&amp;nbsp;&lt;/EM&gt;(which is why they're called 'anonymous'), so the code you posted is not really applicable for non-dynamic anonymous blocks. For example, an associative array is also represented by an anonymous block insertion, that does not have a 'true name', and contains no handles in it's XData.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For any dynamic block reference, the code you show does exactly the same thing that the GetEffectiveName() method shown in the original post above does.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anonymous blocks serve many uses. They can be created by any application for any purpose, and may contain no XData at all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jan 2019 23:52:26 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/effective-block-of-anonymous-block/m-p/8528427#M26383</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2019-01-16T23:52:26Z</dc:date>
    </item>
    <item>
      <title>Re: effective block of anonymous block</title>
      <link>https://forums.autodesk.com/t5/net-forum/effective-block-of-anonymous-block/m-p/8529457#M26384</link>
      <description>&lt;P&gt;A block can be non-dynamic and anonymous and still lead back to get the effective name of the block.&amp;nbsp; It is true that if the block is dynamic then you can just look at the dynamic block table record.&amp;nbsp; However, when the block is non-dynamic and is anonymous and has XData which contains a record to a handle then you can get the effective name this way.&amp;nbsp; If it does not have XData then it would be a true anonymous block and wouldn't have a name as you said.&amp;nbsp; I have lots of drawings which support this conclusion as I see this on a regular basis which is why I had to come up with this solution.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Jan 2019 12:04:24 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/effective-block-of-anonymous-block/m-p/8529457#M26384</guid>
      <dc:creator>ChrisBatt</dc:creator>
      <dc:date>2019-01-17T12:04:24Z</dc:date>
    </item>
    <item>
      <title>Re: effective block of anonymous block</title>
      <link>https://forums.autodesk.com/t5/net-forum/effective-block-of-anonymous-block/m-p/8531801#M26385</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/932124"&gt;@ChrisBatt&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;A block can be non-dynamic and anonymous and still lead back to get the effective name of the block.&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Plain vanilla AutoCAD does not create anonymous, non-dynamic block references containing Xdata with a handle that references another block.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have such a case, it must have been created by a third-party application or an AutoCAD vertical solution, which isn't really relavant to the topic.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The other issue is with your code: it scans through &lt;STRONG&gt;all&lt;/STRONG&gt; Xdata attached to an object looking for the first handle field, without regards for what application stored it there. Any number of applications can store Xdata on the same object. You have to look at the Xdata for a specific application using its registered application name in order to find something. You also haven't mentioned what that registered application name is in the case of the handle that you are reading from Xdata. If you know what the registered application name is (you're supposed to, because without that all Xdata is ambiguous) t&lt;SPAN style="font-family: inherit;"&gt;hen you should be using the GetXdataForApplication() method, rather than the Xdata property.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Jan 2019 05:30:07 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/effective-block-of-anonymous-block/m-p/8531801#M26385</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2019-01-18T05:30:07Z</dc:date>
    </item>
  </channel>
</rss>

