Announcements
Due to scheduled maintenance, the Autodesk Community will be inaccessible from 10:00PM PDT on Oct 16th for approximately 1 hour. We appreciate your patience during this time.
.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Inserting Views on a layout

0 REPLIES 0
Reply
Message 1 of 1
carl3ZPF6
146 Views, 0 Replies

Inserting Views on a layout

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

 

 

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 < '1' || c > '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 && ent is Polyline polyline && 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);
        }
    }
}

 

 

another goal would be to have the inserted layout sheets match match a .dwt title block template

Tags (2)
0 REPLIES 0

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report