Author Topic: finding common properties  (Read 3878 times)

0 Members and 1 Guest are viewing this topic.

Chumplybum

  • Newt
  • Posts: 97
finding common properties
« on: January 18, 2009, 10:49:57 PM »
hi all,

i'm writing a routine that gathers data from autocad entities and displays them in a datagridview, after the user selects the entities, the idea is to have the user select the properties that they want to be display (eg. layer name, length, radius)... similiar to quick select in autocad.
Is there a way of populating, say a list, with the common properties from 2 different classes. My current thoughts are either use reflection to find common properties between the two or step my way up the class tree to find a base class that each object inherits from. Currently i'm leaning more towards using reflection, but just wanted to see if there was another or easier way to do this?


any help would be great

cheers, mark

TonyT

  • Guest
Re: finding common properties
« Reply #1 on: January 19, 2009, 03:06:47 AM »
hi all,

i'm writing a routine that gathers data from autocad entities and displays them in a datagridview, after the user selects the entities, the idea is to have the user select the properties that they want to be display (eg. layer name, length, radius)... similiar to quick select in autocad.


Sounds like AutoCAD Data Extraction.  (?)

Have you used it?

Quote

Is there a way of populating, say a list, with the common properties from 2 different classes. My current thoughts are either use reflection to find common properties between the two or step my way up the class tree to find a base class that each object inherits from. Currently i'm leaning more towards using reflection, but just wanted to see if there was another or easier way to do this?


Not exactly sure what you mean, but my interpretation of 'the common
properties from 2 different classes' are the properties of their common
base classes.

So for example, given a Line and an Arc, the 'common properties' are
the properties exposed by their common base classes (Curve, Entity,
and DBObject).

There's also the matter of custom properties that are added to objects
(which are properties that do not actually exist as 'declared' properties),
which are only accessable via the System.ComponentModel namespace
classes, namely TypeDescriptor.

For example, TypeDescriptor.GetProperties( object ) returns all of the
static and dynamic properties for the given object. The result is a
collection of PropertyDescriptors. You can look at the 'PropertyType'
property of each PropertyDescriptor to determine which base class
exposes each property. You can use the PropertyDescriptor's GetValue()
method to get the value of the property for a given instance that you
pass as the argument.

The only question I would have is, that Data Extraction pretty much
does what you describe, and unless you have special requirements you
didn't touch on, it may be worth a look.

There's also a post on ThroughTheInterface that shows how to control
Data Extraction via code, that may also be worth pursing.


It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8722
  • AKA Daniel
Re: finding common properties
« Reply #2 on: January 19, 2009, 11:07:19 AM »
Data Extraction is new in 09 right?

TonyT

  • Guest
Re: finding common properties
« Reply #3 on: January 19, 2009, 01:58:33 PM »
Data Extraction is new in 09 right?

No, it's been there for a while (EATTEXT), but only 2008 or later
supports all properties, rather than just attributes.

systech

  • Guest
Re: finding common properties
« Reply #4 on: January 19, 2009, 03:50:52 PM »
Is there a way of Automating DATAEXTRACTION?

Chumplybum

  • Newt
  • Posts: 97
Re: finding common properties
« Reply #5 on: January 19, 2009, 05:30:34 PM »
thanks tony, i had known about data extraction but thought it was to do with blocks and attributes (didn't know you could pull any property from any object out)

your interpretation is spot on... the functionality i'm trying to get out from finding common properties is similar to the properties palette, where if you select 1 object the properties are displayed for that particular one, where as if you select 2 different types (a line and an arc) you only get properties that are common between the two... i wasn't sure the best way to go about it, whether it was to search for a common base class or use reflection to find common properties? I have a feeling its better to find a common base class, as you may end up having 2 objects with the same property names but differing object types.


thanks again

cheers, mark




It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8722
  • AKA Daniel
Re: finding common properties
« Reply #6 on: January 19, 2009, 08:11:13 PM »
Data Extraction is new in 09 right?

No, it's been there for a while (EATTEXT), but only 2008 or later
supports all properties, rather than just attributes.

Great! Thanks Tony

TonyT

  • Guest
Re: finding common properties
« Reply #7 on: January 19, 2009, 08:24:59 PM »
Well, since the WinForm's property grid does what you need, just use it:

Code: [Select]

using Acad = Autodesk.AutoCAD.ApplicationServices.Application;

public class PropertyHelper
{
   public static PropertyDescriptorCollection GetCommonProperties( object[] selection, bool includeMiscellaneous )
   {
      PropertyGrid grid = new PropertyGrid();
      grid.PropertySort = PropertySort.Alphabetical;
      grid.SelectedObjects = selection;
      PropertyDescriptorCollection result = PropertyDescriptorCollection.Empty;
      if( grid.SelectedGridItem != null && grid.SelectedGridItem.Parent != null )
      {
         GridItem item = grid.SelectedGridItem.Parent;
         List<PropertyDescriptor> list = new List<PropertyDescriptor>();
         foreach( GridItem child in item.GridItems )
         {
            if( child.GridItemType == GridItemType.Property &&
                    ( includeMiscellaneous || child.PropertyDescriptor.Category != "Misc" ) )
               list.Add( child.PropertyDescriptor );
         }
         result = new PropertyDescriptorCollection(list.ToArray());
      }
      grid.Dispose();
      return result;
   }

   [CommandMethod( "TEST" )]
   public static void TestPropertyHelper()
   {
      PromptSelectionOptions options = new PromptSelectionOptions();
      Editor ed = Acad.DocumentManager.MdiActiveDocument.Editor;
      PromptSelectionResult result = ed.GetSelection( options );
      if( result.Status == PromptStatus.OK )
      {
         PropertyDescriptorCollection pdc = null;
         using( Transaction trans = ed.Document.TransactionManager.StartTransaction() )
         {
            ObjectId[] ids = result.Value.GetObjectIds();
            int count = ids.Length;
            DBObject[] objects = new DBObject[count];
            for( int i = 0; i < count; i++ )
               objects[i] = trans.GetObject( ids[i], OpenMode.ForRead );
            pdc = PropertyHelper.GetCommonProperties( objects, false );
            ed.WriteMessage( "\n{0} objects selected, found {1} common properties:\n ", count, pdc.Count );

            foreach( PropertyDescriptor pd in pdc )
               ed.WriteMessage( "\n    {0}.{1}", pd.ComponentType.Name, pd.DisplayName );

            trans.Commit();
         }
      }
   }

}


thanks tony, i had known about data extraction but thought it was to do with blocks and attributes (didn't know you could pull any property from any object out)

your interpretation is spot on... the functionality i'm trying to get out from finding common properties is similar to the properties palette, where if you select 1 object the properties are displayed for that particular one, where as if you select 2 different types (a line and an arc) you only get properties that are common between the two... i wasn't sure the best way to go about it, whether it was to search for a common base class or use reflection to find common properties? I have a feeling its better to find a common base class, as you may end up having 2 objects with the same property names but differing object types.


thanks again

cheers, mark




« Last Edit: January 20, 2009, 07:22:54 PM by TonyT »

TonyT

  • Guest
Re: finding common properties
« Reply #8 on: January 19, 2009, 08:29:31 PM »
PS:

I wouldn't consider two properties exposed by different types,
having the same name and data type as equivalent, because
they may still have completely different meanings.

thanks tony, i had known about data extraction but thought it was to do with blocks and attributes (didn't know you could pull any property from any object out)

your interpretation is spot on... the functionality i'm trying to get out from finding common properties is similar to the properties palette, where if you select 1 object the properties are displayed for that particular one, where as if you select 2 different types (a line and an arc) you only get properties that are common between the two... i wasn't sure the best way to go about it, whether it was to search for a common base class or use reflection to find common properties? I have a feeling its better to find a common base class, as you may end up having 2 objects with the same property names but differing object types.


thanks again

cheers, mark





Chumplybum

  • Newt
  • Posts: 97
Re: finding common properties
« Reply #9 on: January 19, 2009, 10:59:19 PM »
thank you, that is exactly what i am after... i had no idea the propertygrid allowed you to do this sort of stuff

cheers