Author Topic: Newbie looking for simple C# examples  (Read 8295 times)

0 Members and 1 Guest are viewing this topic.

SteveK

  • Guest
Newbie looking for simple C# examples
« 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.

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
« Last Edit: January 20, 2010, 12:21:38 AM by steve.k »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
« Last Edit: January 22, 2010, 02:51:29 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.

fixo

  • Guest
Re: Newbie looking for simple C# examples
« Reply #2 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.

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'~

Glenn R

  • Guest
Re: Newbie looking for simple C# examples
« Reply #3 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.

SteveK

  • Guest
Re: Newbie looking for simple C# examples
« Reply #4 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
Creating an AutoCAD layer
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.  :?

SteveK

  • Guest
Re: Newbie looking for simple C# examples
« Reply #5 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?

Thanks!!  :-)

steve
« Last Edit: January 20, 2010, 06:49:41 PM by steve.k »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Newbie looking for simple C# examples
« Reply #6 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
« Last Edit: January 22, 2010, 07:47:22 PM 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.

SteveK

  • Guest
Re: Newbie looking for simple C# examples
« Reply #7 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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Newbie looking for simple C# examples
« Reply #8 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
« Last Edit: January 22, 2010, 07:48:19 PM 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.

SteveK

  • Guest
Re: Newbie looking for simple C# examples
« Reply #9 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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Newbie looking for simple C# examples
« Reply #11 on: January 20, 2010, 08:44:53 PM »


Regarding 2)

what do you mean by :

catch (...) ?
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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Newbie looking for simple C# examples
« Reply #12 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
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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Newbie looking for simple C# examples
« Reply #13 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
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.

SteveK

  • Guest
Re: Newbie looking for simple C# examples
« Reply #14 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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Newbie looking for simple C# examples
« Reply #15 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.
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.

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Newbie looking for simple C# examples
« Reply #16 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 :)
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

fixo

  • Guest
Re: Newbie looking for simple C# examples
« Reply #17 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?

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'~

Glenn R

  • Guest
Re: Newbie looking for simple C# examples
« Reply #18 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 it is.

Glenn R

  • Guest
Re: Newbie looking for simple C# examples
« Reply #19 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.

SteveK

  • Guest
Re: Newbie looking for simple C# examples
« Reply #20 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)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Newbie looking for simple C# examples
« Reply #21 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 :)

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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Newbie looking for simple C# examples
« Reply #22 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.
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.

SteveK

  • Guest
Re: Newbie looking for simple C# examples
« Reply #23 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!

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Newbie looking for simple C# examples
« Reply #24 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.
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.

SteveK

  • Guest
Re: Newbie looking for simple C# examples
« Reply #25 on: January 21, 2010, 07:23:50 AM »
you're a real pal Kerry

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Newbie looking for simple C# examples
« Reply #26 on: January 21, 2010, 10:01:57 AM »
lol, would read again
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Maverick®

  • Seagull
  • Posts: 14778
Re: Newbie looking for simple C# examples
« Reply #27 on: January 21, 2010, 10:06:46 AM »
MP IN DA HIZZHOUSE!  :-D

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Newbie looking for simple C# examples
« Reply #28 on: January 22, 2010, 02:32:19 AM »

[ intermission ]

Long time Michael !!

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.

mohnston

  • Bull Frog
  • Posts: 305
  • CAD Programmer
Re: Newbie looking for simple C# examples
« Reply #29 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 . . . .
It's amazing what you can do when you don't know what you can't do.
CAD Programming Solutions

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Newbie looking for simple C# examples
« Reply #30 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 ?
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: 8698
  • AKA Daniel
Re: Newbie looking for simple C# examples
« Reply #31 on: January 22, 2010, 08:15:03 PM »
Real Techies Flout Menus


Oooops sorry Dan
« Last Edit: January 22, 2010, 08:45:55 PM by Kerry Brown »

Glenn R

  • Guest
Re: Newbie looking for simple C# examples
« Reply #32 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 ;)