。net cad:从另一个工程图导入尺寸样式

问题描述 投票:0回答:1

我需要使用C#从dwg文件中导入尺寸样式。我没有在线找到答案。有人有例子吗?先说谢谢。

.net cad
1个回答
0
投票

这是我的解决方法

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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.