TheSwamp
Code Red => .NET => Topic started by: Draftek on November 10, 2005, 05:05:37 PM
-
I need to update a block Insert's attributes from a selection set of dimensions.
Have this little test method that accepts the matching tag property and the textstring value.
It works okay, except the block reference does not get updated....
Any Ideas why the transaction.Commit() is not doing anything?
private void ModifyBlockAtt(string dimNo, string sValue)
{
// already have db and ed defined
Transaction tr;
TypedValue[] filterlist;
Autodesk.AutoCAD.EditorInput.SelectionFilter filter;
Autodesk.AutoCAD.EditorInput.PromptSelectionResult selRes;
Autodesk.AutoCAD.EditorInput.SelectionSet oSS = null;
using(tr = db.TransactionManager.StartTransaction())
{
filterlist = new TypedValue[2];
filterlist[0] = new TypedValue(0, "INSERT");
filterlist[1] = new TypedValue(2, "CrimperDataInputTable");
filter = new SelectionFilter(filterlist);
selRes = ed.SelectAll(filter);
if (selRes.Status != Autodesk.AutoCAD.EditorInput.PromptStatus.OK)
{
ed.WriteMessage("\nNo Table Block in the drawing ");
return;
}
oSS = selRes.Value;
BlockReference oEnt;
oEnt = (BlockReference)tr.GetObject(oSS[0].ObjectId, OpenMode.ForRead);
for (int i = 0; i < oEnt.AttributeCollection.Count; i++)
{
ObjectId id = oEnt.AttributeCollection[i];
AttributeReference aRef = (AttributeReference)tr.GetObject(id, OpenMode.ForWrite);
if (aRef.Tag == dimNo)
{
aRef.TextString = sValue;
tr.Commit();
return;
}
}
}
}
Thanks
-
You should put your tr.Commit() as the last thing before returning from your method. The transaction wraps up all of your work in one lot and completes with Commit(). You may be finalising your trans'n too early ;)
See if that works.
-
Thanks, I'll give that a try.
-
Actually, include it in your using(tr = .... code block, after you've finished bd Mods, I believe ...
-
HA!
Thanks to both of you, I had a transaction in the calling method that I had not Committed.
Once again, proving that I'm nothing more than a hack!
Please remember this question, because in about a month or two, I'll ask the exact same thing again.
seriously.
-
Actually, include it in your using(tr = .... code block, after you've finished bd Mods, I believe ...
Thanks Kerry, that is what I meant too, just wasn't clear.. :oops:
I've actually had mixed results with using a 'using' with transactions so now I just use this as a rule and I've had no problems since
public void SomeFoo()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Transaction tr = db.TransactionManager.StartTransaction();
try
{
//do stuff here!
tr.Commit();
}
catch
{
ed.WriteMessage("KaBoom!..");
}
finally
{
tr.Dispose();
}
}
-
Hi Mick,
Yes, that is suggested by some commentators,
but, according to the Spec's, the 'using' mechanism will automatically clean up, < edit : even in the event of an unhandled exception { need to qualify this statement ! }. >
One issue that I see is that VB.Net can not use 'using' so there may be some semantic difficulties when 'people' try to translate code from one language to another.
-
I've actually had mixed results with using a 'using' with transactions so now I just use this as a rule and I've had no problems since
What kind of mixed results are you seeing Mick?
-
I can't remember exactly Bobby (it was a while ago now), but I think it was when calling methods or passing objects such as the db inside/outside of the using statement.
My lack of experience with the language probably had more to do with it than anything else, but once I started using it the 'standard' way (as above) I've had no trouble since.
-
...heh, just thinking about it, I was probably doing something similar to Draftek with the Commit() method :laugh: