External Command Initialization issue

External Command Initialization issue

chubbard
Collaborator Collaborator
1,511 Views
8 Replies
Message 1 of 9

External Command Initialization issue

chubbard
Collaborator
Collaborator

I have completed the Revit API tutorials and written a number of example classes with general success.

 

However I am having a strange issue that I cannot seem to resolve. 

I created a code snippet of the External Command instantiation rather than type all that into each class. But when I use the snippet VS is erroring out on the IExternalcommand.Execute statement. Now if I type that into a new class it works fine but copy/paste or Snippet generates an error and the class will not debug or build properly. Delete the text and type it verbatim it works. 

The error is 

"Must declare a body because its not marked abstract, extern or partial"

Here is how I am declaring it:

 

 [TransactionAttribute(TransactionMode.Manual)]
    public class DeleteElement : IExternalCommand
    {
        Result IExternalCommand.Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
 
Again this works fine when I type it even using suggestion to fill in, but past or snippet does not work.
0 Likes
1,512 Views
8 Replies
Replies (8)
Message 2 of 9

jeremytammik
Autodesk
Autodesk

Dear Chubbard,

 

Thank you for your query and congratulations on your success so far.

 

Looking at your code snippet above, I see a function signature with no body.

 

Therefore, the error message 'must declare a body' makes perfect sense.

 

I don't quite see what you are trying to achieve.

 

If you like, take a look at The Building Coder samples:

 

https://github.com/jeremytammik/the_building_coder_samples

 

They define a large number of external commands and simply repeat the declaration of the Execute method signature for each.

 

Best regards,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 3 of 9

chubbard
Collaborator
Collaborator

Jeremy

 

I didn't post the entire class, just the signature. In my snippet I have the signature and closing braces defined, I then insert the body code. 

Now if I type the function signature my class builds and runs fine. If I use the exact same code via cut paste or snippet it will not build or run and the error is below. I cannot figure out why it makes any difference how the text gets added to the class. 

What I am trying to accomplish is to be able to create a new class quickly. I have snippets of all sorts of code stored on my system, I have never run into this issue where typing vs insert makes a difference. 

0 Likes
Message 4 of 9

jeremytammik
Autodesk
Autodesk

It definitely makes no difference whatsoever whether the text is typed in character by character or pasted from the clipboard. The result is identical. The difference must lie elsewhere.

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 5 of 9

stever66
Advisor
Advisor

I agree with Jeremy for C#. 

 

If you are using Python, I've found it to be a little more picky when copying and pasting code.  Python uses white space or "returns" to determine where one line ends and another line starts.   And it seems like some white space is different than others.  Maybe spaces or tabs confuse it.  Or maybe there are even different types of "returns" or "new lines" that can be used by different programs.


But I assume you are using C#.  So like Jeremy said, it must be something else.

 

Are you trying to rename a namespace or file name?   Sometimes there is some "behind the scenes" code that needs changed to match. 

 

But to be honest, I'm not exactly sure when or what.  I don't think its a problem with class names, but I think it is with project names and namespaces.  

 

And sometimes, when you rename something, visual studio will ask if you want to update references, and sometimes not.

 

So I just try to avoid renaming any namespaces or files.   

0 Likes
Message 6 of 9

chubbard
Collaborator
Collaborator

yes C#

 

Here is why i am confused. I have lots of code snippets for LinQ SQL etc. Just getting into Revit C#. None of the other ones throw an error at build or debug. (Unless i screw up and mistype etc.)

Only Revit and only this statement. So here is my two processes.

1. Type all the function declarations by hand, then the body and bam it builds.

2. Use clipboard or snippet, type the class body and build hangs on the .execute method of the function. 

Delete the function statement ONLY and type it as above. Builds. Copy from same working class and paste back into same class. Error. Makes absolutely no sense and only occurs on the Revit classes that are external commands. I have checked for spaces Case everything. VS is able to detect and suggest corrections for all of that. 

 

0 Likes
Message 7 of 9

jeremytammik
Autodesk
Autodesk

I never ever type that function signature.

 

I always let Visual Studio build it for me automatically.

 

It can be inferred and automatically generated from the IExternalCommand interface.

 

Just right-click on IExternalCommand and select 'implement interface' in the context menu.

 

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 8 of 9

chubbard
Collaborator
Collaborator

VS is not inferring that function or method for me. I have to type the entire line. Could there be an issue with the API reference? The rest works fine. 

0 Likes
Message 9 of 9

stever66
Advisor
Advisor

Do you have the right references loaded for the revit dll’s?

And these are set for copy local is false?

Im not sure what else it might be.   I assume you can get external commands to run and work.

0 Likes