Author Topic: Export/import ViewTableRecord from one drawing to another  (Read 1411 times)

0 Members and 1 Guest are viewing this topic.

latour_g

  • Newt
  • Posts: 184
Export/import ViewTableRecord from one drawing to another
« on: January 25, 2023, 04:56:25 PM »
Hi,

I would like to be able to copy ViewTableRecord and UCSTableRecord from one drawing to another.  I tried, it does create views and ucs but then I got fatal error in my drawing when I tried to save.  Since I'm not really used with cloning object, I thought maybe I can't clone that type of object so I tried to create new ViewTableRecord manually but there are so many properties to copy, it's difficult to achieve the same result as a cloning would do.

Here is how I did using cloning :

Code - C#: [Select]
  1.         public void btnImportViewCloning()
  2.         {
  3.             Document doc = AcadApp.DocumentManager.MdiActiveDocument;
  4.             Database db = doc.Database;
  5.             Editor ed = doc.Editor;
  6.  
  7.             int nVuesCopie = 0;
  8.             int nVuesNonCopie = 0;
  9.             int nUCSCopie = 0;
  10.             int nUCSNonCopie = 0;
  11.  
  12.             List<string> listUCS_Existant = new List<string>();
  13.             List<string> listVues_Existant = new List<string>();
  14.  
  15.             listeScaleName.Clear();
  16.  
  17.             using (doc.LockDocument())
  18.             {
  19.                 using (Transaction tr = db.TransactionManager.StartTransaction())
  20.                 {
  21.                     ViewTable vt = (ViewTable)tr.GetObject(db.ViewTableId, OpenMode.ForWrite);
  22.                     UcsTable ut = (UcsTable)tr.GetObject(db.UcsTableId, OpenMode.ForWrite);              
  23.                    
  24.                     //listeRecordUCS = SortedList<string, UcsTableRecord>
  25.  
  26.                     foreach (KeyValuePair<string, UcsTableRecord> kvp in listeRecordUCS)
  27.                     {
  28.                         UcsTableRecord ucsData = kvp.Value;
  29.  
  30.                         if (listUCS_Existant.Contains(ucsData.Name.ToUpper())) { nUCSNonCopie = nUCSNonCopie + 1; continue; }
  31.                         else nUCSCopie = nUCSCopie + 1;
  32.  
  33.                         UcsTableRecord newUCS = (UcsTableRecord)ucsData.Clone();
  34.  
  35.                         ut.Add(newUCS);
  36.                         tr.AddNewlyCreatedDBObject(newUCS, true);
  37.                     }
  38.  
  39.                     //listeRecordView = SortedList<string, ViewTableRecord>
  40.  
  41.                     foreach (KeyValuePair<string, ViewTableRecord> kvp in listeRecordView)
  42.                     {
  43.                         ViewTableRecord vueData = kvp.Value;
  44.  
  45.                         if (listVues_Existant.Contains(vueData.Name.ToUpper())) { nVuesNonCopie = nVuesNonCopie + 1; continue; }
  46.                         else nVuesCopie = nVuesCopie + 1;
  47.  
  48.                         ViewTableRecord newVue = (ViewTableRecord)vueData.Clone();
  49.  
  50.                         vt.Add(newVue);
  51.                         tr.AddNewlyCreatedDBObject(newVue, true);
  52.                     }
  53.  
  54.                     tr.Commit();
  55.                 }
  56.             }
  57.         }
  58.  

Here is what I did trying to manually create ViewTableRecord (I used the picture above to help me create the ViewtableRecord.. it's quite hard work!)  :

Code - C#: [Select]
  1.         public void btnImportView()
  2.         {
  3.             Document doc = AcadApp.DocumentManager.MdiActiveDocument;
  4.             Database db = doc.Database;
  5.             Editor ed = doc.Editor;
  6.  
  7.             int nVuesCopie = 0;
  8.             int nVuesNonCopie = 0;
  9.             int nUCSCopie = 0;
  10.             int nUCSNonCopie = 0;
  11.  
  12.             List<string> listUCS_Existant = new List<string>();
  13.             List<string> listVues_Existant = new List<string>();
  14.  
  15.             listeScaleName.Clear();
  16.  
  17.             using (doc.LockDocument())
  18.             {
  19.                 using (Transaction tr = db.TransactionManager.StartTransaction())
  20.                 {
  21.                     ViewTable vt = (ViewTable)tr.GetObject(db.ViewTableId, OpenMode.ForWrite);
  22.                     UcsTable ut = (UcsTable)tr.GetObject(db.UcsTableId, OpenMode.ForWrite);
  23.  
  24.                     //faire une liste des data déjà existant pour ne pas créer de doublons
  25.                     foreach (ObjectId id in vt)
  26.                     {
  27.                         ViewTableRecord vtr = (ViewTableRecord)tr.GetObject(id, OpenMode.ForRead, false);
  28.                         listVues_Existant.Add(vtr.Name.ToUpper());
  29.                     }
  30.  
  31.                     foreach (ObjectId id in ut)
  32.                     {
  33.                         UcsTableRecord utr = (UcsTableRecord)tr.GetObject(id, OpenMode.ForRead, false);
  34.                         listUCS_Existant.Add(utr.Name.ToUpper());
  35.                     }
  36.  
  37.                     foreach (KeyValuePair<string, UCS_Data> kvp in listeUCS)
  38.                     {
  39.                         UCS_Data ucsData = kvp.Value;
  40.  
  41.                         if (listUCS_Existant.Contains(ucsData.name.ToUpper())) { nUCSNonCopie = nUCSNonCopie + 1; continue; }
  42.                         else nUCSCopie = nUCSCopie + 1;
  43.  
  44.                         UcsTableRecord utr = new UcsTableRecord();
  45.                         utr.Name = ucsData.name;
  46.                         utr.Origin = ucsData.ptOrigin;
  47.                         utr.XAxis = ucsData.axisX;
  48.                         utr.YAxis = ucsData.axisY;
  49.  
  50.                         ut.Add(utr);
  51.                         tr.AddNewlyCreatedDBObject(utr, true);
  52.                     }
  53.  
  54.                     foreach (KeyValuePair<string, View_Data> kvp in listeView)
  55.                     {
  56.                         View_Data vueData = kvp.Value;
  57.  
  58.                         if (listVues_Existant.Contains(vueData.name.ToUpper())) { nVuesNonCopie = nVuesNonCopie + 1; continue; }
  59.                         else nVuesCopie = nVuesCopie + 1;
  60.  
  61.                         ViewTableRecord vtr = new ViewTableRecord();
  62.                         vtr.Name = vueData.name;
  63.                         vtr.Target = vueData.viewTarget;
  64.                         vtr.Height = vueData.viewHeight;
  65.                         vtr.CenterPoint = vueData.viewCenter;
  66.                         vtr.Width = vueData.viewWidth;
  67.                         vtr.ViewDirection = vueData.viewDirection;
  68.                         vtr.ViewTwist = vueData.viewTwist;
  69.  
  70.                         //read only vtr.Ucs = vueData.UCS;
  71.                         //read only vtr.UcsName = vueData.UCSname;
  72.                         //read only vtr.UcsOrthographic = vueData.vUcsOrthographic;
  73.                         //read only vtr.ViewOrthographic = vueData.vViewOrthographic;
  74.                         //read only vtr.IsUcsAssociatedToView = vueData.bIsUcsAssociatedToView;
  75.  
  76.                         if (ut.Has(vueData.UCSname))
  77.                         {
  78.                             UcsTableRecord ucsRec = (UcsTableRecord)tr.GetObject(ut[vueData.UCSname], OpenMode.ForRead);
  79.                             vtr.SetUcs(ucsRec.ObjectId);
  80.                         }
  81.  
  82.                         vtr.AmbientLightColor = vueData.colAmbientLightColor;
  83.  
  84.                         vtr.DefaultLightingOn = vueData.bViewDefaultLightingOn;
  85.                         vtr.BackClipEnabled = vueData.bBackClipEnabled;
  86.                         vtr.FrontClipAtEye = vueData.bFrontClipAtEye;
  87.                         vtr.FrontClipEnabled = vueData.bFrontClipEnabled;
  88.                         vtr.PerspectiveEnabled = vueData.bPerspectiveEnabled;
  89.                         vtr.IsPaperspaceView = vueData.bIsPaperspaceView;
  90.                         vtr.BackClipDistance = vueData.dBackClipDistance;
  91.                         vtr.Brightness = vueData.dBrightness;
  92.                         vtr.Contrast = vueData.dContrast;
  93.                         vtr.FrontClipDistance = vueData.dFrontClipDistance;
  94.                         vtr.LensLength = vueData.dLensLength;
  95.                         vtr.DefaultLightingType = vueData.vDefaultLightingType;
  96.  
  97.                         //badUCS vtr.Elevation = vueData.dElevation;
  98.                         //not in database (empty) vtr.LayerState = vueData.bLayerState;
  99.                         //not in database vtr.ViewAssociatedToViewport = vueData.bViewAssociatedToViewport;
  100.                         //not in database vtr.ToneOperatorParameters = vueData.voneOperatorParameters;    
  101.                         //invalid input vtr.Thumbnail = vueData.bitThumbnail;
  102.  
  103.                         /*ObjectContextManager ocm = db.ObjectContextManager;
  104.                         ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");
  105.  
  106.                         if (ocm != null)
  107.                         {                            
  108.                             if (occ != null)
  109.                             {
  110.                                 foreach (AnnotationScale asc in occ)
  111.                                 {
  112.                                     listeScaleName.Add(asc.Name.ToUpper(), asc.Scale.ToString());
  113.                                 }
  114.                             }
  115.                         }
  116.  
  117.                         if (listeScaleName.ContainsKey(vueData.viewAnno.Name.ToUpper()))
  118.                             vtr.AnnotationScale = vueData.viewAnno;
  119.  
  120.                         else
  121.                         {
  122.                             AnnotationScale asc = new AnnotationScale();
  123.                             asc.Name = vueData.viewAnno.Name;
  124.                             asc.DrawingUnits = vueData.viewAnno.DrawingUnits;
  125.                             asc.PaperUnits = vueData.viewAnno.PaperUnits;
  126.                             try
  127.                             {
  128.                                 occ.AddContext(asc);
  129.                             }
  130.                             catch
  131.                             {
  132.                             }
  133.                             vtr.AnnotationScale = vueData.viewAnno;
  134.                         }*/
  135.  
  136.                         vt.Add(vtr);
  137.                         tr.AddNewlyCreatedDBObject(vtr, true);
  138.                     }
  139.  
  140.                     tr.Commit();
  141.                 }
  142.             }
  143.  
  144.             MessageBox.Show(nVuesCopie + " vues copiées, " + nVuesNonCopie + " vues déjà présentes, " + "\n" + nUCSCopie + " SCU copiés, " + nUCSNonCopie + " SCU déjà présentes.", "Terminé", MessageBoxButtons.OK);
  145.         }
  146.  
  147.  

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Export/import ViewTableRecord from one drawing to another
« Reply #1 on: January 25, 2023, 05:49:42 PM »
Hi

You're using Dictionaries containing DBObjects (listeRecordUCS and listeRecordView) which were got or create in another transaction. If this transaction have been disposed, these DBObjects also have been disposed.

You can simply import these records in a single method using a side Database.
Code - C#: [Select]
  1.         public static void ImportUcsViews(Database destDb, string filename, List<string> ucsNames, List<string> viewNames)
  2.         {
  3.             var ucsIds = new ObjectIdCollection();
  4.             var viewIds = new ObjectIdCollection();
  5.             using (var sourceDb = new Database(false, true))
  6.             {
  7.                 sourceDb.ReadDwgFile(filename, FileOpenMode.OpenForReadAndAllShare, false, null);
  8.                 using (var tr = sourceDb.TransactionManager.StartTransaction())
  9.                 {
  10.                     var ucsTable = (UcsTable)tr.GetObject(sourceDb.UcsTableId, OpenMode.ForRead);
  11.                     foreach (string name in ucsNames)
  12.                     {
  13.                         if (ucsTable.Has(name)) ucsIds.Add(ucsTable[name]);
  14.                     }
  15.                     var viewTable = (ViewTable)tr.GetObject(sourceDb.ViewTableId, OpenMode.ForRead);
  16.                     foreach (string name in viewNames)
  17.                     {
  18.                         if (viewTable.Has(name)) viewIds.Add(viewTable[name]);
  19.                     }
  20.                     tr.Commit();
  21.                 }
  22.                 var mapping = new IdMapping();
  23.                 destDb.WblockCloneObjects(ucsIds, destDb.UcsTableId, mapping, DuplicateRecordCloning.Replace, false);
  24.                 mapping = new IdMapping();
  25.                 destDb.WblockCloneObjects(viewIds, destDb.ViewTableId, mapping, DuplicateRecordCloning.Replace, false);
  26.             }
  27.         }
« Last Edit: January 26, 2023, 01:52:01 AM by gile »
Speaking English as a French Frog

latour_g

  • Newt
  • Posts: 184
Re: Export/import ViewTableRecord from one drawing to another
« Reply #2 on: February 01, 2023, 04:57:55 PM »
Thanks Gile, I will try that soon!