Author Topic: Lock all viewports in multiple closed drawings  (Read 1634 times)

0 Members and 1 Guest are viewing this topic.

latour_g

  • Newt
  • Posts: 184
Lock all viewports in multiple closed drawings
« on: August 11, 2021, 04:05:16 PM »
Hi,

I'm trying to lock all viewport in a multiple closed drawings. 

It work for the first drawing but then it get stuck.

(I select files in treeview than I do the code above)

Thanks in advance

Code - C#: [Select]
  1.         private void Metavue_Click(object sender, EventArgs e)
  2.         {      
  3.             Document doc = AcadApp.DocumentManager.MdiActiveDocument;
  4.             bool Ok2Continue = true;
  5.  
  6.             for (int i = 0; i < dwgLst_purger.Count; i++)
  7.             {
  8.                 Database db = new Database(false, true);
  9.                 using (db)
  10.                 {
  11.                     db.RetainOriginalThumbnailBitmap = true;
  12.  
  13.                     string fileName = dwgLst_purger.GetKey(i).ToString();
  14.  
  15.                     try { db.ReadDwgFile(fileName, FileShare.ReadWrite, false, ""); }
  16.  
  17.                     catch (Autodesk.AutoCAD.Runtime.Exception ex)
  18.                     {
  19.                         Ok2Continue = false;
  20.                     }
  21.  
  22.                     HostApplicationServices.WorkingDatabase = db;
  23.  
  24.                     if (Ok2Continue)
  25.                     {
  26.                         using (Transaction tr = db.TransactionManager.StartTransaction())
  27.                         {      
  28.                             // IF I REMOVE FROM HERE (GO DOWN COUPLE LINE)  
  29.  
  30.                             DBDictionary layoutDict = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForWrite);
  31.  
  32.                             foreach (DictionaryEntry layoutEntry in layoutDict)
  33.                             {
  34.                                 Layout layoutObj = (Layout)tr.GetObject((ObjectId)(layoutEntry.Value), OpenMode.ForWrite);
  35.  
  36.                                 if (layoutObj.LayoutName == "Model") continue;
  37.                                 LayoutManager.Current.CurrentLayout = layoutObj.LayoutName;
  38.  
  39.                                 ObjectIdCollection objs = layoutObj.GetViewports();
  40.                                 int nCnt = 0;
  41.                                 foreach (ObjectId obj in objs)
  42.                                 {
  43.                                     if (nCnt++ == 0) continue;
  44.                                     Viewport vp = (Viewport)tr.GetObject(obj, OpenMode.ForWrite);
  45.                                     vp.Locked = true;
  46.                                 }
  47.                             }
  48.                             layoutDict.Dispose();
  49.                            
  50.                            // TO HERE, IT'S WORKING FINE, SO THE PROBLEM IS SOMEWHERE THERE BUT I DON'T SEE WHAT I'M DOING WRONG  
  51.  
  52.                             tr.Commit();
  53.                         }
  54.                     }
  55.  
  56.                     try
  57.                     {
  58.                         db.SaveAs(fileName, true, db.OriginalFileVersion, db.SecurityParameters);
  59.                     }
  60.                     catch
  61.                     {
  62.                         MessageBox.Show("Le dessin " + fileName + " est ouvert. Aucune modification faite sur ce dessin.", "Erreur");
  63.                     }
  64.                 }   // ----------------- IT GET STUCK HERE
  65.                 db.Dispose();
  66.             }
  67.  
  68.             HostApplicationServices.WorkingDatabase = doc.Database;
  69.  
  70.             MessageBox.Show("Traitement terminé");
  71.         }
  72.  
  73.  
  74.  
  75.  

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Lock all viewports in multiple closed drawings
« Reply #1 on: August 13, 2021, 03:31:28 AM »
Hi,
I tried to extract a method which can be tested in different contexts (active drawing, external database) and make it as simple as possible by removing useless statements.
Code - C#: [Select]
  1.         static void LockAllViewports (Database db)
  2.         {
  3.             using (var tr = db.TransactionManager.StartOpenCloseTransaction())
  4.             {
  5.                 var layouts = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);
  6.                 foreach (DBDictionaryEntry entry in layouts)
  7.                 {
  8.                     if (entry.Key == "Model")
  9.                         continue;
  10.                     var layout = (Layout)tr.GetObject(entry.Value, OpenMode.ForRead);
  11.                     var viewportIds = layout.GetViewports();
  12.                     for (int i = 1; i < viewportIds.Count; i++)
  13.                     {
  14.                         var viewport = (Viewport)tr.GetObject(viewportIds[i], OpenMode.ForWrite);
  15.                         viewport.Locked = true;
  16.                     }
  17.                 }
  18.                 tr.Commit();
  19.             }
  20.         }

To call this method from your cade:
Code - C#: [Select]
  1.             var ed = Application.DocumentManager.MdiActiveDocument.Editor;
  2.             foreach (string filename in dwgLst_purger)
  3.             {
  4.                 using(var db = new Database(false, true))
  5.                 {
  6.                     try
  7.                     {
  8.                         db.ReadDwgFile(filename, FileOpenMode.OpenForReadAndAllShare, false, null);
  9.                         LockAllViewports(db);
  10.                         db.SaveAs(filename, DwgVersion.Current);
  11.                     }
  12.                     catch(System.Exception ex)
  13.                     {
  14.                         ed.WriteMessage($"\n{ex.Message} ({filename})");
  15.                     }
  16.                 }
  17.             }
Speaking English as a French Frog

latour_g

  • Newt
  • Posts: 184
Re: Lock all viewports in multiple closed drawings
« Reply #2 on: August 13, 2021, 01:36:52 PM »
It works !  Thank you really much !