Author Topic: Named Views by Viewports  (Read 1889 times)

0 Members and 1 Guest are viewing this topic.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Named Views by Viewports
« on: September 12, 2012, 09:02:36 AM »
Hi all. I apologize for my bad English.

1. I need create named views by each viewport contents. Viewports are located on the Layout. I did it, but I got incorrect sizes by my named views. Why it happened?

2. How can I create Category Name by my named views?

3. How it right to use ViewAssociatedToViewport property?

Read my comments, please:
Code - C#: [Select]
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. //Autodesk namespaces ***************
  6. using acad = Autodesk.AutoCAD.ApplicationServices.Application;
  7. using AcApp = Autodesk.AutoCAD.ApplicationServices;
  8. using AcDb = Autodesk.AutoCAD.DatabaseServices;
  9. using AcEd = Autodesk.AutoCAD.EditorInput;
  10. using AcRtm = Autodesk.AutoCAD.Runtime;
  11. using AcPub = Autodesk.AutoCAD.Publishing;
  12. using AcPlt = Autodesk.AutoCAD.PlottingServices;
  13. using AcGem = Autodesk.AutoCAD.Geometry;
  14. using AcCol = Autodesk.AutoCAD.Colors;
  15. using AcCMod = Autodesk.AutoCAD.ComponentModel;
  16. using AcGInt = Autodesk.AutoCAD.GraphicsInterface;
  17. using AcGSys = Autodesk.AutoCAD.GraphicsSystem;
  18. using AcLayMng = Autodesk.AutoCAD.LayerManager;
  19. using AcWin = Autodesk.AutoCAD.Windows;
  20. using AcComp = ACSMCOMPONENTS17Lib;
  21. //************************************
  22.  
  23. [assembly: AcRtm.CommandClass(typeof(Xany.AutoCAD.SheetSets.Tools))]
  24.  
  25. namespace Xany.AutoCAD.SheetSets {
  26.         public class Tools {
  27.                 [AcRtm.CommandMethod("CreateNamedViewsByLayoutViewports")]
  28.                 public void CreateNamedViewsByLayoutViewports() {
  29.                         AcApp.Document doc = acad.DocumentManager.MdiActiveDocument;
  30.                         AcDb.Database db = doc.Database;
  31.                         AcEd.Editor ed = doc.Editor;
  32.  
  33.                         using (AcDb.Transaction tr = db.TransactionManager.StartTransaction()) {
  34.                                 AcDb.BlockTable bt = (AcDb.BlockTable)tr.GetObject(db.BlockTableId, AcDb.OpenMode.ForRead);
  35.                                 AcDb.ObjectId msId = bt[AcDb.BlockTableRecord.ModelSpace];
  36.                                 AcDb.ObjectId psId = bt[AcDb.BlockTableRecord.PaperSpace];
  37.                                                                
  38.                                 Boolean modalSpaceIsCurrent = ((Int16) acad.GetSystemVariable("TILEMODE")) == 1 ? true : false;
  39.                                 if (modalSpaceIsCurrent) {
  40.                                         ed.WriteMessage("Command canceled. Reason: MODEL tab is current.\n");
  41.                                         return;
  42.                                 }
  43.  
  44.                                 //Boolean viewPortIsCurrent = !modalSpaceIsCurrent && ((Int16)acad.GetSystemVariable("CVPORT")) != 1 ? true : false;
  45.                                 //if (viewPortIsCurrent) {
  46.                                 //    ed.WriteMessage("Coming back to paper from viewport...\n");
  47.                                 //    ed.SwitchToPaperSpace();         
  48.                                 //}
  49.  
  50.                                 AcDb.LayoutManager layMng = AcDb.LayoutManager.Current;
  51.                                 AcDb.ObjectId layoutId = layMng.GetLayoutId(layMng.CurrentLayout);
  52.  
  53.                                 //Current Layout
  54.                                 AcDb.Layout layout = (AcDb.Layout)tr.GetObject(layoutId, AcDb.OpenMode.ForRead);
  55.                                 AcDb.ObjectIdCollection ids = layout.GetViewports();
  56.  
  57.                                 AcDb.ViewTable vt = (AcDb.ViewTable) tr.GetObject(db.ViewTableId, AcDb.OpenMode.ForWrite);
  58.                                
  59.                                 foreach (AcDb.ObjectId id in ids) {
  60.                                         AcDb.Viewport viewport = (AcDb.Viewport) tr.GetObject(id, AcDb.OpenMode.ForRead);
  61.                                         if (viewport.Number != 1) {
  62.                                                 //Get viewport data:
  63.                                                 Double width = viewport.Width;
  64.                                                 Double height = viewport.Height;
  65.                                                 Double scale = viewport.CustomScale;
  66.                                                 AcGem.Point2d point = viewport.ViewCenter;
  67.                                                
  68.                                                 //Creating the new ViewTableRecord based on Viewport data, or opening existing.
  69.                                                 String vtrName = "view-" + viewport.Number;
  70.                                                 Boolean vtHasView = vt.Has(vtrName);
  71.                                                 AcDb.ViewTableRecord vtr = vtHasView ? (AcDb.ViewTableRecord)tr.GetObject(vt[vtrName],
  72.                                                         AcDb.OpenMode.ForWrite) : new AcDb.ViewTableRecord();
  73.  
  74.                                                 //How can I create Category Name? Next row ain't working (I get error);
  75.                                                 //vtr.CategoryName = "Auto generated view";
  76.                                                
  77.                                                 //I get correct CenterPoint
  78.                                                 vtr.CenterPoint = point;
  79.  
  80.                                                 //But I get incorrect width and height. Why?
  81.                                                 vtr.Width = width * scale;
  82.                                                 vtr.Height = height * scale;
  83.  
  84.                                                 //How can I use it?
  85.                                                 //vtr.ViewAssociatedToViewport = true;
  86.  
  87.                                                 if (!vtHasView) {
  88.                                                         vtr.Name = vtrName;
  89.                                                         vt.Add(vtr);
  90.                                                         tr.AddNewlyCreatedDBObject(vtr, true);
  91.                                                 }
  92.                                         }
  93.                                 }
  94.                                 tr.Commit();//Saving all changes.
  95.                         }
  96.                 }
  97.         }
  98. }
  99.  

Regards
« Last Edit: September 12, 2012, 09:07:44 AM by Andrey »

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Named Views by Viewports
« Reply #1 on: September 12, 2012, 09:25:38 AM »
The first question is closed. I should divide into scale factor, instead of to multiply by it:
Code - C#: [Select]
  1. vtr.Width = width / scale;
  2. vtr.Height = height / scale;
  3.