@stefanome a écrit :
@_gile wrote:
@stefanome a écrit :
I think that this will not work because line is returned inside an using block, which means that it has been disposed by the time it is assigned to l1 or l2.
So, why do you create the new Line with a using statement ?
Because I was doing what the documentation says I should do. My code follows this example.
In the linked example nothing is done with the line outside of the (redundent) using statement used to create the new Line.
@stefanome a écrit :
@_gile wrote:
You do not need to dispose it in the CreateLine function. While an entity (or any DBObject) is added to a Transactiion, it will be automatically disposed when the transaction is disposed.
This makes sense, but it is the opposite of what the documentation says on the second point on this page.
If adding it to the space means adding it to the database, then I don't need to dispose because of the third point, but you are mentioning the transaction, and the second point explicitly says the opposite of what you are saying. (I would say that point 3 ensures that Dispose is called, so I can ignore point 2).
This page of the documentation is not very clear.
We should create new DBObjects in a using statement to ensure the object to be disposed in case the object may not be added to the transaction (e.g. an exception occurs).
For example, this is not safe because if the user don't choose "Yes", the newly created Line won't be diposed.
using (var tr = db.TransactionManager.StartTransaction())
{
var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
var line = new Line(new Point3d(5, 5, 0), new Point3d(12, 3, 0));
var pkr = ed.GetKeywords("\nAdd the line to databse [Yes/No]:", "Yes No");
if (pkr.Status == PromptStatus.OK && pkr.StringResult == "Yes")
{
curSpace.AppendEntity(line);
tr.AddNewlyCreatedDBObject(line, true);
}
tr.Commit();
}
Creating the Line in a using statement would be a solution to ensure the line to be disposed whatever the user reply.
using (var tr = db.TransactionManager.StartTransaction())
{
var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
using (var line = new Line(new Point3d(5, 5, 0), new Point3d(12, 3, 0)))
{
var pkr = ed.GetKeywords("\nAdd the line to databse [Yes/No]:", "Yes No");
if (pkr.Status == PromptStatus.OK && pkr.StringResult == "Yes")
{
curSpace.AppendEntity(line);
tr.AddNewlyCreatedDBObject(line, true);
}
}
tr.Commit();
}
But immediately adding the Line to the Transaction is also a solution which ensure the Line to be disposed with the Transaction whatever le user reply.
using (var tr = db.TransactionManager.StartTransaction())
{
var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
var line = new Line(new Point3d(5, 5, 0), new Point3d(12, 3, 0));
curSpace.AppendEntity(line);
tr.AddNewlyCreatedDBObject(line, true);
var pkr = ed.GetKeywords("\nAdd the line to databse [Yes/No]:", "Yes No");
if (pkr.Status != PromptStatus.OK && pkr.StringResult == "Yes")
{
line.Erase();
}
tr.Commit();
}