TheSwamp

Code Red => .NET => Topic started by: SteveK on January 19, 2010, 11:51:14 PM

Title: Newbie looking for simple C# examples
Post by: SteveK on January 19, 2010, 11:51:14 PM
Hi,

I've just been through the C# Hello World example here http://www.cadalyst.com/cad/autocad/autocad-commands-c-cad-clinic-civil-3d-tutorial-3069 (http://www.cadalyst.com/cad/autocad/autocad-commands-c-cad-clinic-civil-3d-tutorial-3069).

Does anyone have any small C# examples I can learn from? An example would be searching a drawing for text that is green and changing it to blue.

And as an aside: is there an ssget equivalent in C#?

Thanks,

steve
Title: Re: Newbie looking for simple C# examples
Post by: Kerry on January 19, 2010, 11:59:46 PM
A couple to start with :

http://www.autodesk.com/autocad-net-developers-guide
http://images.autodesk.com/adsk/files/autocad_2010_dotnet_training.zip
http://images.autodesk.com/adsk/files/objectarx_addendum_-_ribbon.exe
http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=785550  [edit: link fixed]

http://usa.autodesk.com/adsk/servlet/index?siteID=123112&id=1911627
http://through-the-interface.typepad.com/


[added:]
and have a look around theSwamp of course ... there is a bit tucked away here :)
Title: Re: Newbie looking for simple C# examples
Post by: fixo on January 20, 2010, 04:11:59 AM
Hi,

I've just been through the C# Hello World example here http://www.cadalyst.com/cad/autocad/autocad-commands-c-cad-clinic-civil-3d-tutorial-3069 (http://www.cadalyst.com/cad/autocad/autocad-commands-c-cad-clinic-civil-3d-tutorial-3069).

Does anyone have any small C# examples I can learn from? An example would be searching a drawing for text that is green and changing it to blue.

And as an aside: is there an ssget equivalent in C#?

Thanks,

steve

Here is what you asked about

Feel free to change to your suit

~'J'~
Title: Re: Newbie looking for simple C# examples
Post by: Glenn R on January 20, 2010, 11:10:51 AM
Start off by learning the language first - forget the autocad side of it for now, as it sounds like you're at the very beginning. You'll be glad you did in the long run.
Title: Re: Newbie looking for simple C# examples
Post by: SteveK on January 20, 2010, 04:20:44 PM
http://through-the-interface.typepad.com/
Cheers Kerry, that site (above) is quite good. Here's a couple I found I can read through.
Using pickfirst selection
http://through-the-interface.typepad.com/through_the_interface/2006/09/using_the_pickf.html (http://through-the-interface.typepad.com/through_the_interface/2006/09/using_the_pickf.html)
Creating an AutoCAD layer
http://through-the-interface.typepad.com/through_the_interface/2010/01/creating-an-autocad-layer-using-net.html (http://through-the-interface.typepad.com/through_the_interface/2010/01/creating-an-autocad-layer-using-net.html)


Here is what you asked about
Feel free to change to your suit
~'J'~
Thanks very much Fixo! I'll get back to you with questions   :-)


Start off by learning the language first - forget the autocad side of it for now, as it sounds like you're at the very beginning. You'll be glad you did in the long run.
I got plenty of holes in my knowledge, but I know the basics of C++ so I'm gonna see how far I can get with that.  :?
Title: Re: Newbie looking for simple C# examples
Post by: SteveK on January 20, 2010, 06:17:35 PM
Fixo, I just went through your code. It actually made sense! The only thing that looks a bit foreign is the bit bracketed: "using (Transaction..."

Can I ask a few questions?...

1. Where does the object Regex come from? As in the line:
Code: [Select]
newtext = Regex.Replace(dbt.TextString, sFind, sReplace);
2. Is the
Code: [Select]
catch (Autodesk.AutoCAD.Runtime.Exception ex) the common error catcher? Ie, what if I use:
Code: [Select]
catch (...)?

3. What does
Code: [Select]
[assembly: CommandClass(typeof(SelText.TextMod))] at the top of the cs file do?

4. How does autoCAD know to run the Initialize function in InitCommand.cs? Don't we need to call it because it's in a function?

5. Out of interest, do we ever use classes apart from a single class to house our commands?

6. Lastly, I just want to make sure there's no apropos button in SharpDevelop, like there is in the Visual Lisp Editor. And the best thing to use as a quick reference is http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer%27s%20Guide/index.html (http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer%27s%20Guide/index.html)?

Thanks!!  :-)

steve
Title: Re: Newbie looking for simple C# examples
Post by: Kerry on January 20, 2010, 06:59:35 PM
1) System.Text.RegularExpressions.Regex

2) what do you mean by ; catch (...)
You catch exceptions thrown (Please Read The Documentation )

3) not mandatory, but improves loading performances. (Please Read The Documentation )

4) public class InitCommand : Autodesk.AutoCAD.Runtime.IExtensionApplication

5) yes (Please Read The Documentation )

6) You mean Intellisence ?
   re the quick reference : did you see my first link ?

regards
Kerry

[edit]
RTFM changed to Please Read The Documentation
Title: Re: Newbie looking for simple C# examples
Post by: SteveK on January 20, 2010, 07:42:06 PM
Kerry, I know you know a great deal, but if you don't want to genuinely help then please don't post! Let someone else.
Title: Re: Newbie looking for simple C# examples
Post by: Kerry on January 20, 2010, 07:50:05 PM
Steve

ALL of my answers are correct and relevant to your question.

The (Please Read The Documentation ) is for additional information.

If you expect me ( or anyone) to write a thesis to cover information already included in documentation you will be disappointed I think.

... oh, and you're welcome :)


RTFM changed to Please Read The Documentation
Title: Re: Newbie looking for simple C# examples
Post by: SteveK on January 20, 2010, 08:13:27 PM
Kerry, You expect a thanks when you were being rude? There's a difference between correct and helpful.

You know yourself you posted what you did because you've either seen these questions a hundred times or you've read them countless times in the .NET manual and you think I could have found the answer if I'd searched it or the web.

In answer I have made a search for these questions. And I just like finding out details in a forum. It's how myself and countless others have learnt lisp. Yes I've read a hundred questions that boil down to the same one asked last year, but so long as someone show's an effort to learn I quite enjoy helping them along.

Anyways, that's enough of that. Thanks Kerry, question 1 was beneficial to me.
Title: Re: Newbie looking for simple C# examples
Post by: Kerry on January 20, 2010, 08:43:22 PM

How about 4)
do you understand the inheritance implications of  Autodesk.AutoCAD.Runtime.IExtensionApplication

http://through-the-interface.typepad.com/through_the_interface/2006/09/optimizing_the_.html
http://through-the-interface.typepad.com/through_the_interface/2006/09/initialization_.html
http://discussion.autodesk.com/forums/message.jspa?messageID=6287126
http://through-the-interface.typepad.com/through_the_interface/2009/06/a-new-project-wizard-for-autocad-net-development.html
Title: Re: Newbie looking for simple C# examples
Post by: Kerry on January 20, 2010, 08:44:53 PM


Regarding 2)

what do you mean by :

catch (...) ?
Title: Re: Newbie looking for simple C# examples
Post by: Kerry on January 20, 2010, 08:51:22 PM

Regarding 3)
Quote
What does
[assembly: CommandClass(typeof(SelText.TextMod))]at the top of the cs file do?
do

I answered
3) not mandatory, but improves loading performances.

http://www.autodesk.com/autocad-net-developers-guide
http://through-the-interface.typepad.com/through_the_interface/2006/09/optimizing_the_.html
Title: Re: Newbie looking for simple C# examples
Post by: Kerry on January 20, 2010, 08:52:47 PM
Regarding 5)

yes.
I have no further comment other than :
If you don't know this I suggest you read Glenn's post
Title: Re: Newbie looking for simple C# examples
Post by: SteveK on January 20, 2010, 09:33:38 PM
Thanks Kerry.

2. Ignore this. catch (...) was an afterthought from C++. It doesn't work. My bad.
3. That link is helpful.
4. No I don't understand that, but once again those links put me on the right track I think.
5. It was more a general question I just thought an experienced C#'er could comment on. I'm sure I'll notice when I come across it, was just curious if classes are used in code that run simple autocad commands. I can't think how, and it doesn't matter.

Thanks again Kerry, and sorry I didn't mean to be a blight on your day.
Title: Re: Newbie looking for simple C# examples
Post by: Kerry on January 20, 2010, 09:41:42 PM

Quote
.... and sorry I didn't mean to be a blight on your day.

No worries, To quote an Irish Farmer who lived next door to us.

"I've been stuffed around by experts and you've got a long way to go to qualify boy."

"I only get upset by what I consider important, this isn't important. "


Happy learning.
Title: Re: Newbie looking for simple C# examples
Post by: MickD on January 20, 2010, 10:42:22 PM

5. Out of interest, do we ever use classes apart from a single class to house our commands?


Firstly, everything in C# is a class - 'everything', I think BOO,F#, IronPython/Ruby et al may be the exceptions where you can write simple functions to do a task without being a class method.
Yes, the commands class is what carries out your orders but any application with any sort of real purpose will be comprise of many classes. In the case of acad, a lot of them may be termed as 'helper' classes and some real world objects to hold and manipulate data. Besides that you wouldn't want 5000 lines of code for a command class I wouldn't think :)
Title: Re: Newbie looking for simple C# examples
Post by: fixo on January 21, 2010, 04:58:04 AM
Fixo, I just went through your code. It actually made sense! The only thing that looks a bit foreign is the bit bracketed: "using (Transaction..."

Can I ask a few questions?...

1. Where does the object Regex come from? As in the line:
Code: [Select]
newtext = Regex.Replace(dbt.TextString, sFind, sReplace);
2. Is the
Code: [Select]
catch (Autodesk.AutoCAD.Runtime.Exception ex) the common error catcher? Ie, what if I use:
Code: [Select]
catch (...)?

3. What does
Code: [Select]
[assembly: CommandClass(typeof(SelText.TextMod))] at the top of the cs file do?

4. How does autoCAD know to run the Initialize function in InitCommand.cs? Don't we need to call it because it's in a function?

5. Out of interest, do we ever use classes apart from a single class to house our commands?

6. Lastly, I just want to make sure there's no apropos button in SharpDevelop, like there is in the Visual Lisp Editor. And the best thing to use as a quick reference is http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer%27s%20Guide/index.html (http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer%27s%20Guide/index.html)?

Thanks!!  :-)

steve

Steve, sorry for the belating, I've been sick as always
And thanks to Kerry Brown for saveing my time because of
my poor english level I would be spend a ton of time on it :)

~'J'~
Title: Re: Newbie looking for simple C# examples
Post by: Glenn R on January 21, 2010, 05:07:02 AM
If I remember correctly, catch(...) in C++ is a catch-all (almost like On Error Resume Next in VB - let the fight begin ;-) ) and the equivalent in C# is catch().

I seriously discourage it's use however. As all exceptions derive from System.Exception, that's your last catch clause in a multi-catch.

I do remember reading somewhere, that there's an System.ApplicationException...ah, here (http://msdn.microsoft.com/en-us/library/system.applicationexception.aspx) it is.
Title: Re: Newbie looking for simple C# examples
Post by: Glenn R on January 21, 2010, 05:12:17 AM
It appears from that link I posted, that MS have changed their recommended exception handling strategies from when I first heard about ApplicationException.

BTW, I reiterate - get a good book and start there. Forget about doing anything in AutoCAD until you have a solid grasp of the language.
Title: Re: Newbie looking for simple C# examples
Post by: SteveK on January 21, 2010, 05:30:48 AM
Hi Glenn, thanks for clearing that up (about catch). I'll stick with the runtime exception. I suppose some people probably wouldn't get far enough (before getting bored) to do anything worthwhile if they started with a textbook. Without doubt, their knowledge would be full of holes and they'll never get to the level of some people on this forum, but they'll be able to do some simple fun stuff, and be content.

Fixo, no worries mate, you've already helped a ton! If you have any more simple examples by all means post em! Eg Changing an attribute in a block.  :wink:

And thanks Mick, you understood my question. I like reading those type of comments, I reckan it helps.  :-)

Thanks guys, sorry to be an irritation, there just aren't too many AutoCAD C# forums around for noob questions. (I'll wait for a link Kerry)
Title: Re: Newbie looking for simple C# examples
Post by: Kerry on January 21, 2010, 05:52:25 AM
Quote
Eg Changing an attribute in a block.

'cause I'm feeling a little mellow from limechicken and a couple of glasses of red ;

a piccy to ponder :)

Title: Re: Newbie looking for simple C# examples
Post by: Kerry on January 21, 2010, 05:53:45 AM
< .. >  there just aren't too many AutoCAD C# forums around for noob questions.

 :angel:

I know of one.
Title: Re: Newbie looking for simple C# examples
Post by: SteveK on January 21, 2010, 06:07:37 AM
a piccy to ponder :)
Yeah I pondered but I haven't had enough red wine to ponder for too long. Ok I get the point, no more unimportant noob questions!
Title: Re: Newbie looking for simple C# examples
Post by: Kerry on January 21, 2010, 06:11:40 AM

Steve
If you pull your head out of your arse for a little while you may be able to learn something here.
Title: Re: Newbie looking for simple C# examples
Post by: SteveK on January 21, 2010, 07:23:50 AM
you're a real pal Kerry
Title: Re: Newbie looking for simple C# examples
Post by: MP on January 21, 2010, 10:01:57 AM
lol, would read again (http://www.theswamp.org/screens/mp/snacks.gif)
Title: Re: Newbie looking for simple C# examples
Post by: Maverick® on January 21, 2010, 10:06:46 AM
MP IN DA HIZZHOUSE!  :-D
Title: Re: Newbie looking for simple C# examples
Post by: Kerry on January 22, 2010, 02:32:19 AM

[ intermission ]

Long time Michael !!

Title: Re: Newbie looking for simple C# examples
Post by: mohnston on January 22, 2010, 07:02:49 PM
Kerry, You expect a thanks when you were being rude? There's a difference between correct and helpful.

You know yourself you posted what you did because you've either seen these questions a hundred times or you've read them countless times in the .NET manual and you think I could have found the answer if I'd searched it or the web.

In answer I have made a search for these questions. And I just like finding out details in a forum. It's how myself and countless others have learnt lisp. Yes I've read a hundred questions that boil down to the same one asked last year, but so long as someone show's an effort to learn I quite enjoy helping them along.

Anyways, that's enough of that. Thanks Kerry, question 1 was beneficial to me.

This is an interesting reaction.
I think it has to do with what a person thinks "RTFM" stands for. There is certainly room for (mis)interpretation.
I've heard it used as:
Reboot The *expletive* Machine
or
Read The *expletive* Manual

Either way it seems harsh.

Or maybe it was supposed to be:
Run To First Method
Rob The Flea Market
Rate Two Fat Monkeys
Ring Twice For M . . . .
Title: Re: Newbie looking for simple C# examples
Post by: Kerry on January 22, 2010, 07:45:40 PM

This is easily fixed.
I'll change the offending RTFM to ' Please Read The Documentation '



.... but really, does that change anything important ?
Title: Re: Newbie looking for simple C# examples
Post by: It's Alive! on January 22, 2010, 08:15:03 PM
Real Techies Flout Menus


Oooops sorry Dan
Title: Re: Newbie looking for simple C# examples
Post by: Glenn R on January 23, 2010, 03:21:19 PM
I knew where Kerry was coming from and I'm sure Steve did as well - no harm done. It's an aussie thing ;)