public partial class myClass
{
private Hatch _hatch = null;
private void AddNewHighlightingHatch()
{
// works
Document document = Bricscad.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database database = document.Database;
Editor editor = document.Editor;
if (_hatch == null)
{
ObjectId selectedObjectId = AutoCADController.GetObjectIdByHandle(currentItemProperty.AutoCADHandle);
_hatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");
_hatch.ColorIndex = AutoCADController.MyColorIndex;
_hatch.Associative = true;
_hatch
.Transparency = new Transparency
(127); _hatch.EvaluateHatch(true);
using (DocumentLock documentLock = document.LockDocument())
{
database.Fillmode = true;
using (Transaction transaction = database.TransactionManager.StartTransaction())
{
BlockTable blockTable;
blockTable = transaction.GetObject(database.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord blockTableRecord;
blockTableRecord = transaction.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
using (ObjectIdCollection objectIdCollection
= new ObjectIdCollection
()) {
objectIdCollection.Add(selectedObjectId);
// Create the hatch object and append it to the block table record
blockTableRecord.AppendEntity(_hatch);
_hatch.AppendLoop(HatchLoopTypes.Outermost, objectIdCollection);
transaction.AddNewlyCreatedDBObject(_hatch, true);
editor.Regen();
}
transaction.Commit();
}
}
}
}
private void DeleteOldHighlightingHatch()
{
try
{
Document document = Bricscad.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database database = document.Database;
Editor editor = document.Editor;
if (_hatch != null)
{
//hatch is not null - it should be cached by AddNewHighlightingHatch
database.Fillmode = true;
using (DocumentLock documentLock = document.LockDocument())
{
using (Transaction transaction = database.TransactionManager.StartTransaction())
{
logger.Debug("using transaction");
BlockTable blockTable;
blockTable = transaction.GetObject(database.BlockTableId, OpenMode.ForRead) as BlockTable;
using (BlockTableRecord blockTableRecord = transaction.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord)
{
logger.Debug("_hatch.ObjectId.Handle: " + _hatch.ObjectId.Handle); //Exception eNotOpenForRead
_hatch = null;
editor.Regen();
}
logger.Debug("transaction.Commit();");
transaction.Commit();
}
}
}
}
catch (Exception ex)
{
logger.Error(ex.Message + "\n" + ex.StackTrace);
}
}
}