Author Topic: Mixing current viewport ViewDirection with Custom UCS  (Read 2531 times)

0 Members and 1 Guest are viewing this topic.

graham

  • Guest
Mixing current viewport ViewDirection with Custom UCS
« on: August 01, 2013, 10:11:00 AM »
We're currently working to produce a system that lets engineers select a group of objects containing amongst other things, a Dynamic Block sized to the bounds of the selection group.

We're then moving this into a new document/db for use with stand alone production drawings...

Problem is though, when we're defining our new UCS and then setting this to viewports it's obliterating the initial view directions (OrthographicView) that were set in these.

With this in mind might anyone have any idea on how to "mingle" (for want of a better word) the Custom UCS with the current View Direction so that we're orienting the object group correctly for production purposes whilst still maintaining the ViewDirection set in the template?

Any thoughts on how this might be acheived would be absolutely fantastic, and please feel free to point out any stupidity I may have missed with this.

Cheers!

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Mixing current viewport ViewDirection with Custom UCS
« Reply #1 on: August 01, 2013, 10:52:48 AM »
Without code samples its hard to see how you've gone about this but I suspect you're trying to set the new viewport up in the new UCS with the settings from the original UCS. You might get the desired result if you transform the ViewDirection property to your new UCS

graham

  • Guest
Re: Mixing current viewport ViewDirection with Custom UCS
« Reply #2 on: August 02, 2013, 04:37:24 AM »
Cheers for the response Will.

Currently this is more of a conceptual exercise so there's little working code to post.

I've been looking at sorting this out manually by opening view manager and then changing the "Set Relative To" property of the views to my Custom Named UCS, which seems to give the desired result when moving into paper space and doing a plan, so next steps are hopefully looking into any ways to acheive the same thing in .NET code.

again, any suggestions would be welcome!

Cheers!

graham

  • Guest
Re: Mixing current viewport ViewDirection with Custom UCS
« Reply #3 on: August 02, 2013, 07:24:30 AM »
Here's a look at the SetUCS method I've now built out.

It sort of works in that it does set the module-001-ucs settings (via the newUcsMatrix var) to the viewports in the loop, but the problem is doing a plan on them sets their view to whatever view was initially defined in the model space where that ucs was created (ie the top view for this arrangement).

Code: [Select]
private static void SetViewportUcs()
        {

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
           
            using (DocumentLock docloc = doc.LockDocument())
            {

                Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
                Database db = HostApplicationServices.WorkingDatabase;

                AcadDocument acadDoc = (AcadDocument)doc.GetAcadDocument();

                using (Transaction trans = db.TransactionManager.StartTransaction())
                {

                    LayoutManager layoutMgr = LayoutManager.Current;
                    Layout layoutObj;
                    DBDictionary layoutDict;

                    UcsTableRecord ucsTblRec;

                    UcsTable ucsTbl = trans.GetObject(db.UcsTableId, OpenMode.ForRead) as UcsTable;

                    if (ucsTbl.Has("module-001-ucs"))
                    {
                        ucsTblRec = (UcsTableRecord)trans.GetObject(ucsTbl["module-001-ucs"], OpenMode.ForWrite);
                    }
                    else
                    {
                        ucsTblRec = new UcsTableRecord();
                        ucsTblRec.Name = "module-001-ucs";
                        ucsTbl.UpgradeOpen();
                        ucsTbl.Add(ucsTblRec);
                        trans.AddNewlyCreatedDBObject(ucsTblRec, true);
                    }

                    Point3d toOrigin;
                    Vector3d toXAxis, toYAxis, toZAxis;

                    toOrigin = ucsTblRec.Origin;
                    toXAxis = ucsTblRec.XAxis;
                    toYAxis = ucsTblRec.YAxis;

                    toZAxis = toXAxis.CrossProduct(toYAxis);
                    Matrix3d newUcsMatrix = Matrix3d.AlignCoordinateSystem(
                                    Point3d.Origin,
                                    Vector3d.XAxis,
                                    Vector3d.YAxis,
                                    Vector3d.ZAxis,
                                    toOrigin, toXAxis, toYAxis, toZAxis
                    );

                    using (layoutDict = trans.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary)
                    {

                        foreach (DictionaryEntry layoutEntry in layoutDict)
                        {
                           
                            using (layoutObj = trans.GetObject((ObjectId)(layoutEntry.Value), OpenMode.ForRead) as Layout)
                            {

                                if (layoutObj.LayoutName != "Model")
                                {

                                    acadDoc.ActiveLayout = acadDoc.Layouts.Item(layoutObj.LayoutName);

                                    // Set the CurrentLayout to the layout if it is not Modelspace
                                    layoutMgr.CurrentLayout = layoutObj.LayoutName;

                                    //loop over viewports in current layout
                                    for (int i = 1; i < acadDoc.PaperSpace.Count; i++)
                                    {

                                        try
                                        {

                                            AcadPViewport acadViewport = acadDoc.PaperSpace.Item(i) as AcadPViewport;
                                           
                                            if (acadViewport.ObjectName == "AcDbViewport")
                                            {

                                                acadViewport.Display(true);
                                                acadViewport.ViewportOn = true;

                                                acadDoc.MSpace = true;
                                                acadDoc.ActivePViewport = acadViewport;
                                               
                                                acadViewport.UCSPerViewport = true;

                                                ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

                                                ViewTableRecord vtr = ed.GetCurrentView();
                                                Vector3d vDirection = vtr.ViewDirection;
                                               
                                                vDirection.TransformBy(newUcsMatrix);
                                                ViewTableRecord newVtr = new ViewTableRecord();
                                                newVtr.ViewDirection = vDirection;

                                                ed.CurrentUserCoordinateSystem = newUcsMatrix;
                                                ed.SetCurrentView(newVtr);
                                               
                                                acadDoc.Regen(AcRegenType.acActiveViewport);
                                                //doc.SendStringToExecute("_PLAN C ", true, false, true);

                                            }

                                        }
                                        catch
                                        {

                                        }
                                    }
                                }
                            }
                        }
                   
                    }

                    trans.Commit();
                }

            }
        }


The following lines are where I'm trying to do a transform of the current ViewDirection to the ucs matrix3D, which don't seem to be working as expected, but may well be doing exactly what they should realistically, with my limited knowledge.

Code: [Select]
ViewTableRecord vtr = ed.GetCurrentView();
 Vector3d vDirection = vtr.ViewDirection;
                                               
vDirection.TransformBy(newUcsMatrix);
ViewTableRecord newVtr = new ViewTableRecord();
newVtr.ViewDirection = vDirection;

Once Again. Cheers to anyone who looks over this and points out any programmer error...  :laugh:

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Mixing current viewport ViewDirection with Custom UCS
« Reply #4 on: August 02, 2013, 01:37:34 PM »
Code: [Select]
ViewTableRecord vtr = ed.GetCurrentView();
 Vector3d vDirection = vtr.ViewDirection;
                                               
vDirection.TransformBy(newUcsMatrix);
ViewTableRecord newVtr = new ViewTableRecord();
newVtr.ViewDirection = vDirection;
I ran into something similar the other day which completely confused me but I had to set my vector equal to the result as though the method wasn't just working on the operand
Code - C#: [Select]
  1. ViewTableRecord vtr = ed.GetCurrentView();
  2.  Vector3d vDirection = vtr.ViewDirection;
  3.                                                
  4. ViewTableRecord newVtr = new ViewTableRecord();
  5. newVtr.ViewDirection = vDirection.TransformBy(newUcsMatrix);