Use Python in VS Code instead of external Add-ins

Use Python in VS Code instead of external Add-ins

Anonymous
Not applicable
5,615 Views
5 Replies
Message 1 of 6

Use Python in VS Code instead of external Add-ins

Anonymous
Not applicable

Hi!

 

I wonder if I can use Python in VS Code (Visual Studio Code) for Revit API programming instead of using external add-ins like pyRevit or Revit Shell. I have seen many people suggest using add-ins for easier but I have already have knowledge about Python. Moreover, I don't like manipulating through folders like pyRevit) and I want to using directly from VS Code.

 

Is there a way for me? 

 

Thank you in advance.

0 Likes
Accepted solutions (2)
5,616 Views
5 Replies
Replies (5)
Message 2 of 6

RPTHOMAS108
Mentor
Mentor
Accepted solution

Iron Python is the .Net implementation of Python.

 

I assume you can do with it anything you can do with one of the other CLI languages.

Message 3 of 6

architect.bim
Collaborator
Collaborator

Hi!

There was a similiar question recently. I wonder how are you actually going to get the "entry point" to Revit directly from VS Code? I thought it is not possible (may be I am mistaken?).

I respect VS Code much and always use it to write Python code for Revit API. But not for executing it. I still have to load my python files with Dynamo or Revit Python Shell to make something happen in Revit as they provide an instance of UIApplication class.

Do you really know any working solution to interact with Revit directly from VS Code?


Maxim Stepannikov | Architect, BIM Manager, Instructor
0 Likes
Message 4 of 6

RPTHOMAS108
Mentor
Mentor
Accepted solution

I don't really know that much about VS code i.e. the advantages it has over VS community edition. Although it seems orientated towards web development (and .Net vs .Net Framework). From what I read it allows attaching to process, not sure about referencing of assemblies that would be required etc.

 

Iron Python is supported by visual studio as is (not sure about the free community edition), so it should be possible to use it for compilation of .Net assemblies with RevitAPI references. Although that doesn't seem to be the path for most Revit Python developers. Again I'm not an expert on Python so can't describe the disadvantages of using it that way. Python is often touted as something you don't need to re-compile and run in Revit (so perhaps that is the reason) but in reality you can achieve similar with VB/C# (just nobody has put the plumbing in place for that).

 

Entry points are straightforward enough you just need an add-in on the Revit side to access. This then calls your dynamically compiled assembly and invokes a member on it. CodeDOM supports VB.Net and C#, so in theory if I only had a VB/C# text file from VS Code I could compile it with CodeDOM and invoke it (both within a Revit side add-in). Not sure about Python you may have to compile externally but then it could be referenced like any other CLR library. 

 

I know this approach works as I have an add-in that compiles a VB.Net text file this way, I don't use it much because it is basically a text box with plain text and no code formatting. Plus there are also issues where VendorIds are used e.g. extensible storage, dynamic model updaters etc. the vendor Id from the main entry point assembly is used. Add-in manager used to suffer from this which I why I never used it for long, I'd rather just plan my Revit restarts. Debugging is also inherantly harder, although with CodeDOM it gives you line numbers and compilation errors etc.

Message 5 of 6

architect.bim
Collaborator
Collaborator

Thank you for such a detailed answer! 😃


Maxim Stepannikov | Architect, BIM Manager, Instructor
0 Likes
Message 6 of 6

RPTHOMAS108
Mentor
Mentor

No worries, it's an interesting topic. People with a lot of construction industry knowledge but less computer programming experience are using such languages to solve very specific problems to them. If I were starting out today I'd probably go down the RPS route.

 

Although C# seems to be the language of the future, Microsoft are evolving it into VB.Net...(conspiracy theory).

0 Likes