Issue
The earlier post shows how to import/export styles from/to another drawing, using Cloning Helper utility class in ACA .NET API. Can we use the same function to copy styles within the same drawing? Could you also show me how to use the merge options = 3 when there is possible overlaps?
Solution
You can use the Cloning helper utility class to clone a style within the same drawing, too. The following code demonstrates an usage. Notice that it simply sets the destination and source database as the same database. The code also shows a merge option = 3; i.e., if a style overlaps, it is added as anonymous style name, which starts with "*".
// clone a style within the same drawing.
[CommandMethod("cloneStrMemberStyleTest")]
public void cloneStrMemberStyleTest()
{
try
{
// both source and destination file is the current db
Database dbDestination =
HostApplicationServices.WorkingDatabase;
Database dbSource = dbDestination;
// get the source dictionary
DictionaryMemberStyle dictStyle =
new DictionaryMemberStyle(dbSource);
// get the list of style ids that you want to copy.
//
// (1) if you want to duplicate everything, use this.
// the list of ids in the style dictionary.
//ObjectIdCollection objCollectionSrc =
// dictStyle.Records;
// (2) if you want to duplicate a specific style,
// use this.
ObjectIdCollection objCollectionSrc =
new ObjectIdCollection();
// we assume you know the name of style
// you want to copy.
objCollectionSrc.Add(dictStyle.GetAt("Standard"));
// now use CloningHelper class to clone a style.
// there are four options for merge type:
// Normal = 0, // no overwrite
// Overwrite = 1, // this is default.
// Unique = 2, // rename it if the same name
// // exists.
// Merge = 3 // no overwrite.
// add overlapping
// ones as anonymous name
CloningHelper helpme = new CloningHelper();
// uncomment one of these if you want to have a
// behavior other than default (i.e., overwrite).
//helpme.MergeType =
// DictionaryRecordMergeBehavior.Unique;
helpme.MergeType =
DictionaryRecordMergeBehavior.Merge;
//helpme.MergeType =
// DictionaryRecordMergeBehavior.Normal;
// finally call clone.
helpme.Clone(
dbSource, dbDestination,
objCollectionSrc, dictStyle.RecordType, true);
// find anonymous name and print them out.
// Anonymous copy has "*" at the beginning.
// We will rename it to something else.
Transaction trans =
Application.DocumentManager.MdiActiveDocument.
TransactionManager.StartTransaction();
try
{
ObjectIdCollection ids = dictStyle.Records;
foreach (ObjectId id in ids)
{
MemberStyle aStyle =
(MemberStyle)trans.GetObject(
id, OpenMode.ForRead);
ed.WriteMessage(
"aStyle name = " + aStyle.Name);
// an anonymous copy starts with "*".
if (aStyle.Name.StartsWith("*"))
{
// Note: if you want to assign a new name,
// you may also want to double check
// if the new name already exists.
// Here we assume no same name exists.
String newName =
"ACANet" + aStyle.Name.Substring(1);
ed.WriteMessage("(" + newName + ")");
dictStyle.Rename(
aStyle.Name, newName, trans);
}
ed.WriteMessage("\n");
}
trans.Commit();
}
catch (System.Exception e)
{
ed.WriteMessage(e.Message);
//trans.Abort();
}
finally
{
trans.Dispose();
}
}
catch (System.Exception e)
{
ed.WriteMessage(e.Message);
}
}