.net cad: импортировать размерный стиль из другого чертежа - PullRequest
0 голосов
/ 15 октября 2019

Мне нужно импровизировать размерные стили из файла DWG в C #. Я не нашел ответ в Интернете. У кого-нибудь есть пример? Скажи заранее спасибо.

1 Ответ

0 голосов
/ 17 октября 2019

Это моё решение

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;
    }
...