Author Topic: Unable to retrieve values from Viewport.GetUcs Method  (Read 1734 times)

0 Members and 1 Guest are viewing this topic.

KevinPluck

  • Guest
Unable to retrieve values from Viewport.GetUcs Method
« on: October 25, 2016, 06:46:55 AM »
Hello,

We have been attempting to retrieve the UCS of a viewport given a viewPortId.  It appears that the autocad API has not set the GetUcs parameters with ref or out keywords. Are we missing something?  As you can see below BricsCad has employed the ref keyword and we can successfully retrieve the UCS.

Code - C#: [Select]
  1.             AcDb.Viewport theViewport = (AcDb.Viewport)_tr.GetObject(viewPortId, AcDb.OpenMode.ForRead);
  2.  
  3.             AcGe.Point3d theOrigin = new AcGe.Point3d(666,666,666);
  4.             AcGe.Vector3d theXAxis = new AcGe.Vector3d();
  5.             AcGe.Vector3d theYAxis = new AcGe.Vector3d();
  6.  
  7. #if ACAD
  8.             theViewport.GetUcs(theOrigin, theXAxis, theYAxis);
  9. #endif
  10.  
  11. #if BCAD
  12.             theViewport.GetUcs(ref theOrigin, ref theXAxis, ref theYAxis);
  13. #endif

The result of theOrigin still contains 666,666,666, i.e. it is not being replaced with a new value.  The same effect happens if I do not set it to 666
We have submitted a bug to AutoDesk and are now actively looking for a workaround.  Your assistance and ideas would be greatly appreciated.

Alexander Rivilis

  • Bull Frog
  • Posts: 214
  • Programmer from Kyiv (Ukraine)

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Unable to retrieve values from Viewport.GetUcs Method
« Reply #2 on: October 25, 2016, 06:50:25 PM »
It's pretty sad that this has been an issue (unfixed bug) since 2006!
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

KevinPluck

  • Guest
Re: Unable to retrieve values from Viewport.GetUcs Method
« Reply #3 on: October 26, 2016, 04:23:34 AM »
Thanks guys, I have a response from AutoCad:

Haven't tried it out yet but fingers crossed.

Quote
Sorry, we already have a change request logged for this behavior.

As a workaround, the COM API can be used to set and get the values.
Here is a sample code:

 
Code - C#: [Select]
  1. using Autodesk.AutoCAD.Interop;
  2. using Autodesk.AutoCAD.Interop.Common;
  3.  
  4. [CommandMethod("SetUCS", CommandFlags.NoTileMode)]
  5. public void SetViewportUcs()
  6. {
  7.     Document doc = Application.DocumentManager.MdiActiveDocument;
  8.     Editor ed = doc.Editor;
  9.     Database db = doc.Database;
  10.  
  11.     PromptEntityOptions peo = new PromptEntityOptions("Select a viewport : ");
  12.     peo.SetRejectMessage("Select a viewport.");
  13.     peo.AddAllowedClass(typeof(Autodesk.AutoCAD.DatabaseServices.Viewport), true);
  14.     PromptEntityResult per = ed.GetEntity(peo);
  15.     if (per.Status != PromptStatus.OK)
  16.         return;
  17.     ObjectId vpId = per.ObjectId;
  18.  
  19.     AcadDocument acadDoc = doc.GetAcadDocument() as AcadDocument;
  20.     acadDoc.MSpace = true;
  21.     using (Transaction tr = db.TransactionManager.StartTransaction())
  22.     {
  23.         Autodesk.AutoCAD.DatabaseServices.Viewport vp = tr.GetObject(vpId, OpenMode.ForWrite) as Autodesk.AutoCAD.DatabaseServices.Viewport;
  24.                
  25.         AcadPViewport acadViewport = vp.AcadObject as AcadPViewport;
  26.         acadDoc.ActivePViewport = acadViewport;
  27.  
  28.         Matrix3d newUcsMat = Matrix3d.AlignCoordinateSystem(new Point3d(0, 0, 0), new Vector3d(1, 0, 0), new Vector3d(0, 1, 0), new Vector3d(0, 0, 1),
  29.         new Point3d(0, 0, 0), new Vector3d(0, 1, 0), new Vector3d(-1, 0, 0), new Vector3d(0, 0, 1));
  30.  
  31.         ed.CurrentUserCoordinateSystem = newUcsMat;
  32.         tr.Commit();
  33.     }
  34.     acadDoc.MSpace = false;
  35. }
  36.  
  37. [CommandMethod("GetUCS", CommandFlags.NoTileMode)]
  38. public void GetUCS_PaperSpace()
  39. {
  40.     Document doc = Application.DocumentManager.MdiActiveDocument;
  41.     Editor ed = doc.Editor;
  42.     Database db = doc.Database;
  43.  
  44.     PromptEntityOptions peo = new PromptEntityOptions("Select a viewport : ");
  45.     peo.SetRejectMessage("Select a viewport.");
  46.     peo.AddAllowedClass(typeof(Autodesk.AutoCAD.DatabaseServices.Viewport), true);
  47.     PromptEntityResult per = ed.GetEntity(peo);
  48.     if (per.Status != PromptStatus.OK)
  49.         return;
  50.     ObjectId vpId = per.ObjectId;
  51.  
  52.     AcadDocument acadDoc = doc.GetAcadDocument() as AcadDocument;
  53.     acadDoc.MSpace = true;
  54.  
  55.     using (Transaction tr = db.TransactionManager.StartTransaction())
  56.     {
  57.         Autodesk.AutoCAD.DatabaseServices.Viewport vp = tr.GetObject(vpId, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Viewport;
  58.                
  59.         AcadPViewport acadViewport = vp.AcadObject as AcadPViewport;
  60.         acadDoc.ActivePViewport = acadViewport;
  61.  
  62.         Matrix3d ucsMat = ed.CurrentUserCoordinateSystem;
  63.         CoordinateSystem3d cs = ucsMat.CoordinateSystem3d;
  64.  
  65.         ed.WriteMessage(String.Format("\nOrigin : {0}", cs.Origin));
  66.         ed.WriteMessage(String.Format("\nX Vec  : {0}", cs.Xaxis));
  67.         ed.WriteMessage(String.Format("\nY Vec  : {0}", cs.Yaxis));
  68.  
  69.         tr.Commit();
  70.     }
  71.  
  72.     acadDoc.MSpace = false;
  73. }

KevinPluck

  • Guest
Re: Unable to retrieve values from Viewport.GetUcs Method
« Reply #4 on: November 14, 2016, 04:56:12 AM »
We had a great deal of problems with this as the work around was working inconsistently and inaccurately.

Turns out this was due to our clients providing us with dwgs that had not had their viewports regenerated.

So before using the work around code it is recommended to execute:

PS
REGENALL
MS
REGENALL