Author Topic: Read properties of a viewport  (Read 3247 times)

0 Members and 1 Guest are viewing this topic.

latour_g

  • Newt
  • Posts: 184
Read properties of a viewport
« on: March 05, 2014, 10:14:53 AM »
Hi,

When a viewport is selected, I need to read the scale of it.  I already have a function that detect when a viewport is selected (SystemVariableChanged event) but I don't know how to get the viewport properties without a PromptEntityResult ?

Code: [Select]
        public static void SystemVariableChanged(object o, Autodesk.AutoCAD.ApplicationServices.SystemVariableChangedEventArgs e)
        {
            ....
            if (e.Name.Equals("CVPORT") && (Int16)AcadApp.GetSystemVariable("TILEMODE") == 0 && ((Int16)AcadApp.GetSystemVariable("CVPORT") > 1))
            {
                    ---> a viewport have been selected and I want to read the scale of it
            }
            ....
        }
« Last Edit: March 05, 2014, 10:48:14 AM by latour_g »

kaefer

  • Guest
Re: Read properties of a viewport
« Reply #1 on: March 05, 2014, 10:51:36 AM »
When a viewport is selected, I need to read the scale of it.  I already have a function that detect when a viewport is selected (SystemVariableChanged event) but I don't know how to get the viewport properties without a PromptEntityResult ?

When you are monitoring the CVPORT system variable, then its value will correspond to the Autodesk.AutoCAD.DatabaseServices.Viewport.Number property. You'd need to collect all viewports of the current layout and find that with the matching number.
« Last Edit: March 06, 2014, 02:23:59 AM by kaefer »

latour_g

  • Newt
  • Posts: 184
Re: Read properties of a viewport
« Reply #2 on: March 06, 2014, 09:31:41 AM »
Thanks kaefer ! I understand what to do but I must admit that yesterday I googled for a long time to find how to collect viewport from the current view.

Does it have something to do with : ViewportTableRecord vtr = (ViewportTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead); ?

I see that ViewportTableRecord has no enumerator so I'm kind of stuck...   If someone can help for that, I would appreciate !

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Read properties of a viewport
« Reply #3 on: March 06, 2014, 09:43:25 AM »
What do you mean by a viewport selected?

That a user selected the viewport by clicking on it?
The user double clicked inside  it activated it?

latour_g

  • Newt
  • Posts: 184
Re: Read properties of a viewport
« Reply #4 on: March 06, 2014, 10:01:38 AM »
The user double clicked inside it and activated it.  What I want to do after that is to check the viewport properties to check the annotation scale value and the standard scale value.


Mithraz87

  • Guest
Re: Read properties of a viewport
« Reply #5 on: March 06, 2014, 10:19:28 AM »
I have been using a function to loop through viewports for a different purpose, but this could get you started. I created this with help from other users here on The Swamp.

Code - C#: [Select]
  1. private void VPLOOP()
  2. {
  3.         Document doc = Core.Application.DocumentManager.MdiActiveDocument;
  4.         Autodesk.AutoCAD.GraphicsSystem.Manager gsm = Core.Application.DocumentManager.MdiActiveDocument.GraphicsManager;
  5.         Database db = doc.Database;
  6.         Editor ed = doc.Editor;
  7.         LayoutManager lm = LayoutManager.Current;
  8.  
  9.         int x = 0;
  10.  
  11.         List<string> layers = new List<string>();
  12.  
  13.         foreach (AcadLayer layer in ThisDrawing.Layers) {
  14.                 layers.Add(layer.Name);
  15.         }
  16.  
  17.         using (tr == db.TransactionManager.StartTransaction()) {
  18.  
  19.                 ObjectId idLay = default(ObjectId);
  20.  
  21.                 ObjectId idLayTblRcd = default(ObjectId);
  22.  
  23.                 LayerTableRecord lt = default(LayerTableRecord);
  24.  
  25.                 Point3d llc = default(Point3d);
  26.                 Point3d urc = default(Point3d);
  27.                 // Save the name of the current layout
  28.                 currentLayout = lm.CurrentLayout;
  29.  
  30.                 ObjectId loid = LayoutManager.Current.GetLayoutId(lm.CurrentLayout);
  31.                 Layout lo = tr.GetObject(loid, OpenMode.ForRead);
  32.                 //For Each de As DBDictionaryEntry In layoutDict
  33.  
  34.                 lm.CurrentLayout = currentLayout;
  35.                 string layoutName = lo.LayoutName;
  36.  
  37.                 if (x == 0 && layoutName != "Model") {
  38.                         //Loop thru layout viewports
  39.                         //Note, the first vp is the layout vp itself
  40.                         ObjectIdCollection vpIdCol = lo.GetViewports();
  41.  
  42.                         //Looping through the collection of viewports
  43.  
  44.                         foreach (ObjectId vpId in vpIdCol) {
  45.                                 Viewport vp = vpId.GetObject(OpenMode.ForRead);
  46.  
  47.                                 //Make sure we're not checking the overall layout viewport
  48.  
  49.                                 if (vp.Number > 1) {
  50.                                         LayerTable ltt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead, false);
  51.  
  52.  
  53.                                         foreach (string lname in layers) {
  54.                                                 idLay = ltt(lname);
  55.  
  56.                                                 lt = tr.GetObject(idLay, OpenMode.ForRead);
  57.  
  58.  
  59.                                                 if (ltt.Has(lname)) {
  60.                                                         idLayTblRcd = ltt.Item(lname);
  61.  
  62.  
  63.                                                 } else {
  64.                                                         ed.WriteMessage("Layer: \"" + lname + "\" not available");
  65.  
  66.                                                         VPortX = VPortX + 1;
  67.                                                         break;
  68.  
  69.                                                 }
  70.  
  71.  
  72.                                                 if (vpId.IsNull()) {
  73.                                                         ed.WriteMessage("No Viewport current.");
  74.                                                         VPortX = VPortX + 1;
  75.                                                         //I would rather just 'Exit Sub' but the transaction needs to be disposed, so we go down to that point in the code.
  76.                                                         goto endusing;
  77.  
  78.                                                 }
  79.  
  80.  
  81.                                                 if (!vp.IsLayerFrozenInViewport(idLayTblRcd)) {
  82.                                                         LayerTableRecord newLayer = (DatabaseServices.LayerTableRecord)db.TransactionManager.GetObject(idLayTblRcd, DatabaseServices.OpenMode.ForRead);
  83.  
  84.                                                         if (!newLayer.IsFrozen && !newLayer.IsOff) {
  85.                                                                 int vpn = vp.Number - 1;
  86.  
  87.                                                                 if (!ValidLayers.Contains(newLayer.Name + "," + vpn.ToString())) {
  88.                                                                         ValidLayers.Add(newLayer.Name + "," + vpn.ToString());
  89.                                                                 }
  90.                                                         }
  91.                                                 }
  92.                                         }
  93.  
  94.                                         //Continuing the loop if the viewport has been erased
  95.                                         if (vp.IsErased) {
  96.                                                 continue;
  97.                                         }
  98.  
  99.                                         //This is run if the viewport isn't a rectangle
  100.                                         if (vp.NonRectClipOn) {
  101.                                                 //Adding the number of viewports
  102.                                                 NumberVPorts = NumberVPorts + 1;
  103.  
  104.                                                 ObjectId nonrectoid = vp.NonRectClipEntityId;
  105.                                                 Polyline nrvp = tr.GetObject(nonrectoid, OpenMode.ForRead);
  106.                                                 Point3dCollection stretchPoints = new Point3dCollection();
  107.  
  108.                                                 nrvp.GetStretchPoints(stretchPoints);
  109.  
  110.                                                 //Adding up the number of vertices
  111.                                                 VPortVertices.Add(stretchPoints.Count + 1);
  112.  
  113.                                                 Point2d firstpt = default(Point2d);
  114.                                                 for (i = 0; i <= stretchPoints.Count - 1; i++) {
  115.                                                         Point3d newpt = new Point3d(stretchPoints(i).X, stretchPoints(i).Y, stretchPoints(i).Z);
  116.  
  117.                                                         Vector3d newvect = vp.CenterPoint.GetAsVector;
  118.  
  119.                                                         newpt = newpt.Subtract(newvect);
  120.                                                         newpt = newpt.MultiplyBy(1.0 / vp.CustomScale);
  121.  
  122.                                                         Autodesk.AutoCAD.GraphicsSystem.View vpview = new Autodesk.AutoCAD.GraphicsSystem.View();
  123.  
  124.                                                         gsm.SetViewFromViewport(vpview, vp.Number);
  125.                                                         Matrix3d viewmat = vpview.ViewingMatrix.Inverse();
  126.  
  127.                                                         Point3d newpt3d = newpt.TransformBy(viewmat);
  128.                                                         Point2d newpt2d = new Point2d(newpt3d.X, newpt3d.Y);
  129.  
  130.                                                         //Adding each point into the list of coordinates
  131.                                                         VPortCoordinates.Add(newpt2d);
  132.  
  133.                                                         if (i == 0) {
  134.                                                                 firstpt = newpt2d;
  135.                                                         }
  136.  
  137.                                                         if (i == stretchPoints.Count - 1) {
  138.                                                                 //And adding the first point again, so we can go around in a circle
  139.                                                                 VPortCoordinates.Add(firstpt);
  140.                                                         }
  141.                                                 }
  142.  
  143.                                         //This is run if it is a rectangle
  144.                                         } else {
  145.                                                 llc = vp.Bounds.Value.MinPoint;
  146.                                                 urc = vp.Bounds.Value.MaxPoint;
  147.  
  148.                                                 llc = llc.Subtract(vp.CenterPoint.GetAsVector);
  149.                                                 urc = urc.Subtract(vp.CenterPoint.GetAsVector);
  150.  
  151.                                                 llc = llc.MultiplyBy(1.0 / vp.CustomScale);
  152.                                                 urc = urc.MultiplyBy(1.0 / vp.CustomScale);
  153.  
  154.                                                 Autodesk.AutoCAD.GraphicsSystem.View vpview = new Autodesk.AutoCAD.GraphicsSystem.View();
  155.  
  156.                                                 gsm.SetViewFromViewport(vpview, vp.Number);
  157.                                                 Matrix3d viewmat = vpview.ViewingMatrix.Inverse();
  158.  
  159.                                                 Point3d newllc = llc.TransformBy(viewmat);
  160.                                                 Point3d newurc = urc.TransformBy(viewmat);
  161.  
  162.                                                 Point2d llc2d = new Point2d(newllc.X, newllc.Y);
  163.                                                 Point2d lrc2d = new Point2d(newurc.X, newllc.Y);
  164.                                                 Point2d urc2d = new Point2d(newurc.X, newurc.Y);
  165.                                                 Point2d ulc2d = new Point2d(newllc.X, newurc.Y);
  166.  
  167.                                                 //Adding the vertices and number of viewports
  168.                                                 VPortVertices.Add(5);
  169.                                                 NumberVPorts = NumberVPorts + 1;
  170.                                                 //Adding each coordinate in, and the first again
  171.                                                 VPortCoordinates.Add(llc2d);
  172.                                                 VPortCoordinates.Add(lrc2d);
  173.                                                 VPortCoordinates.Add(urc2d);
  174.                                                 VPortCoordinates.Add(ulc2d);
  175.                                                 VPortCoordinates.Add(llc2d);
  176.                                         }
  177.                                 }
  178.                         }
  179.                 }
  180.                 x = x + 1;
  181.                 //Next
  182.                 lm.CurrentLayout = currentLayout;
  183.                 endusing:
  184.                 //Dispose of the transaction
  185.         }
  186.  
  187. }
  188.  

The part of the code that could be most helpful to you is the "vp.CustomScale".

Note: I converted this to C# from VB to try and make it easier for you.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Read properties of a viewport
« Reply #6 on: March 06, 2014, 10:19:53 AM »
You can do something like

Code - C#: [Select]
  1.         [CommandMethod("Vpscale", CommandFlags.NoTileMode)]
  2.         public void Vpscale()
  3.         {
  4.             short cvport = (short)Application.GetSystemVariable("CVPORT");
  5.        
  6.             if (cvport > 1)
  7.             {
  8.                 using (Transaction trx = Doc.TransactionManager.StartTransaction())
  9.                 {
  10.                     Viewport vp = (Viewport)Ed.ActiveViewportId.GetObject(OpenMode.ForRead);
  11.                     Ed.WriteLine(vp.CustomScale);
  12.                     Ed.WriteLine(vp.AnnotationScale.Name);
  13.  
  14.                     trx.Commit();
  15.                 }
  16.             }
  17.  
  18.         }
  19.  

latour_g

  • Newt
  • Posts: 184
Re: Read properties of a viewport
« Reply #7 on: March 06, 2014, 10:41:44 AM »
Thanks A LOT to both of you !! That's solve my problem.   :-D