乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 27|回复: 1

如何插入DWG为块并同时分解块?

[复制链接]

52

主题

380

帖子

11

银币

中流砥柱

Rank: 25

铜币
588
发表于 2011-5-6 11:32:00 | 显示全部楼层 |阅读模式
如何插入DWG为块并同时分解块?
下面的代码似乎不可行。。。请指教。
                using (Database db = new Database(false, false))
                {
                    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);
                        db.ReadDwgFile(blockFile, System.IO.FileShare.Read, true, null);
                        ObjectId blockId = doc.Database.Insert(blockName, db, false);
                        
                        BlockReference br = new BlockReference(blockPoint, blockId);
                        btr.AppendEntity(br);
                        trans.AddNewlyCreatedDBObject(br, true);
                                                
                        //块分解
                        br.ExplodeToOwnerSpace();
                        br.Erase();     
                  
                        //清理块
                        ObjectIdCollection ids = new ObjectIdCollection();
                        ids.Add(br.ObjectId);
                        db.Purge(ids);
                        
                        trans.Commit();
                    }
                }
以下是清理图形的代码。
  1. // [url=http://www.theswamp.org/index.php?topic=25880.0]http://www.theswamp.org/index.php?topic=25880.0[/url]
  2. using System;
  3. using System.Text;
  4. using System.Collections.Generic;
  5. using Autodesk..Runtime;
  6. using Autodesk.AutoCAD.EditorInput;
  7. using Autodesk.AutoCAD.DatabaseServices;
  8. using Autodesk.AutoCAD.ApplicationServices;
  9. [assembly: ExtensionApplication(typeof(cgabriel.PurgeTools))]
  10. [assembly: CommandClass(typeof(cgabriel.PurgeTools))]
  11. namespace cgabriel
  12. {
  13.     public class PurgeTools : IExtensionApplication
  14.     {
  15.         public void Initialize() { }
  16.         public void Terminate() { }
  17.         public static bool purgeItems(Database db, ObjectIdCollection tableIds, bool silent)
  18.         {
  19.             Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  20.             bool itemsPurged = false;
  21.             using (Transaction tr = db.TransactionManager.StartTransaction())
  22.             {
  23.                 ObjectIdCollection purgeableIds = new ObjectIdCollection();
  24.                 foreach (ObjectId tableId in tableIds)
  25.                 {
  26.                     SymbolTable table = (SymbolTable)tr.GetObject(tableId, OpenMode.ForRead, false);
  27.                     foreach (ObjectId recordId in table)
  28.                         purgeableIds.Add(recordId);
  29.                 }
  30.                 db.Purge(purgeableIds);
  31.                 if (purgeableIds.Count != 0)
  32.                 {
  33.                     itemsPurged = true;
  34.                     foreach (ObjectId id in purgeableIds)
  35.                     {
  36.                         SymbolTableRecord record = (SymbolTableRecord)tr.GetObject(id, OpenMode.ForWrite);
  37.                         string recordName = record.Name;
  38.                         if (!silent)
  39.                         {
  40.                             if (!recordName.Contains("|"))
  41.                             {
  42.                                 ed.WriteMessage("\nPurging " +
  43.                                     record.GetType().Name + " " + recordName);
  44.                             }
  45.                         }
  46.                         record.Erase();
  47.                     }
  48.                 }
  49.                 tr.Commit();
  50.             }
  51.             return itemsPurged;
  52.         }
  53.         public static bool purgeAll(Database db, bool silent)
  54.         {
  55.             ObjectIdCollection tableIds = new ObjectIdCollection();
  56.             tableIds.Add(db.BlockTableId);
  57.             tableIds.Add(db.DimStyleTableId);
  58.             tableIds.Add(db.LayerTableId);
  59.             tableIds.Add(db.LinetypeTableId);
  60.             tableIds.Add(db.RegAppTableId);
  61.             tableIds.Add(db.TextStyleTableId);
  62.             tableIds.Add(db.UcsTableId);
  63.             tableIds.Add(db.ViewportTableId);
  64.             tableIds.Add(db.ViewTableId);
  65.             return purgeItems(db, tableIds, silent);
  66.         }
  67.         [CommandMethod("PurgeTools", "PurgeAll", CommandFlags.Modal | CommandFlags.DocExclusiveLock)]
  68.         public static void purgeAll()
  69.         {
  70.             while (purgeAll(Application.DocumentManager.MdiActiveDocument.Database, false))
  71.                 continue;
  72.         }
  73.     }
  74. }
回复

使用道具 举报

52

主题

380

帖子

11

银币

中流砥柱

Rank: 25

铜币
588
发表于 2011-5-6 11:41:00 | 显示全部楼层
忘了突出我的问题了:
不知道如何清理块。。。
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-6-29 12:50 , Processed in 2.975656 second(s), 56 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表