<?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: how to get geometric extents of a nested block reference? in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/how-to-get-geometric-extents-of-a-nested-block-reference/m-p/13234111#M1327</link>
    <description>&lt;P&gt;The example above doesn't deal with deeply-nested insertions, which requires a slightly-different approach.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This one should work with insertions nested to any depth:&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 class NestedExtentsExample
{
   [CommandMethod("STDCOL")]
   public static void CommandMethod()
   {
      Document doc = Application.DocumentManager.MdiActiveDocument;
      Database db = doc.Database;
      var ed = doc.Editor;
      using(Transaction tr = db.TransactionManager.StartTransaction())
      {
         var pner = ed.GetNestedEntity("\nSelect block - possibly nested");
         if(pner.Status != PromptStatus.OK)
            return;
         var objIds = pner.GetContainers();
         if(objIds is null || objIds.Length == 0)
            return;
         var pl = StandardColumnCreator(tr, objIds[0], pner.Transform);
         var btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
         btr.AppendEntity(pl);
         tr.AddNewlyCreatedDBObject(pl, true);
         tr.Commit();
      }
   }

   static Polyline PolylineFromExtents(Extents3d ext)
   {
      var min = ext.MinPoint;
      var max = ext.MaxPoint;
      var p2 = new Point3d(max.X, min.Y, 0.0);
      var p4 = new Point3d(min.X, max.Y, 0.0);
      var pline = (Polyline)
         Curve.CreateFromGeCurve(
            new CompositeCurve3d(
               new LineSegment3d[]{
                  new LineSegment3d(min, p2),
                  new LineSegment3d(p2, max),
                  new LineSegment3d(max, p4) }));
      pline.Closed = true;
      return pline;
   }

   private static Polyline StandardColumnCreator(Transaction tr, ObjectId blockrefId, Matrix3d xform)
   {
      if(blockrefId.IsNull)
         throw new ArgumentException(nameof(blockrefId));
      var blkref = (BlockReference)tr.GetObject(blockrefId, OpenMode.ForRead);
      var btr = (BlockTableRecord) tr.GetObject(blkref.BlockTableRecord, OpenMode.ForRead);
      Extents3d extents = new Extents3d();
      extents.AddBlockExtents(btr);
      Polyline pline = PolylineFromExtents(extents);
      pline.TransformBy(xform);
      return pline;
   }

}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 29 Dec 2024 22:40:15 GMT</pubDate>
    <dc:creator>ActivistInvestor</dc:creator>
    <dc:date>2024-12-29T22:40:15Z</dc:date>
    <item>
      <title>how to get geometric extents of a nested block reference?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-get-geometric-extents-of-a-nested-block-reference/m-p/13233681#M1323</link>
      <description>&lt;P&gt;Hi and wish every one a peaceful year.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Im confused with blkRef.BlockTransform concept.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a block reference which is nested.&lt;/P&gt;&lt;P&gt;I use GetNestedEntity to select a nested entity but Im only interested in the block references. so I would use the GetContainer method.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but Im not getting the right geometric extents.&lt;/P&gt;&lt;P&gt;what am I doing wrong?&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;using (Transaction tr = db.TransactionManager.StartTransaction())
{
    var pner = ed.GetNestedEntity("\nSelect block - possibly nested");
    if (pner.Status != PromptStatus.OK)
        return;
    var objIds = pner.GetContainers().ToList();
    if (objIds is null)
        return;
    if (objIds.Count == 0)
        return;

    var child = tr.GetObject(objIds[0], OpenMode.ForRead) as BlockReference;
    var parent = tr.GetObject(objIds[objIds.Count-1], OpenMode.ForRead) as BlockReference;

    var pl = StandardColumnCreator(child, parent.BlockTransform);
    var md = tr.GetObject(db.CurrentSpaceId,OpenMode.ForWrite) as BlockTableRecord;
    md.AppendEntity(pl);
    tr.AddNewlyCreatedDBObject(pl, true);
    tr.Commit();
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;private static Polyline StandardColumnCreator(Entity rawEntity, Matrix3d? transform = null)
{
    var extents = rawEntity.GeometricExtents;
    var point1 = extents.MinPoint;
    var point2 = extents.MaxPoint;
    if (transform.HasValue)
    {
        point1 = point1.TransformBy(transform.Value);
        point2 = point2.TransformBy(transform.Value);
    }
    var minPoint = new Point2d(
        point1.X &amp;lt; point2.X ? point1.X : point2.X, point1.Y &amp;lt; point2.Y ? point1.Y : point2.Y);
    var maxPoint = new Point2d(
        point1.X &amp;gt; point2.X ? point1.X : point2.X, point1.Y &amp;gt; point2.Y ? point1.Y : point2.Y);

    var rectanglePolyline = new Polyline();
    rectanglePolyline.AddVertexAt(0, minPoint, 0, 0, 0);
    rectanglePolyline.AddVertexAt(1, new Point2d(maxPoint.X, minPoint.Y), 0, 0, 0);
    rectanglePolyline.AddVertexAt(2, maxPoint, 0, 0, 0);
    rectanglePolyline.AddVertexAt(3, new Point2d(minPoint.X, maxPoint.Y), 0, 0, 0);
    rectanglePolyline.Closed = true;
    return rectanglePolyline;
}&lt;/LI-CODE&gt;</description>
      <pubDate>Sun, 29 Dec 2024 13:04:46 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-get-geometric-extents-of-a-nested-block-reference/m-p/13233681#M1323</guid>
      <dc:creator>a.kouchakzadeh</dc:creator>
      <dc:date>2024-12-29T13:04:46Z</dc:date>
    </item>
    <item>
      <title>Re: how to get geometric extents of a nested block reference?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-get-geometric-extents-of-a-nested-block-reference/m-p/13233914#M1324</link>
      <description>&lt;P&gt;It would be easier to draw the polyline in the same coordinate system as the nested insertion, and then transform it to the current UCS using the BlockTransform of the owning or parent BlockReference.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public static class NestedExtentsExample
{
   [CommandMethod("STDCOL")]
   public static void CommandMethod()
   {
      Document doc = Application.DocumentManager.MdiActiveDocument;
      Database db = doc.Database;
      var ed = doc.Editor;
      using(Transaction tr = db.TransactionManager.StartTransaction())
      {
         var pner = ed.GetNestedEntity("\nSelect block - possibly nested");
         if(pner.Status != PromptStatus.OK)
            return;
         var objIds = pner.GetContainers();
         if(objIds is null || objIds.Length == 0)
            return;
         BlockReference parent = null;
         if(objIds.Length &amp;gt; 1)
            parent = tr.GetObject(objIds[1], OpenMode.ForRead) as BlockReference;
         var blkref = tr.GetObject(objIds[0], OpenMode.ForRead) as BlockReference;
         var pl = StandardColumnCreator(blkref, parent);
         var btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
         btr.AppendEntity(pl);
         tr.AddNewlyCreatedDBObject(pl, true);
         tr.Commit();
      }
   }

   static Polyline PolylineFromExtents(Extents3d ext)
   {
      var min = ext.MinPoint;
      var max = ext.MaxPoint;
      var p2 = new Point3d(max.X, min.Y, 0.0);
      var p4 = new Point3d(min.X, max.Y, 0.0);
      var pline = (Polyline)
         Curve.CreateFromGeCurve(
            new CompositeCurve3d(
               new LineSegment3d[]{
                  new LineSegment3d(min, p2),
                  new LineSegment3d(p2, max),
                  new LineSegment3d(max, p4) }));
      pline.Closed = true;
      return pline;
   }

   static Polyline StandardColumnCreator(Entity rawEntity, BlockReference parent = null)
   {
      var blockref = rawEntity as BlockReference;
      if(parent is not null &amp;amp;&amp;amp; blockref is not null)
         return PolylineFromExtents(blockref.GeometryExtentsBestFit(parent.BlockTransform));
      Extents3d extents;
      if(blockref is not null)
         extents = blockref.GeometryExtentsBestFit();
      else
         extents = rawEntity.GeometricExtents;
      var pline = PolylineFromExtents(extents);
      if(parent is not null)
         pline.TransformBy(parent.BlockTransform);
      return pline;
   }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 29 Dec 2024 17:37:53 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-get-geometric-extents-of-a-nested-block-reference/m-p/13233914#M1324</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2024-12-29T17:37:53Z</dc:date>
    </item>
    <item>
      <title>Re: how to get geometric extents of a nested block reference?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-get-geometric-extents-of-a-nested-block-reference/m-p/13234031#M1325</link>
      <description>&lt;P&gt;Thanks for the reply&lt;/P&gt;&lt;P&gt;I have 3 blocks "c1" nested in "c2" and "c2" nested in "c3";&lt;/P&gt;&lt;P&gt;when I select c1 , the bounding box polyline is drawn in the area I have marked in the picture.&lt;/P&gt;&lt;P&gt;which is the bounding box of "c1" IF inserted on (0,0,0) with scale factor 1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm confused with blocktransform because I thought its suppose to give me the point of the block reference.&lt;/P&gt;&lt;P&gt;how can I get the actual bounding box of the nested "c1" ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.JPG" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1449783i48C46068CB38A752/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Capture.JPG" alt="Capture.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description>
      <pubDate>Sun, 29 Dec 2024 19:49:02 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-get-geometric-extents-of-a-nested-block-reference/m-p/13234031#M1325</guid>
      <dc:creator>a.kouchakzadeh</dc:creator>
      <dc:date>2024-12-29T19:49:02Z</dc:date>
    </item>
    <item>
      <title>Re: how to get geometric extents of a nested block reference?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-get-geometric-extents-of-a-nested-block-reference/m-p/13234079#M1326</link>
      <description>&lt;P&gt;I'm assuming you ran the test code I posted above. That code should create the bounding polyline of the most-deeply-nested block reference whose definition contains the nested entity that was selected.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you're not getting that result, please post the drawing you cited above where the result was not what you expected, and I will have a look at it.&lt;/P&gt;</description>
      <pubDate>Sun, 29 Dec 2024 21:13:51 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-get-geometric-extents-of-a-nested-block-reference/m-p/13234079#M1326</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2024-12-29T21:13:51Z</dc:date>
    </item>
    <item>
      <title>Re: how to get geometric extents of a nested block reference?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-get-geometric-extents-of-a-nested-block-reference/m-p/13234111#M1327</link>
      <description>&lt;P&gt;The example above doesn't deal with deeply-nested insertions, which requires a slightly-different approach.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This one should work with insertions nested to any depth:&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 class NestedExtentsExample
{
   [CommandMethod("STDCOL")]
   public static void CommandMethod()
   {
      Document doc = Application.DocumentManager.MdiActiveDocument;
      Database db = doc.Database;
      var ed = doc.Editor;
      using(Transaction tr = db.TransactionManager.StartTransaction())
      {
         var pner = ed.GetNestedEntity("\nSelect block - possibly nested");
         if(pner.Status != PromptStatus.OK)
            return;
         var objIds = pner.GetContainers();
         if(objIds is null || objIds.Length == 0)
            return;
         var pl = StandardColumnCreator(tr, objIds[0], pner.Transform);
         var btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
         btr.AppendEntity(pl);
         tr.AddNewlyCreatedDBObject(pl, true);
         tr.Commit();
      }
   }

   static Polyline PolylineFromExtents(Extents3d ext)
   {
      var min = ext.MinPoint;
      var max = ext.MaxPoint;
      var p2 = new Point3d(max.X, min.Y, 0.0);
      var p4 = new Point3d(min.X, max.Y, 0.0);
      var pline = (Polyline)
         Curve.CreateFromGeCurve(
            new CompositeCurve3d(
               new LineSegment3d[]{
                  new LineSegment3d(min, p2),
                  new LineSegment3d(p2, max),
                  new LineSegment3d(max, p4) }));
      pline.Closed = true;
      return pline;
   }

   private static Polyline StandardColumnCreator(Transaction tr, ObjectId blockrefId, Matrix3d xform)
   {
      if(blockrefId.IsNull)
         throw new ArgumentException(nameof(blockrefId));
      var blkref = (BlockReference)tr.GetObject(blockrefId, OpenMode.ForRead);
      var btr = (BlockTableRecord) tr.GetObject(blkref.BlockTableRecord, OpenMode.ForRead);
      Extents3d extents = new Extents3d();
      extents.AddBlockExtents(btr);
      Polyline pline = PolylineFromExtents(extents);
      pline.TransformBy(xform);
      return pline;
   }

}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 29 Dec 2024 22:40:15 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-get-geometric-extents-of-a-nested-block-reference/m-p/13234111#M1327</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2024-12-29T22:40:15Z</dc:date>
    </item>
    <item>
      <title>Re: how to get geometric extents of a nested block reference?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-get-geometric-extents-of-a-nested-block-reference/m-p/13234532#M1328</link>
      <description>&lt;P&gt;prefect. that did the trick.&lt;/P&gt;&lt;P&gt;can you explain what does AddBlockExtents(btr) do?&lt;/P&gt;</description>
      <pubDate>Mon, 30 Dec 2024 09:35:11 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-get-geometric-extents-of-a-nested-block-reference/m-p/13234532#M1328</guid>
      <dc:creator>a.kouchakzadeh</dc:creator>
      <dc:date>2024-12-30T09:35:11Z</dc:date>
    </item>
    <item>
      <title>Re: how to get geometric extents of a nested block reference?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-get-geometric-extents-of-a-nested-block-reference/m-p/13234665#M1329</link>
      <description>&lt;P&gt;&lt;SPAN&gt;It&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;adds the extents of all entities in the block.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Dec 2024 11:21:52 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-get-geometric-extents-of-a-nested-block-reference/m-p/13234665#M1329</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2024-12-30T11:21:52Z</dc:date>
    </item>
  </channel>
</rss>

