Search code examples
.netcad

.net cad: Import dimension style from another drawing


I need to improt dimension styles from dwg file by C#. I didn't find the answer online. Does anyone have an example? Say thank you in advance.


Solution

  • This is my solution

    internal static bool ImportDimensionStyleFromFile(string sourceCADFilePath)
        {
            if (File.Exists(sourceCADFilePath))
            {
                Document currentDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
                Database currentDb = currentDoc.Database;
                Database sourceDb = new Database(false, true);
                using (sourceDb)
                {
                    string fileExtension = System.IO.Path.GetExtension(sourceCADFilePath);
                    string fileName = System.IO.Path.GetFileNameWithoutExtension(sourceCADFilePath);  //use fileName as blockName.
                    try
                    {
                        if (fileExtension == ".dxf")
                            sourceDb.DxfIn(sourceCADFilePath, null);
                        else if (fileExtension == ".dwg")
                            sourceDb.ReadDwgFile(sourceCADFilePath, FileOpenMode.OpenForReadAndReadShare, false, null);
                        else
                        {
    
                            return false;
                        }
                    }
                    catch (System.Exception)
                    {
    
                        return false;
                    }
    
                    ObjectIdCollection idsForInsert = new ObjectIdCollection();
                    using (Transaction acTrans = currentDb.TransactionManager.StartTransaction())
                    {
                        DimStyleTable acDimTable = acTrans.GetObject(currentDb.DimStyleTableId, OpenMode.ForWrite) as DimStyleTable;
                        using (Transaction scTrans = sourceDb.TransactionManager.StartTransaction())
                        {
                            DimStyleTable scDimStyleTable = scTrans.GetObject(sourceDb.DimStyleTableId, OpenMode.ForRead) as DimStyleTable;
    
                            foreach (ObjectId id in scDimStyleTable)
                            {
                                DimStyleTableRecord scStyleRecord = (DimStyleTableRecord)scTrans.GetObject(id, OpenMode.ForRead);
                                if (scStyleRecord != null && acDimTable.Has(scStyleRecord.Name) == false)
                                {
                                    idsForInsert.Add(id);
                                }
                            }
                        }
                        if (idsForInsert.Count != 0)
                        {
                            IdMapping iMap = new IdMapping();
                            currentDb.WblockCloneObjects(idsForInsert, currentDb.DimStyleTableId, iMap, DuplicateRecordCloning.Ignore, false);
                        }
                        acTrans.Commit();
                    }
                }
            }
            return true;
        }