I have a merge textstyle form that runs the code below, some of it may be helpful.
I found it was easier to merge a textstyle than delete it.
class MergeTextstyles
{
[CommandMethod("MergeTextstyle")]
public void Merge()
{
Database db=HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
TextStyleTable tsr = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
if (tsr == null)
{
MessageBox.Show("There are no textstyles to merge, exiting");
return;
}
}
Form MergeTextstyleform = new MergeTextstyleform.form();
MergeTextstyleform.ShowDialog();
if (MergeTextstyleform.ActiveControl == null) return;
string newTstyle = "", oldTstyle = "";
foreach (Control ct in MergeTextstyleform.Controls)
{
if (ct.Name == "textBox1")
{
newTstyle = ct.Text;
break;
}
}
foreach (Control ct in MergeTextstyleform.Controls)
{
if (ct.Name == "textBox2")
{
oldTstyle = ct.Text;
break;
}
}
MergeTextstyleform.Dispose();
Mergetstyles(newTstyle, oldTstyle);
}
public void Mergetstyles(string newTstyle,string oldTstyle)
{
Database db = HostApplicationServices.WorkingDatabase;
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
TextStyleTable tstyles;
if (newTstyle =="") return;
if (oldTstyle == "") return;
if (oldTstyle == "Standard")
{
MessageBox.Show("The Standard textstyle cannot be deleted.");
return;
}
TypedValue[] tv = { new TypedValue(0, "Text,Mtext,Acad_Table,Dimension") };
SelectionFilter sf = new SelectionFilter(tv);
PromptSelectionResult psr = ed.SelectAll(sf);
if (psr.Status != PromptStatus.OK)
{
MessageBox.Show("There is no text or Mtext in this drawing, exiting now:");
return;
}
SelectionSet ss = psr.Value;
Entity ent;
DBText text;
MText mtext;
Dimension dim;
ObjectId oldId = ObjectId.Null,newId=ObjectId.Null;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
tstyles = tr.GetObject
(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
oldId = Tony.DBUtils.GetSymbolTableRecordId(db.TextStyleTableId, oldTstyle);
newId = Tony.DBUtils.GetSymbolTableRecordId(db.TextStyleTableId, newTstyle);
//Dimstyles
DimStyleTable dims = tr.GetObject
(db.DimStyleTableId, OpenMode.ForRead) as DimStyleTable;
DimStyleTableRecord activeDim = null;
DimStyleTableRecord tempActiveDim = tr.GetObject
(dims["Standard"], OpenMode.ForRead) as DimStyleTableRecord;
foreach (ObjectId dimId in dims)
{
using (Transaction tr2 = db.TransactionManager.StartTransaction())
{
DimStyleTableRecord dtr = tr2.GetObject(
dimId, OpenMode.ForRead) as DimStyleTableRecord;
if (dtr.Dimtxsty == oldId)
{
dtr.UpgradeOpen();
dtr.Dimtxsty = newId;
dtr.DowngradeOpen();
//Changing the active dimstyle results in an override
if (dimId == db.Dimstyle)
{
activeDim = dtr;
db.SetDimstyleData(tempActiveDim);
}
}
tr2.Commit();
}
}
if (activeDim != null)
db.SetDimstyleData(activeDim);
//blocks
BlockTable blocks = tr.GetObject
(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr;
foreach (ObjectId blockId in blocks)
{
btr = tr.GetObject(blockId, OpenMode.ForRead) as BlockTableRecord;
if (btr.IsFromExternalReference) continue;
if (btr.IsLayout) continue;
if (btr.Name.Contains("*D")) continue;
foreach (ObjectId entId in btr)
{
ent = tr.GetObject(entId, OpenMode.ForRead) as Entity;
if (ent is DBText)
{
text = (DBText)ent;
if (text.TextStyleId == oldId)
{
text.UpgradeOpen();
text.TextStyleId = newId;
}
//We need to look at all blockrefs in case
//the attdef textstyle was changed after the insertion
if (ent is AttributeDefinition)
{
AttributeDefinition att = (AttributeDefinition)ent;
ObjectIdCollection ids = btr.GetBlockReferenceIds(true, true);
BlockReference br;
foreach (ObjectId blockrefId in ids)
{
br = tr.GetObject
(blockrefId, OpenMode.ForWrite) as BlockReference;
foreach (ObjectId id in br.AttributeCollection)
{
AttributeReference attref = tr.GetObject
(id, OpenMode.ForWrite) as AttributeReference;
if (attref.TextStyleId == oldId)
attref.TextStyleId = newId;
}
}
}
}
if (ent is MText)
{
mtext = (MText)ent;
if (mtext.TextStyleId == oldId)
{
mtext.UpgradeOpen();
mtext.TextStyleId = newId;
}
}
}
}
tr.Commit();
}
using (Transaction tr = db.TransactionManager.StartTransaction())
{
foreach (SelectedObject so in ss)
{
ent = tr.GetObject(so.ObjectId, OpenMode.ForWrite) as Entity;
if (ent is Table)
{
Table table = (Table)ent;
for (int cols = 0; cols < table.Columns.Count; cols++)
{
for (int rows = 0; rows < table.Rows.Count; rows++)
{
if (table.Cells[rows, cols].TextStyleId == oldId)
table.Cells[rows, cols].TextStyleId = newId;
}
}
continue;
}
if (ent is DBText)
{
text = (DBText)ent;
if (text.TextStyleId == oldId)
text.TextStyleId = newId;
continue;
}
if (ent is Dimension)
{
dim = (Dimension)ent;
if (dim.DimensionStyle == oldId)
dim.DimensionStyle = newId;
continue;
}
mtext = (MText)ent;
if (mtext.TextStyleId == oldId)
mtext.TextStyleId = newId;
}//Next
tr.Commit();
}
GetTableStyles(oldId, newId);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
TextStyleTableRecord oldTs = tr.GetObject
(oldId, OpenMode.ForWrite) as TextStyleTableRecord;
db.Textstyle= newId;
oldTs.Erase();
ObjectIdCollection ids = new ObjectIdCollection();
ids.Add(oldId);
db.Purge(ids);
tr.Commit();
}//End using
} //End Mergetstyles
public static void GetTableStyles(ObjectId oldId, ObjectId newId)
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
DBDictionary nod = tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead) as DBDictionary;
ObjectId id = nod.GetAt("ACAD_TABLESTYLE");
DBDictionary dic = tr.GetObject(id, OpenMode.ForRead) as DBDictionary;
TableStyle table;
foreach (DictionaryEntry eDict in dic)
{
table = tr.GetObject((ObjectId)eDict.Value, OpenMode.ForWrite) as TableStyle;
if (table == null) continue;
foreach (RowType rt in Enum.GetValues(typeof(RowType)))
{
ObjectId idv = table.TextStyle(rt);
if (table.TextStyle(rt) == oldId)
table.SetTextStyle(newId, (int)rt);
}
}
tr.Commit();
}
}//End GetTableStyles
public static void MultiLine()
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
DBDictionary nod = tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead) as DBDictionary;
ObjectId id = nod.GetAt("ACAD_MLINESTYLE");
DBDictionary dic = tr.GetObject(id, OpenMode.ForRead) as DBDictionary;
MlineStyle mlinestyle;
foreach (DictionaryEntry eDict in dic)
{
mlinestyle = tr.GetObject((ObjectId)eDict.Value, OpenMode.ForWrite) as MlineStyle;
// mlinestyle.Set(mlinestyle, false);
MlineStyleElementCollection mc = mlinestyle.Elements;
foreach (MlineStyleElement me in mc)
{
Util.Debug.Print(me.Offset.ToString());
}
//mlinestyle.Erase();
// mlinestyle.Elements = mc;
} tr.Commit();
}
} //End MultiLine
} //end Class MergeTextstle