Issue
I need to load styles for doors and members etc etc from an AutoCAD Architecture template file to a drawing file. This can be done easily in style dialog by copy and paste through the UI. But I need to do this programmatically using .NET. I tried to use WBlockCloneObjects. But it does not seem to work all the time. Is there a way to more reliably to do this?
Solution
There is a helper function called CloningHelper under Autodesk.Aec.ApplicationServices.Utility Namespace. This allows you to import/export style based information in the same manner as UI's styles copy/paste operations between drawings. Below are the sample commands that demonstrate the usage of this class. It is written for AutoCAD Architecture (ACA). But the same concept should apply to the object that you see in the style dialog in AutoCAD MEP.
In the code below, please look at the lines:
Autodesk.Aec.ApplicationServices.Utility.CloningHelper helpme =
new Autodesk.Aec.OmfSamples.CloningHelper();
helpme.Clone(dbSource, dbDestination, objCollectionSrc, rxcls, true);
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
// (Intended for behind the scenes further processing.)
Note #1: WBlockCloneObject may work with simple cases. But when it has more complex relations, it may not work. For example, we are aware that it won't work with Schedule Table Styles and Structural Members Styles. CloningHelper is the one ACA uses internally.
Note #2: CloningHelper is exposed in ACA 2009 and later versions in .NET. Only C++/OMF version is available for ACA 2008 and earlier versions.
public class Class1
{
static Editor ed =
Application.DocumentManager.MdiActiveDocument.Editor;
//
// Import a door style from an external drawing.
//
[CommandMethod("importDoorStyleTest")]
public void importDoorStyleTest()
{
try
{
// our destination file is the current db.
Database dbDestination =
HostApplicationServices.WorkingDatabase;
// our source file from which we import styles.
// The name of source drawing is harded for
// simplicity
string SourcePath =
@"C:\temp\Door Styles (Metric).dwg";
Database dbSource = new Database(false, true);
dbSource.ReadDwgFile(
SourcePath,
System.IO.FileShare.Read,
true,
""
);
// get the source dictionary
DictionaryDoorStyle dictStyle =
new DictionaryDoorStyle(dbSource);
// get the list of style ids that you want to import.
//
// (1) if you want to import everything, use this.
//
// the list of ids in the style dictionary.
//ObjectIdCollection objCollectionSrc =
// dictStyle.Records;
// (2) if you want to import a specific style,
// use this.
//
// we assume you know the name of style you want to
// import.
ObjectIdCollection objCollectionSrc =
new ObjectIdCollection();
objCollectionSrc.Add(
dictStyle.GetAt("Bifold - Double")
);
// now use CloningHelper class to import styles.
// 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 as anonymous
Autodesk.Aec.ApplicationServices.Utility.
CloningHelper helpme =
new Autodesk.Aec.ApplicationServices.Utility.
CloningHelper();
// uncomment below if you want a behavior other than
// default.
//helpme.MergeType =
// Autodesk.Aec.ApplicationServices.Utility.
// DictionaryRecordMergeBehavior.Unique;
//helpme.MergeType =
// Autodesk.Aec.ApplicationServices.Utility.
// DictionaryRecordMergeBehavior.Merge;
//helpme.MergeType =
// Autodesk.Aec.ApplicationServices.Utility.
// DictionaryRecordMergeBehavior.Normal;
// finally call clone.
helpme.Clone(
dbSource,
dbDestination,
objCollectionSrc,
dictStyle.RecordType,
true
);
}
catch (System.Exception e)
{
ed.WriteMessage(e.Message);
}
}
}