Author Topic: CommandLine Class (Tonys)  (Read 14796 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: CommandLine Class (Tonys)
« Reply #15 on: April 16, 2007, 12:50:41 AM »
From a quick read, the 'static Commandline()' method is not actually a constructor in the sense we know of.

The constructor is defined as an instance initializer which is named .ctor in the IL. 

The 'static Commanline()' is a type initializer which is named .cctor in the IL. Type initializers are called static constructors in the C++/CLI and C# languages.
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.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8702
  • AKA Daniel
Re: CommandLine Class (Tonys)
« Reply #16 on: April 16, 2007, 02:58:46 AM »
Neato, I learned something new today   :-D

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: CommandLine Class (Tonys)
« Reply #17 on: April 16, 2007, 03:04:14 AM »
Neato, I learned something new today   :-D

:-D
I've had a good day in that regard as well !
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: CommandLine Class (Tonys)
« Reply #18 on: April 17, 2007, 12:10:40 AM »
Hi Luis.

The Command() method is supposed to be
the static contstructor, but I changed the
name of the class at the last minute, and
forget to also change the name of this too.

To make it work do the following:

Quote
static Commands()

Change to:

Quote
static void Commands()

or you can make your Hashtable inside of TypedValueFromObject() like:

Quote
static TypedValue TypedValueFromObject(Object val)
{
    Hashtable ResTypes = new Hashtable();
    ResTypes.Clear();
// with the following too or as it is in the original below
    ////ResTypes.Add(typeof(string), RTSTR);
    ////ResTypes.Add(typeof(double), RTREAL);
    ////ResTypes.Add(typeof(Point3d), RT3DPOINT);
    ////ResTypes.Add(typeof(ObjectId), RTENAME);
    ////ResTypes.Add(typeof(Int32), RTLONG);
    ////ResTypes.Add(typeof(Int16), RTSHORT);
    ////ResTypes.Add(typeof(Point2d), RTPOINT);
    ResTypes[typeof(string)] = RTSTR;
    ResTypes[typeof(double)] = RTREAL;
    ResTypes[typeof(Point3d)] = RT3DPOINT;
    ResTypes[typeof(ObjectId)] = RTENAME;
    ResTypes[typeof(Int32)] = RTLONG;
    ResTypes[typeof(Int16)] = RTSHORT;
    ResTypes[typeof(Point2d)] = RTPOINT;

    object o = ResTypes[val.GetType()];

    if (o == null)
        throw new InvalidOperationException("Unsupported type in Command() method");
    return new TypedValue(o.GetHashCode(), val); //((int)o, val);
}

And as the code above use GetHashCode() instead of the (int) cast.
« Last Edit: April 17, 2007, 12:17:22 AM by TonyT »

TonyT

  • Guest
Re: CommandLine Class (Tonys)
« Reply #19 on: April 17, 2007, 12:16:20 AM »
Sorry all, I renamed the class at the last minute, from
'Commands' to 'CommandLine', and forget to rename
the static constructor.

:-o

Thanks to Kerry for stumbling over that, as I didn't
know that it was broken.

Updated verison .cs and .vb versions are on my
website now, and hopefully they work.

Thanks guys ..I made some time to test this.

I believe you were correct first time Daniel ..
I made it a static constructor ;
static CommandLine()
{
...
}

and

changed
return new TypedValue((int) o, val)
to
return new TypedValue(Convert.ToInt32(o), val);
because of a casting error ..

and got back to where I started .. loading a CUI menu ..
Code: [Select]
        //
        //
        [CommandMethod("Test_ZE")]
        public void test_ze()
        {
            CommandLine.ZoomExtents();
        }
        [CommandMethod("Test_LoadDbCUI")]
        public void test_Laconic()
        {
           string MenuQualifiedName = "K:\\DbCon.cui";
           string MenuGroupName = "DBCONNECT";
           CommandLine.Command(
                "FileDia", "0",
                "CuiUnLoad", MenuGroupName,
                "CuiLoad", MenuQualifiedName,
                "FileDia", "1"
                );
        }

.. and thanks to Tony for the original.


Quote
Command: Test_LoadDbCUI
Customization file unloaded successfully. Customization Group: DBCONNECT
Customization file loaded successfully. Customization Group: DBCONNECT
Command:

DUH: fat finger spelling !

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: CommandLine Class (Tonys)
« Reply #20 on: April 17, 2007, 12:53:15 AM »
Thanks for confirming that Tony.

I notice you've changed the TypedValueFromObject method to more refined code.

I'm still getting an InvalidCastException using the (int).... cast though.
When I use the Convert.ToInt32(ResTypes[t]) the methos executes without a qualm.

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: CommandLine Class (Tonys)
« Reply #21 on: April 17, 2007, 10:37:29 AM »
Thanks again. 

The only change I needed to make was to change the
cast from (int) to (short).

I posted an updated version that should work now.

Thanks for confirming that Tony.

I notice you've changed the TypedValueFromObject method to more refined code.

I'm still getting an InvalidCastException using the (int).... cast though.
When I use the Convert.ToInt32(ResTypes[t]) the methos executes without a qualm.



LE

  • Guest
Re: CommandLine Class (Tonys)
« Reply #22 on: April 17, 2007, 10:41:55 AM »
Thanks again. 

The only change I needed to make was to change the
cast from (int) to (short).

I posted an updated version that should work now.

Have not seen your updated code Tony, but before knowing the previous code status, I added the use of GetHashCode() in your TypedValueFromObject() function and the function samples were working without any
exceptions, in this case it is better to use the cast?

Thanks.

TonyT

  • Guest
Re: CommandLine Class (Tonys)
« Reply #23 on: April 18, 2007, 02:16:37 AM »
Luis - Sorry, but I'm not really sure I understand what
you mean by 'use of GetHashCode()'...

Perhaps you can post the subject code and
it might be clearer.


Thanks again. 

The only change I needed to make was to change the
cast from (int) to (short).

I posted an updated version that should work now.

Have not seen your updated code Tony, but before knowing the previous code status, I added the use of GetHashCode() in your TypedValueFromObject() function and the function samples were working without any
exceptions, in this case it is better to use the cast?

Thanks.

TonyT

  • Guest
Re: CommandLine Class (Tonys)
« Reply #24 on: April 18, 2007, 03:41:32 AM »
Out of what transpired in a thread on Autodesk's .NET
newsgroup, I made a minor enhancement to this class,
that allows you to set a static property, that when set
to true, causes the Command() method to automatically
insert "_non" into the command list, in front of Point3d
or Point2d arguments, to supress running osnaps.

I posted the update just now

Thanks for confirming that Tony.

I notice you've changed the TypedValueFromObject method to more refined code.

I'm still getting an InvalidCastException using the (int).... cast though.
When I use the Convert.ToInt32(ResTypes[t]) the methos executes without a qualm.



Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: CommandLine Class (Tonys)
« Reply #25 on: April 18, 2007, 05:15:07 AM »
Yes, I noticed that thread this morning.
I was thinking at the time that all thats needed now is nil and T  :-)

Thanks again Tony, this has been a good educator.
« Last Edit: April 18, 2007, 05:22:46 AM by Kerry Brown »
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: CommandLine Class (Tonys)
« Reply #26 on: April 18, 2007, 10:45:50 AM »
Luis - Sorry, but I'm not really sure I understand what
you mean by 'use of GetHashCode()'...

Perhaps you can post the subject code and
it might be clearer.

This one Tony;

In your previous code I tested with:

Code: [Select]
static TypedValue TypedValueFromObject(Object val)
{
    object o = ResTypes[val.GetType()];
    if (o == null)
        throw new InvalidOperationException("Unsupported type in Command() method");
   return new TypedValue(o.[color=blue]GetHashCode[/color](), val); //((int)o, val); <<====
 }

And your class works without a problem (testing the functions you have as sample there).

Thanks!

TonyT

  • Guest
Re: CommandLine Class (Tonys)
« Reply #27 on: April 18, 2007, 08:53:28 PM »
Luis - We don't use GetHashCode() to convert an object
to an integer type, we use typecasting or Convert.

My code works fine if the cast is to (short) rather than (int).

Luis - Sorry, but I'm not really sure I understand what
you mean by 'use of GetHashCode()'...

Perhaps you can post the subject code and
it might be clearer.

This one Tony;

In your previous code I tested with:

Code: [Select]
static TypedValue TypedValueFromObject(Object val)
{
    object o = ResTypes[val.GetType()];
    if (o == null)
        throw new InvalidOperationException("Unsupported type in Command() method");
   return new TypedValue(o.[color=blue]GetHashCode[/color](), val); //((int)o, val); <<====
 }

And your class works without a problem (testing the functions you have as sample there).

Thanks!

LE

  • Guest
Re: CommandLine Class (Tonys)
« Reply #28 on: April 18, 2007, 09:15:43 PM »
Quote
We don't use GetHashCode() to convert an object
to an integer type, we use typecasting or Convert.

My code works fine if the cast is to (short) rather than (int).

Thank you Tony

It make sense now... strange that was working here, but now I get it... :)

TonyT

  • Guest
Re: CommandLine Class (Tonys)
« Reply #29 on: April 19, 2007, 08:40:05 AM »
Just updated it again.

Another 'enhancement' is that it can automatically
transform coordinate input from WCS to current UCS.

Yes, I noticed that thread this morning.
I was thinking at the time that all thats needed now is nil and T  :-)

Thanks again Tony, this has been a good educator.