明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2269|回复: 1

[基础] 如何插入DWG为块并同时分解块?

[复制链接]
发表于 2011-5-6 11:32 | 显示全部楼层 |阅读模式
如何插入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.AutoCAD.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. }



 楼主| 发表于 2011-5-6 11:41 | 显示全部楼层
忘了突出我的问题了:

不知道如何清理块。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-17 13:47 , Processed in 0.198077 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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