.NET

Reply
Active Member
a.hajihasani
Posts: 6
Registered: ‎08-28-2012
Message 1 of 3 (125 Views)

Create Custom Viewports

125 Views, 2 Replies
03-10-2014 05:02 AM

Hi every body.

I'm trying to create some sheets trough a path on a dawing by creating a new Layout for each sheet, and a viewport on it, pointing to a specified point on drawing and have a custom scale. I've been searching so far, and got this:

private void Button_runplanning_Click(object sender, EventArgs e)
        {
            using (DocumentLock lck = doc.LockDocument())
            {
                CAD_Additions obj = new CAD_Additions();
                double sheetLength = 500;
                double sheetWidth = 200;
                //Point2dCollection pc2 = new Point2dCollection();
                #region Get centerline object
                Polyline CL = new Polyline();
                try
                {
                    using (Transaction tr = curdb.TransactionManager.StartTransaction())
                    {
                        long val1 = Convert.ToInt64(CenterLineDataTable.Rows[0]["handle"]);
                        ObjectId clid = curdb.GetObjectId(false, new Handle(val1), 0);
                        CL = (Polyline)tr.GetObject(clid, OpenMode.ForRead);
                        tr.Commit();
                    }
                }
                catch (System.Exception exp)
                {
                    RadMessageBox.Show("An error occured. Try again please.\r\n" + exp.Message, "Error", MessageBoxButtons.OK, RadMessageIcon.Error);
                    return;
                }

                if (CL == null)
                {
                    RadMessageBox.Show("An error occured. Try again please.", "Error", MessageBoxButtons.OK, RadMessageIcon.Error);
                    return;
                }
                #endregion
                using (Transaction tr = curdb.TransactionManager.StartTransaction())
                {
                    LayoutManager acLayoutmgr = Autodesk.AutoCAD.DatabaseServices.LayoutManager.Current;
                    Point3d stPoint = new Point3d(Convert.ToDouble(CenterLineDataTable.Rows[0]["stx"]), Convert.ToDouble(CenterLineDataTable.Rows[0]["stY"]), 0);
                    int sheetCount = 0;
                    while (true)
                    {
                        if (CL.GetDistAtPoint(stPoint) + (sheetCount * sheetLength) + sheetLength / 2 > CL.Length)
                        {
                            break;
                        }
                        Point3d sheetCenter = CL.GetPointAtDist(CL.GetDistAtPoint(stPoint) + (sheetCount * sheetLength) + sheetLength / 2);
                        string layoutName = "Layout_" + (sheetCount + 1).ToString();
                        ObjectId newLayoutId = acLayoutmgr.CreateLayout(layoutName);
                        Layout lay = (Layout)tr.GetObject(newLayoutId, OpenMode.ForRead);
                        CreateViewport(lay.ObjectId, sheetCenter, sheetLength, sheetWidth);
                        //EditCurrentViewPort(lay.ObjectId, sheetCenter, sheetLength, sheetWidth);
                        sheetCount++;
                    }
                    tr.Commit();
                }
            }
        }

 

public ObjectId CreateViewport(ObjectId layoutId, Point3d center, double length, double width)
        {
            using (DocumentLock lck = doc.LockDocument())
            {
                ObjectId oid;

                try
                {
                    using (Transaction tr = curdb.TransactionManager.StartTransaction())
                    {
                        Layout LayoutDest = (Layout)tr.GetObject(layoutId, OpenMode.ForRead);
                        //Layout LayoutDest = (Layout)tr.GetObject(curdb.CurrentSpaceId, OpenMode.ForRead);
                        BlockTableRecord btrDest = (BlockTableRecord)tr.GetObject(LayoutDest.BlockTableRecordId, OpenMode.ForWrite);

                        Autodesk.AutoCAD.DatabaseServices.Viewport vpNew = new Autodesk.AutoCAD.DatabaseServices.Viewport();
                        vpNew.SetDatabaseDefaults();
                        vpNew.Width = length;
                        vpNew.Height = width;
                        vpNew.CenterPoint = center;
                        vpNew.ViewTarget = center;
                        vpNew.ViewCenter = new Point2d(center.X, center.Y);
                        vpNew.ViewHeight = width;
                        vpNew.Visible = true;
                        vpNew.StandardScale = StandardScaleType.CustomScale;
                        vpNew.CustomScale = 1;
                        oid = btrDest.AppendEntity(vpNew);
                        tr.AddNewlyCreatedDBObject(vpNew, true);

                        vpNew.ViewDirection = Vector3d.ZAxis;
                        vpNew.On = true;

                        tr.Commit();
                        return oid;
                    }
                }
                catch (System.Exception ex)
                {
                    ed.WriteMessage(ex.Message);
                }
            }
            return layoutId;
        }

Tthis works without any error, some layouts are created ("Layout_x"), but all of them, have a same viewport pointing to the drawing extents.

Can anyone help me on this?

 

*Expert Elite*
norman.yuan
Posts: 965
Registered: ‎04-27-2009
Message 2 of 3 (108 Views)

Re: Create Custom Viewports

03-10-2014 06:32 AM in reply to: a.hajihasani

It looks to me that the point you passed as view centre point (stPoint) is never chanbed. That is, the following line of code

 

Point3d stPoint = new Point3d(Convert.ToDouble(CenterLineDataTable.Rows[0]["stx"]), Convert.ToDouble(CenterLineDataTable.Rows[0]["stY"]), 0);

 

ALWAYS gets the first row of the CentreLineDataTable.

 

I guess is that this line should be placed in the while(true){...} loop and the row index (CenterLineDataTable.Rows[index] should be the sheet count, if there is multiple rows in that table, which staores X and Y values for each sheet's viewport center.

 

 

Active Member
a.hajihasani
Posts: 6
Registered: ‎08-28-2012
Message 3 of 3 (101 Views)

Re: Create Custom Viewports

03-10-2014 06:56 AM in reply to: norman.yuan

Thank's norman, but that's not exactly what I'm searching for. The first row of the CenterLineDataTable contains data refering to a single polyline object, which user is asked to select it somewhere else, and It's the Center Line which I'm trying to generate and arrange sheets through it.

I tried more, and figured out that its possible to modify a viewport using these codes:

string layoutName = "Layout_" + (sheetCount + 1).ToString();
                        ObjectId newLayoutId = acLayoutmgr.CreateLayout(layoutName);
                        Layout lay = (Layout)tr.GetObject(newLayoutId, OpenMode.ForRead);
                        acLayoutmgr.CurrentLayout = layoutName;
                        ObjectIdCollection vptids = lay.GetViewports();
                        for (int i = 0; i < vptids.Count-1; i++)
                        {
                            ObjectId vptid = vptids[i];
                            Viewport vp = (Viewport)tr.GetObject(vptid, OpenMode.ForWrite);
                            //vp.CenterPoint = new Point3d(0, 0, 0);
                            vp.CustomScale = 1;
                            //vp.ViewTarget = new Point3d(.001, .001, 0);
                            //vp.SetUcs(new Point3d(0, 0, 0), new Vector3d(1, 1, 0), new Vector3d(1, -1, 0));
                            //vp.ViewDirection = new Vector3d(1, 1, 0);
                        }

 But, which property of the Viewport I should modify to make it point to my desired point?

When I use CenterPoint, or ViewTarget or vp.ViewCenter and pass my desired coordinate components (sheetCenter.X ,sheetCenter.Y) to it, the viewport points to an undesired location (like -3412531,-41143523). Seems that the CenterPoint property referse to the paperspace coordinate system, but is that same for the two others? It looks strange to me. Is that a problem of some USC definition? Or somthing like mismatch in scales? Hmmmm,...  I'm confused somewhat. Any help would be useful and appreciated.

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community