Well This is what I came up with. I have looked through everything block related both in the swamp and over at AD .Net (about 100 threads so far). I did try everything else for clues and most examples do only a part of what I need to have working.
I ended up doing it the way I do most everything....get the handle of something, store it in a binary file and use it to return an object.
This is unorthodox (hacked by trial and error), but I think it gets rid of the isErased issues inherit in the acad DB. I am thinking that if I have the real handle then I can get the real object back and don't care about orphaned clutter in the DB. In the test case things seem to be proving this. I will find out shortly when I try to plug it into the app. I am not really satisfied with it after seeing better approaches, but it's all I know for now and it's working.
here it is in VB and C# below(converted)
VB
Public Sub BlockTest()
Dim blkDefHnd As String = vbNullString
Dim blkRefHnd As String = vbNullString
Dim blkName As String = "TESTBLOCK"
'Make a block reference containing 1 solid and 1 block definition
addSomething(blkName, blkDefHnd)
'No go and remove everything
addBlockRef(blkName, blkRefHnd)
adjBlockDef(blkDefHnd, blkName)
adjBlockRef(blkName, blkRefHnd)
End Sub
Public Sub addSomething(ByVal blkName As String, _
ByRef BlockDefHnd As String)
Dim aPnt As New Point3d(0, 0, 0)
Dim bPnt As New Point3d(55, 0, 0)
Dim cPnt As New Point3d(55, 5, 0)
Dim dPnt As New Point3d(0, 5, 0)
Dim colregion As DBObjectCollection = New DBObjectCollection()
Dim colPoly As DBObjectCollection = New DBObjectCollection()
Dim db As Database = HostApplicationServices.WorkingDatabase
Dim region As DatabaseServices.Region = New DatabaseServices.Region()
Dim tr As Transaction = db.TransactionManager.StartTransaction()
Dim pntCol As New Point3dCollection
Try
Dim doclock As DocumentLock = AcAp.DocumentManager.MdiActiveDocument.LockDocument()
Dim bt As BlockTable = tr.GetObject(db.BlockTableId, DatabaseServices.OpenMode.ForWrite, True)
If bt Is Nothing Then Return
pntCol.Add(aPnt) : pntCol.Add(bPnt) : pntCol.Add(cPnt) : pntCol.Add(dPnt)
Dim plRegion As New Polyline3d(Poly3dType.SimplePoly, pntCol, True)
'create a region from the polyline
colPoly.Add(plRegion)
colregion = DatabaseServices.Region.CreateFromCurves(colPoly)
region = CType(colregion(0), DatabaseServices.Region)
'create the solid
Dim solid As Solid3d = New Solid3d()
solid.Extrude(region, 5, 0)
If (bt.Has(blkName)) Then
Dim btr As BlockTableRecord = TryCast(tr.GetObject(bt(blkName), OpenMode.ForWrite, False), BlockTableRecord)
If btr Is Nothing Then Return
btr.AppendEntity(solid)
tr.AddNewlyCreatedDBObject(solid, True)
BlockDefHnd = btr.ObjectId.Handle.Value.ToString()
Else 'Create a new block definition
Dim btr As BlockTableRecord = New BlockTableRecord()
btr.Name = blkName
bt.Add(btr)
tr.AddNewlyCreatedDBObject(btr, True)
btr.AppendEntity(solid)
tr.AddNewlyCreatedDBObject(solid, True)
BlockDefHnd = btr.ObjectId.Handle.Value.ToString()
End If
tr.Commit()
doclock.Dispose()
colPoly.Dispose()
colregion.Dispose()
plRegion.Dispose()
pntCol.Dispose()
region.Dispose()
solid.Dispose()
Catch ex As Autodesk.AutoCAD.Runtime.Exception
tr.Abort()
MsgBox(ex.StackTrace)
Finally
tr.Dispose()
End Try
End Sub
Public Sub addBlockRef(ByVal blkName As String, ByRef blkRefhnd As String)
Dim db As Database = HostApplicationServices.WorkingDatabase()
Dim tr As Transaction = db.TransactionManager.StartTransaction()
Dim doclock As DocumentLock = AcAp.DocumentManager.MdiActiveDocument.LockDocument()
Dim block As BlockReference = Nothing
Try
Dim bt As BlockTable = tr.GetObject(db.BlockTableId, DatabaseServices.OpenMode.ForRead, True)
Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite, True)
block = New BlockReference(New Point3d(0, 0, 0), bt(blkName))
btr.AppendEntity(block)
tr.AddNewlyCreatedDBObject(block, True)
blkRefhnd = block.ObjectId.Handle.Value.ToString() 'return handle to calling sub
tr.Commit()
doclock.Dispose()
Catch ex As Autodesk.AutoCAD.Runtime.Exception
tr.Abort()
MsgBox(ex.StackTrace)
Finally
tr.Dispose()
End Try
End Sub
Public Sub adjBlockRef(ByVal blkName As String, ByVal BlockRefHnd As String)
Dim db As Database = HostApplicationServices.WorkingDatabase()
Dim doclock As DocumentLock = AcAp.DocumentManager.MdiActiveDocument.LockDocument()
Dim tr As Transaction = db.TransactionManager.StartTransaction()
Try
Dim id As ObjectId = db.GetObjectId(False, New Handle(Convert.ToInt64(BlockRefHnd)), 0)
Dim Block As BlockReference = DirectCast(tr.GetObject(id, DatabaseServices.OpenMode.ForWrite), BlockReference)
Block.Erase()
tr.Commit()
doclock.Dispose()
Catch ex As Autodesk.AutoCAD.Runtime.Exception
tr.Abort()
MsgBox(ex.StackTrace)
Finally
tr.Dispose()
End Try
End Sub
Public Sub adjBlockDef(ByVal BlockDefHnd As String, ByVal blkName As String)
Dim db As Database = AcAp.DocumentManager.MdiActiveDocument.Database
Dim trans As Transaction = db.TransactionManager.StartTransaction()
Try
Dim doclock As DocumentLock = AcAp.DocumentManager.MdiActiveDocument.LockDocument()
Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForWrite)
Dim id As ObjectId = db.GetObjectId(False, New Handle(Convert.ToInt64(BlockDefHnd)), 0)
Dim BlockDef As BlockTableRecord = trans.GetObject(id, OpenMode.ForWrite)
BlockDef.Erase()
BlockDef.IsUnloaded = True
BlockDefHnd = vbNullString
trans.Commit()
doclock.Dispose()
Catch ex As Autodesk.AutoCAD.Runtime.Exception
trans.Abort()
MsgBox(ex.StackTrace)
Finally
trans.Dispose()
End Try
End Sub
C#
public void BlockTest()
{
string blkDefHnd = Microsoft.VisualBasic.Constants.vbNullString;
string blkRefHnd = Microsoft.VisualBasic.Constants.vbNullString;
string blkName = "TESTBLOCK";
//Make a block reference containing 1 solid and 1 block definition
addSomething(blkName, blkDefHnd);
//No go and remove everything
addBlockRef(blkName, blkRefHnd);
adjBlockDef(blkDefHnd, blkName);
adjBlockRef(blkName, blkRefHnd);
{
public void addSomething(string blkName, ref string BlockDefHnd)
{
Point3d aPnt = new Point3d(0, 0, 0);
Point3d bPnt = new Point3d(55, 0, 0);
Point3d cPnt = new Point3d(55, 5, 0);
Point3d dPnt = new Point3d(0, 5, 0);
DBObjectCollection colregion = new DBObjectCollection();
DBObjectCollection colPoly = new DBObjectCollection();
Database db = HostApplicationServices.WorkingDatabase;
DatabaseServices.Region region = new DatabaseServices.Region();
Transaction tr = db.TransactionManager.StartTransaction();
Point3dCollection pntCol = new Point3dCollection();
try
{
DocumentLock doclock = AcAp.DocumentManager.MdiActiveDocument.LockDocument();
BlockTable bt = tr.GetObject(db.BlockTableId, DatabaseServices.OpenMode.ForWrite, true);
if (bt == null)
{
return;
}
pntCol.Add(aPnt);
pntCol.Add(bPnt);
pntCol.Add(cPnt);
pntCol.Add(dPnt);
Polyline3d plRegion = new Polyline3d(Poly3dType.SimplePoly, pntCol, true);
//create a region from the polyline
colPoly.Add(plRegion);
colregion = DatabaseServices.Region.CreateFromCurves(colPoly);
region = (DatabaseServices.Region)(colregion(0));
//create the solid
Solid3d solid = new Solid3d();
solid.Extrude(region, 5, 0);
if (bt.Has(blkName))
{
BlockTableRecord btr = tr.GetObject(bt(blkName), Microsoft.VisualBasic.OpenMode.ForWrite, false) as BlockTableRecord;
if (btr == null)
{
return;
}
btr.AppendEntity(solid);
tr.AddNewlyCreatedDBObject(solid, true);
BlockDefHnd = btr.ObjectId.Handle.Value.ToString();
}
else //Create a new block definition
{
BlockTableRecord btr = new BlockTableRecord();
btr.Name = blkName;
bt.Add(btr);
tr.AddNewlyCreatedDBObject(btr, true);
btr.AppendEntity(solid);
tr.AddNewlyCreatedDBObject(solid, true);
BlockDefHnd = btr.ObjectId.Handle.Value.ToString();
}
tr.Commit();
doclock.Dispose();
colPoly.Dispose();
colregion.Dispose();
plRegion.Dispose();
pntCol.Dispose();
region.Dispose();
solid.Dispose();
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
tr.Abort();
MessageBox.Show(ex.StackTrace);
}
finally
{
tr.Dispose();
}
}
public void addBlockRef(string blkName, ref string blkRefhnd)
{
Database db = HostApplicationServices.WorkingDatabase();
Transaction tr = db.TransactionManager.StartTransaction();
DocumentLock doclock = AcAp.DocumentManager.MdiActiveDocument.LockDocument();
BlockReference block = null;
try
{
BlockTable bt = tr.GetObject(db.BlockTableId, DatabaseServices.OpenMode.ForRead, true);
BlockTableRecord btr = tr.GetObject(bt(BlockTableRecord.ModelSpace), Microsoft.VisualBasic.OpenMode.ForWrite, true);
block = new BlockReference(new Point3d(0, 0, 0), bt(blkName));
btr.AppendEntity(block);
tr.AddNewlyCreatedDBObject(block, true);
blkRefhnd = block.ObjectId.Handle.Value.ToString(); //return handle to calling sub
tr.Commit();
doclock.Dispose();
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
tr.Abort();
MessageBox.Show(ex.StackTrace);
}
finally
{
tr.Dispose();
}
}
public void adjBlockRef(string blkName, string BlockRefHnd)
{
Database db = HostApplicationServices.WorkingDatabase();
DocumentLock doclock = AcAp.DocumentManager.MdiActiveDocument.LockDocument();
Transaction tr = db.TransactionManager.StartTransaction();
try
{
ObjectId id = db.GetObjectId(false, new Handle(Convert.ToInt64(BlockRefHnd)), 0);
BlockReference Block = (BlockReference)(tr.GetObject(id, DatabaseServices.OpenMode.ForWrite));
Block.Erase();
tr.Commit();
doclock.Dispose();
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
tr.Abort();
MessageBox.Show(ex.StackTrace);
}
finally
{
tr.Dispose();
}
}
Public Sub adjBlockDef(ByVal BlockDefHnd As String, ByVal blkName As String)
Dim db As Database = AcAp.DocumentManager.MdiActiveDocument.Database
Dim trans As Transaction = db.TransactionManager.StartTransaction()
Try
Dim doclock As DocumentLock = AcAp.DocumentManager.MdiActiveDocument.LockDocument()
Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForWrite)
Dim id As ObjectId = db.GetObjectId(False, New Handle(Convert.ToInt64(BlockDefHnd)), 0)
Dim BlockDef As BlockTableRecord = trans.GetObject(id, OpenMode.ForWrite)
BlockDef.Erase()
BlockDef.IsUnloaded = True
BlockDefHnd = vbNullString
trans.Commit()
doclock.Dispose()
Catch ex As Autodesk.AutoCAD.Runtime.Exception
trans.Abort()
MsgBox(ex.StackTrace)
Finally
trans.Dispose()
End Try
End Sub