向图档插入外部DWG图片 auto CAD .NET二次开发

处理方法:读取指定文件路径的dwg图。

核心代码:


                db.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndReadShare, true, null);

                    ObjectId id = doc.Database.Insert(blockName, db, false);如果,以上几篇文章能读懂,阅读该方法应该没有问题。请自行阅读。

输入参数:

插入块名称

外部DWG文件名称(路径)

插入点

输出:

插入块后的实体id

 ///


        /// 插入文件到当前模型空间中
        ///

        /// 块名
        /// 文件名
        /// 插入点
        public ObjectId GetDwgFile(string blockName, string fileName, Point3d insertPnt)
        {
            
            Document doc = Application.DocumentManager.MdiActiveDocument;
       
            ObjectId oId = ObjectId.Null;
          
            using (Database db = new Database(false, false))
            {
              
                db.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndReadShare, true, null);
            
                using (Transaction trans = doc.TransactionManager.StartTransaction())
                {
                
                    BlockTable bt = (BlockTable)trans.GetObject(doc.Database.BlockTableId, OpenMode.ForRead);
                   
                    BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                    
                    ObjectId id = doc.Database.Insert(blockName, db, false);
                
                    using (BlockReference brf = new BlockReference(insertPnt, id))
                    {
                      
                        oId = btr.AppendEntity(brf);
                     
                        trans.AddNewlyCreatedDBObject(brf, true);
                    
                        trans.Commit();
                    }
                }
            }
            return oId;
        }

如有问题请QQ1419226548

 

你可能感兴趣的:(CAD,二次开发(C#))