<?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 Inserting Views on a layout in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/inserting-views-on-a-layout/m-p/12840871#M3594</link>
    <description>&lt;P&gt;I am trying to write a basic script, I have polyline rectangles in the models space on layers that consist of only numbers (1 through x), I want to make a layout sheet for each of these polylines, then insert a view on each of these layouts that corresponds to the bounding box of each of those corresponding polylines. Here is the code I have so far, all I can get it to do is make the layout sheets, it does nothing with the views, also see attached file dwg I was using as input. For this input I would want 3 sheets, one with a line in the viewport, one with a circle in the viewport, and one with a rectangle in the viewport. Scale of these viewports is not of huge importance, it just needs to fit in the paper space. Size of these polyline rectangles is variable&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;using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices.Core;

[assembly: CommandClass(typeof(KdubServices.Test0315.MyCommands))]

namespace KdubServices.Test0315
{
    public class MyCommands
    {
        // Method to check if the layer name consists of only digits (excluding 0)
        private bool IsLayerNumeric(string layerName)
        {
            foreach (char c in layerName)
            {
                if (c &amp;lt; '1' || c &amp;gt; '9')
                    return false;
            }
            return true;
        }

        [CommandMethod("InsertLayoutsForPolylines")]
        public void InsertLayoutsForPolylines()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            using (var tr = db.TransactionManager.StartTransaction())
            {
                var ms = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);

                // Iterate through all entities in model space
                foreach (ObjectId entId in ms)
                {
                    var ent = tr.GetObject(entId, OpenMode.ForRead) as Entity;

                    // Check if the entity is a closed polyline
                    if (ent != null &amp;amp;&amp;amp; ent is Polyline polyline &amp;amp;&amp;amp; polyline.Closed)
                    {
                        // Check if the layer name consists of only digits (excluding 0)
                        if (IsLayerNumeric(polyline.Layer))
                        {
                            // Create a new layout for each closed polyline
                            var layoutName = $"Layout for {polyline.Layer}";
                            var layoutId = CreateLayout(db, tr, layoutName);

                            if (!layoutId.IsNull)
                            {
                                var layout = (Layout)tr.GetObject(layoutId, OpenMode.ForRead);

                                // Get the extents of the polyline
                                var extents = polyline.GeometricExtents;
                                var viewCenter = extents.MinPoint + (extents.MaxPoint - extents.MinPoint) / 2.0;

                                // Create a view with the extents of the polyline
                                var viewId = CreateView(db, tr, layout.BlockTableRecordId, viewCenter, extents.MaxPoint - viewCenter);

                                // Output layout info for demo:
                                ed.WriteMessage($"\nTab Order: {layout.TabOrder}" +
                                                $"\nTab Selected: {layout.TabSelected}" +
                                                $"\nBlock Table Record ID: {layout.BlockTableRecordId}"
                                );

                                // Zoom to the extents of the layout
                                doc.SendStringToExecute("._zoom _extents ", true, false, false);
                            }
                        }
                    }
                }

                tr.Commit();
            }
        }

        // Method to create a new layout
        private ObjectId CreateLayout(Database db, Transaction tr, string layoutName)
        {
            var layoutMgr = LayoutManager.Current;
            return layoutMgr.CreateLayout(layoutName);
        }

        // Method to create a view
        private ObjectId CreateView(Database db, Transaction tr, ObjectId layoutId, Point3d center, Vector3d viewDir)
        {
            // Check if layout exists
            if (!layoutId.IsValid || layoutId.IsNull)
            {
                return ObjectId.Null;
            }

            var layout = tr.GetObject(layoutId, OpenMode.ForWrite) as Layout;

            // Ensure layout is not null
            if (layout == null)
            {
                return ObjectId.Null;
            }

            var btr = (BlockTableRecord)tr.GetObject(layout.BlockTableRecordId, OpenMode.ForWrite);

            // Create the viewport
            var view = new Viewport();
            view.CenterPoint = center;
            view.ViewDirection = viewDir.GetNormal();
            view.Height = viewDir.Length;
            view.Width = viewDir.Length * 1.5; // Aspect ratio can be adjusted
            view.CustomScale = 1; // Setting a custom scale to avoid distortion

            // Append the viewport to the layout's block table record
            return btr.AppendEntity(view);
        }
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;another goal would be to have the inserted layout sheets match match a .dwt title block template&lt;/P&gt;</description>
    <pubDate>Fri, 14 Jun 2024 20:40:23 GMT</pubDate>
    <dc:creator>carl3ZPF6</dc:creator>
    <dc:date>2024-06-14T20:40:23Z</dc:date>
    <item>
      <title>Inserting Views on a layout</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-views-on-a-layout/m-p/12840871#M3594</link>
      <description>&lt;P&gt;I am trying to write a basic script, I have polyline rectangles in the models space on layers that consist of only numbers (1 through x), I want to make a layout sheet for each of these polylines, then insert a view on each of these layouts that corresponds to the bounding box of each of those corresponding polylines. Here is the code I have so far, all I can get it to do is make the layout sheets, it does nothing with the views, also see attached file dwg I was using as input. For this input I would want 3 sheets, one with a line in the viewport, one with a circle in the viewport, and one with a rectangle in the viewport. Scale of these viewports is not of huge importance, it just needs to fit in the paper space. Size of these polyline rectangles is variable&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;using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices.Core;

[assembly: CommandClass(typeof(KdubServices.Test0315.MyCommands))]

namespace KdubServices.Test0315
{
    public class MyCommands
    {
        // Method to check if the layer name consists of only digits (excluding 0)
        private bool IsLayerNumeric(string layerName)
        {
            foreach (char c in layerName)
            {
                if (c &amp;lt; '1' || c &amp;gt; '9')
                    return false;
            }
            return true;
        }

        [CommandMethod("InsertLayoutsForPolylines")]
        public void InsertLayoutsForPolylines()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            using (var tr = db.TransactionManager.StartTransaction())
            {
                var ms = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);

                // Iterate through all entities in model space
                foreach (ObjectId entId in ms)
                {
                    var ent = tr.GetObject(entId, OpenMode.ForRead) as Entity;

                    // Check if the entity is a closed polyline
                    if (ent != null &amp;amp;&amp;amp; ent is Polyline polyline &amp;amp;&amp;amp; polyline.Closed)
                    {
                        // Check if the layer name consists of only digits (excluding 0)
                        if (IsLayerNumeric(polyline.Layer))
                        {
                            // Create a new layout for each closed polyline
                            var layoutName = $"Layout for {polyline.Layer}";
                            var layoutId = CreateLayout(db, tr, layoutName);

                            if (!layoutId.IsNull)
                            {
                                var layout = (Layout)tr.GetObject(layoutId, OpenMode.ForRead);

                                // Get the extents of the polyline
                                var extents = polyline.GeometricExtents;
                                var viewCenter = extents.MinPoint + (extents.MaxPoint - extents.MinPoint) / 2.0;

                                // Create a view with the extents of the polyline
                                var viewId = CreateView(db, tr, layout.BlockTableRecordId, viewCenter, extents.MaxPoint - viewCenter);

                                // Output layout info for demo:
                                ed.WriteMessage($"\nTab Order: {layout.TabOrder}" +
                                                $"\nTab Selected: {layout.TabSelected}" +
                                                $"\nBlock Table Record ID: {layout.BlockTableRecordId}"
                                );

                                // Zoom to the extents of the layout
                                doc.SendStringToExecute("._zoom _extents ", true, false, false);
                            }
                        }
                    }
                }

                tr.Commit();
            }
        }

        // Method to create a new layout
        private ObjectId CreateLayout(Database db, Transaction tr, string layoutName)
        {
            var layoutMgr = LayoutManager.Current;
            return layoutMgr.CreateLayout(layoutName);
        }

        // Method to create a view
        private ObjectId CreateView(Database db, Transaction tr, ObjectId layoutId, Point3d center, Vector3d viewDir)
        {
            // Check if layout exists
            if (!layoutId.IsValid || layoutId.IsNull)
            {
                return ObjectId.Null;
            }

            var layout = tr.GetObject(layoutId, OpenMode.ForWrite) as Layout;

            // Ensure layout is not null
            if (layout == null)
            {
                return ObjectId.Null;
            }

            var btr = (BlockTableRecord)tr.GetObject(layout.BlockTableRecordId, OpenMode.ForWrite);

            // Create the viewport
            var view = new Viewport();
            view.CenterPoint = center;
            view.ViewDirection = viewDir.GetNormal();
            view.Height = viewDir.Length;
            view.Width = viewDir.Length * 1.5; // Aspect ratio can be adjusted
            view.CustomScale = 1; // Setting a custom scale to avoid distortion

            // Append the viewport to the layout's block table record
            return btr.AppendEntity(view);
        }
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;another goal would be to have the inserted layout sheets match match a .dwt title block template&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jun 2024 20:40:23 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-views-on-a-layout/m-p/12840871#M3594</guid>
      <dc:creator>carl3ZPF6</dc:creator>
      <dc:date>2024-06-14T20:40:23Z</dc:date>
    </item>
  </channel>
</rss>

