Author Topic: Reactors in .NET  (Read 6399 times)

0 Members and 1 Guest are viewing this topic.

BlackBox

  • King Gator
  • Posts: 3770
Reactors in .NET
« on: July 08, 2011, 11:58:43 AM »
I'm a Visual LISP guy who is in the process of learning C#, and VB.NET as I transition from Land Desktop 2009 to Civil 3D 2011. I understand, write code for, and use reactors daily (in VL), and want to know where I can find out how to create reactors in .NET

Rough concept of idea:

A System Variable Reactor, that programmatically adds/removes one or more ribbon panels to the active ribbon tab, say when I switch to or from the Layout tab, to the Model tab.

Admittedly, I've not yet undertaken this project in VL (specifically the parts about adding/removing items from CUI), but this functionality would help me (if not others) a lot.

I'm still in the process of writing basic commands in .NET, so forgive me if I have some elementary questions about Visual Studio. Comments welcome.

Note - Using Visual Studio 2010 Express.
"How we think determines what we do, and what we do determines what we get."

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Reactors in .NET
« Reply #1 on: July 08, 2011, 12:53:40 PM »
Hi,

Look at the 'Use Events' topic in the AutoCAD .NET Developer's Guide.

The LayoutManager class have a LayoutSwitched event.
« Last Edit: July 08, 2011, 01:01:43 PM by gile »
Speaking English as a French Frog

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Reactors in .NET
« Reply #2 on: July 08, 2011, 01:00:04 PM »
In my experience, hold off on using Events for a bit until you've got a good handle on coding in dotNET.  VL reactors are tricky enough but Events in dotNET will really twist you around unless your code is tight.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Reactors in .NET
« Reply #3 on: July 08, 2011, 01:19:30 PM »
I would think you are learning coding or .NET from a non-autocad source, but if you are(not required to use them) once you get a good understanding of delegates.... events are delegates under the hood.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Reactors in .NET
« Reply #4 on: July 08, 2011, 01:22:15 PM »
Here's a quicky to show a way to handle the LayoutManager.LayoutSwitched event:

Code: [Select]
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace LayoutManagerSample
{
    public class Commands
    {
        // private field
        private LayoutManager _lm;

        // class constructor (intialize the field value)
        public Commands()
        {
            _lm = LayoutManager.Current;
        }

        // Command 'On' activate the event handler
        [CommandMethod("On")]
        public void On()
        {
            _lm.LayoutSwitched += new LayoutEventHandler(onLayoutSwitched);
        }

        // Command 'Off' deactivate the event handler
        [CommandMethod("Off")]
        public void Off()
        {
            _lm.LayoutSwitched -= new LayoutEventHandler(onLayoutSwitched);
        }

        // Event handler method
        void onLayoutSwitched(object sender, LayoutEventArgs e)
        {
            AcAp.ShowAlertDialog(e.Name);
        }
    }
}
Speaking English as a French Frog

BlackBox

  • King Gator
  • Posts: 3770
Re: Reactors in .NET
« Reply #5 on: July 08, 2011, 02:04:41 PM »
As always, I really appreciate the feedback, this is educational for me. <TakingCopiousNotes>

Thanks for the terminology correct, Reactors=Visual LISP, Events (Delegates) = .NET.

I'm still taking in all of the added "meat" that Visual Studio provides as compared to the VLIDE. Not quit sure how I want to (or *should*) setup my code, meaning all within one project (single netload call) having each function in it's own file (.CS, or .VB respectively), or if I should split things up - like I said, I'm just diving in.

in any event (no pun intended)... Lot's to learn; it's nice knowing there's folks who can help.

Cheers! :beer:
"How we think determines what we do, and what we do determines what we get."

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Reactors in .NET
« Reply #6 on: July 08, 2011, 02:13:28 PM »
Remember you when it comes to coding for AutoCAD  in the end your code is making calls to ObjcetARX functions--or for simplicity your code must talk to AutoCAD source code.

So some examples are for learning the API and not always the best practices, I would encourage to also study and write applications that have nothing to do AutoCAD. 

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Reactors in .NET
« Reply #7 on: July 08, 2011, 02:20:44 PM »
Generally, each class goes in its own file for management purposes.  How you break up your needs into classes is up to you.  Aside from object representations, you may also end up with a single "utility" class and a UI class with only public/static methods.  At a minimum a project should contain classes specific to its own use.  You may create additional DLLs with generic utility functions and reference them into other projects, or add the class itself into your project if you want it to be stand-alone.  For now, I would look into doing self-contained projects to avoid dependancy related problems.  Later on you can start factoring out common methods for re-use.

And more terminology: OOP uses the term "method" for what you might call a "function" (i.e. the object can do something, or something can be done to the object).  "Function" is sometimes used to describe common methods not associated with a specific object beyond a class set up to manage them.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

BlackBox

  • King Gator
  • Posts: 3770
Re: Reactors in .NET
« Reply #8 on: July 08, 2011, 02:32:08 PM »
Remember you when it comes to coding for AutoCAD  in the end your code is making calls to ObjcetARX functions--or for simplicity your code must talk to AutoCAD source code.

So some examples are for learning the API and not always the best practices, I would encourage to also study and write applications that have nothing to do AutoCAD.

Thanks, Jeff... The latter part is where I feel that I'm getting hung up most.

(In Visual LISP) I understand of how to utilize functions, sub-functions, passing arguments, global & local variables, etc. But I feel silly because I do not know how to do this basic level handling (never mind error trapping) with .NET (yet), for example:

Code: [Select]
...
Dim ed As Editor = AutoCAD.DocumentManager.MdiActiveDocument.Editor
...

Edit: Removed [color ] tags from "ed" variable. It would be awesome if we could colorize code snips.

... is "ed" local, or because I'm using a .NET <CommandMethod()> is that not not of concern... feels like real little things are tripping me up. I well know if you don't have good fundamentals, you're going to go wrong down the road.

My company won't pay for me to attend a training course, hell they won't restore the paycuts we've undertaken, so I'm trying to learn on my own with VS 2010 Express, and my internet connection.  :-D



"How we think determines what we do, and what we do determines what we get."

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Reactors in .NET
« Reply #9 on: July 08, 2011, 02:42:41 PM »
Quote
I would encourage to also study and write applications that have nothing to do AutoCAD.
I do agree.
There're many books and tutos to learn OOP, .NET, Visual Studio and a supported language (C#, VB, F#, ...) related to Windows programation.
There're not so many applications which only refer to AutoCAD .NET API, most of them refer to Sytem (for integers, doubles, strings, ...), System.Collections and or System.Collections.Generic (arrays, hashtables, lists, dictionaries, ...), System.Windows.Form (dialog boxes, palettes), System.IO (files and folders), Microsoft.Win32 (registry), and so on...
« Last Edit: July 08, 2011, 03:18:05 PM by gile »
Speaking English as a French Frog

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Reactors in .NET
« Reply #10 on: July 08, 2011, 02:46:20 PM »
<Command Method> is a attribute.

Just think of it a way for Autocad to search through your code to figure out what command names to add and what method to call when command used.
I would guess similar to C:func in lisp


I do not know how to create a global variable in .NET. You can make one static(C#) or shared(vb) at class level.
'ed' depends on it's scope.
Where is it declared?
here is some code to show some different Scopes
Code: [Select]
        <CommandMethod("EditorScope")> _
        Public Sub EditorScope()
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim edMethodScope As Editor = doc.Editor

            edMethodScope.Regen()
            Using trx As Transaction = db.TransactionManager.StartTransaction()

                Dim edUsingBlockScope As Editor = doc.Editor

                edUsingBlockScope.Regen()
                edMethodScope.Regen()

                If 1 = 1 Then
                    Dim edIfScopeEditor As Editor = doc.Editor
                    edIfScopeEditor.Regen()
                    edMethodScope.Regen()
                End If

                edMethodScope.Regen()
                ''''ERROR edIfScopeEditor.Regen()
                edUsingBlockScope.Regen()
            End Using
            ''''ERROR edUsingBlockScope.Regen()
            edMethodScope.Regen()
        End Sub
        ''''ERROR edMethodScope.Regen()

Notice  if editor variable is declared in If block can only use in If block, etc...

BlackBox

  • King Gator
  • Posts: 3770
Re: Reactors in .NET
« Reply #11 on: July 08, 2011, 02:49:38 PM »
Generally, each class goes in its own file for management purposes.  How you break up your needs into classes is up to you.  Aside from object representations, you may also end up with a single "utility" class and a UI class with only public/static methods.  At a minimum a project should contain classes specific to its own use.  You may create additional DLLs with generic utility functions and reference them into other projects, or add the class itself into your project if you want it to be stand-alone.  For now, I would look into doing self-contained projects to avoid dependancy related problems.  Later on you can start factoring out common methods for re-use.

And more terminology: OOP uses the term "method" for what you might call a "function" (i.e. the object can do something, or something can be done to the object).  "Function" is sometimes used to describe common methods not associated with a specific object beyond a class set up to manage them.

Dgorsman,

That explanation is common sensical, and thusly most helpful. Trying to take all this in, on my own (read *blind*), can sometimes lead me to look right past the practical answer. I've taken several AU courses for AutoCAD+.NET but most of them presume a certain skill level and non-verbal knowns (i.e., add this "using" or that "imports" call is implied), and it's a lot of the implied stuff that I have no clue about.

This is an akward, yet necessary position for me to be in, because I am very comfortable writing complex code with Visual LISP, but find myself questioning "Okay, what button do I hit to do <X>?" within Visual Studio. LoL

There is so much information out there for VS... Would it be possible to narrow down one or two solid references for beginners like me to really take me through the basics?
"How we think determines what we do, and what we do determines what we get."

BlackBox

  • King Gator
  • Posts: 3770
Re: Reactors in .NET
« Reply #12 on: July 08, 2011, 02:56:14 PM »
I do not know how to create a global variable in .NET. You can make one static(C#) or shared(vb) at class level.
'ed' depends on it's scope.
Where is it declared?
here is some code to show some different Scopes
...
Notice  if editor variable is declared in If block can only use in If block, etc...

*click*
That, right there helps lots.

Keeping the VB.NET example going... I've been using ed immediately under <CommandMethod()>, however some of the AU tutorial use Imports ed ... blah, blah, blah. Your clarifying that ed is limited to only being used in the level at which it is defined is just great.

Edit: Perhaps they used Dim ed... blah, blah, blah up near the Imports - I don't recall. Anyway, you get my point (I hope).

Gile's and your examples are something I'll be referring back to - I appreciate you guys sharing some code here, it always helps when you have a good example to go by.
« Last Edit: July 08, 2011, 03:00:19 PM by RenderMan »
"How we think determines what we do, and what we do determines what we get."

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Reactors in .NET
« Reply #13 on: July 08, 2011, 03:14:29 PM »
I'm right there with you - boss won't pay but the users want advanced tools and while my LISP is sharp it can only go so far.  Its a matter of self-defence.  For references, grab any current dotNET book for your language.  I use a C# 2005 book and the online C# reference and between them I can find most answers.

"Global" and "local" translate roughly to scope in dotNET.  Read up on public and private scope.  Protected is something you will need later when dealing with inheritance which can be a real mind-bender.

If you are looking to learn programming techniques, find a simple open-source project someplace like SourceForge.net (I contribute to MegaMek which is Java, but close enough to C# that the techniques are transferrable).  The project admins can get you started on some simple updates to get your feet wet and the code base makes for some excellent examples of both how, and how not to do things.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Reactors in .NET
« Reply #14 on: July 08, 2011, 03:22:08 PM »
First off I hope I am not coming across like I think I know alot. I probably fall in the category of being to point where I realize how much I suck and how much more I need to to learn.

People might disagree but the AutoCAD API to me is just learning to use MgdDb and looking at arxdoc.chm, just learn how to find things.

.Net makes things easier and hides alot the details from you, For Example I can not think of any .NET resources that give good information about arrays. Not the different methods etc...

That is why I would suggest something like C++ how to program
Using the Array in C++ you can do this
Code: [Select]
int nums[10];
nums[0] = 5;
1[nums] = 7;
It helps shows that the name of a array points to the address of the first element and nums[2] is same thing as *(nums + 2) or *(2 + num) which is get value at address that is the size of 2 of the array's data type size (so for 32 bit int (4 bytes)) move 8 memory locations. nums[0](brackets are is as *(nums + 0) or *nums.

To put it short you get a better idea of what is going on at a lower level and get a better understanding of fundamentals.
So when you get a grasp on that you do not need to think about syntax instead you will be learning the structure and the ins and out of API