I've tried and failed to do this in vba and now I am failing in C#.
Textstyles are nasty beasts when it comes down to it and they can display fairly odd behavior.
If you copyclip some text from a drawing that has a tablestyle using the text's textstyle, the tablestyle comes along for the ride.
I've ran the following code on a few different drawings and while it appears to work a save will bring up the error "Error writing/closing file"
If you try format->Multiline style you get a fatal error.
I can't imagine what Multiline style can have to do with it as there are no linetypes with text loaded
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") };
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;
ObjectId oldId = ObjectId.Null,newId=ObjectId.Null;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Entity ent;
DBText text;
MText mtext;
tstyles = tr.GetObject
(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
//oldId = tstyles[oldTstyle];
// newId =tstyles[newTstyle];
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)
{
DimStyleTableRecord dim = tr.GetObject(
dimId, OpenMode.ForRead) as DimStyleTableRecord;
//Changing the active dimstyle results in an override
if (dimId == db.Dimstyle)
{
activeDim = dim;
db.SetDimstyleData(tempActiveDim);
}
if (dim.Dimtxsty == oldId)
{
dim.UpgradeOpen();
dim.Dimtxsty = newId;
}
}
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.TextStyle == oldId)
{
text.UpgradeOpen();
text.TextStyle = 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.TextStyle == oldId)
attref.TextStyle = newId;
}
}
}
}
if (ent is MText)
{
mtext = (MText)ent;
if (mtext.TextStyle == oldId)
{
mtext.UpgradeOpen();
mtext.TextStyle = newId;
}
}
}
}
foreach (SelectedObject so in ss)
{
ent = tr.GetObject(so.ObjectId, OpenMode.ForWrite) as Entity;
if (ent is Table)
{
Table table = (Table)ent;
table.UpgradeOpen();
for (int cols = 0; cols < table.NumColumns; cols++)
{
for (int rows = 0; rows < table.NumRows; rows++)
{
if (table.TextStyle(rows, cols) == oldId)
table.SetTextStyle(rows, cols, newId);
}
}
continue;
}
if (ent is DBText)
{
text = (DBText)ent;
if (text.TextStyle == oldId)
text.TextStyle = newId;
continue;
}
mtext = (MText)ent;
if (mtext.TextStyle == oldId)
mtext.TextStyle = newId;
}
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();
}
}
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();
}
}