Author Topic: Whatís the best way to import a text style from a dwt in the support path?  (Read 1772 times)

0 Members and 1 Guest are viewing this topic.

Atook

  • Swamp Rat
  • Posts: 1029
  • AKA Tim
Whatís the best way to import a text style from a dwt in the support path into the active drawing?

Iíve got a dwt template in my dll directory that has layers, blocks, textstyles, etc that I would like to use in my app. This allows users to modify the blocks, etc to customize their drawings. It also keeps me from hardcoding the blocks, styles, etc. The dwt file is in the support path.

Is WblockCloneObjects the best way to do this for textstyles too?

Right now I import blocks with the following:
Code - C: [Select]
  1. /// <summary>
  2. /// Gets the block from specified drawing and adds it to the blocktable of the active drawing..
  3. /// </summary>
  4. /// <param name="blockName">The blockname.</param>
  5. /// <param name="path">The path to dwg/dwt to extract block from.</param>
  6. private static bool GetBlockFromDWG(string blockName, string path)
  7. {
  8.    using (Database openDb = new Database(false, true))
  9.    {
  10.       openDb.ReadDwgFile(path,
  11.          System.IO.FileShare.ReadWrite, true, "");
  12.  
  13.       ObjectIdCollection ids = new ObjectIdCollection();
  14.       using (Transaction tr = openDb.TransactionManager.StartTransaction())
  15.       {
  16.          var bt = (BlockTable)tr.GetObject(openDb.BlockTableId, OpenMode.ForRead);
  17.  
  18.          if (bt.Has(blockName))
  19.          {
  20.             ids.Add(bt[blockName]);
  21.          }
  22.          tr.Commit();
  23.       }
  24.  
  25.       //if found, add the block
  26.       if (ids.Count != 0)
  27.       {
  28.          //get the current drawing database
  29.          Active.Document.LockDocument(DocumentLockMode.ProtectedAutoWrite, null, null, true);
  30.          IdMapping iMap = new IdMapping();
  31.          Active.Database.WblockCloneObjects(ids, Active.Database.BlockTableId
  32.             , iMap, DuplicateRecordCloning.Ignore, false);
  33.          return true;
  34.       }
  35.       else
  36.       {
  37.          return false;
  38.       }
  39.    }
  40. }
  41.  
  42.  
« Last Edit: November 23, 2015, 01:51:45 AM by Atook »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>

Quote
Is WblockCloneObjects the best way to do this for textstyles too?
Yes,
good for any TableRecord imports.

Don't forget to post your code when you're done. :)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Atook

  • Swamp Rat
  • Posts: 1029
  • AKA Tim
Thanks for the feedback Kerry. I ended up using WblockCloneObjects. Worked a treat.

Here's my code. I have something similar for blocks.
Code - C: [Select]
  1. /// <summary>
  2. /// Checks if the style definition is in the active drawing, if not, pull it from the template and add it
  3. /// </summary>
  4. /// <param name="styleName">Name of the style.</param>
  5. /// <returns>ObjectID of Textstyle, ObjectId.Null on error</returns>
  6. public static ObjectId GetStyleId(String styleName)
  7. {
  8.     ObjectId styleId = ObjectId.Null; ;
  9.     ObjectIdCollection ids = new ObjectIdCollection();
  10.     using (Transaction acTr = Active.TransactionManager.StartTransaction())
  11.     {
  12.         TextStyleTable styleTable = (TextStyleTable)acTr.GetObject(Active.Database.TextStyleTableId, OpenMode.ForRead);
  13.         if (styleTable.Has(styleName))
  14.         {
  15.             styleId = acTr.GetObject(styleTable[styleName], OpenMode.ForRead).ObjectId;
  16.             acTr.Commit();
  17.             return styleId;
  18.         }
  19.         acTr.Commit();
  20.     }
  21.     // it's not in active drawing, try to pull it from the template dwt
  22.     string dllPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  23.     String dwtPath = dllPath + AID_Default.TemplatePath;
  24.     if (GetStyleFromDWG(styleName, dwtPath))
  25.     {
  26.         using (Transaction acTr = Active.TransactionManager.StartTransaction())
  27.         {
  28.             TextStyleTable styleTable = (TextStyleTable)acTr.GetObject(Active.Database.TextStyleTableId, OpenMode.ForRead);
  29.             if (styleTable.Has(styleName))
  30.             {
  31.                 styleId = acTr.GetObject(styleTable[styleName], OpenMode.ForRead).ObjectId;
  32.             }
  33.             acTr.Commit();
  34.             return styleId;
  35.         }
  36.     }
  37.     // Anything below here is an error, and returns ObjectID.Null
  38.     Active.WriteMessage("\nError creating text style: " + styleName);
  39.     return styleId;
  40. }
  41.  
  42.  
  43.  
  44. /// <summary>
  45. /// Gets the textstyle from specified drawing and adds it to the TextsStyleTable of the active drawing.
  46. /// Returns true if successful, returns false if style isn't found.
  47. /// </summary>
  48. /// <param name="styleName">The stylename.</param>
  49. /// <param name="path">The path to dwg/dwt to extract style from.</param>
  50. private static bool GetStyleFromDWG(string styleName, string path)
  51. {
  52.     using (Database openDb = new Database(false, true))
  53.     {
  54.         openDb.ReadDwgFile(path,
  55.             System.IO.FileShare.ReadWrite, true, "");
  56.  
  57.         ObjectIdCollection ids = new ObjectIdCollection();
  58.         using (Transaction tr = openDb.TransactionManager.StartTransaction())
  59.         {
  60.             var styleTable = (TextStyleTable)tr.GetObject(openDb.TextStyleTableId, OpenMode.ForRead);
  61.  
  62.             if (styleTable.Has(styleName))
  63.             {
  64.                 ids.Add(styleTable[styleName]);
  65.             }
  66.             tr.Commit();
  67.         }
  68.  
  69.         //if found, add the style
  70.         if (ids.Count != 0)
  71.         {
  72.             //get the current drawing database
  73.             Active.Document.LockDocument(DocumentLockMode.ProtectedAutoWrite, null, null, true);
  74.             IdMapping iMap = new IdMapping();
  75.             Active.Database.WblockCloneObjects(ids, Active.Database.TextStyleTableId
  76.                 , iMap, DuplicateRecordCloning.Ignore, false);
  77.             return true;
  78.         }
  79.         else
  80.         {
  81.             return false;
  82.         }
  83.     }
  84. }
  85. /// <summary>
  86. /// Checks if the style definition is in the active drawing, if not, pull it from the template and add it
  87. /// </summary>
  88. /// <param name="styleName">Name of the style.</param>
  89. /// <returns>ObjectID of Textstyle, ObjectId.Null on error</returns>
  90. public static ObjectId GetStyleId(String styleName)
  91. {
  92.     ObjectId styleId = ObjectId.Null; ;
  93.     ObjectIdCollection ids = new ObjectIdCollection();
  94.     using (Transaction acTr = Active.TransactionManager.StartTransaction())
  95.     {
  96.         TextStyleTable styleTable = (TextStyleTable)acTr.GetObject(Active.Database.TextStyleTableId, OpenMode.ForRead);
  97.         if (styleTable.Has(styleName))
  98.         {
  99.             styleId = acTr.GetObject(styleTable[styleName], OpenMode.ForRead).ObjectId;
  100.             acTr.Commit();
  101.             return styleId;
  102.         }
  103.         acTr.Commit();
  104.     }
  105.     // it's not in active drawing, try to pull it from the template dwt
  106.     string dllPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  107.     String dwtPath = dllPath + AID_Default.TemplatePath;
  108.     if (GetStyleFromDWG(styleName, dwtPath))
  109.     {
  110.         using (Transaction acTr = Active.TransactionManager.StartTransaction())
  111.         {
  112.             TextStyleTable styleTable = (TextStyleTable)acTr.GetObject(Active.Database.TextStyleTableId, OpenMode.ForRead);
  113.             if (styleTable.Has(styleName))
  114.             {
  115.                 styleId = acTr.GetObject(styleTable[styleName], OpenMode.ForRead).ObjectId;
  116.             }
  117.             acTr.Commit();
  118.             return styleId;
  119.         }
  120.     }
  121.     // Anything below here is an error, and returns ObjectID.Null
  122.     Active.WriteMessage("\nError creating text style: " + styleName);
  123.     return styleId;
  124. }
  125.  
  126.  
  127.  
  128. /// <summary>
  129. /// Gets the textstyle from specified drawing and adds it to the TextsStyleTable of the active drawing.
  130. /// Returns true if successful, returns false if style isn't found.
  131. /// </summary>
  132. /// <param name="styleName">The stylename.</param>
  133. /// <param name="path">The path to dwg/dwt to extract style from.</param>
  134. private static bool GetStyleFromDWG(string styleName, string path)
  135. {
  136.     using (Database openDb = new Database(false, true))
  137.     {
  138.         openDb.ReadDwgFile(path,
  139.             System.IO.FileShare.ReadWrite, true, "");
  140.  
  141.         ObjectIdCollection ids = new ObjectIdCollection();
  142.         using (Transaction tr = openDb.TransactionManager.StartTransaction())
  143.         {
  144.             var styleTable = (TextStyleTable)tr.GetObject(openDb.TextStyleTableId, OpenMode.ForRead);
  145.  
  146.             if (styleTable.Has(styleName))
  147.             {
  148.                 ids.Add(styleTable[styleName]);
  149.             }
  150.             tr.Commit();
  151.         }
  152.  
  153.         //if found, add the style
  154.         if (ids.Count != 0)
  155.         {
  156.             //get the current drawing database
  157.             Active.Document.LockDocument(DocumentLockMode.ProtectedAutoWrite, null, null, true);
  158.             IdMapping iMap = new IdMapping();
  159.             Active.Database.WblockCloneObjects(ids, Active.Database.TextStyleTableId
  160.                 , iMap, DuplicateRecordCloning.Ignore, false);
  161.             return true;
  162.         }
  163.         else
  164.         {
  165.             return false;
  166.         }
  167.     }
  168. }
  169.