.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Create Custom Viewports

2 REPLIES 2
Reply
Message 1 of 3
a.hajihasani
459 Views, 2 Replies

Create Custom Viewports

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?

 

2 REPLIES 2
Message 2 of 3
norman.yuan
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.

 

 

Message 3 of 3
a.hajihasani
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.

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost