What's new

AlibreScript Programming and Learning with VS Code | AlibreScriptAPIVSCodeDemo

stepalibre

Alibre Super User
I would like to share an experimental project for using VS Code to assist in Alibre Script programming and learning. It requires Python programming and VS Code knowledge. I utilize standard Python techniques and VS Code features, no special tools and libraries required. You should install Python extensions for full support along with a Python environment.

Main Files:
Example program : main.py
Mock/stub Alibre Script API file: AlibreScript.py [ AlibreScript.py is Python 3, Alibre Script is IronPython 2.7. AlibreScript.py is made to be compatible with Python 2.7 ]
Example python notebook : NOTEBOOK.ipynb [ code is not able to run from inside notebooks ]

You can download the repo to the Alibre Script Library folder and run the main.py file inside the Alibre Script addon. This is an experiment, I can't guarantee that the full Alibre Script API works as expected. Many Alibre Script members that are global are not included by default. I will add additional global members as needed. For example CurrentPart was added today.

This project is used in development of another Alibre Python project that is more fully featured. I made it into it's own project because I found it useful as a standalone project. I work in VS Code and Python daily and it is really nice to control Alibre from my other projects. Even simple tasks like exporting a file is made even easier. It is also really helpful in learning Alibre Script in a standard Python way.

The goal of the included mock/stub API is only code completion. Function parameter hints and comments are not included.

1700085924491.png
You can use many techniques to find what the parameter types are.
1700088512270.png
I often work side by side.
1700086209480.png


Related work:
 
Last edited:

stepalibre

Alibre Super User
If AlibreScript.py is not working you can add or delete classes, functions and properties or add new functionality for your needs.
You will need to rewrite existing scripts and write new scripts in a slightly different way in order to have code completion work. Existing simple scripts should work as is.
Only from AlibreScript import * is required and it work inside the Alibre Script addon.
Units Script:
Python:
# demonstrates using multiple units in a script
# create a part and a sketch
MyPart = Part('My Part')
XYPlane = MyPart.GetPlane('XY-Plane')
Sketch = MyPart.AddSketch('Sketch', XYPlane)
# set units to mm - this is implied at the start of every script
Units.Current = UnitTypes.Millimeters
# create circle 50mm in diameter
Sketch.AddCircle(0, 0, 50, False)
# set units to inches
# all values from now on are in inches
Units.Current = UnitTypes.Inches
# create a circle 1.34 inches in diameter
Sketch.AddCircle(0, 0, 1.34, False)
# switch to cm
# now all values from this point until the next units change are in cm
Units.Current = UnitTypes.Centimeters
# create a circle 4.2cm in diameter
Sketch.AddCircle(0, 0, 4.2, False)
Units Script using AlibreScript.py:
Python:
from AlibreScript import *
# demonstrates using multiple units in a script
# create a part and a sketch
MyPart = Part('My Part')
XYPlane = MyPart.GetPlane('XY-Plane')
def Figures():
    # set units to mm - this is implied at the start of every script
    Units.Current = UnitTypes.Millimeters
    # create circle 50mm in diameter
    Sketch.AddCircle(0, 0, 50, False)
    # set units to inches
    # all values from now on are in inches
    Units.Current = UnitTypes.Inches
    # create a circle 1.34 inches in diameter
    Sketch.AddCircle(0, 0, 1.34, False)
    # switch to cm
    # now all values from this point until the next units change are in cm
    Units.Current = UnitTypes.Centimeters
    # create a circle 4.2cm in diameter
    Sketch.AddCircle(0, 0, 4.2, False)
Sketch = MyPart.AddSketch('Sketch', XYPlane)
Figures()
1700105951581.png
I believe the technical reason for this is because the mock API is static. You need to use classes as is for code completion to work with the mock API. This is also the only way to get Alibre Script code written in VS Code to work as is in the Alibre Script addon. There are other approaches but this is the simplest setup I found for a mock/stub API.

Any questions, comments or better ideas are welcome.
 
Last edited:

stepalibre

Alibre Super User
You can load AlibreScript.py inside the VS Code console for further learning using the dir function.
Python:
exec(open(".\AlibreScript.py").read())
from AlibreScript import *
dir()
dir(Sketch)
dir(Part)
1700111884044.png
Outline view is useful:

1700112221239.png
 

stepalibre

Alibre Super User
Because the stub file is made for Python 3 it should work inside any Python IDE. I tried a few on my mac.

1700226685307.png
Screenshot 2023-11-17 at 6.56.56 AM.png
Screenshot 2023-11-17 at 6.51.46 AM.png
 

stepalibre

Alibre Super User
Final Update:
Follow this repo for Alibre Script stub file generation:
StubFileGenerationTools are scripts that write Python classes and functions made to be compatible with Alibre Script IronPython 2.7.
AlibreScriptAPIVSCodeDemo:
You can control Alibre for a more complete workflow with macros and scripts that click the Launch or Run buttons from VS Code.
 
Last edited:
Top