Author Topic: Where did the grid go? "Vieport" .Net C#  (Read 1309 times)

0 Members and 1 Guest are viewing this topic.

Enrike

  • Mosquito
  • Posts: 1
Where did the grid go? "Vieport" .Net C#
« on: May 29, 2020, 11:55:07 AM »
Hi. I'm starting to learn .Net autocad.  :uglystupid2:
In the example below, I create a layout. Then I delete the standard viewport, create my own. But for some reason, the ability to control the grid disappears.
Tested on different versions of AutoCAD.
Code - C#: [Select]
  1.     public class MyCommands
  2.     {
  3.         [CommandMethod("all")]
  4.         public void Create()
  5.         {
  6.             NewLayout();
  7.             CreateVieport();
  8.         }
  9.  
  10.         [CommandMethod("createEmptyLayout")]
  11.         public void NewLayout()
  12.         {
  13.             string nameLayout = "MyLayout";
  14.  
  15.             Document doc = Application.DocumentManager.MdiActiveDocument;
  16.             Database DB = doc.Database;
  17.  
  18.             LayoutManager lm = LayoutManager.Current;
  19.  
  20.             ObjectId layoutId = lm.CreateLayout(nameLayout);
  21.  
  22.             lm.CurrentLayout = nameLayout;
  23.  
  24.             using (Transaction tr = DB.TransactionManager.StartTransaction())
  25.             {
  26.                 Layout layout = tr.GetObject(layoutId, OpenMode.ForRead) as Layout;
  27.  
  28.                 ObjectIdCollection LayoutIds = layout.GetViewports();
  29.                 if (LayoutIds.Count > 0)
  30.                 {
  31.                     Viewport vp = tr.GetObject(LayoutIds[1], OpenMode.ForWrite) as Viewport;
  32.                     vp.Erase();
  33.                 }
  34.                 tr.Commit();
  35.             }
  36.         }
  37.  
  38.         [CommandMethod("createVP")]
  39.  
  40.         public void CreateVieport()
  41.         {
  42.            
  43.             Document acDoc = Application.DocumentManager.MdiActiveDocument;
  44.             Database acCurDb = acDoc.Database;
  45.  
  46.             using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  47.             {
  48.                 BlockTable acBlkTbl;
  49.                 acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
  50.                                              OpenMode.ForRead) as BlockTable;
  51.                                
  52.                 BlockTableRecord acBlkTblRec;
  53.                 acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.PaperSpace],
  54.                                                 OpenMode.ForWrite) as BlockTableRecord;
  55.                                
  56.                 Application.SetSystemVariable("TILEMODE", 0);
  57.                 acDoc.Editor.SwitchToPaperSpace();
  58.                                
  59.                 using (Viewport acVport = new Viewport())
  60.                 {
  61.                     acVport.CenterPoint = new Point3d(100, 100, 0);
  62.                     acVport.Width = 200;
  63.                     acVport.Height = 200;
  64.  
  65.                    
  66.                     acVport.CustomScale = 2;
  67.                     acVport.ViewTarget = new Point3d(1000, 1000, 0);
  68.                     acVport.ViewCenter = new Point2d(0, 0);
  69.                    
  70.                     using (Circle acCirc = new Circle())
  71.                     {
  72.                         acCirc.Center = acVport.CenterPoint;
  73.                         acCirc.Radius = 100;
  74.  
  75.                        
  76.                         acBlkTblRec.AppendEntity(acCirc);
  77.                         acTrans.AddNewlyCreatedDBObject(acCirc, true);
  78.  
  79.                        
  80.                         acVport.NonRectClipEntityId = acCirc.ObjectId;
  81.                         acVport.NonRectClipOn = true;
  82.                     }
  83.                    
  84.                     acBlkTblRec.AppendEntity(acVport);
  85.                     acTrans.AddNewlyCreatedDBObject(acVport, true);
  86.  
  87.                     acVport.On = true;
  88.                     acVport.GridOn = true;
  89.  
  90.                 }
  91.                
  92.                 acTrans.Commit();
  93.             }
  94.         }
  95.  
  96.     }