Author Topic: Accessing OwnerID of Nested Entity with variable type  (Read 3814 times)

0 Members and 1 Guest are viewing this topic.

SteveK

  • Guest
Accessing OwnerID of Nested Entity with variable type
« on: February 09, 2010, 08:09:01 PM »
Hi,

I have an open nested object. If I don't know the type of the object, how do I find out it's owner?

Put another way, I'm using GetNestedEntity() to select an attribute, however if an attribute is not selected but a part of a block is, I want to be able to access the block object.

Thanks,

Steve


It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8764
  • AKA Daniel
Re: Accessing OwnerID of Nested Entity with variable type
« Reply #1 on: February 09, 2010, 08:23:40 PM »
use the method GetContainers in PromptNestedEntityResult


Code: [Select]
void Commands::doit()
  {
    Editor ^pEditor = Application::DocumentManager->MdiActiveDocument->Editor;
    Database ^pDatabase = HostApplicationServices::WorkingDatabase;
    try
    {
      PromptNestedEntityResult ^pr = pEditor->GetNestedEntity("\nSelect Nested Ent: ");
      if(pr->Status == PromptStatus::OK)
      {
        for each(ObjectId id in pr->GetContainers())
        {
          pEditor->WriteMessage("\n{0}",id);
        }
      }
    }
    catch (System::Exception ^ex)
    {
      pEditor->WriteMessage("(\n:{0}\n:{1})",ex->Message,ex->StackTrace);
    }
  }
« Last Edit: February 09, 2010, 08:26:44 PM by Daniel »

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8764
  • AKA Daniel
Re: Accessing OwnerID of Nested Entity with variable type
« Reply #2 on: February 09, 2010, 08:25:27 PM »
I'm not sure of the order, but I think the first container is the first owner

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8764
  • AKA Daniel
Re: Accessing OwnerID of Nested Entity with variable type
« Reply #3 on: February 09, 2010, 08:35:35 PM »
Sorry, to get the block from a blockref use the BlockTableRecord property
BlockReference::BlockTableRecord
 

SteveK

  • Guest
Re: Accessing OwnerID of Nested Entity with variable type
« Reply #4 on: February 09, 2010, 09:06:18 PM »
Thanks Daniel  :-)

Because getContainers is plural I'm not too sure what other objects it might contain, so if I'm just after the highest blockreference would I be safe in doing this?
Code: [Select]
foreach (ObjectId id in pner.GetContainers())
{
Object openedObj = acTrans.GetObject(id, OpenMode.ForRead);
if ( openedObj is BlockReference ) break;
}
Though I suppose there could be multiple block references right?

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8764
  • AKA Daniel
Re: Accessing OwnerID of Nested Entity with variable type
« Reply #5 on: February 09, 2010, 09:09:15 PM »
just get the first ref  :-)

Code: [Select]
void Commands::doit()
  {
    Editor ^pEditor = Application::DocumentManager->MdiActiveDocument->Editor;
    Database ^pDatabase = HostApplicationServices::WorkingDatabase;
    AcDb::TransactionManager ^pManager = pDatabase->TransactionManager;
    try
    {
      PromptNestedEntityResult ^pr = pEditor->GetNestedEntity("\nSelect Nested Ent: ");
      if(pr->Status == PromptStatus::OK)
      {
        array<ObjectId> ^Owners = pr->GetContainers();
        if(Owners->Length > 0)
        {
          Transaction ^pTransaction = pManager->StartTransaction();
          try
          {
            BlockReference ^pBlockReference = nullptr;

            for each(ObjectId id in Owners)
            {
               pBlockReference = dynamic_cast<BlockReference^>
               (pTransaction->GetObject(id, OpenMode::ForRead));
               if(pBlockReference != nullptr)
                 break;
            }

            if(pBlockReference == nullptr)
              throw gcnew System::NullReferenceException("pBlockReference is null");

            BlockTableRecord ^pBlockTableRecord = dynamic_cast<BlockTableRecord^>
              (pTransaction->GetObject(pBlockReference->BlockTableRecord, OpenMode::ForRead));

            pEditor->WriteMessage("\n{0}",pBlockTableRecord->Name);
            pTransaction->Commit();
          }
          finally
          {
            delete pTransaction;
          }
        }
      }
    }
    catch (System::Exception ^ex)
    {
      pEditor->WriteMessage("(\n:{0}\n:{1})",ex->Message,ex->StackTrace);
    }
  }

fixed
« Last Edit: February 09, 2010, 09:22:00 PM by Daniel »

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8764
  • AKA Daniel
Re: Accessing OwnerID of Nested Entity with variable type
« Reply #6 on: February 09, 2010, 09:12:46 PM »
Thanks Daniel  :-)

Because getContainers is plural I'm not too sure what other objects it might contain, so if I'm just after the highest blockreference would I be safe in doing this?
Code: [Select]
foreach (ObjectId id in pner.GetContainers())
{
Object openedObj = acTrans.GetObject(id, OpenMode.ForRead);
if ( openedObj is BlockReference ) break;
}
Though I suppose there could be multiple block references right?

that should work

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8764
  • AKA Daniel
Re: Accessing OwnerID of Nested Entity with variable type
« Reply #7 on: February 09, 2010, 09:15:26 PM »
Doh, Mine would fail if you selected a polyline segment in a block, so you do need to loop though until you hit the first blockref 

SteveK

  • Guest
Re: Accessing OwnerID of Nested Entity with variable type
« Reply #8 on: February 09, 2010, 09:19:03 PM »
Ah ok great, I'll loop then. Thanks a lot.  :-)

fixo

  • Guest
Re: Accessing OwnerID of Nested Entity with variable type
« Reply #9 on: February 10, 2010, 02:48:18 PM »
just get the first ref  :-)

Code: [Select]
void Commands::doit()
  {
    Editor ^pEditor = Application::DocumentManager->MdiActiveDocument->Editor;
    Database ^pDatabase = HostApplicationServices::WorkingDatabase;
    AcDb::TransactionManager ^pManager = pDatabase->TransactionManager;
    try
    {
      PromptNestedEntityResult ^pr = pEditor->GetNestedEntity("\nSelect Nested Ent: ");
      if(pr->Status == PromptStatus::OK)
      {
        array<ObjectId> ^Owners = pr->GetContainers();
        if(Owners->Length > 0)
        {
          Transaction ^pTransaction = pManager->StartTransaction();
          try
          {
            BlockReference ^pBlockReference = nullptr;

            for each(ObjectId id in Owners)
            {
               pBlockReference = dynamic_cast<BlockReference^>
               (pTransaction->GetObject(id, OpenMode::ForRead));
               if(pBlockReference != nullptr)
                 break;
            }

            if(pBlockReference == nullptr)
              throw gcnew System::NullReferenceException("pBlockReference is null");

            BlockTableRecord ^pBlockTableRecord = dynamic_cast<BlockTableRecord^>
              (pTransaction->GetObject(pBlockReference->BlockTableRecord, OpenMode::ForRead));

            pEditor->WriteMessage("\n{0}",pBlockTableRecord->Name);
            pTransaction->Commit();
          }
          finally
          {
            delete pTransaction;
          }
        }
      }
    }
    catch (System::Exception ^ex)
    {
      pEditor->WriteMessage("(\n:{0}\n:{1})",ex->Message,ex->StackTrace);
    }
  }

fixed

Should not working for me when the attribute selected
How to fix it?

~'J'~

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Accessing OwnerID of Nested Entity with variable type
« Reply #10 on: February 10, 2010, 04:20:49 PM »
PromptNestedEntityResults ( pner ) has an ObjectId property.  Convert that to an entity, then test to see if that entity is an ' AttributeReference ', and if so, then you can grab the ' OwnerId ' property of that entity, and have the block reference.  If the entity is not an ' AttributeReference ', then the first ' id ' in the collection returned by the ' GetContainers ' method of ' pner ' will be the block reference.

Hope that helps.
Tim

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

Please think about donating if this post helped you.

fixo

  • Guest
Re: Accessing OwnerID of Nested Entity with variable type
« Reply #11 on: February 10, 2010, 05:09:11 PM »
PromptNestedEntityResults ( pner ) has an ObjectId property.  Convert that to an entity, then test to see if that entity is an ' AttributeReference ', and if so, then you can grab the ' OwnerId ' property of that entity, and have the block reference.  If the entity is not an ' AttributeReference ', then the first ' id ' in the collection returned by the ' GetContainers ' method of ' pner ' will be the block reference.

Hope that helps.

Thanks, Tim

I feel like a dumb now :)

Regards,

~'J'~

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8764
  • AKA Daniel
Re: Accessing OwnerID of Nested Entity with variable type
« Reply #12 on: February 10, 2010, 07:33:44 PM »
PromptNestedEntityResults ( pner ) has an ObjectId property.  Convert that to an entity, then test to see if that entity is an ' AttributeReference ', and if so, then you can grab the ' OwnerId ' property of that entity, and have the block reference.  If the entity is not an ' AttributeReference ', then the first ' id ' in the collection returned by the ' GetContainers ' method of ' pner ' will be the block reference.

Hope that helps.

Thanks for clarifying that, I was thinking that it might pickup other stuff if a complex entity was selected.


T.Willey

  • Needs a day job
  • Posts: 5251
Re: Accessing OwnerID of Nested Entity with variable type
« Reply #13 on: February 12, 2010, 12:50:20 PM »
Glad I was able to help you guys out.  Doesn't happen much.  :-)

<snip>  I was thinking that it might pickup other stuff if a complex entity was selected.

I haven't seen it done yet, but maybe you have more complex items then what I'm selecting.  I have some code I wrote that I can test ideas one if you want, or I can PM it to you.  Don't want to pass it out yet.  Might think of selling it at one point in time.
Tim

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

Please think about donating if this post helped you.