Import/Export styles from/to another drawing in .NET

By Mikako Harada

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:tempDoor 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);
            }
 
        }

    }


Comments

3 responses to “Import/Export styles from/to another drawing in .NET”

  1. Dear Mikako,
    Could you please tell us the references dll name with Autodesk.Aec.ApplicationServices.Utility.CloningHelper

  2. Hi Lennin,
    It’s in the assembly, AecUiBaseMgd.dll.
    Just as future reference –
    If you are search for this kind of info, you can search in the reference guide acamgd.chm found in the product install.
    Under the CloningHelper Class description, in this case, there is a section like this:

    >
    Requirements
    Namespace: Autodesk.Aec.ApplicationServices.Utility
    Assembly: AecUiBaseMgd (in AecUiBaseMgd.dll)
    <<<<
    FYI

  3. Dear Mikako,
    Thank you very much for your answer and advice.
    It’s very helpful, especially for me who just beginning in autocad .net.

Leave a Reply to LenninCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading