Using ChatGPT for Altium Scripting

Ari Mahpour
|  Created: July 21, 2023  |  Updated: July 28, 2024
Chat GPT for Altium Scripting

 

By now it’s been a few months for folks to get acquainted with Generative AI and, most famously, ChatGPT. In my last article, Using ChatGPT for Automated Testing, I introduced the ChatGPT service, showed you how to get familiar with it, and use it to generate code to run tests on your hardware. In this article we’re going to look at how to best utilize ChatGPT to write DelphiScript code to run automated functions within Altium itself using Altium Scripting.

Getting Started

Before we dive into the implementation, it’s important to understand what we’re working with here. Altium Scripting is best supported using DelphiScript (over other languages). While a quick search online yields results like, “It's 2022—Isn't It Time You Started Coding in Delphi?” and catchy headers such as, “The Pascal language that [is] used in Delphi is one of the most popular and universal programming languages,” I have never seen Delphi used outside of Altium Designer. That’s not to say it’s a bad language - it’s just not as commonly used as Python, C++, etc. There’s a lot to learn with a new language (especially if you’re not a native programmer), so Altium put together a lot of resources and even tooling around coding in DelphiScript here. If you’re looking to sink your teeth into the language, the resources there can be helpful.

Saving Your Project

As you get started with Altium Scripting, you will very quickly grow a large repository of scripts that you may want to save and/or share. To create a new Scripting project in Altium Designer, navigate to File > New > Script > Script Project. Right-click on your newly created project and navigate to Add New to Project > Script Unit. This will create a new .pas file which will be your script:

Chat GPT

Figure 1: Adding a script file to the existing project

We will want to save this to our Altium 365 workspace. First ensure you have saved the project locally. After that right click on the project again and navigate to History & Version Control > Add Project to Version Control…

Chat GPT

Figure 2: Adding project to the Altium 365 Workspace

 

Follow the prompts afterward to save the project to your Altium 365 Workspace. For further instructions on how to write, run, and debug DelphiScript code within Altium Designer consult the Altium documentation.

Full Generative AI

Now that we’re all set up, we’re ready to dive in. ChatGPT has the “potential” to completely write DelphiScript from beginning to end. I use " potential " instead of “capability” here because it doesn’t always get things right. Let’s take a simple example where we ask ChatGPT to generate a script in DelphiScript to list all the nets in my project. I started with a prompt that was, to be fair, very vague:

ChatGPT

Figure 3: Initial ChatGPT response

This automatically understood that I wanted to list all nets from a PcbDoc. I actually wanted it from the schematic, so I corrected it:

ChatGPT

Figure 4: One of many ChatGPT’s attempts to write the correct script

 

I haven’t displayed all the code that it generated for me, but what transpired after that was me running the code multiple times only to get Altium Designer to fail on errors such as “Undeclared Identifier: eNet,” or “Expected: in For Pin in Component. Pins Do.” All I did was respond to ChatGPT with those errors multiple times, and on the fifth try, it generated the code I was looking for.

What’s clear is that it isn’t terrific at generating DelphiScript from scratch, but it is capable of doing so. Remember that before you throw your keyboard at the wall in frustration.

AI Helper

ChatGPT can also be available as more of a sidekick or peer programmer. You can use ChatGPT to analyze your code, find bugs, or help you tweak specific functions. We can take an existing Altium Scripting example and have ChatGPT tweak it to meet our needs.

There are resources out there (especially hosted on GitHub) that provide Altium Scripting examples for you. I’m going to use the ones officially provided by Altium. They can be found here.

I’m starting with the PlaceAPort example located under \DelphiScript Scripts\SCH\Sch_Scripts.PrjScr. I’ve given ChatGPT a very trivial task:

ChatGPT

ChatGPT gave me the code that followed exactly this request. To be honest this is pretty simple if you’re semi-comfortable with programming. Let’s make it a bit harder that requires contextual knowledge of Altium Scripting.

Here’s my updated prompt to ChatGPT:

Using ChatGPT for Altium Scripting

 

It fails on the first shot with an Undeclared Identifier:

ChatGPT

Figure 5: Debugger error from Altium Designer

I let it know that and it apologizes, of course. It fails again with an “Undeclared identifier: Name” error but, by the third time, I get my net label exactly where it’s supposed to be:

ChatGPT

Figure 6: New net label and port generated by modified scripts

It’s obvious that with an increase in complexity the less of a chance ChatGPT will get your script right in the first shot. I’ve found that, with persistence, it is able to eventually get things right, especially if you can assist it a little (assuming you understand the code it’s generating). 

Conclusion

In this article we started with an introduction to Altium Scripting, how to create a project, and save it to your Altium 365 workspace. We dove into examples with ChatGPT where it completely generated scripts from scratch and also modified scripts that we provided it with. We demonstrated its capabilities and how it doesn’t always get things right. While it’s not perfect it certainly can act as a guide throughout your Altium Scripting journey. Most importantly, it’s certainly better to have it than to not have it at all.

About Author

About Author

Ari is an engineer with broad experience in designing, manufacturing, testing, and integrating electrical, mechanical, and software systems. He is passionate about bringing design, verification, and test engineers together to work as a cohesive unit.

Related Resources

Related Technical Documentation

Back to Home
Thank you, you are now subscribed to updates.