Author Topic: Import blocks and styles from external DWG  (Read 1632 times)

0 Members and 1 Guest are viewing this topic.

krkec

  • Guest
Import blocks and styles from external DWG
« on: January 16, 2014, 04:17:12 AM »
Hi

I am trying to import blocks and styles from external DWG. I have no errors, but my code doesn work.

Code - C#: [Select]
  1.  
  2. private void ubaci_Click(object sender, EventArgs e)
  3. {
  4.     Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  5.     Database wdb = doc.Database;
  6.     Editor ed = doc.Editor;
  7.     //Database wdb = HostApplicationServices.WorkingDatabase;
  8.         try
  9.         {
  10.                 using (Transaction tr = wdb.TransactionManager.StartTransaction())
  11.                 {
  12.                         LayerTable lt = (LayerTable)wdb.LayerTableId.GetObject(OpenMode.ForWrite);
  13.                         TextStyleTable tt = (TextStyleTable)wdb.TextStyleTableId.GetObject(OpenMode.ForWrite);
  14.                         DimStyleTable dt = (DimStyleTable)wdb.DimStyleTableId.GetObject(OpenMode.ForWrite);
  15.                         BlockTable bt = (BlockTable)wdb.BlockTableId.GetObject(OpenMode.ForWrite);
  16.                         Database ndb = new Database(false, true);
  17.                         ndb.ReadDwgFile(putanja, FileOpenMode.OpenForReadAndAllShare, true, "");
  18.                         using (Transaction tr2 = ndb.TransactionManager.StartTransaction())
  19.                         {
  20.                                 LayerTable nlt = (LayerTable)ndb.LayerTableId.GetObject(OpenMode.ForWrite);
  21.                                 IdMapping mylMap = new IdMapping();
  22.                                 ObjectIdCollection lidcol = new ObjectIdCollection();
  23.                                 foreach (string laye in ULayer.SelectedItems)
  24.                                 {
  25.                                         LayerTableRecord nltr = (LayerTableRecord)nlt[laye].GetObject(OpenMode.ForRead);
  26.                                         lidcol.Add(nltr.ObjectId);
  27.                                 }
  28.                                 //ndb.WblockCloneObjects(lidcol, lt.ObjectId, mylMap, DuplicateRecordCloning.Replace, false);
  29.                 ndb.WblockCloneObjects(lidcol, wdb.LayerTableId, mylMap, DuplicateRecordCloning.Replace, false);
  30.  
  31.                                 TextStyleTable ntt = (TextStyleTable)ndb.TextStyleTableId.GetObject(OpenMode.ForWrite);
  32.                                 IdMapping mytMap = new IdMapping();
  33.                                 ObjectIdCollection tidcol = new ObjectIdCollection();
  34.                                 foreach (string tsty in Utst.SelectedItems)
  35.                                 {
  36.                                         TextStyleTableRecord nltr = (TextStyleTableRecord)ntt[tsty].GetObject(OpenMode.ForRead);
  37.                                         tidcol.Add(nltr.ObjectId);
  38.                                 }
  39.                                 ndb.WblockCloneObjects(tidcol,wdb.TextStyleTableId, mytMap, DuplicateRecordCloning.Replace, false);
  40.  
  41.                                 DimStyleTable ndt = (DimStyleTable)ndb.DimStyleTableId.GetObject(OpenMode.ForWrite);
  42.                                 IdMapping mydMap = new IdMapping();
  43.                                 ObjectIdCollection didcol = new ObjectIdCollection();
  44.                                 foreach (string dimsty in Dst.SelectedItems)
  45.                                 {
  46.                                         DimStyleTableRecord ndtr = (DimStyleTableRecord)ndt[dimsty].GetObject(OpenMode.ForRead);
  47.                                         didcol.Add(ndtr.ObjectId);
  48.                                 }
  49.                                 ndb.WblockCloneObjects(didcol, wdb.DimStyleTableId, mydMap, DuplicateRecordCloning.Replace, false);
  50.  
  51.                                 BlockTable nbt = (BlockTable)ndb.BlockTableId.GetObject(OpenMode.ForWrite);
  52.                                 IdMapping mybMap = new IdMapping();
  53.                                 ObjectIdCollection bidcol = new ObjectIdCollection();
  54.                                 foreach (string blok in Ublok.SelectedItems)
  55.                                 {
  56.                                         BlockTableRecord nbtr = (BlockTableRecord)nbt[blok].GetObject(OpenMode.ForRead);
  57.                                         bidcol.Add(nbtr.ObjectId);
  58.                                 }
  59.                                 ndb.WblockCloneObjects(bidcol, wdb.BlockTableId, mybMap, DuplicateRecordCloning.Replace, false);
  60.  
  61.  
  62.                                 //tr2.Commit();
  63.                                 //wdb.SaveAs("c:\\temp\\dwgs\\CopyTest.dwg", DwgVersion.Current);
  64.                         }
  65.                 }
  66.         }
  67.          catch (Autodesk.AutoCAD.Runtime.Exception ex)
  68.     {
  69.         ed.WriteMessage("\nError during clone: " + ex.Message);
  70.     }
  71.  
  72. }
  73.  

edit-kdub: code tags amended.
« Last Edit: January 16, 2014, 03:22:38 PM by Kerry »

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Import blocks and styles from external DWG
« Reply #1 on: January 16, 2014, 07:57:58 AM »
Are you missing a tr.commit()?
Revit 2019, AMEP 2019 64bit Win 10

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Import blocks and styles from external DWG
« Reply #2 on: January 16, 2014, 11:57:19 AM »
The tr2.commit() is commented out and there is no commit for the first transaction as MexicanMustard mentioned.  Since there is no tr.commit and since tr2 is nested inside of the first transaction then even if you uncomment the tr2.commit() it will still not do anything.  Nested transactions must be committed in all transactions in order to work properly.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Import blocks and styles from external DWG
« Reply #3 on: January 16, 2014, 12:02:15 PM »
Also when you paste your code into the post if you label it as csharp then it will make it easier to read.
 
So instead of doing this [ code ] do this instead [ code=csharp ].  note: I added extra spaces between brackets so it wouldn't post as code.
 
 
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013