Jump to content

Turning on viewport in C# causes sporadic errors


jacksoncarl5

Recommended Posts

I have written the following C# code that takes user input of an integer, then plots views in sheets corresponding to polyines in the model space on different layers. For example in the attached file when 3 is input, then 3 views are inserted on sheet 1, sheet 2, and sheet 3. I have the viewports inserting correctly per the code (with proper scale base point and everything, so I can set other vp. settings), but when I un-comment the vp.on line I get the error: Autodesk.AutoCAD.Runtime.Exception: 'eNotInPaperspace'. But in the drawing I can easily turn these views on no problem, any ideas here?

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;

namespace AutoCADExtents
{
    public class Commands
    {
        [CommandMethod("POLYEXTENTS")]
        public void GetPolylineExtents()
        {
            // Get the current document and database
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;

            // Start a transaction
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                // Open the Block table for read
                BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                // Get the active document and editor
                Editor ed = doc.Editor;

                // Prompt user for the number of layers/views
                PromptIntegerOptions numLayersPromptOptions = new PromptIntegerOptions("\nEnter number of layers/views to create: ");
                numLayersPromptOptions.AllowNegative = false;
                numLayersPromptOptions.AllowZero = false;
                PromptIntegerResult numLayersPromptResult = ed.GetInteger(numLayersPromptOptions);

                if (numLayersPromptResult.Status != PromptStatus.OK)
                {
                    ed.WriteMessage("\nInvalid input or user cancelled.");
                    return;
                }

                int numLayers = numLayersPromptResult.Value;

                // Open the LayoutManager
                LayoutManager layoutManager = LayoutManager.Current;

                for (int i = 1; i <= numLayers; i++)
                {
                    string sheetName = "Sheet " + i;

                    // Get or create the layout
                    Layout layout = tr.GetObject(layoutManager.GetLayoutId(sheetName), OpenMode.ForWrite) as Layout;

                    // Create or get the first viewport in the layout
                    Viewport vp = GetOrCreateViewport(tr, layout);

                    // Set the viewport properties based on overall extents and layout size
                    SetViewportProperties(tr, layout, vp, bt, ed, i);
                 

                    // Inform user
                    ed.WriteMessage($"\nViewport created and adjusted successfully in '{sheetName}'.");
                }

                tr.Commit(); // Commit the transaction
            }
        }

        private Viewport GetOrCreateViewport(Transaction tr, Layout layout)
        {
            Viewport vp = null;

            // Open the BlockTableRecord for layout
            BlockTableRecord layoutRecord = tr.GetObject(layout.BlockTableRecordId, OpenMode.ForWrite) as BlockTableRecord;

            // Remove existing viewport if any
            foreach (ObjectId objId in layoutRecord)
            {
                if (objId.ObjectClass.IsDerivedFrom(RXObject.GetClass(typeof(Viewport))))
                {
                    vp = tr.GetObject(objId, OpenMode.ForWrite) as Viewport;
                    if (vp != null)
                    {
                        vp.Erase();
                        break;
                    }
                }
            }

            // Create a new viewport
            vp = new Viewport();
            layoutRecord.AppendEntity(vp);
            tr.AddNewlyCreatedDBObject(vp, true);

            return vp;
        }

        private void SetViewportProperties(Transaction tr, Layout layout, Viewport vp, BlockTable bt, Editor ed, int layerNumber)
        {
            // Get the overall extents of the entities in model space for the specified layer number
            Extents3d overallExtents = GetOverallExtents(tr, bt, layerNumber);

            // Check if valid extents were found
            if (overallExtents.MinPoint != Point3d.Origin && overallExtents.MaxPoint != Point3d.Origin)
            {
                // Calculate the center of the extents in model space
                Point3d centerPointModelSpace = overallExtents.MinPoint + ((overallExtents.MaxPoint - overallExtents.MinPoint) / 2.0);

                // Calculate the layout dimensions in inches (convert mm to inches)
                double layoutWidth = 20.3867;
                double layoutLength = 25.2982;

                // Determine scale factors based on layout dimensions
                double scaleHeight = layoutWidth / (overallExtents.MaxPoint.Y - overallExtents.MinPoint.Y);
                double scaleWidth = layoutLength / (overallExtents.MaxPoint.X - overallExtents.MinPoint.X);

                // Choose the smaller scale factor to fit either width or height
                double scaleFactor = Math.Min(scaleHeight, scaleWidth);

                // Set the viewport center in paper space
                vp.CenterPoint = new Point3d(13.3056, 13.3432, 0.0);

                // Set the viewport size based on scale factor
                vp.Height = (overallExtents.MaxPoint.Y - overallExtents.MinPoint.Y) * scaleFactor;
                vp.Width = (overallExtents.MaxPoint.X - overallExtents.MinPoint.X) * scaleFactor;

                // Set the view center in model space
                vp.ViewCenter = new Point2d(centerPointModelSpace.X, centerPointModelSpace.Y);

                // Set the view scale (optional)
                vp.CustomScale = scaleFactor;

                //vp.On = true;



            }
            else
            {
                // No valid extents found
                ed.WriteMessage($"\nNo valid extents found for Layer {layerNumber}. Viewport not adjusted.");
            }
        }

        private Extents3d GetOverallExtents(Transaction tr, BlockTable bt, int layerNumber)
        {
            // Initialize extents
            Extents3d overallExtents = new Extents3d();

            // Open the Block table record Model space for read
            BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;

            // Iterate through the entities in Model space
            foreach (ObjectId objId in btr)
            {
                Entity ent = tr.GetObject(objId, OpenMode.ForRead) as Entity;

                // Check if the entity is a Polyline and on the specified layer
                if (ent != null && ent is Polyline && ent.Layer == $"{layerNumber}")
                {
                    Polyline pline = (Polyline)ent;

                    // Get the extents of the polyline
                    Extents3d extents = pline.GeometricExtents;

                    // Extend overall extents
                    overallExtents.AddExtents(extents);
                }
            }

            return overallExtents;
        }
    }
}

 

Drawing1_test.dwg

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...