Author Topic: Selecting the right entity.  (Read 17874 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)

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Selecting the right entity.
« Reply #15 on: August 02, 2006, 07:08:03 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)
I thought to use this, I needed the .Net 2.0 framwork?  If not, then I will dl it tomorrow, and start using that.
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 #16 on: August 02, 2006, 07:10:05 PM »
Nope you don't need that.... it will allow you to open .SLN (solutions as they are in VS)

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Selecting the right entity.
« Reply #17 on: August 02, 2006, 07:13:50 PM »
Nope you don't need that.... it will allow you to open .SLN (solutions as they are in VS)
OOOoooooo.... Nice!  Thanks Luis.  I will get it tomorrow then, and see what kind of trouble I can cause.  :evil:
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 #18 on: August 02, 2006, 07:15:57 PM »
I'm in the same boat .... if I can I will start posting some code here to the table.... and maybe a more mess can do more trouble then....  :oops:

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Selecting the right entity.
« Reply #19 on: August 02, 2006, 07:19:22 PM »
I'm in the same boat .... if I can I will start posting some code here to the table.... and maybe a more mess can do more trouble then....  :oops:
Then maybe we can learn from each other.  If I can post anything useful in the .Net section.  Not for a while I suspect, but will be fun to learn.  Looking forward to your future posts Luis.  :roll:
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 #20 on: August 02, 2006, 07:30:58 PM »
Shorter than you thought, huh Mick?

Mick, do you understand what Kerry was talking about earlier? 

.......

Yep, what he was talking about was a type of filter for selection, to use these filters you would use the PromptXXXXXOptions classes to set these options 'if' available. I don't see the AddAllowedClass property in the PromptNestedEntityOptions so the way you are doing it is porbably the best way to go...for now.
"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 #21 on: August 02, 2006, 07:36:02 PM »
Shorter than you thought, huh Mick?

Mick, do you understand what Kerry was talking about earlier? 

.......

Yep, what he was talking about was a type of filter for selection, to use these filters you would use the PromptXXXXXOptions classes to set these options 'if' available. I don't see the AddAllowedClass property in the PromptNestedEntityOptions so the way you are doing it is porbably the best way to go...for now.
Thanks Mick, I will commit this info to the grey matter.
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 #22 on: August 02, 2006, 07:39:37 PM »
That's ok, but don't take my word for it, consult the doc's. While somewhere inside my grey matter it can put it together, putting it on paper isn't always so successful ;)
« Last Edit: August 02, 2006, 07:42:06 PM by MickD »
"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: Selecting the right entity.
« Reply #23 on: August 02, 2006, 09:05:38 PM »
Tim.
This was the Method I was referring to for the PromptEntityOptions Class

... looks like they are not available for the PromptNestedEntityOptions Class
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.

TonyT

  • Guest
Re: Selecting the right entity.
« Reply #24 on: August 02, 2006, 10:18:02 PM »
Tim.
This was the Method I was referring to for the PromptEntityOptions Class

... looks like they are not available for the PromptNestedEntityOptions Class

Not really a big deal. You can easily roll your own version of
what AddAllowedClass() and SetRejectMessage() does.
In fact, I did just that before R17 came along.

The code below is from MTextFragmentSample.cs available
on caddzone.com. It does essentially the same thing the
aforementioned methods do, 'cept it also automates the
generation of the 'reject message'.

Should be fairly trivial to do a variant (SelectNestedObject?)
that also supported the automated entity type validation,
with the added benefit that it works on on R16 later.

Another possible enhancement I've pondered by never
got around to doing, is to use the errno system variable
to distinguish a 'missed' pick, and retry.

The more important message here, for Tim and other
learners, is to keep in mind that the APIs and .NET
make it relatively simple to 'roll your own' reusable
solutions for common functionality needs like this.

So, don't look at what the AutoCAD API's do or do
not offer, and assume you're stuck with that and only
that.  If you keep in mind that much of the managed
ObjectARX API is actually implemented in managed
code (as opposed to native code), it becomes clear
that you can solve many problems and/or address
deficiencies in the API yourself, and do it in ways that
are highly reusable.

Code: [Select]

;; Excerpt from http://www.caddzone.com/MTextFragmentSample.cs

static ObjectId SelectObject(string prompt, System.Type objectClass)
{
   Editor e = AcadApp.DocumentManager.MdiActiveDocument.Editor;
   using ( Transaction tr = e.Document.Database.TransactionManager.StartTransaction() )
   {
      while( true )
      {
         PromptEntityResult res = e.GetEntity(prompt);
         if( res.Status != PromptStatus.OK )
            return ObjectId.Null;
         DBObject ob = tr.GetObject(res.ObjectId, OpenMode.ForRead);
         if( objectClass.IsAssignableFrom(ob.GetType()) )
            return res.ObjectId;
         e.WriteMessage("\nInvalid selection, {0} entity expected",
            RXClass.GetClass(objectClass).DxfName);
      }
   }
}


MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Selecting the right entity.
« Reply #25 on: August 02, 2006, 11:17:15 PM »
Hi Tony, great tip and welcome aboard!
I've used something 'similar' in a very broad sense of the word (i.e., a hack) and used 'is' and such in a while loop to keep going until the right type was selected but your example is much more re-usable and concise. Definitely a keeper!
Thanks.
"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

LE

  • Guest
Re: Selecting the right entity.
« Reply #26 on: August 03, 2006, 12:19:12 AM »
Thank you and welcome Tony;

Well since I am starting playing with C# (and do not have installed the IDE here at home), I have a question how the nested object is selected? or it is just a code skeleton to follow? (I see there is more than one question)

To compare with ObjectARX I have done in there something like this (very academic):

Code: [Select]
  static void SelectAttribute(void)
  {
  Acad::ErrorStatus es;
  ads_point ptres;
  ads_name entres;
  ads_matrix xformres;
  resbuf *refstkres = NULL;
  int ret = acedNEntSelP("\nSelect attribute: ", entres, ptres, FALSE, xformres, &refstkres);
  if (ret == RTCAN) return;
  if (ret == RTNORM)
  {
  AcDbObjectId objId;
  acdbGetObjectId (objId, entres);
  AcDbObjectPointer<AcDbEntity> pEnt(objId, AcDb::kForRead);
  if ((es = pEnt.openStatus ()) != Acad::eOk) {
  acutPrintf("\nError on open object %s", acadErrorStatusText(es));
  return;
  }
  AcDbAttribute *pAttribute = NULL;
  if (AcDbAttribute::cast(pEnt.object()))
  {
  acutPrintf("\nAttribute selected!.");
  pAttribute = static_cast<AcDbAttribute *>(pEnt.object());
  acutPrintf("\nTag %s", pAttribute->tag());
  acutPrintf("\nValue %s", pAttribute->textString());
  }
  }
  }

And have seen a nest selection done in C# with some code like the following:

Code: [Select]
    AcDb.Database db = AcAp.Application.DocumentManager.MdiActiveDocument.Database;
    AcEd.Editor ed = AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
    AcEd.PromptEntityOptions entOpts = new AcEd.PromptEntityOptions("\nMy Selection:");
    AcEd.PromptEntityResult entRes ;
    AcDb.TransactionManager tm = db.TransactionManager;
    while (true) {
        using (AcDb.Transaction tr = tm.StartTransaction()) {
            AcDb.ObjectId objId;
            entRes = ed.GetEntity(entOpts);
            if (entRes.Status != AcEd.PromptStatus.OK) break;
            objId = entRes.ObjectId;
            AcEd.PromptNestedEntityOptions neOpts = new AcEd.PromptNestedEntityOptions("");
            neOpts.UseNonInteractivePickPoint = true;
            neOpts.NonInteractivePickPoint = entRes.PickedPoint;
            AcEd.PromptNestedEntityResult neRes = ed.GetNestedEntity(neOpts);
            if (neRes.Status == AcEd.PromptStatus.OK) objId = neRes.ObjectId;
            AcDb.Entity ent = (AcDb.Entity)tm.GetObject(objId, AcDb.OpenMode.ForRead);
            ed.WriteMessage("\n" + ent.ToString());
            tr.Commit();
        }
    }

Thanks.
« Last Edit: August 03, 2006, 11:16:53 AM by LE »

TR

  • Guest
Re: Selecting the right entity.
« Reply #27 on: August 03, 2006, 01:07:56 AM »
Nope you don't need that.... it will allow you to open .SLN (solutions as they are in VS)
SD2 certianly does need the .NET 2.0 Framework installed. From http://www.icsharpcode.net/OpenSource/SD/Download/
Quote
Notes

    * You need to have at least the .NET 2.0 runtime installed on your machine. The SDK is optional but recommended.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Selecting the right entity.
« Reply #28 on: August 03, 2006, 08:44:54 AM »
.....................  it becomes clear
that you can solve many problems and/or address
deficiencies in the API yourself, and do it in ways that
are highly reusable.

Thanks for your insight Tony.
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.

LE

  • Guest
Re: Selecting the right entity.
« Reply #29 on: August 03, 2006, 09:19:19 AM »
SD2 certianly does need the .NET 2.0 Framework installed.

Thanks Tim.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Selecting the right entity.
« Reply #30 on: August 03, 2006, 11:11:56 AM »
Thanks Tony, happy to see you here, for the code and explanation.  One question I have is; Can you set .Net (C#) code up the way you can lisp?  Mean you load toolbox type codes, from one dll, and call it from another?  If so, is it the same idea as lisp, or other programming languages?

SD2 certianly does need the .NET 2.0 Framework installed.

Thanks Tim.
Thanks for the info Tim, and thanks Luis for trying.

Thanks Luis for the code also.  Something more to study.

And thanks to everyone else, for all the information stored here.  It is a lot, and feels good to start understand it.
Tim

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

Please think about donating if this post helped you.

TonyT

  • Guest
Re: Selecting the right entity.
« Reply #31 on: August 03, 2006, 02:36:01 PM »
Thanks Tony, happy to see you here, for the code and explanation.  One question I have is; Can you set .Net (C#) code up the way you can lisp?  Mean you load toolbox type codes, from one dll, and call it from another?  If so, is it the same idea as lisp, or other programming languages?

Thanks Tim.

Yes and no :-)

You can create reusable library code in .NET, but it must be
wrapped in a class and accessed via the class. In other words
there are no 'global' variables or functions like in LISP.

There are 'global' objects, which can have methods (both
static and instance), and you can access those methods
from another assembly. To do that, you reference the
assembly that contains your objects, and then you can
call the methods of the object from code in the referencing
assembly.

However, if you're just learning the basics of .NET and OOP,
I would consider putting this aside until you first have a
good grasp of the basic principles of OOP, as this is the
most radical difference between .NET and LISP or VBA.

In other words, until you're able to develop working code
that can do something useful, you should probably keep
all of it in a single assembly :-)

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Selecting the right entity.
« Reply #32 on: August 03, 2006, 02:58:28 PM »
Thanks Tony.
Tim

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

Please think about donating if this post helped you.

TonyT

  • Guest
Re: Selecting the right entity.
« Reply #33 on: August 04, 2006, 04:10:08 PM »
Thank you and welcome Tony;

Well since I am starting playing with C# (and do not have installed the IDE here at home), I have a question how the nested object is selected? or it is just a code skeleton to follow? (I see there is more than one question)


Hi Luis.

I think you answered your own question.

Nested entity selection is done with the GetNestedEntity() method of the Editor.

LE

  • Guest
Re: Selecting the right entity.
« Reply #34 on: August 04, 2006, 04:50:18 PM »
Nested entity selection is done with the GetNestedEntity() method of the Editor.

Got it, thanks Tony.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Selecting the right entity.
« Reply #35 on: August 05, 2006, 05:17:10 AM »

;; Excerpt from http://www.caddzone.com/MTextFragmentSample.cs

static ObjectId SelectObject(string prompt, System.Type objectClass)
{
   Editor e = AcadApp.DocumentManager.MdiActiveDocument.Editor;
   using ( Transaction tr = e.Document.Database.TransactionManager.StartTransaction() )
   {
      while( true )
      {
         PromptEntityResult res = e.GetEntity(prompt);
         if( res.Status != PromptStatus.OK )
            return ObjectId.Null;
         DBObject ob = tr.GetObject(res.ObjectId, OpenMode.ForRead);
         if( objectClass.IsAssignableFrom(ob.GetType()) )
            return res.ObjectId;
         e.WriteMessage("\nInvalid selection, {0} entity expected",
            RXClass.GetClass(objectClass).DxfName);
      }
   }
}

Nice construct .. I blinked when I first saw  while( true ) though :-)
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 #36 on: August 05, 2006, 02:48:55 PM »

;; Excerpt from http://www.caddzone.com/MTextFragmentSample.cs

static ObjectId SelectObject(string prompt, System.Type objectClass)
{
   Editor e = AcadApp.DocumentManager.MdiActiveDocument.Editor;
   using ( Transaction tr = e.Document.Database.TransactionManager.StartTransaction() )
   {
      while( true )
      {
         PromptEntityResult res = e.GetEntity(prompt);
         if( res.Status != PromptStatus.OK )
            return ObjectId.Null;
         DBObject ob = tr.GetObject(res.ObjectId, OpenMode.ForRead);
         if( objectClass.IsAssignableFrom(ob.GetType()) )
            return res.ObjectId;
         e.WriteMessage("\nInvalid selection, {0} entity expected",
            RXClass.GetClass(objectClass).DxfName);
      }
   }
}

Nice construct .. I blinked when I first saw  while( true ) though :-)
I still blink, but hopefully soon I will understand why.
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 #37 on: August 05, 2006, 05:01:06 PM »
Hi Tim,

The while (true)  would appear to run forever.  .. but the two return options are the escape routes.

... while is more usually seen with a more conventional conditional.
This is a nice way to handle a "keep asking untill one of several conditions are met" situation.
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 #38 on: August 06, 2006, 03:07:01 PM »
Hi Tim,

The while (true)  would appear to run forever.  .. but the two return options are the escape routes.

... while is more usually seen with a more conventional conditional.
This is a nice way to handle a "keep asking untill one of several conditions are met" situation.
Thanks Kerry.  I think it threw me because it is different from lisp, but with this explaination, I think I'm closer to understand it.  I haven't looked to hard at it yet, just because I don't know enough of the lanague yet.
Tim

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

Please think about donating if this post helped you.

TonyT

  • Guest
Re: Selecting the right entity.
« Reply #39 on: August 06, 2006, 10:57:56 PM »

;; Excerpt from http://www.caddzone.com/MTextFragmentSample.cs

static ObjectId SelectObject(string prompt, System.Type objectClass)
{
   Editor e = AcadApp.DocumentManager.MdiActiveDocument.Editor;
   using ( Transaction tr = e.Document.Database.TransactionManager.StartTransaction() )
   {
      while( true )
      {
         PromptEntityResult res = e.GetEntity(prompt);
         if( res.Status != PromptStatus.OK )
            return ObjectId.Null;
         DBObject ob = tr.GetObject(res.ObjectId, OpenMode.ForRead);
         if( objectClass.IsAssignableFrom(ob.GetType()) )
            return res.ObjectId;
         e.WriteMessage("\nInvalid selection, {0} entity expected",
            RXClass.GetClass(objectClass).DxfName);
      }
   }
}

Nice construct .. I blinked when I first saw  while( true ) though :-)
I still blink, but hopefully soon I will understand why.

Hi Tim.  The while( true ) {...} business might cause a LISP programmer to blink since LISP (or at least the dialect we use) has no control-transfer 'statements' other than (exit), which takes you to the end of your program (that is, unless you handle it with vl-catch-xxxxx functions).

In most imperative languages, there are a number of control transfer statements (e.g., 'return'; 'break' and (gasp) 'GOTO' are found C, C# and C++), and additionally 'Exit <control_loop_statement>' in VB, like 'Exit For'.

These statements allow you to exit a loop from any point within it, without the need for a potentially complex (and IMHO, confusing) looping condition, like we often see in LISP.

Let us know when you start scratching your head over try/catch/finallly :-)

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Selecting the right entity.
« Reply #40 on: August 07, 2006, 11:17:10 AM »

;; Excerpt from http://www.caddzone.com/MTextFragmentSample.cs

static ObjectId SelectObject(string prompt, System.Type objectClass)
{
   Editor e = AcadApp.DocumentManager.MdiActiveDocument.Editor;
   using ( Transaction tr = e.Document.Database.TransactionManager.StartTransaction() )
   {
      while( true )
      {
         PromptEntityResult res = e.GetEntity(prompt);
         if( res.Status != PromptStatus.OK )
            return ObjectId.Null;
         DBObject ob = tr.GetObject(res.ObjectId, OpenMode.ForRead);
         if( objectClass.IsAssignableFrom(ob.GetType()) )
            return res.ObjectId;
         e.WriteMessage("\nInvalid selection, {0} entity expected",
            RXClass.GetClass(objectClass).DxfName);
      }
   }
}

Nice construct .. I blinked when I first saw  while( true ) though :-)
I still blink, but hopefully soon I will understand why.

Hi Tim.  The while( true ) {...} business might cause a LISP programmer to blink since LISP (or at least the dialect we use) has no control-transfer 'statements' other than (exit), which takes you to the end of your program (that is, unless you handle it with vl-catch-xxxxx functions).

In most imperative languages, there are a number of control transfer statements (e.g., 'return'; 'break' and (gasp) 'GOTO' are found C, C# and C++), and additionally 'Exit <control_loop_statement>' in VB, like 'Exit For'.

These statements allow you to exit a loop from any point within it, without the need for a potentially complex (and IMHO, confusing) looping condition, like we often see in LISP.

Let us know when you start scratching your head over try/catch/finallly :-)

Thanks Tony.  I think I understand it a little more now.

I have also seen the 'return' statement in other's code, but haven't read up on it yet.  That one is different, but looks useful.

These answers are very good information for me to know! very helpful.  Thanks all.
Tim

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

Please think about donating if this post helped you.

KaS

  • Guest
Re: Selecting the right entity.
« Reply #41 on: November 18, 2010, 05:39:19 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.


--- there is no AddAllowedClass method for PromptNestedEntityOptions. Is there any other way to check for a right entity selection?

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Selecting the right entity.
« Reply #42 on: November 19, 2010, 07:21:27 PM »
PromptNestedEntityResult pnr=ed.GetNestedEntity("\nPick a line");
            if (pnr.Status != PromptStatus.OK) return;
            if(pnr.ObjectId.ObjectClass.DxfName!="LINE")
            {
                MessageBox.Show("Must be a line:");
                return;
            }