- 积分
- 7540
- 明经币
- 个
- 注册时间
- 2006-4-6
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
如何插入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();
}
}
以下是清理图形的代码。
- // [url=http://www.theswamp.org/index.php?topic=25880.0]http://www.theswamp.org/index.php?topic=25880.0[/url]
- using System;
- using System.Text;
- using System.Collections.Generic;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.ApplicationServices;
- [assembly: ExtensionApplication(typeof(cgabriel.PurgeTools))]
- [assembly: CommandClass(typeof(cgabriel.PurgeTools))]
- namespace cgabriel
- {
- public class PurgeTools : IExtensionApplication
- {
- public void Initialize() { }
- public void Terminate() { }
- public static bool purgeItems(Database db, ObjectIdCollection tableIds, bool silent)
- {
- Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
- bool itemsPurged = false;
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- ObjectIdCollection purgeableIds = new ObjectIdCollection();
- foreach (ObjectId tableId in tableIds)
- {
- SymbolTable table = (SymbolTable)tr.GetObject(tableId, OpenMode.ForRead, false);
- foreach (ObjectId recordId in table)
- purgeableIds.Add(recordId);
- }
- db.Purge(purgeableIds);
- if (purgeableIds.Count != 0)
- {
- itemsPurged = true;
- foreach (ObjectId id in purgeableIds)
- {
- SymbolTableRecord record = (SymbolTableRecord)tr.GetObject(id, OpenMode.ForWrite);
- string recordName = record.Name;
- if (!silent)
- {
- if (!recordName.Contains("|"))
- {
- ed.WriteMessage("\nPurging " +
- record.GetType().Name + " " + recordName);
- }
- }
- record.Erase();
- }
- }
- tr.Commit();
- }
- return itemsPurged;
- }
- public static bool purgeAll(Database db, bool silent)
- {
- ObjectIdCollection tableIds = new ObjectIdCollection();
- tableIds.Add(db.BlockTableId);
- tableIds.Add(db.DimStyleTableId);
- tableIds.Add(db.LayerTableId);
- tableIds.Add(db.LinetypeTableId);
- tableIds.Add(db.RegAppTableId);
- tableIds.Add(db.TextStyleTableId);
- tableIds.Add(db.UcsTableId);
- tableIds.Add(db.ViewportTableId);
- tableIds.Add(db.ViewTableId);
- return purgeItems(db, tableIds, silent);
- }
- [CommandMethod("PurgeTools", "PurgeAll", CommandFlags.Modal | CommandFlags.DocExclusiveLock)]
- public static void purgeAll()
- {
- while (purgeAll(Application.DocumentManager.MdiActiveDocument.Database, false))
- continue;
- }
- }
- }
|
|