/// <summary>
/// Checks if the style definition is in the active drawing, if not, pull it from the template and add it
/// </summary>
/// <param name="styleName">Name of the style.</param>
/// <returns>ObjectID of Textstyle, ObjectId.Null on error</returns>
public static ObjectId GetStyleId(String styleName)
{
ObjectId styleId = ObjectId.Null; ;
ObjectIdCollection ids = new ObjectIdCollection();
using (Transaction acTr = Active.TransactionManager.StartTransaction())
{
TextStyleTable styleTable = (TextStyleTable)acTr.GetObject(Active.Database.TextStyleTableId, OpenMode.ForRead);
if (styleTable.Has(styleName))
{
styleId = acTr.GetObject(styleTable[styleName], OpenMode.ForRead).ObjectId;
acTr.Commit();
return styleId;
}
acTr.Commit();
}
// it's not in active drawing, try to pull it from the template dwt
string dllPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
String dwtPath = dllPath + AID_Default.TemplatePath;
if (GetStyleFromDWG(styleName, dwtPath))
{
using (Transaction acTr = Active.TransactionManager.StartTransaction())
{
TextStyleTable styleTable = (TextStyleTable)acTr.GetObject(Active.Database.TextStyleTableId, OpenMode.ForRead);
if (styleTable.Has(styleName))
{
styleId = acTr.GetObject(styleTable[styleName], OpenMode.ForRead).ObjectId;
}
acTr.Commit();
return styleId;
}
}
// Anything below here is an error, and returns ObjectID.Null
Active.WriteMessage("\nError creating text style: " + styleName);
return styleId;
}
/// <summary>
/// Gets the textstyle from specified drawing and adds it to the TextsStyleTable of the active drawing.
/// Returns true if successful, returns false if style isn't found.
/// </summary>
/// <param name="styleName">The stylename.</param>
/// <param name="path">The path to dwg/dwt to extract style from.</param>
private static bool GetStyleFromDWG(string styleName, string path)
{
using (Database openDb = new Database(false, true))
{
openDb.ReadDwgFile(path,
System.IO.FileShare.ReadWrite, true, "");
ObjectIdCollection ids = new ObjectIdCollection();
using (Transaction tr = openDb.TransactionManager.StartTransaction())
{
var styleTable = (TextStyleTable)tr.GetObject(openDb.TextStyleTableId, OpenMode.ForRead);
if (styleTable.Has(styleName))
{
ids.Add(styleTable[styleName]);
}
tr.Commit();
}
//if found, add the style
if (ids.Count != 0)
{
//get the current drawing database
Active.Document.LockDocument(DocumentLockMode.ProtectedAutoWrite, null, null, true);
IdMapping iMap = new IdMapping();
Active.Database.WblockCloneObjects(ids, Active.Database.TextStyleTableId
, iMap, DuplicateRecordCloning.Ignore, false);
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// Checks if the style definition is in the active drawing, if not, pull it from the template and add it
/// </summary>
/// <param name="styleName">Name of the style.</param>
/// <returns>ObjectID of Textstyle, ObjectId.Null on error</returns>
public static ObjectId GetStyleId(String styleName)
{
ObjectId styleId = ObjectId.Null; ;
ObjectIdCollection ids = new ObjectIdCollection();
using (Transaction acTr = Active.TransactionManager.StartTransaction())
{
TextStyleTable styleTable = (TextStyleTable)acTr.GetObject(Active.Database.TextStyleTableId, OpenMode.ForRead);
if (styleTable.Has(styleName))
{
styleId = acTr.GetObject(styleTable[styleName], OpenMode.ForRead).ObjectId;
acTr.Commit();
return styleId;
}
acTr.Commit();
}
// it's not in active drawing, try to pull it from the template dwt
string dllPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
String dwtPath = dllPath + AID_Default.TemplatePath;
if (GetStyleFromDWG(styleName, dwtPath))
{
using (Transaction acTr = Active.TransactionManager.StartTransaction())
{
TextStyleTable styleTable = (TextStyleTable)acTr.GetObject(Active.Database.TextStyleTableId, OpenMode.ForRead);
if (styleTable.Has(styleName))
{
styleId = acTr.GetObject(styleTable[styleName], OpenMode.ForRead).ObjectId;
}
acTr.Commit();
return styleId;
}
}
// Anything below here is an error, and returns ObjectID.Null
Active.WriteMessage("\nError creating text style: " + styleName);
return styleId;
}
/// <summary>
/// Gets the textstyle from specified drawing and adds it to the TextsStyleTable of the active drawing.
/// Returns true if successful, returns false if style isn't found.
/// </summary>
/// <param name="styleName">The stylename.</param>
/// <param name="path">The path to dwg/dwt to extract style from.</param>
private static bool GetStyleFromDWG(string styleName, string path)
{
using (Database openDb = new Database(false, true))
{
openDb.ReadDwgFile(path,
System.IO.FileShare.ReadWrite, true, "");
ObjectIdCollection ids = new ObjectIdCollection();
using (Transaction tr = openDb.TransactionManager.StartTransaction())
{
var styleTable = (TextStyleTable)tr.GetObject(openDb.TextStyleTableId, OpenMode.ForRead);
if (styleTable.Has(styleName))
{
ids.Add(styleTable[styleName]);
}
tr.Commit();
}
//if found, add the style
if (ids.Count != 0)
{
//get the current drawing database
Active.Document.LockDocument(DocumentLockMode.ProtectedAutoWrite, null, null, true);
IdMapping iMap = new IdMapping();
Active.Database.WblockCloneObjects(ids, Active.Database.TextStyleTableId
, iMap, DuplicateRecordCloning.Ignore, false);
return true;
}
else
{
return false;
}
}
}