Вопрос

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?

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top