[C#] 纯文本查看 复制代码
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using LayerAndTextTools;
using System.Text;
using acDbSvs = Autodesk.AutoCAD.DatabaseServices;
using System.IO;
using System.Text.RegularExpressions;
using Autodesk.AutoCAD.Internal;
using static ZgxCommomLib.StaticGeoMatrix;
using static LayerAndTextTools.layermanager;
namespace ZgxCommomLib
{
/// <summary> /// 从外部文件拷贝块定义、字型、标注样式等 /// </summary> /// public static class CopyStyleFromDwg {
public static bool CopyStyleOrBlockFromDwg<T>(string path, DuplicateRecordCloning duplicateRecordCloning) where T : SymbolTable //where T1:SymbolTableRecord {
if (!File.Exists(path)) return false;
Document doc = Application.DocumentManager.MdiActiveDocument; Database db = Application.DocumentManager.MdiActiveDocument.Database; Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ObjectId sourceTableId = new ObjectId(); ObjectId destTableId = new ObjectId();
string tName = typeof(T).Name;//类型名称
using (Database sourceDb = new Database(false, true)) { sourceDb.ReadDwgFile(path, FileOpenMode.OpenForReadAndAllShare, true, "");
//此处只能强制转换,用(ObjectId),不能用 as ObjectId destTableId = (ObjectId)typeof(Database) .GetProperty(tName + "Id").GetValue(db, null);
sourceTableId = (ObjectId)typeof(Database) .GetProperty(tName + "Id").GetValue(sourceDb, null);
ObjectIdCollection ids = new ObjectIdCollection();
using (Transaction tr = sourceDb.TransactionManager.StartTransaction()) { T styleTable = (T)tr.GetObject(sourceTableId, OpenMode.ForRead); BlockTableRecord btr; foreach (ObjectId style in styleTable) { btr = tr.GetObject(style, OpenMode.ForRead) as BlockTableRecord; if (btr != null) { if (!btr.IsAnonymous && !btr.IsLayout)//不复制匿名块和布局 ids.Add(style); } else ids.Add(style); } tr.Commit(); }
//if found, add the style if (ids.Count != 0) { //get the current drawing database using (doc.LockDocument(DocumentLockMode.ProtectedAutoWrite, null, null, true)) { IdMapping iMap = new IdMapping(); db.WblockCloneObjects(ids, destTableId , iMap, duplicateRecordCloning, false); return true; } } else return false; }// sourceDb.Dispose(); }
public static bool CopyStyleOrBlockFromDwg<T>(string path, DuplicateRecordCloning duplicateRecordCloning, params string[] styleNames) where T : SymbolTable //where T1:SymbolTableRecord
{
if (!File.Exists(path)) return false;
Document doc = Application.DocumentManager.MdiActiveDocument; Database db = Application.DocumentManager.MdiActiveDocument.Database; Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ObjectId sourceTableId = new ObjectId(); ObjectId destTableId = new ObjectId();
string tName = typeof(T).Name;//类型名称,如 "BlockTable" using (Database sourceDb = new Database(false, true)) { sourceDb.ReadDwgFile(path, FileOpenMode.OpenForReadAndAllShare, true, "");
//此处只能强制转换,用(ObjectId),不能用 as ObjectId //此处利用反射获得块表属性,如BlockTableId destTableId = (ObjectId)typeof(Database) .GetProperty(tName + "Id").GetValue(db, System.Reflection.BindingFlags.Default, null, null, System.Globalization.CultureInfo.CurrentCulture);//得到块表属性,如BlockTableId
sourceTableId = (ObjectId)typeof(Database).GetProperty(tName + "Id").GetValue(sourceDb, System.Reflection.BindingFlags.Default, null, null, System.Globalization.CultureInfo.CurrentCulture);//得到块表属性,如BlockTableId
ObjectIdCollection ids = new ObjectIdCollection(); using (Transaction tr = sourceDb.TransactionManager.StartTransaction())
{
T styleTable = (T)tr.GetObject(sourceTableId, OpenMode.ForRead); BlockTableRecord btr;
foreach (string styleName in styleNames)
{
if (styleTable.Has(styleName))
{
btr = tr.GetObject(styleTable[styleName], OpenMode.ForRead) as BlockTableRecord; if (btr != null)
{
if (!btr.IsAnonymous && !btr.IsLayout)//不复制匿名块和布局 ids.Add(styleTable[styleName]); } else ids.Add(styleTable[styleName]); } else { tr.Commit(); return false; } } tr.Commit(); }
//if found, add the style if (ids.Count != 0) { using (doc.LockDocument(DocumentLockMode.ProtectedAutoWrite, null, null, true)) { IdMapping iMap = new IdMapping(); db.WblockCloneObjects(ids, destTableId , iMap, duplicateRecordCloning, false); return true; } } else return false; } }
}
}