• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Contributor
    Posts: 19
    Registered: ‎05-03-2010
    Accepted Solution

    Create Named View - ViewTwist Problems??

    325 Views, 5 Replies
    08-09-2012 03:31 PM
    Hi, I'm trying to create a function that creates a ucs, defines a named view and algins to the current view based on a blockreference (in this case it will be the last selected entity). So far everythings working except I can't get the view to rotate or "twist" properly? Can you have a look to see where I'm going wrong. Here's the full code, the polyline that's created should frame the view.

    [CommandMethod("VM")]
            public static void NamedView()
            {
                // Test Values for Named View
                string viewname = "100 m";
                double width = 420;
                double height = 297;
                double angle = 0;

                Document doc = Application.DocumentManager.MdiActiveDocument;
                Editor ed = doc.Editor;
                Database db = doc.Database;

                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                    BlockTableRecord ms = (BlockTableRecord)tr.GetObject(
                        bt[BlockTableRecord.ModelSpace],
                        OpenMode.ForWrite);

                    // Catch last entity (MVF Block) and align UCS
                    ObjectId ent = Autodesk.AutoCAD.Internal.Utils.EntLast();

                    if (ent.ObjectClass.DxfName == "INSERT")
                    {
                        BlockReference blk = (BlockReference)tr.GetObject(ent, OpenMode.ForRead);
                        ed.CurrentUserCoordinateSystem = blk.BlockTransform;
                        angle = blk.Rotation;
                    }
                    else
                    {
                        ed.WriteMessage("Last entity was not a block reference");
                        return;
                    }

                    // Get Current coordinate system
                    CoordinateSystem3d cs = ed.CurrentUserCoordinateSystem.CoordinateSystem3d;
                    Point3d ucsOrigin = cs.Origin; // Used later for View Origin creation

                    //==================================================================
                    // Create New User UCS
                    //==================================================================

                    UcsTable ut = (UcsTable)tr.GetObject(db.UcsTableId, OpenMode.ForRead);
                    UcsTableRecord utr;

                    if (ut.Has(viewname))
                    {
                        utr = (UcsTableRecord)tr.GetObject(
                            ut[viewname], OpenMode.ForWrite);
                    }
                    else
                    {
                        utr = new UcsTableRecord();
                        utr.Name = viewname;
                        ut.UpgradeOpen();
                        ut.Add(utr);
                        tr.AddNewlyCreatedDBObject(utr, true);
                    }

                    utr.Origin = ucsOrigin;
                    utr.XAxis = cs.Xaxis;
                    utr.YAxis = cs.Yaxis;

                    ViewportTableRecord vptr = tr.GetObject(doc.Editor.ActiveViewportId,
                        OpenMode.ForWrite) as ViewportTableRecord;

                    vptr.IconAtOrigin = true;
                    vptr.IconEnabled = true;

                    vptr.SetUcs(utr.ObjectId);
                    doc.Editor.UpdateTiledViewportsFromDatabase();

                    //==================================================================
                    // Create View
                    //==================================================================

                    ViewTable vt = (ViewTable)tr.GetObject(db.ViewTableId, OpenMode.ForRead);
                    ViewTableRecord vtr;

                    // Check if View Name exit's
                    if (vt.Has(viewname))
                    {
                        vtr = (ViewTableRecord)tr.GetObject(vt[viewname], OpenMode.ForWrite);
                    }
                    else
                    {
                        vtr = new ViewTableRecord();
                        vtr.Name = viewname;
                        vt.UpgradeOpen();
                        ObjectId vtId = vt.Add(vtr);
                        tr.AddNewlyCreatedDBObject(vtr, true);
                    }

                    // Calculate center point if view
                    double centerX = width / 2;
                    double centerY = height / 2;
                    Point2d ctr = new Point2d(centerX, centerY);

                    //Convert to ucs origin to Point2d
                    Point2d viewOrigin = new Point2d(ucsOrigin.X, ucsOrigin.Y);

                    //Create a rotation Matrix for X and Y Axis
                    Matrix2d mat = Matrix2d.Rotation(angle, Point2d.Origin);
                    Matrix2d mat2d = new Matrix2d();
                    mat2d = Matrix2d.AlignCoordinateSystem(
                        Point2d.Origin,
                        Vector2d.XAxis,
                        Vector2d.YAxis,
                        viewOrigin,
                        new Vector2d(1, 0).TransformBy(mat),
                        new Vector2d(0, 1).TransformBy(mat)
                        );


                    Point2d viewCenter = ctr.TransformBy(mat2d);

                    vtr.ViewTwist = -angle;
                    vtr.IsPaperspaceView = false;
                    vtr.CenterPoint = viewCenter;
                    vtr.SetUcs(utr.ObjectId);
                    vtr.Height = height;
                    vtr.Width = width;

                    // Set new view to current
                    ed.SetCurrentView(vtr);

                    //==================================================================
                    // Create Polyline test geometry
                    //==================================================================

                    // Defined bouding box to new View
                    Point2d pt1 = new Point2d(0, 0);
                    Point2d pt2 = new Point2d(width, 0);
                    Point2d pt3 = new Point2d(width, height);
                    Point2d pt4 = new Point2d(0, height);

                    Point2d[] pts = { pt1, pt2, pt3, pt4 };

                    // Define Polyline Vertices
                    Polyline pl = new Polyline(4);

                    int max = pts.GetUpperBound(0);

                    for (int i = 0; i <= max; i++)
                    {
                        int j = (i == max ? 0 : i + 1);
                        pl.AddVertexAt(i, pts.TransformBy(mat2d), 0, 0, 0);
                    }
                    pl.Closed = true;

                    // Add the boundary to modelspace
                    ObjectId id = ms.AppendEntity(pl);
                    tr.AddNewlyCreatedDBObject(pl, true);

                    tr.Commit();
                }
            }
    Please use plain text.
    Contributor
    Posts: 19
    Registered: ‎05-03-2010

    Re: Create Named View - ViewTwist Problems??

    08-15-2012 04:48 PM in reply to: matinau

    Can anyone help, still no closer to fixing this problem. Cheers

    Please use plain text.
    *Expert Elite*
    Posts: 3,046
    Registered: ‎07-22-2003

    Re: Create Named View - ViewTwist Problems??

    08-15-2012 08:31 PM in reply to: matinau

    Well, I'm guessing there was an error introduced by the copy/paste operation, as this needed correcting:

    pl.AddVertexAt(i, pts.TransformBy(mat2d), 0, 0, 0);

     

    to this:

    pl.AddVertexAt(i, pts[j].TransformBy(mat2d), 0, 0, 0);

     

    But once that was done, it appears to work fine as far as the rotation goes. It's not centering the view on the polyline or the blockreference, but I'm not sure if it's supposed to be.

     

    In this image, the dashed line is the edge of the drawing canvas when the created view is is current. But, the UCS and code generated polyline appear to match the rotation of the water valve block which was inserted with a rotation of 33 degrees.

    8-15-2012 8-28-19 PM.png

     

    Hope that helps you track down what you are looking for.

    Jeff_M, also a frequent Swamper
    Please use plain text.
    Contributor
    Posts: 19
    Registered: ‎05-03-2010

    Re: Create Named View - ViewTwist Problems??

    08-15-2012 08:49 PM in reply to: Jeff_M

    Hi thanks for the reply not sure how that happened? however part I need help with is how to center the view within the polyline. this is what happens when I edit the boundaries of the named view create "100 m"

     

     

     

    viewstuff

     

    the Yellow line is the block, the white is the polyline created on the VM command and the black rectangle is the boundaries of "100 m" view?

     

    I want this to be centered within the polyline?

     

    If I remove the vtr.viewtwist line the view is centered wwithin the polyline but the rotations out....if I uncomment this line I get the above result.

     

    Any ideas?

    Please use plain text.
    ADN Support Specialist
    Balaji_Ram
    Posts: 351
    Registered: ‎03-21-2011

    Re: Create Named View - ViewTwist Problems??

    08-22-2012 01:51 PM in reply to: matinau

    Hi,

     

    The CenterPoint of the ViewtableRecord is to be provided in DCS. In the below sample code, I set the view width and height for the current view. I then perform the transformation of the center point from UCS to DCS and set it as the centerpoint of the viewtable record.

     

    [CommandMethod("VM")]
    public static void NamedView()
    {
        // Test Values for Named View
        string viewname = "100 m";
        double width = 420;
        double height = 297;
        double angle = 0;
    
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Editor ed = doc.Editor;
        Database db = doc.Database;
    
        using (Transaction tr = db.TransactionManager.StartTransaction())
        {
            BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
            BlockTableRecord ms = (BlockTableRecord)tr.GetObject(
                bt[BlockTableRecord.ModelSpace],
                OpenMode.ForWrite);
    
            // Catch last entity (MVF Block) and align UCS
            ObjectId ent = Autodesk.AutoCAD.Internal.Utils.EntLast();
    
            if (ent.ObjectClass.DxfName == "INSERT")
            {
                BlockReference blk = (BlockReference)tr.GetObject(ent, OpenMode.ForRead);
                ed.CurrentUserCoordinateSystem = blk.BlockTransform;
                angle = blk.Rotation;
            }
            else
            {
                ed.WriteMessage("Last entity was not a block reference");
                return;
            }
    
            // Get Current coordinate system
            CoordinateSystem3d cs = ed.CurrentUserCoordinateSystem.CoordinateSystem3d;
            Point3d ucsOrigin = cs.Origin; // Used later for View Origin creation
    
            //==================================================================
            // Create New User UCS
            //==================================================================
    
            UcsTable ut = (UcsTable)tr.GetObject(db.UcsTableId, OpenMode.ForRead);
            UcsTableRecord utr;
    
            if (ut.Has(viewname))
            {
                utr = (UcsTableRecord)tr.GetObject(
                    ut[viewname], OpenMode.ForWrite);
            }
            else
            {
                utr = new UcsTableRecord();
                utr.Name = viewname;
                ut.UpgradeOpen();
                ut.Add(utr);
                tr.AddNewlyCreatedDBObject(utr, true);
            }
    
    
            utr.Origin = ucsOrigin;
            utr.XAxis = cs.Xaxis;
            utr.YAxis = cs.Yaxis;
    
            ViewportTableRecord vptr = tr.GetObject(doc.Editor.ActiveViewportId,
                OpenMode.ForWrite) as ViewportTableRecord;
    
            vptr.IconAtOrigin = true;
            vptr.IconEnabled = true;
    
            vptr.SetUcs(utr.ObjectId);
            doc.Editor.UpdateTiledViewportsFromDatabase();
    
            //==================================================================
            // Create View
            //==================================================================
    
            ViewTable vt = (ViewTable)tr.GetObject(db.ViewTableId, OpenMode.ForRead);
            ViewTableRecord vtr;
    
            // Check if View Name exists
            if (vt.Has(viewname))
            {
                vtr = tr.GetObject(vt[viewname], OpenMode.ForWrite) as ViewTableRecord;
            }
            else
            {
                vtr = new ViewTableRecord();
                vtr.Name = viewname;
                vt.UpgradeOpen();
                ObjectId vtId = vt.Add(vtr);
                tr.AddNewlyCreatedDBObject(vtr, true);
            }
    
            // Calculate center point if view
            double centerX = width / 2;
            double centerY = height / 2;
            Point2d ctr = new Point2d(centerX, centerY);
    
            //Convert to ucs origin to Point2d
            Point2d viewOrigin = new Point2d(ucsOrigin.X, ucsOrigin.Y);
    
            //Create a rotation Matrix for X and Y Axis
            Matrix2d mat = Matrix2d.Rotation(angle, Point2d.Origin);
            Matrix2d mat2d = new Matrix2d();
            mat2d = Matrix2d.AlignCoordinateSystem(
                Point2d.Origin,
                Vector2d.XAxis,
                Vector2d.YAxis,
                viewOrigin,
                new Vector2d(1, 0).TransformBy(mat),
                new Vector2d(0, 1).TransformBy(mat)
                );
    
            vtr.ViewTwist = -angle;
            vtr.IsPaperspaceView = false;
            vtr.SetUcs(utr.ObjectId);
            vtr.Height = height;
            vtr.Width = width;
    
            // Set new view to current
            ed.SetCurrentView(vtr);
    
    		//////////////////////////////////////////////////////////////////////////////////////////////////////////////
    		//// Set the CenterPoint of the VTR in DCS
    		ResultBuffer ucs = new ResultBuffer(new TypedValue(5003, 1));    // UCS
    		ResultBuffer dcs = new ResultBuffer(new TypedValue(5003, 2));    // DCS
    
    		Point3d viewCenter3d_ucs = new Point3d(ctr.X, ctr.Y, 0.0);
    		double[] fromPt = viewCenter3d_ucs.ToArray();
    		double[] toPt = new double[] { 0, 0, 0 };
    		acedTrans(fromPt, ucs.UnmanagedObject, dcs.UnmanagedObject, 0, toPt);
    		Point3d viewCenter3d_dcs = new Point3d(toPt[0], toPt[1], toPt[2]);
    
    		vtr.CenterPoint = new Point2d(viewCenter3d_dcs.X, viewCenter3d_dcs.Y); 
    		//////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    
            //==================================================================
            // Create Polyline test geometry
            //==================================================================
    
            // Defined bouding box to new View
            Point2d pt1 = new Point2d(0, 0);
            Point2d pt2 = new Point2d(width, 0);
            Point2d pt3 = new Point2d(width, height);
            Point2d pt4 = new Point2d(0, height);
    
            Point2d[] pts = { pt1, pt2, pt3, pt4 };
    
            // Define Polyline Vertices
            Polyline pl = new Polyline(4);
    
            int max = pts.GetUpperBound(0);
    
            for (int i = 0; i <= max; i++)
            {
                int j = (i == max ? 0 : i + 1);
                pl.AddVertexAt(i, pts[j].TransformBy(mat2d), 0, 0, 0);
            }
            pl.Closed = true;
    
            // Add the boundary to modelspace
            ObjectId id = ms.AppendEntity(pl);
            tr.AddNewlyCreatedDBObject(pl, true);
    
            tr.Commit();
        }
    }

     

     

     

     

     



    Balaji
    Developer Technical Services
    Autodesk Developer Network

    Please use plain text.
    Contributor
    Posts: 19
    Registered: ‎05-03-2010

    Re: Create Named View - ViewTwist Problems??

    03-08-2013 01:21 PM in reply to: Balaji_Ram

    Same idea, slightly different approach for anyone interested

     

    Matrix3d WCS2DCS =
    Matrix3d
    .PlaneToWorld(vtr.ViewDirection)
    .PreMultiplyBy(Matrix3d.Displacement(vtr.Target - Point3d.Origin))
    .PreMultiplyBy(Matrix3d.Rotation(-vtr.ViewTwist, vtr.ViewDirection, vtr.Target))
    .Inverse();
    Point3d viewCenter = blk.Position.TransformBy(WCS2DCS);
    vtr.CenterPoint = new Point2d(viewCenter.X + width / 2.0, viewCenter.Y + height / 2.0);

    vtr.SetUcs(utr.ObjectId);

    Please use plain text.