找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1173|回复: 1

[每日一码] 拷贝外部文件 图层、图块、标注样式...

[复制链接]

已领礼包: 58个

财富等级: 招财进宝

发表于 2020-11-1 22:38:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
[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;            }        }

    }
                }
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 226个

财富等级: 日进斗金

发表于 2020-11-2 08:12:34 | 显示全部楼层
谢谢分享!!!
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-12-18 20:59 , Processed in 0.372343 second(s), 30 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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