用ARX.NET将Dwg文件以块的方式插入CAD中(.net)

  1. 源程序:用ARX.NET将Dwg文件以块的方式插入CAD中 
      public class Testinsert 
      { 
        [CommandMethod(“testinsert”)] 
        static public void DoIt() 
        { 
            Document doc = Application.DocumentManager.MdiActiveDocument; 
            Editor ed = doc.Editor; 
            PromptResult res = ed.GetString(“Give me a file to insert”); 
            if (res.Status != PromptStatus.OK) 
              return; 
            string fname = res.StringResult; 
            if (!File.Exists(fname)) 
              fname = HostApplicationServices.Current.FindFile(fname, doc.Database, FindFileHint.Default); 
            using (Database db = new Database(false, false)) 
            { 
              //read drawing 
              db.ReadDwgFile(fname, FileShare.Read, true, null); 
              using (Transaction t = doc.TransactionManager.StartTransaction()) 
              { 
                //insert it as a new block 
                ObjectId idBTR = doc.Database.Insert(“test”, db, false); 
                //create a ref to the block 
                BlockTable bt = (BlockTable)t.GetObject(doc.Database.BlockTableId, OpenMode.ForRead); 
                BlockTableRecord btr = (BlockTableRecord)t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); 
                using (BlockReference bref = new BlockReference(Point3d.Origin, idBTR)) 
                { 
                    btr.AppendEntity(bref); 
                    t.AddNewlyCreatedDBObject(bref, true); 
                } 
                t.Commit(); 
              } 
            } 
        } 
      } 

发表回复