GetSystemVariable, Numeric Values.

GetSystemVariable, Numeric Values.

Anonymous
Not applicable
1,169 Views
2 Replies
Message 1 of 3

GetSystemVariable, Numeric Values.

Anonymous
Not applicable
R2006 Vs 2003 NF 1.1

Newbie C#.

I need some ctitiqueing on this.
After much internet searching(without finding much),
I finally solved my problem.
I needed the values of the ViewDir sysvar to make a vector3d.

My question is:
Did I end up with a KludgDrudgery way of doing this?
Or is this the way it's done in a Strongly Typed API?
Seems like a lot of type conversion to me.
Comments welcome.

string VwDir = AcadApp.GetSystemVariable("VIEWDIR").ToString();
VwDir = VwDir.TrimStart('(');
VwDir = VwDir.TrimEnd(')');
string[] VwXYZ = VwDir.Split(',');
double VwX = System.Convert.ToDouble(VwXYZ[0]);
double VwY = System.Convert.ToDouble(VwXYZ[1]);
double VwZ = System.Convert.ToDouble(VwXYZ[2]);
Vector3d viewVec = new Vector3d(VwX, VwY, VwZ);
Plane plane = new Plane(new Point3d(0, 0, 0), viewVec);

TIA

Bill
0 Likes
1,170 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable
Yes Bill, sorry to say it, but that's pretty horrifying :), but kudos for having the courage to post it and to seek advice/counsel.

The problem is that you're hacking, rather than taking some time to learn to use not only the programming language, but also the API. It's clear from code you've posted that you don't yet fully understand strongly-typed programming langauges, which would explain why your code often resorts to converting things to strings, something that is not only unnecessary, but extremely inefficient, and well.... just plain ugly 🙂

The VIEWDIR system variable is a Point3d type.

Point3d viewdir = (Point3d) AcadApp.GetSystemVariable("VIEWDIR");

So, the first thing you need to do is learn about typecasting. The above type casts the result of GetSystemVariable() to a Point3d. That's what the '(Point3d)' in parens does (think 'right-to-left'). If you step through the code that calls GetSystemVariable() and assign the result to a variable of type 'object', when you look at the variable in the debugger's watch window, you will see that the actual type of the object is Point3d. The typecast is what allows you to use it as a Point3d type, and/or assign its value to a Point3d variable.

If you wanted the individual elements of a Point3d as doubles, you just reference them as if the Point3d were array:

double x = viewdir[0];
double y = viewdir[1];

There is no need to convert anything to strings. However, to do what you want, there's no need for you to work with the individual elements of the Point3d in the first place.

Given a point representing the view direction vector, the only thing you have to do to get a Plane is this:

Plane plane = new Plane(Point3d.Origin, viewdir.GetAsVector());

So, to summarize, all of that ummmm... code you posted could be replaced with this:

Point3d viewdir = (Point3d) AcadApp.GetSystemVariable("VIEWDIR");
Plane plane = new Plane( Point3d.Origin, viewdir.GetAsVector() );

I'll leave it to you to look up 'GetAsVector()' in the docs.

While you're there, also have a look at some of the other members of the Point3d type, and try to become more familiar with the class, as that's what will allow to solve simple problems like this, yourself.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009

http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm

wrote in message news:[email protected]...
R2006 Vs 2003 NF 1.1

Newbie C#.

I need some ctitiqueing on this.
After much internet searching(without finding much),
I finally solved my problem.
I needed the values of the ViewDir sysvar to make a vector3d.

My question is:
Did I end up with a KludgDrudgery way of doing this?
Or is this the way it's done in a Strongly Typed API?
Seems like a lot of type conversion to me.
Comments welcome.

string VwDir = AcadApp.GetSystemVariable("VIEWDIR").ToString();
VwDir = VwDir.TrimStart('(');
VwDir = VwDir.TrimEnd(')');
string[] VwXYZ = VwDir.Split(',');
double VwX = System.Convert.ToDouble(VwXYZ[0]);
double VwY = System.Convert.ToDouble(VwXYZ[1]);
double VwZ = System.Convert.ToDouble(VwXYZ[2]);
Vector3d viewVec = new Vector3d(VwX, VwY, VwZ);
Plane plane = new Plane(new Point3d(0, 0, 0), viewVec);

TIA

Bill
0 Likes
Message 3 of 3

Anonymous
Not applicable
Thanks so much Tony.

I knew something wasn't right. 🙂

At one point, I had realized it was a Point3d and had it cast as such. Hmmm.

On the side: I'm hoping for an upgrade to the API.
I think it has some bugs as it limits me to what I can do under the debugger drop down.
If I use "Start", "Step into" or "Step over" I get an error (attached .png, top) very frustrating. 😞
I can only use "Start without debugging" so most of my debugging is done with try blocks and ed.WriteMessage.
The intellisense works so that's a plus but when I use the help "index" to look up certain terms, not everything I look for shows up in the list of items(mostly the geometry stuff).

On the bright side, most code on this project looks pretty good as I sourced it from some of Kean W's examples and also what I found here at the NG and on related links.
Our users seem to like the way it works so with good feedback, I can make a pitch to upgrade soon.
And yes, I've cleaned up a lot of what you saw in my earlier posts with all the string ummm.... stuff. 😉

The ViewDir is just a "finishing touch" so the part image
*follows* whatever view the user has on the AutoCAD screen. (rest of attached).

Again, thanks for your patience and constructive pointers!

Bill
0 Likes