TheSwamp

Code Red => .NET => Topic started by: nobody on August 14, 2017, 10:27:15 PM

Title: Check if Nested or Not
Post by: nobody on August 14, 2017, 10:27:15 PM
Want people to select an entity (polylines/lines/arcs) whether it's in an xref or not.  I can't think of any other way except prompting for a point "near" then testing to see if it's nested if it returns a valid object ID. Is there a better way? 
Title: Re: Check if Nested or Not
Post by: Jeff H on August 15, 2017, 01:42:43 AM
Not sure I follow exactly what your asking and why you need to prompt for a point near it but here is an example where command prompts for selecting an object in an xref then opens the xrefs and zooms to it.

It checks the ObjectId.Database.UnmangedObject matches the current Database.UnmanagedObject.
Code - C#: [Select]
  1.         private static Handle? hnd = null;
  2.         [CommandMethod("zoomxref")]
  3.         public void zoomxref()
  4.         {
  5.             hnd = null;
  6.             var pneo = new PromptNestedEntityOptions("\nSelect xref object");
  7.             var pner = Ed.GetNestedEntity(pneo);
  8.             if (pner.Status != PromptStatus.OK)
  9.             {
  10.                 return;
  11.             }
  12.  
  13.             var id = pner.ObjectId;
  14.             if (id.Database.UnmanagedObject == Db.UnmanagedObject)
  15.             {
  16.                 Ed.WriteLine("Select xref object");
  17.                 return;
  18.             }
  19.  
  20.             foreach (ObjectId conId in pner.GetContainers())
  21.             {
  22.                 if (conId.Database.UnmanagedObject == pner.ObjectId.Database.UnmanagedObject)
  23.                 {
  24.                     id = conId;
  25.                 }
  26.  
  27.             }
  28.  
  29.             using (Transaction trx = Doc.TransactionManager.StartTransaction())
  30.             {
  31.                 var ent = id.GetEntity();
  32.                 hnd = ent.Handle;
  33.                 trx.Commit();
  34.             }
  35.  
  36.             var doc = Application.DocumentManager.Open(pner.ObjectId.Database.Filename, false);
  37.             doc.SendStringToExecute("zoomxrefobject ", true, false, false);
  38.  
  39.         }
  40.  
  41.  
  42.         [CommandMethod("zoomxrefobject", CommandFlags.NoHistory)]
  43.         public void zoomxrefobject()
  44.         {
  45.             if (!hnd.HasValue) return;
  46.             var id = Db.GetObjectId(false, (Handle)hnd, 0);
  47.             var ss = SelectionSet.FromObjectIds(new ObjectId[] { id });
  48.             Ed.Command("_Zoom", "_Object", ss, "");
  49.         }
  50.  
  51.  
  52.  
Title: Re: Check if Nested or Not
Post by: nobody on August 15, 2017, 09:37:36 PM
Thanks!
Title: Re: Check if Nested or Not
Post by: nobody on August 18, 2017, 10:07:35 PM
Strangly enough...I did not think to even try at first...but the prompt nested entity thing worked for my object that wasn't an xref or block XD  Had no idea it would bahhahaa
Title: Re: Check if Nested or Not
Post by: Keith Brown on August 21, 2017, 08:43:07 AM
Jeff,


Can you explain why you would use the UnmanagedObject property of the Database vs just comparing Databases?  Is it more correct?
Title: Re: Check if Nested or Not
Post by: Jeff H on August 21, 2017, 09:41:42 AM
Jeff,


Can you explain why you would use the UnmanagedObject property of the Database vs just comparing Databases?  Is it more correct?
I am trying to remember why.
I will look more into later day and see why I did that. I think looking at "==" in reflector it had a heavy-handed way of checking.
Title: Re: Check if Nested or Not
Post by: Keith Brown on August 21, 2017, 09:48:46 AM
i tried looking at the database object in reflector but I couldnt find the == operator.  I will try to check more thoroughly this time.
Title: Re: Check if Nested or Not
Post by: Jeff H on August 21, 2017, 04:58:10 PM
It looks like it inherits it from DisposableWrapper and pretty much does the same thing?
Code - C#: [Select]
  1. [return: MarshalAs(UnmanagedType.U1)]
  2. public static bool operator ==(DisposableWrapper a, DisposableWrapper b)
  3. {
  4.     if (a == null)
  5.     {
  6.         return (b == null);
  7.     }
  8.     return a.Equals(b);
  9. }
  10.  
  11.  

Code - C#: [Select]
  1. [return: MarshalAs(UnmanagedType.U1)]
  2. public override bool Equals(object obj)
  3. {
  4.     DisposableWrapper wrapper = obj as DisposableWrapper;
  5.     if (wrapper == null)
  6.     {
  7.         return false;
  8.     }
  9.     GC.KeepAlive(this);
  10.     IntPtr unmanagedObject = wrapper.UnmanagedObject;
  11.     return (this.m_imp == unmanagedObject);
  12. }
  13.  
  14.  

I thought there was a reason but maybe not.
Title: Re: Check if Nested or Not
Post by: Keith Brown on August 22, 2017, 08:37:45 AM
Thanks.  I just wanted to make sure that I was not missing anything.
Title: Re: Check if Nested or Not
Post by: Jeff H on August 22, 2017, 09:16:26 AM
Thanks.  I just wanted to make sure that I was not missing anything.

Ahhh "==" calls Equals which takes an object and would not make much of a difference in this example but when comparing a bunch of objects the casting could be a performance issue.
I think there is a thread here where Tony pointed that out.