找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1021|回复: 0

[分享] Copy selected objects to other document

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-6-7 03:02:17 | 显示全部楼层 |阅读模式

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

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

×
        //using System.Windows.Forms;

        static public void createCopyDoc(string fileName) // This method can have any name
        {
            DocumentCollection dm = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
            Document doc = dm.MdiActiveDocument;
            try
            {
            using (DocumentLock docloc = doc.LockDocument())
            {

                if (!File.Exists(fileName))
                {
                    Document newdoc = dm.Add("");
                    Database newdb=newdoc.Database;
                    newdb.SaveAs(fileName, DwgVersion.Current);
                    newdoc.CloseAndDiscard();
                }
            }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        [CommandMethod("CSset", CommandFlags.Session)]

        static public void CopySelectedText() // This method can have any name
        {
            string fileName = "c:\\test\\CopySet.dwg"; // add target drawing full name here
            DocumentCollection dm = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
            Document doc = dm.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            try
            {
            using (DocumentLock docloc = doc.LockDocument())
            {
                Transaction tr = db.TransactionManager.StartTransaction();

                using (tr)
                {
                    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);

                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);

                    // build filter to select texts and mtexts in the model space
                    TypedValue[] tv = new TypedValue[] { new TypedValue((int)DxfCode.Start, "*text"), new TypedValue(410, "Model") };

                    SelectionFilter filt = new SelectionFilter(tv);
                    //perform selection on screen
                    PromptSelectionResult res = ed.GetSelection(filt);

                    SelectionSet sset = res.Value;

                    ObjectId[] idcoll = sset.GetObjectIds();

                    ed.WriteMessage("\nSelected: {0}",  idcoll.Length);

                    ObjectIdCollection ids = new ObjectIdCollection();

                    foreach (ObjectId id in idcoll)
                    {
                        ids.Add(id);
                    } // may use ids.CopyTo(idcoll,idcoll.Length-1) instead

                    // create drawing if does not exists
                    if (!File.Exists(fileName))
                    {
                        createCopyDoc(fileName);

                    }

                    using (Database newdb = new Database(false, true))
                    {

                        newdb.ReadDwgFile(fileName,    System.IO.FileShare.ReadWrite, true, "");

                        using (Transaction newtr = newdb.TransactionManager.StartTransaction())
                        {

                           // Get Block table of target database
                            BlockTable  newbt = (BlockTable)newtr.GetObject(newdb.BlockTableId, OpenMode.ForRead);
                            // Get model space of target database
                            BlockTableRecord newbtr = (BlockTableRecord)newtr.GetObject(newbt[BlockTableRecord.ModelSpace], OpenMode.ForRead);

                        if (ids.Count != 0)
                        {
                            IdMapping iMap = new IdMapping();

                            db.WblockCloneObjects(ids, newbtr.ObjectId, iMap, DuplicateRecordCloning.Ignore, false);
                            // Save target drawing
                            newdb.SaveAs(fileName, DwgVersion.Current);// [A2010] this syntax ought be different for other Acad release
                           

                        }
                        }

                    }

                    tr.Commit();
                }//end using transaction
            }
            }
            catch (System.Exception ex)
            {
                // informative message
                ed.WriteMessage("\n{0}\n", ex.ToString());
                MessageBox.Show(ex.ToString());
            }
        }
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-5-22 15:09 , Processed in 0.360317 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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