如何插入DWG为块并同时分解块?
如何插入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();
}
}
以下是清理图形的代码。
// http://www.theswamp.org/index.php?topic=25880.0
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;
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);
}
public static void purgeAll()
{
while (purgeAll(Application.DocumentManager.MdiActiveDocument.Database, false))
continue;
}
}
}
忘了突出我的问题了:
不知道如何清理块。。。
页:
[1]