Hi all,
I've been seeing quite a good bit of success with the addin I mentioned starting to write back in August, but just lately I have a bunch of examples where I will place a blockreference in a drawing, and it just disappears moments later.
In and around the placement, a bunch of things happen- namely placing a dimension or two and depending upon the situation it may or may not draw a line or some piece of text (mainly for my own debugging purposes) and I wondered if someone has some general rules on transactions and use of the AutoCAD database in general.
Years ago on here I think it was Daniel or someone else who said something like "if you 'new' it, dispose of it or commit" or words to that effect.
One method I have which occasionally produces "invisible" blocks is this:
public void InsertBlockReference(MasonryBracketSystemViewModel systemDefinition, Matrix3d transformMatrix, BlockReference blkRef)
{
Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = db.TransactionManager;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord modelSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
if (blkRef != null)
{
blkRef.TransformBy(transformMatrix);
blkRef.SetDatabaseDefaults();
modelSpace.AppendEntity(blkRef);
tr.AddNewlyCreatedDBObject(blkRef, true);
var attrInfos = new Dictionary<string, AttrInfo>();
SetBlockRefAttributeValuesBasedOnParameters(systemDefinition, false, tr, blkRef, out attrInfos);
SetBlockRefDynPropsBasedOnParameters(systemDefinition, 0, blkRef);
blkRef.RecordGraphicsModified(true);
AddXRecordToBlockReference(blkRef, "systemName", systemDefinition.AssignedSystemName.SystemName);
AddXRecordToBlockReference(blkRef, "instanceNumber", systemDefinition.AssignedSystemName.InstanceNumber);
AddXRecordToBlockReference(blkRef, "level", systemDefinition.AssignedSystemName.Level);
SetXRecordsUsingViewModel(systemDefinition, blkRef);
modelSpace.UpdateAnonymousBlocks();
ed.Regen();
ed.UpdateScreen();
db.Regenmode = true;
db.UpdateExt(true);
tm.QueueForGraphicsFlush();
tr.Commit();
}
else
{
throw new System.Exception("The block reference was null");
}
}
}
(db in this case is external to that method but I wonder whether it should in fact be local?):
public static Database db; // = HostApplicationServices.WorkingDatabase;
public static Document doc; // = Application.DocumentManager.MdiActiveDocument;
public static Editor ed;
public BlockHelpers()
{
uXSettings = new UXSettings();
doc = CadApp.DocumentManager.MdiActiveDocument;
db = doc.Database;
ed = doc.Editor;
}