eNotOpenForRead
Hi,
I am newbee in Bricscad development and have been stucked in a beginng. Here is a snippet that works fine in AutoCAD, but this is crushing down in Bricscad. Am I right that object doesn't exist outside of transaction? If so, what is approach?
public static void samp()
{
_AcEd.Editor editor = _AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
var h = do_getEntity();
editor.WriteMessage($"{h.ColorIndex.ToString()}\n");
}
public static Hatch do_getEntity()
{
_AcDb.Hatch h = null;
_AcEd.Editor editor = _AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
_AcEd.PromptEntityOptions peo = new _AcEd.PromptEntityOptions("Select a Hatch");
peo.SetRejectMessage("Not a Hatch!");
peo.AddAllowedClass(typeof(_AcDb.Hatch), true);
_AcEd.PromptEntityResult per = editor.GetEntity(peo);
if (per.Status == _AcEd.PromptStatus.OK)
{
_AcDb.Database database = _AcDb.HostApplicationServices.WorkingDatabase;
_AcDb.TransactionManager manager = database.TransactionManager;
//using (_AcDb.Transaction action = manager.StartTransaction())
using (_AcDb.Transaction action = manager.StartOpenCloseTransaction())
{
_AcDb.Hatch hatch = action.GetObject(per.ObjectId, _AcDb.OpenMode.ForRead) as _AcDb.Hatch;
hatch = action.GetObject(per.ObjectId, _AcDb.OpenMode.ForWrite) as _AcDb.Hatch;
if (hatch != null)
{
_AcAp.Application.ShowAlertDialog("Area is " + hatch.Area.ToString());
h = hatch;
}
action.Commit();
}
}
return h;
}
Comments
-
You deduced correctly. I recommend changing your function signature to return ObjectId of the hatch instead. The ObjectId is a way to identify an object without requiring it to remain open.
0 -
Thank you, Owen. Could you be so kind to clarify, do I need to to open transaction in a beginning of program and use its object as argument of methods? or how?
0 -
You should keep objects open only as long as necessary. The minimum scope depends entirely on the goals and context of the operations you're performing.
0