0

I'm very new to Autocad programming, few months new, but I managed to write an application with about 10 Autocad commands. Most of these commands show a window and everything else is done from that window. I try to make my application MVVM..ish. Because I have an almost 1 to 1 correspondence between Models ViewModels Views and commands I felt it would be appropriate to put the command inside one of the 3 parts of MVVM. First I decided to put the command in the ViewModel, then I realised that my command is only showing a window, and the ViewModel shouldn't handle windows, so I moved the command to the View's Codebehind. As I understood, it's ok to have code in the codebehind as long as it's strictly view related. Then I read about the CommandClass attribute on some tutorial provided by Autodesk and I found this:

CommandClassAttribute This custom attribute class is used to mark a type as the application's command class. An application may designate one, and only one, type as its command class. AutoCAD looks for an application's command methods on the type that bears this attribute.

This suggests that I should have one class to include all my commands. Then I read this, which confirms the above:

For an instance command method, the method's enclosing type is instantiated separately for each open document.

So my first approach to put commands in views or viewmodels was utterly wrong, because I would have viewmodel instances not doing anything else than running the command. Then I read this in ObjectARX's documentation:

If an application uses the CommandClass attribute, it must declare an instance of this attribute for every type that contains an AutoCAD command handler method.

Which bluntly contradicts the tutorial quoted above and also suggest that it's an approved practice to have more than one class to handle commands.

All Autocad .NET tutorials are projects with one class and one command, so you don't have to many choices about where to put what.

Could some experienced Autocad .NET developer provide a best practice, or at least an ok, or not wrong practice for managing Autocad commands in a rather large project?

Andrei
  • 961
  • 1
  • 9
  • 29
  • [Try to see the link below,it might help you][1] [1]: http://stackoverflow.com/questions/1338955/autocad-in-vb-net?rq=1 – VB.NET LEARNER Jul 08 '13 at 10:09
  • Thank you for your response, but I don't have a "getting started" problem. I already visited most of those links a few months ago when I was actually getting started with Autocad .NET. Unfortunately, most of those cover the basic hello world problem. – Andrei Jul 08 '13 at 10:57

1 Answers1

1

In my work, I have that CommandClass attribute attached to a single class, as provided by the wizard I used to start.

Inside that command class, I put the commands, ALL of them. Each one with the CommandMethod attribute.

So they are not attached to any object or view (and I really thought that was only possible using VBA, not .NET)

But their code is not written in there, I make different classes to manage the commands, so the main command class works mostly like a command index, each command calling its respective body in different classes I made for my own purposes.

So, in your place, I'd probably make a MyCommands with CommandClass attibute having all commands. Each command simply calls the command body from the ViewCommands class below.

Create a ViewCommands class (and many others you need to make it easy for your model). This class doesn't have the CommandClass attribute, just have regular methods. It will have the core of the commands to be called from CommandClass methods.

I never tried to work with more than one CommandClass, and I do believe it's better to concentrate all commands in a single place, to keep a good track of what your addin makes.

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
  • Thank you for your input and for sharing your experience, but I would like to leave the question unanswered for a few more days so that maybe an Autocad.NET guru see it and provide a better argued answer. – Andrei Jul 09 '13 at 06:41
  • As I recently found out, the help is simply wrong when it says that it can be only one commandclass attribute, so it wasn't Autodesk intention to have only one commandclass. – Andrei Jul 09 '13 at 06:49
  • So, it's up to you. Probably best practices will come from general developing practices, as I've seen nothing regarding Autocad .NET specifically. Probably you don't even need the `CommandClass` attribute, my help within the addin wizard tells it's not mandatory but improves performance. – Daniel Möller Jul 09 '13 at 16:14