<?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: Place a block in a specific rectangle in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/place-a-block-in-a-specific-rectangle/m-p/7313667#M30092</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/3011731"&gt;@BKSpurgeon&lt;/a&gt; wrote:&lt;BR /&gt;&lt;P&gt;Personally, and this is a matter of preference, I would strongly recommend you use a transaction to open/close objects - unless performance is a huge concern, this is probably a good way to go. unless you are really careful, it's very easy to potentially forget to close an object. using transactions obviates the need to remember. also please don't forget to commit or abort your transaction when you are done with it otherwise the code will run slower.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;He &lt;EM&gt;is&lt;/EM&gt; using a Transaction.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the (simplified) source code for&amp;nbsp;ObjectId.GetObject():&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;&lt;PRE&gt;public DBObject GetObject(OpenMode mode)
{
&amp;nbsp; &amp;nbsp; return this.Database.TransactionManager.TopTransaction.GetObject(this, mode);
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ObjectId.GetObject() is just another way of using the top transaction, if there is one. If there isn't one, an exception is raised.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If performance is important in cases where many objects are being opened sequentially, ObjectId.GetObject() is obviously not the best way to do it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It's also not recommended if the calling code needs to work with any&amp;nbsp;kind of transaction.&lt;/P&gt;</description>
    <pubDate>Sun, 20 Aug 2017 07:26:22 GMT</pubDate>
    <dc:creator>ActivistInvestor</dc:creator>
    <dc:date>2017-08-20T07:26:22Z</dc:date>
    <item>
      <title>Place a block in a specific rectangle</title>
      <link>https://forums.autodesk.com/t5/net-forum/place-a-block-in-a-specific-rectangle/m-p/7307361#M30089</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can place a block using a Point as a position and&amp;nbsp;change his scale using the bellow code &amp;nbsp;:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;                    string blockName = name;
                    BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
                    BlockTableRecord blockDef = bt[blockName].GetObject(OpenMode.ForRead) as BlockTableRecord;
                    BlockTableRecord ms = bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite) as BlockTableRecord;

                    using (BlockReference blockRef = new BlockReference(position, blockDef.ObjectId))
                    {
                        blockRef.ScaleFactors = new Scale3d(scale);
                        blockRef.Layer = layer;
                        id = ms.AppendEntity(blockRef);
                        myT.AddNewlyCreatedDBObject(blockRef, true);
                    }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but i want to place the block in a specific rectangle for which i can recover the&amp;nbsp;extent, means that i need to use just &amp;nbsp;the extent to place the block in the rectange&amp;nbsp;without using the scale. is that possible an how ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Aug 2017 10:28:02 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/place-a-block-in-a-specific-rectangle/m-p/7307361#M30089</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-08-17T10:28:02Z</dc:date>
    </item>
    <item>
      <title>Re: Place a block in a specific rectangle</title>
      <link>https://forums.autodesk.com/t5/net-forum/place-a-block-in-a-specific-rectangle/m-p/7313548#M30090</link>
      <description>&lt;P&gt;Personally, and this is a matter of preference, I would strongly recommend you use a transaction to open/close objects - unless performance is a huge concern, this is probably a good way to go. unless you are really careful, it's very easy to potentially forget to close an object. using transactions obviates the need to remember. also please don't forget to commit or abort your transaction when you are done with it otherwise the code will run slower.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Perhaps try something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;public class MyCommands
{
           [CommandMethod("PlaceBlockReferenceInRectangle")]
        public static void PlaceBlockReferenceInRectangle()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                // set up variablees
                string blockName = "blahblah";

                    // get the extents of the Rectangle
                Extents3d rectangleExtents = GetRectangleExtents(); // get the extents using the rectangle. you will have to substitute your custom code here.
                double xAverage = (rectangleExtents.MinPoint.X + rectangleExtents.MaxPoint.X) / 2.0;
                double yAverage = (rectangleExtents.MinPoint.Y + rectangleExtents.MaxPoint.Y) / 2.0;
                Point3d insertionPointInMiddleOfRectangle = new Point3d(xAverage, yAverage, 0);


                BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord ms = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
                BlockTableRecord blockDef = tr.GetObject(bt[blockName], OpenMode.ForRead) as BlockTableRecord;

                    // this should place it
                using (BlockReference blockRef = new BlockReference(insertionPointInMiddleOfRectangle, blockDef.ObjectId))
                {
                    //// omit the scale and all the layer information from this example for simplicity.
                    

                    
                    ms.AppendEntity(blockRef);
                    tr.AddNewlyCreatedDBObject(blockRef, true);
                }


                tr.Commit();
            }
        }

           private static Extents3d GetRectangleExtents()
           {
               // write your custom code here.
               return new Extents3d();
           }
}&lt;/PRE&gt;</description>
      <pubDate>Sun, 20 Aug 2017 00:49:29 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/place-a-block-in-a-specific-rectangle/m-p/7313548#M30090</guid>
      <dc:creator>BKSpurgeon</dc:creator>
      <dc:date>2017-08-20T00:49:29Z</dc:date>
    </item>
    <item>
      <title>Re: Place a block in a specific rectangle</title>
      <link>https://forums.autodesk.com/t5/net-forum/place-a-block-in-a-specific-rectangle/m-p/7313658#M30091</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;BKSpurgeon a écrit&amp;nbsp;:&lt;BR /&gt;
&lt;P&gt;Personally, and this is a matter of preference, I would strongly recommend you use a transaction to open/close objects - unless performance is a huge concern, this is probably a good way to go. unless you are really careful, it's very easy to potentially forget to close an object.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The upper code does use a transaction.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From &lt;A href="http://adndevblog.typepad.com/autocad/2012/08/the-right-tools-for-the-job-autocad-part-5.html" target="_blank"&gt;this topic&lt;/A&gt;:&lt;/P&gt;
&lt;P&gt;"The ObjectId.GetObject() style utilizes the normal Transaction object mechanism, but this way gives you a slightly difference access point."&lt;/P&gt;</description>
      <pubDate>Sun, 20 Aug 2017 07:08:07 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/place-a-block-in-a-specific-rectangle/m-p/7313658#M30091</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2017-08-20T07:08:07Z</dc:date>
    </item>
    <item>
      <title>Re: Place a block in a specific rectangle</title>
      <link>https://forums.autodesk.com/t5/net-forum/place-a-block-in-a-specific-rectangle/m-p/7313667#M30092</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/3011731"&gt;@BKSpurgeon&lt;/a&gt; wrote:&lt;BR /&gt;&lt;P&gt;Personally, and this is a matter of preference, I would strongly recommend you use a transaction to open/close objects - unless performance is a huge concern, this is probably a good way to go. unless you are really careful, it's very easy to potentially forget to close an object. using transactions obviates the need to remember. also please don't forget to commit or abort your transaction when you are done with it otherwise the code will run slower.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;He &lt;EM&gt;is&lt;/EM&gt; using a Transaction.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the (simplified) source code for&amp;nbsp;ObjectId.GetObject():&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;&lt;PRE&gt;public DBObject GetObject(OpenMode mode)
{
&amp;nbsp; &amp;nbsp; return this.Database.TransactionManager.TopTransaction.GetObject(this, mode);
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ObjectId.GetObject() is just another way of using the top transaction, if there is one. If there isn't one, an exception is raised.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If performance is important in cases where many objects are being opened sequentially, ObjectId.GetObject() is obviously not the best way to do it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It's also not recommended if the calling code needs to work with any&amp;nbsp;kind of transaction.&lt;/P&gt;</description>
      <pubDate>Sun, 20 Aug 2017 07:26:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/place-a-block-in-a-specific-rectangle/m-p/7313667#M30092</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2017-08-20T07:26:22Z</dc:date>
    </item>
    <item>
      <title>Re: Place a block in a specific rectangle</title>
      <link>https://forums.autodesk.com/t5/net-forum/place-a-block-in-a-specific-rectangle/m-p/7313697#M30093</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the purpose is to insert and scale the block so that is fills a rectangle, you can get some inspiration from this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;        [CommandMethod("TEST")]
        public void Test()
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            // get the block name
            var pr = ed.GetString("\nEnter the block name: ");
            if (pr.Status != PromptStatus.OK)
                return;
            var blockName = pr.StringResult;
            if (string.IsNullOrWhiteSpace(blockName))
                return;
            using (var tr = db.TransactionManager.StartTransaction())
            {
                // check if block exists in the block table
                var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                if (!bt.Has(blockName))
                {
                    ed.WriteMessage($"\nBlock '{blockName}' not found.");
                    return;
                }

                // get the rectangle
                var peo = new PromptEntityOptions("\nSelect a rectangle: ");
                peo.SetRejectMessage("\nSelected object is not a polyline.");
                peo.AddAllowedClass(typeof(Polyline), true);
                var per = ed.GetEntity(peo);
                if (per.Status != PromptStatus.OK)
                    return;
                var pline = (Polyline)tr.GetObject(per.ObjectId, OpenMode.ForRead);
                var rectangle = pline.GeometricExtents;

                // insert the block
                var br = new BlockReference(rectangle.MinPoint, bt[blockName]);
                var space = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                space.AppendEntity(br);
                tr.AddNewlyCreatedDBObject(br, true);

                // local functions (C#7, use delegates for prior versions)
                double GetLength(Extents3d extents) =&amp;gt;
                    extents.MaxPoint.X - extents.MinPoint.X;

                double GetWidth(Extents3d extents) =&amp;gt;
                    extents.MaxPoint.Y - extents.MinPoint.Y;

                Point3d GetCenter(Extents3d extents) =&amp;gt;
                    extents.MinPoint + (extents.MaxPoint - extents.MinPoint) / 2.0;

                // scale
                var brExtents = br.GeometricExtents;
                double xScale = GetLength(rectangle) / GetLength(brExtents);
                double yScale = GetWidth(rectangle) / GetWidth(brExtents);
                br.ScaleFactors = new Scale3d(xScale, yScale, 1);
                
                // move
                brExtents = br.GeometricExtents;
                br.TransformBy(Matrix3d.Displacement(GetCenter(rectangle) - GetCenter(brExtents)));
                tr.Commit();
            }
        }&lt;/PRE&gt;</description>
      <pubDate>Sun, 20 Aug 2017 08:23:08 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/place-a-block-in-a-specific-rectangle/m-p/7313697#M30093</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2017-08-20T08:23:08Z</dc:date>
    </item>
    <item>
      <title>Re: Place a block in a specific rectangle</title>
      <link>https://forums.autodesk.com/t5/net-forum/place-a-block-in-a-specific-rectangle/m-p/7313957#M30094</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/4476837"&gt;@ActivistInvestor&lt;/a&gt;&amp;nbsp;- thank you both for the clarification. you are both correct - i made a mistake in the above.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;rgds&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Aug 2017 14:04:15 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/place-a-block-in-a-specific-rectangle/m-p/7313957#M30094</guid>
      <dc:creator>BKSpurgeon</dc:creator>
      <dc:date>2017-08-20T14:04:15Z</dc:date>
    </item>
  </channel>
</rss>

