Author Topic: Selecting the right entity.  (Read 17897 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
Selecting the right entity.
« on: August 02, 2006, 03:15:33 PM »
Is this a good example of making sure you have the right entity? or is there a better way to test?  I tried other ways, but couldn't get them to work right, then I remembered the try/catch/finally I saw in others code.

Any/all comments/opinions welcomed.

Thanks in advance.

Code: [Select]
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using acadEd = Autodesk.AutoCAD.EditorInput.Editor;

[assembly: CommandClass (typeof(Test.ChangeAttribute))]

namespace Test
{
/// <summary>
/// Description of ChangeAttribute.
/// </summary>
public class ChangeAttribute
{
[CommandMethod("AttTest")]
public void ChangeAttributes()
{
acadEd ed = acadApp.DocumentManager.MdiActiveDocument.Editor;
Database db = HostApplicationServices.WorkingDatabase;
Transaction trans = db.TransactionManager.StartTransaction();
PromptNestedEntityResult result = ed.GetNestedEntity("Select attribute: ");
if (result.Status == PromptStatus.OK)
{
//ObjectId ObjId = result.GetObjectId();
//Entity Ent = (Entity)trans.GetObject(ObjId, OpenMode.ForWrite);
try
{
AttributeReference Ent = (AttributeReference)trans.GetObject(result.ObjectId, OpenMode.ForWrite);
Ent.TextString = "It worked!!";
trans.Commit();
}
catch
{
}
finally
{
trans.Dispose();

}
}
}
}
}
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Selecting the right entity.
« Reply #1 on: August 02, 2006, 03:53:44 PM »
Tim,

Is this ->   http://www.theswamp.org/index.php?topic=7904.0  any assistance   ?
It's a little imature, but may give you some ideas ...
public void
        GetEntity()
        { ....

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.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Selecting the right entity.
« Reply #2 on: August 02, 2006, 04:00:24 PM »
Tim,

Is this ->   http://www.theswamp.org/index.php?topic=7904.0  any assistance   ?
It's a little imature, but may give you some ideas ...
public void
        GetEntity()
        { ....


Kerry,

  I tried looking at that before, and couldn't understand it.  Now it makes more sense to me, and might be a big help.  Thanks for refreshing my mind.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Selecting the right entity.
« Reply #3 on: August 02, 2006, 04:06:24 PM »
 ... does the PromptNestedEntity class have an AddAllowedClass method ?

I'm running out the door to catch a train, so ...
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.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Selecting the right entity.
« Reply #4 on: August 02, 2006, 04:59:01 PM »
... does the PromptNestedEntity class have an AddAllowedClass method ?

I'm running out the door to catch a train, so ...
If I'm reading this correctly, it does.  Hope you caught your train.  I will study this, and see if I can get it to work.  Thanks Kerry.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

LE

  • Guest
Re: Selecting the right entity.
« Reply #5 on: August 02, 2006, 05:17:21 PM »
Future NET master Tim.... are you using SharpDevelopment ? (for what AutoCAD version) and following the Lab's SDK - Just curios, Sir.

Thanks.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Selecting the right entity.
« Reply #6 on: August 02, 2006, 05:25:27 PM »
Future NET master Tim.... are you using SharpDevelopment ? (for what AutoCAD version) and following the Lab's SDK - Just curios, Sir.

Thanks.
No master title for me.   :-)

Yup, SharpDevelop 1.1 (work doesn't want me to install the .Net 2.0 framwork right now)
Acad 2006 Electrical.
I read the labs, but trying stuff, and then using them for reference right now.  I learn more/better by trial, with little helps here and there.  I seem to be doing a lot of research, searching the net for answers, and reading pdf files I have found.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Selecting the right entity.
« Reply #7 on: August 02, 2006, 05:59:30 PM »
You could try something like this Tim -

AttributeReference Ent = trans.GetObject(result.ObjectId, OpenMode.ForWrite) as AttributeReference;

then, before you use the 'Ent', test if it's not null which is the return when using 'as' if it is not the correct type like so -

if(Ent != null)
{
     //you have your proper object, work on it:
}

or similarly -

if(Ent == null)
    return; //not what we wanted, return quietly:
"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

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Selecting the right entity.
« Reply #8 on: August 02, 2006, 06:33:55 PM »
Thanks Mick.  I have seen that before, now that you post it, but didn't think at all to use it here.  Is that way pretty dependable to use with other things, ie collections, dictionaries?  or mainly for selecting objects?  I would think the former, but might as well ask why I can.   :-)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Selecting the right entity.
« Reply #9 on: August 02, 2006, 06:40:12 PM »
Yes, you can use it for any 'object'. What 'as' does is it trys to cast an object given to the object type described -

ent as Circle;//or any other type you want the ent to be cast to.

- if it fails it return 'null' which can be checked against.

There is also the 'is' keyword but it doesn't perform the cast it just checks the ent against the type first, then you would have to do the casting after.
When you're expecting a certain type and you will probably use it straight away (not just for comparison say) 'as' would be quicker (er...easier) to use as it performs both the type checking (returns null on the type test) and casting in one go.
"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: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Selecting the right entity.
« Reply #10 on: August 02, 2006, 06:43:02 PM »
the other way of doing it is called 'boxing' which is basically casting but you have no way of telling if you have done the cast succesfully. eg.

ent = (Circle)tr.getEntity(.......etc.

how do you know if your cast/boxing worked? You would have to do a check or your code will fail, as is much nicer and convenient :)
"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

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Selecting the right entity.
« Reply #11 on: August 02, 2006, 06:48:48 PM »
Thanks Mick!!

I did the second one you posted, with the try/catch/finally, and it seemed to work well.  If I selected a line from a block, then it would do nothing, but before I used the try/catch/finally, it would error pretty bad.  I like that fact that there is more than one way to do it.  Thanks for showing me both, and explaining them.  :-)

Now it's time to setup a dialog, and see what I can do with that.   :-D
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Selecting the right entity.
« Reply #12 on: August 02, 2006, 06:54:18 PM »
no prob's, we'll hear from you shortly then :D
"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

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Selecting the right entity.
« Reply #13 on: August 02, 2006, 07:01:10 PM »
Shorter than you thought, huh Mick?

Mick, do you understand what Kerry was talking about earlier?  I have been doing "real" work, and haven't had the time to research it.  I think he was pointing out that I might be able to restrict the selection to only AttributeReference's with the prompt, but I not sure.  If not, it's cool, I think I might have time before it's time to go home today. :wink:

Thanks Again!
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

LE

  • Guest
Re: Selecting the right entity.
« Reply #14 on: August 02, 2006, 07:06:38 PM »
SharpDevelop 1.1 

Hey Tim;

Are you not using the latest version - SharpDevelop 2.0 - with this, you can open the SDK Lab samples.... (ignore this if not apply)