明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1040|回复: 2

[符号表] 拷贝外部文件 图层、图块、标注样式...

[复制链接]
发表于 2020-11-1 22:37 | 显示全部楼层 |阅读模式
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Geometry;
  5. using Autodesk.AutoCAD.Runtime;
  6. using LayerAndTextTools;
  7. using System.Text;
  8. using acDbSvs = Autodesk.AutoCAD.DatabaseServices;
  9. using System.IO;
  10. using System.Text.RegularExpressions;
  11. using Autodesk.AutoCAD.Internal;
  12. using static ZgxCommomLib.StaticGeoMatrix;
  13. using static LayerAndTextTools.layermanager;





  14. namespace ZgxCommomLib
  15. {

  16.     /// <summary>
  17.     /// 从外部文件拷贝块定义、字型、标注样式等
  18.     /// </summary>
  19.     ///
  20.     public static class CopyStyleFromDwg
  21.     {

  22.         public static bool CopyStyleOrBlockFromDwg<T>(string path, DuplicateRecordCloning duplicateRecordCloning)
  23.     where T : SymbolTable //where T1:SymbolTableRecord
  24.         {

  25.             if (!File.Exists(path))
  26.                 return false;

  27.             Document doc = Application.DocumentManager.MdiActiveDocument;
  28.             Database db = Application.DocumentManager.MdiActiveDocument.Database;
  29.             Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

  30.             ObjectId sourceTableId = new ObjectId();
  31.             ObjectId destTableId = new ObjectId();

  32.             string tName = typeof(T).Name;//类型名称      

  33.             using (Database sourceDb = new Database(false, true))
  34.             {
  35.                 sourceDb.ReadDwgFile(path,
  36.                     FileOpenMode.OpenForReadAndAllShare, true, "");

  37.                 //此处只能强制转换,用(ObjectId),不能用 as ObjectId
  38.                 destTableId = (ObjectId)typeof(Database)
  39.                     .GetProperty(tName + "Id").GetValue(db, null);


  40.                 sourceTableId = (ObjectId)typeof(Database)
  41.                    .GetProperty(tName + "Id").GetValue(sourceDb, null);

  42.                 ObjectIdCollection ids = new ObjectIdCollection();

  43.                 using (Transaction tr = sourceDb.TransactionManager.StartTransaction())
  44.                 {
  45.                     T styleTable = (T)tr.GetObject(sourceTableId, OpenMode.ForRead);
  46.                     BlockTableRecord btr;
  47.                     foreach (ObjectId style in styleTable)
  48.                     {
  49.                         btr = tr.GetObject(style, OpenMode.ForRead) as BlockTableRecord;
  50.                         if (btr != null)
  51.                         {
  52.                             if (!btr.IsAnonymous && !btr.IsLayout)//不复制匿名块和布局
  53.                                 ids.Add(style);
  54.                         }
  55.                         else
  56.                             ids.Add(style);
  57.                     }
  58.                     tr.Commit();
  59.                 }

  60.                 //if found, add the style
  61.                 if (ids.Count != 0)
  62.                 {
  63.                     //get the current drawing database
  64.                     using (doc.LockDocument(DocumentLockMode.ProtectedAutoWrite, null, null, true))
  65.                     {
  66.                         IdMapping iMap = new IdMapping();
  67.                         db.WblockCloneObjects(ids, destTableId
  68.                             , iMap, duplicateRecordCloning, false);
  69.                         return true;
  70.                     }
  71.                 }
  72.                 else
  73.                     return false;
  74.             }//  sourceDb.Dispose();
  75.         }

  76.         public static bool CopyStyleOrBlockFromDwg<T>(string path, DuplicateRecordCloning duplicateRecordCloning,
  77.             params string[] styleNames)
  78.             where T : SymbolTable //where T1:SymbolTableRecord

  79.         {

  80.             if (!File.Exists(path))
  81.                 return false;

  82.             Document doc = Application.DocumentManager.MdiActiveDocument;
  83.             Database db = Application.DocumentManager.MdiActiveDocument.Database;
  84.             Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

  85.             ObjectId sourceTableId = new ObjectId();
  86.             ObjectId destTableId = new ObjectId();

  87.             string tName = typeof(T).Name;//类型名称,如 "BlockTable"
  88.             using (Database sourceDb = new Database(false, true))
  89.             {
  90.                 sourceDb.ReadDwgFile(path,
  91.                     FileOpenMode.OpenForReadAndAllShare, true, "");

  92.                 //此处只能强制转换,用(ObjectId),不能用 as ObjectId
  93.                 //此处利用反射获得块表属性,如BlockTableId
  94.                 destTableId = (ObjectId)typeof(Database)
  95.                     .GetProperty(tName + "Id").GetValue(db,
  96.                     System.Reflection.BindingFlags.Default, null, null,
  97.                     System.Globalization.CultureInfo.CurrentCulture);//得到块表属性,如BlockTableId

  98.                 sourceTableId = (ObjectId)typeof(Database)
  99.                     .GetProperty(tName + "Id").GetValue(sourceDb,
  100.                     System.Reflection.BindingFlags.Default, null, null,
  101.                     System.Globalization.CultureInfo.CurrentCulture);//得到块表属性,如BlockTableId

  102.                 ObjectIdCollection ids = new ObjectIdCollection();
  103.                 using (Transaction tr = sourceDb.TransactionManager.StartTransaction())
  104.                 {
  105.                     T styleTable = (T)tr.GetObject(sourceTableId, OpenMode.ForRead);
  106.                     BlockTableRecord btr;

  107.                     foreach (string styleName in styleNames)
  108.                     {
  109.                         if (styleTable.Has(styleName))
  110.                         {
  111.                             btr = tr.GetObject(styleTable[styleName], OpenMode.ForRead) as BlockTableRecord;
  112.                             if (btr != null)
  113.                             {
  114.                                 if (!btr.IsAnonymous && !btr.IsLayout)//不复制匿名块和布局
  115.                                     ids.Add(styleTable[styleName]);
  116.                             }
  117.                             else
  118.                                 ids.Add(styleTable[styleName]);
  119.                         }
  120.                         else
  121.                         {
  122.                             tr.Commit();
  123.                             return false;
  124.                         }
  125.                     }
  126.                     tr.Commit();
  127.                 }

  128.                 //if found, add the style
  129.                 if (ids.Count != 0)
  130.                 {
  131.                     using (doc.LockDocument(DocumentLockMode.ProtectedAutoWrite, null, null, true))
  132.                     {
  133.                         IdMapping iMap = new IdMapping();
  134.                         db.WblockCloneObjects(ids, destTableId
  135.                             , iMap, duplicateRecordCloning, false);
  136.                         return true;
  137.                     }
  138.                 }
  139.                 else
  140.                     return false;
  141.             }
  142.         }


  143.     }

  144. }


发表于 2020-11-2 09:04 | 显示全部楼层
不错,赞一个
发表于 2020-11-2 13:02 | 显示全部楼层
不错,赞一个
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-2 04:47 , Processed in 0.222357 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表