Author Topic: Check if Nested or Not  (Read 3114 times)

0 Members and 2 Guests are viewing this topic.

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Check if Nested or Not
« 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? 

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Check if Nested or Not
« Reply #1 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.  

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: Check if Nested or Not
« Reply #2 on: August 15, 2017, 09:37:36 PM »
Thanks!

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: Check if Nested or Not
« Reply #3 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

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Check if Nested or Not
« Reply #4 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?
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Check if Nested or Not
« Reply #5 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.

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Check if Nested or Not
« Reply #6 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.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Check if Nested or Not
« Reply #7 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.

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Check if Nested or Not
« Reply #8 on: August 22, 2017, 08:37:45 AM »
Thanks.  I just wanted to make sure that I was not missing anything.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Check if Nested or Not
« Reply #9 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.