TheSwamp
Code Red => .NET => Topic started by: Kerry on April 15, 2007, 10:37:55 AM
-
I've been having a look at Tony's CommandLine class ;
Can someone who's familiar give me a hand please.
I've added using System.Collections; Namespace for the Hashtable class references .. I assume that's correct ?
The bit in the attached piccy has me stumped.
Part of the problem may be that I've never played with Hashtables, but the compiler errors have me scratching my head.
I'll just attach the relevant part .. Any Ideas ??
-
is this a constructor where the class name is Commands? I can't see the class name from your pic
ps where can I see the whole class?
-
Hi Daniel,
The ClassName is CommandLine
I'm reticent about posting the class ..
You'll find it here. http://www.caddzone.com/CommandLine.cs
-
We’d probably better ask the boss but since there is no “return type”
I would say it’s a constructor and should be renamed CommandLine….
But But.. I really have no idea sorry for butting in.. ^-^
-
don't be sorry, I'm glad to have someone sane to talk with at this hour of the night .. :D
-
To make it work do the following:
static Commands()
Change to:
static void Commands()
or you can make your Hashtable inside of TypedValueFromObject() like:
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.
-
Thanks for the comments Luis.
One issue with assuming it's a void method is that it never gets called from inside the class .. so the hashtable would never get filled, unless I misunderstand what's happening .. which IS likely.
How efficient would it be clearing and reloading the hashtable for each call to TypedValueFromObject ?
-
Thanks for the comments Luis.
One issue with assuming it's a void method is that it never gets called from inside the class .. so the hashtable would never get filled, unless I misunderstand what's happening .. which IS likely.
How efficient would it be clearing and reloading the hashtable for each call to TypedValueFromObject ?
I tried to use the code as-is and simple was adding a line in:
public static int Command(params object[] args)
{
Commands();
...
But, it gets filled every time is called, so that's why I used, what I have done in some of my functions, since the hashtable is being clear every time before is filled, and it is a very simple one, I don't think there is a overhead, but wait for the masters...
-
I tested also by using the following to avoid the Clear() call
if (ResTypes.Count == 0)
{
//ed.WriteMessage("\nEmpty ResTypes HatchTable!");
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;
//ed.WriteMessage("\nHatchTable count is {0}", ResTypes.Count);
}
-
How efficient would it be clearing and reloading the hashtable for each call to TypedValueFromObject ?
Correct, that’s why I still think it’s a static constructor.
The static constructor and the static hashtable would be initialized upon loading of the class. Correct?
-
Yes...
For doing that, change:
static void Commands()
To:
public CommandLine()
And test the class again... btw, the use of Clear() is not bad sample at all.
-
I think that would be incorrect Luis, as only a static constructor initializes its object upon loading of the assembly
Change
static Commands()
to
static CommandLine()
,
if you need to make another instance, you would need to add a public to the non static constructor the methods are static
public class myClass
{
public myClass()
{
// Constructor.
}
static myClass()
{
// Initialization code goes here.
// Can only access static members here.
}
//...
}
-
I am using the arx wizard and was following the default constructors it creates, but I checked with the static and works too, here is the class modified.
-
Great, you didn’t need to comment the first constructor.
It would not be the same as constructor overloading.
-
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 ..
//
//
[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.
Command: Test_LoadDbCUI
Customization file unloaded successfully. Customization Group: DBCONNECT
Customization file loaded successfully. Customization Group: DBCONNECT
Command:
DUH: fat finger spelling !
-
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.
-
Neato, I learned something new today :-D
-
Neato, I learned something new today :-D
:-D
I've had a good day in that regard as well !
-
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:
static Commands()
Change to:
static void Commands()
or you can make your Hashtable inside of TypedValueFromObject() like:
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.
-
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 ..
//
//
[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.
Command: Test_LoadDbCUI
Customization file unloaded successfully. Customization Group: DBCONNECT
Customization file loaded successfully. Customization Group: DBCONNECT
Command:
DUH: fat finger spelling !
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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:
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!
-
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:
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!
-
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... :)
-
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.
-
Here I am, 3 years later, just now needing to use this Class of Tony's. First I'd like to say thanks to Tony for making this code available. It works fine, except for the full context that I would like for it to....
Background, Civil3D makes renumbering Curve/Line/Spiral Tag labels a slow and cumbersome task, especially if one wants to renumber matching items to the same number. For instance, my line labels L1, L5, L6, and L12 all have the same bearing & length so I want to renumber all of them to L1. This cannot currently be done using the C3D API's, at least not that I've yet discovered, so I'm using the "EditTagLabels" C3D command and Tony's code to call it.
2 issues I've run into, that I'm not sure how to fix. First is that the edited label's display does not update until after my command ends, which makes it difficult to keep track of which labels have already been changed.
Second, and more of a problem, is that the "EditTagLabels" prompts changes when the new number duplicates an existing number. Which means I need to provide an extra response for the command when a duplicate is found (which will be most of the time, but I still need to account for both scenarios). Is there a way to accommodate for this? These are the 2 lines that I somehow need to be just 1 that works in either case:
CaddZone.ApplicationServices.CommandLine.Command("EditTagNumbers", entRes.PickedPoint, "");
CaddZone.ApplicationServices.CommandLine.Command("EditTagNumbers", entRes.PickedPoint, "c", "");
The biggest hurdle is that I have no way of determining before the command is run which numbers are already in use.
Any hints on either one, or both, of these issues would be greatly appreciated.
Jeff
-
As usual, as soon as I post a question I manage to solve at least a portion of it within minutes....the first problem of the labels not updating was solved by changing where I start/commit the transaction. And now that I can see the labels change, I see that the code still works whether the number exists or not, it just is throwing up a warning that gets bypassed.....
So, it all appears to be working fine now.
-
That happens to me a lot
.. it's the reason I usually sleep on problems before posting .. I think resolving has something to do with verbalising the issue.
I noticed this from Tony last night ,,, looks interesting
http://forums.autodesk.com/t5/NET/New-Sample-Application-Defined-System-Variables/m-p/2738909#U2738909
-
I noticed this from Tony last night ,,, looks interesting
http://forums.autodesk.com/t5/NET/New-Sample-Application-Defined-System-Variables/m-p/2738909#U2738909
Yes it does, thanks for making note of it here.
-
You're welcome Jeff :)
another one of Tony's which I'll reference since he hasn't visited here much recently.
ExtensionApplicationInfo.zip
in this thread :
http://forums.autodesk.com/t5/NET/Netload-help/m-p/2736243/highlight/true#M20202
-
That happens to me a lot
.. it's the reason I usually sleep on problems before posting .. I think resolving has something to do with verbalising the issue.
I noticed this from Tony last night ,,, looks interesting
http://forums.autodesk.com/t5/NET/New-Sample-Application-Defined-System-Variables/m-p/2738909#U2738909
Goes bang. 'Autodesk.AutoCAD.Runtime.Variable' does not contain a definition for 'StorageType' is just one of the compile errors.
Anyone else?
Win 7 Pro x64
AutoCAD 2010 x64
VC# 2008 Express
Linked against 2010\inc-x64 SDK
-
Goes bang. 'Autodesk.AutoCAD.Runtime.Variable' does not contain a definition for 'StorageType' is just one of the compile errors.
Anyone else?
Win 7 Pro x64
AutoCAD 2010 x64
VC# 2008 Express
Linked against 2010\inc-x64 SDK
It builds fine for 2011, but I get the same results as you with 2010.
-
If I remember the API webcast correctly, they were in place but not enabled for the 2010 release.