Author Topic: Update Block Attributes  (Read 8167 times)

0 Members and 1 Guest are viewing this topic.

Draftek

  • Guest
Update Block Attributes
« 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?

Code: [Select]
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

MickD

  • King Gator
  • Posts: 3667
  • (x-in)->[process]->(y-out) ... simples!
Re: Update Block Attributes
« Reply #1 on: November 10, 2005, 05:17:13 PM »
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.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.
- J.R.R. Tolkien

Draftek

  • Guest
Re: Update Block Attributes
« Reply #2 on: November 10, 2005, 05:31:44 PM »
Thanks, I'll give that a try.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Update Block Attributes
« Reply #3 on: November 10, 2005, 05:37:46 PM »
Actually, include it in your using(tr = ....  code block, after you've finished bd Mods, I believe ...
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Draftek

  • Guest
Re: Update Block Attributes
« Reply #4 on: November 10, 2005, 05:40:42 PM »
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.

MickD

  • King Gator
  • Posts: 3667
  • (x-in)->[process]->(y-out) ... simples!
Re: Update Block Attributes
« Reply #5 on: November 10, 2005, 05:48:20 PM »
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
Code: [Select]
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();
}
}
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.
- J.R.R. Tolkien

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Update Block Attributes
« Reply #6 on: November 10, 2005, 05:58:08 PM »
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.

« Last Edit: November 11, 2005, 07:27:38 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Bobby C. Jones

  • Swamp Rat
  • Posts: 516
  • Cry havoc and let loose the dogs of war.
Re: Update Block Attributes
« Reply #7 on: November 11, 2005, 11:10:01 AM »
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?
Bobby C. Jones

MickD

  • King Gator
  • Posts: 3667
  • (x-in)->[process]->(y-out) ... simples!
Re: Update Block Attributes
« Reply #8 on: November 11, 2005, 05:17:11 PM »
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.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.
- J.R.R. Tolkien

MickD

  • King Gator
  • Posts: 3667
  • (x-in)->[process]->(y-out) ... simples!
Re: Update Block Attributes
« Reply #9 on: November 11, 2005, 05:19:18 PM »
...heh, just thinking about it, I was probably doing something similar to Draftek with the Commit() method  :laugh:
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.
- J.R.R. Tolkien