<?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: Blockreference original position value changes after adding attribute value in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/blockreference-original-bounds-value-changes-after-adding/m-p/13209490#M1497</link>
    <description>&lt;P&gt;actually I want to find the center of blockreference, so I have updated my post&lt;/P&gt;</description>
    <pubDate>Fri, 13 Dec 2024 09:50:24 GMT</pubDate>
    <dc:creator>mayur_kapadnis</dc:creator>
    <dc:date>2024-12-13T09:50:24Z</dc:date>
    <item>
      <title>Blockreference original Bounds value changes after adding attribute value</title>
      <link>https://forums.autodesk.com/t5/net-forum/blockreference-original-bounds-value-changes-after-adding/m-p/13209466#M1495</link>
      <description>&lt;P&gt;I have a blockreference with some TextString attribute value, if I try to take its position, i am getting the block.Bounds value in between somewhere in the text, but i want its original center without any attribute, I have this code which is working perfectly, but is there any other simple way to do this?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="mayur_kapadnis_0-1734082406272.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1445438i49EB2B05A06FF0BC/image-size/medium?v=v2&amp;amp;px=400" role="button" title="mayur_kapadnis_0-1734082406272.png" alt="mayur_kapadnis_0-1734082406272.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public static Point3d GetBlockGeometricCenterWithoutAttribute(BlockReference originalBlock)
{
   // Dictionary to store original attribute values
   Dictionary&amp;lt;AttributeReference, string&amp;gt; originalAttributes = new Dictionary&amp;lt;AttributeReference, string&amp;gt;();

   // Store the original attribute values
   using (Transaction trans = originalBlock.Database.TransactionManager.StartTransaction())
   {
      foreach (ObjectId attId in originalBlock.AttributeCollection)
      {
         AttributeReference attrRef = (AttributeReference)trans.GetObject(attId, OpenMode.ForWrite);
         // Store the original value
         originalAttributes[attrRef] = attrRef.TextString;
         // Clear the attribute value
         attrRef.TextString = string.Empty;
      }

      // Commit the transaction (attributes are cleared)
      trans.Commit();
   }

   // Get the geometric center (position)
   Point3d center = GetBlockCenter(originalBlock);

   // Restore the original attribute values
   using (Transaction trans = originalBlock.Database.TransactionManager.StartTransaction())
   {
      foreach (var attrPair in originalAttributes)
      {
         AttributeReference attrRef = attrPair.Key;
         string originalValue = attrPair.Value;

         // Restore the original value
         if (!attrRef.IsErased)
         {
            attrRef.TextString = originalValue;
         }
      }

      // Commit the transaction (attributes are restored)
      trans.Commit();
   }
   originalAttributes.Clear();
   return center;  // Return the geometric center of the block
}

public static Point3d GetBlockCenter(BlockReference table)
{
   Point3d cornerPointMax = table.GeometricExtents.MaxPoint;
   Point3d cornerPointMin = table.GeometricExtents.MinPoint;
   return new Point3d((cornerPointMax.X + cornerPointMin.X) / 2, (cornerPointMax.Y + cornerPointMin.Y) / 2, 0);
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2024 09:49:46 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/blockreference-original-bounds-value-changes-after-adding/m-p/13209466#M1495</guid>
      <dc:creator>mayur_kapadnis</dc:creator>
      <dc:date>2024-12-13T09:49:46Z</dc:date>
    </item>
    <item>
      <title>Re: Blockreference original position value changes after adding attribute value</title>
      <link>https://forums.autodesk.com/t5/net-forum/blockreference-original-bounds-value-changes-after-adding/m-p/13209481#M1496</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The BlockReference.Position (aka block insertion point) has nothing to do with the block attributes.&lt;/P&gt;
&lt;P&gt;You should post your block if you want someone to be able to help you.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2024 09:43:03 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/blockreference-original-bounds-value-changes-after-adding/m-p/13209481#M1496</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2024-12-13T09:43:03Z</dc:date>
    </item>
    <item>
      <title>Re: Blockreference original position value changes after adding attribute value</title>
      <link>https://forums.autodesk.com/t5/net-forum/blockreference-original-bounds-value-changes-after-adding/m-p/13209490#M1497</link>
      <description>&lt;P&gt;actually I want to find the center of blockreference, so I have updated my post&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2024 09:50:24 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/blockreference-original-bounds-value-changes-after-adding/m-p/13209490#M1497</guid>
      <dc:creator>mayur_kapadnis</dc:creator>
      <dc:date>2024-12-13T09:50:24Z</dc:date>
    </item>
    <item>
      <title>Re: Blockreference original position value changes after adding attribute value</title>
      <link>https://forums.autodesk.com/t5/net-forum/blockreference-original-bounds-value-changes-after-adding/m-p/13209539#M1498</link>
      <description>&lt;P&gt;Another way could be getting the center of the block definition (without attribute definitions) and transforming it with the BlockTransform matrix of the block reference.&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;private static Point3d GetBlockCenter(BlockReference br, Transaction tr)
{
    var btr = (BlockTableRecord)tr.GetObject(br.BlockTableRecord, OpenMode.ForRead);
    var attDefClass = RXObject.GetClass(typeof(AttributeDefinition));
    var extents = new Extents3d();
    foreach (ObjectId id in btr)
    {
        if (id.ObjectClass != attDefClass)
        {
            var entity = (Entity)tr.GetObject(id, OpenMode.ForRead);
            var bounds = entity.Bounds;
            if (bounds.HasValue)
                extents.AddExtents(bounds.Value);
        }
    }
    var center = extents.MinPoint + extents.MinPoint.GetVectorTo(extents.MaxPoint) * 0.5;
    return center.TransformBy(br.BlockTransform);
}&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 13 Dec 2024 10:18:23 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/blockreference-original-bounds-value-changes-after-adding/m-p/13209539#M1498</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2024-12-13T10:18:23Z</dc:date>
    </item>
  </channel>
</rss>

