Altium Designer SDK 快速入门指南
概览
- 了解 SDK 扩展开发的先决条件和环境配置步骤。
- 学习如何在 Altium Designer 中实现、注册和运行“显示网络”命令。
- 查找命令行模板和参考资源,为开发下一个扩展做好准备。
Altium Designer SDK 可让您构建直接集成到 Altium Designer 中的自定义扩展,通过托管 .NET API 添加命令、自动执行工作流程并访问设计数据。
本指南将引导您安装开发工具、创建扩展项目、添加命令,并在 Altium Designer 中运行该命令。
前提条件
-
注册 Altium 开发者中心 ——这是获取 Altium Designer SDK 及其扩展访问权限的必要条件
-
兼容 .NET 的 IDE(Visual Studio 或支持 C# 的 VS Code)
第 1 步:安装 Altium Developer 扩展
Altium Developer 扩展可将项目模板和 SDK 工具直接添加到 Altium Designer 中。
-
在 Altium Designer 中,转到 Extensions and Updates → Available。
-
在 Software Extensions 部分,搜索 Altium Developer。
-
安装扩展,然后根据提示重新启动 Altium Designer。
第 2 步:创建新的扩展项目
-
在 Altium Designer 中,转到 File → New → Other → Extension。
-
Extension ID:
ShowNetsExtension -
Extension Kind:
Altium Designer Server -
Development Language:
C# -
IDE Version:
latest -
SDK Version:
latest
-
-
扩展创建完成后,Altium Designer 会显示其详细信息。记下 Source Location ——在 IDE 中打开该路径下的
.csproj文件。
第 3 步:添加您的第一个命令
打开 Source Code\Commands.cs,并将以下方法添加到 Commands 类中:
/// <summary>
/// Determines the enabled/visible state for the "Show Nets" command in Altium Designer.
/// </summary>
/// <param name="argContext">The current server document view context.</param>
/// <param name="argParameters">Command parameters (unused).</param>
/// <param name="argEnabled">Set to true if the command should be enabled.</param>
/// <param name="argChecked">Set to true if the command should appear checked (unused).</param>
/// <param name="argVisible">Set to true if the command should be visible (unused).</param>
/// <param name="argCaption">Command caption (unused).</param>
/// <param name="argImageFile">Command image file (unused).</param>
public static void GetState_ShowNets(IServerDocumentView argContext, ref string argParameters, ref bool argEnabled,
ref bool argChecked, ref bool argVisible, ref string argCaption, ref string argImageFile)
{
// Retrieve the currently focused project from Altium Designer's workspace.
var project = DXP.GlobalVars.DXPWorkSpace.DM_FocusedProject() as IProject;
// If the project needs to be compiled (e.g., netlist out of date), compile it.
if (project?.DM_NeedsCompile() == true)
project.DM_Compile();
// Enable the command only if a valid project is focused and it's not the "Free Documents" project.
argEnabled = project != null && !IsFreeDocumentsProject(project);
}
/// <summary>
/// Command handler to display all net names in the current schematic document or project.
/// </summary>
/// <param name="view">The current server document view.</param>
/// <param name="parameters">Command parameters (unused).</param>
public static void Command_ShowNets(IServerDocumentView view, ref string parameters)
{
// Get the currently focused project from Altium Designer.
var project = DXP.GlobalVars.DXPWorkSpace.DM_FocusedProject() as IProject;
if (project == null || IsFreeDocumentsProject(project))
return;
string documentName;
// Get the currently focused document (e.g., schematic sheet).
var document = DXP.GlobalVars.DXPWorkSpace.DM_FocusedDocument() as IDocument;
if (document == null)
{
// If no document is focused, ensure the project is compiled and use the flattened project document.
if (project.DM_NeedsCompile()) project.DM_Compile();
document = project.DM_DocumentFlattened();
documentName = "Project";
}
else
{
// Only proceed if the focused document is a schematic (DocKindSch).
if (document.DM_DocumentKind() != EDPConstant.DocKindSch) return;
documentName = document.DM_FileName();
}
// Enumerate all nets in the document, retrieve their full names, and sort them alphabetically.
var netNames = Enumerable.Range(0, document.DM_NetCount())
.Select(document.DM_Nets)
.Select(net => net.DM_FullNetName())
.OrderBy(name => name);
// Display the list of net names in an information dialog within Altium Designer.
DXP.Utils.ShowInfo(string.Join(Environment.NewLine, netNames), $"Nets [{documentName}]");
}
/// <summary>
/// Checks if the given project is the special "Free Documents" project in Altium Designer.
/// </summary>
/// <param name="project">The project to check.</param>
/// <returns>True if the project is the Free Documents project; otherwise, false.</returns>
private static bool IsFreeDocumentsProject(IProject project)
{
return project == DXP.GlobalVars.DXPWorkSpace.DM_FreeDocumentsProject();
}
此处使用的类型(IProject、 IDocument 和 DXP.GlobalVars )在 Altium Designer SDK 参考文档中有详细说明。
打开 Source Code\Main.cs ,并在 PluginServerModule 类中注册该命令:
protected override void InitializeCommands()
{
((CommandLauncher)CommandLauncher).RegisterCommand("ShowNets", Commands.Command_ShowNets, Commands.GetState_ShowNets);
}
构建项目以安装扩展。
第 4 步:在 Altium Designer 中运行命令
-
启动 Altium Designer。
-
右键单击任意工具栏,然后选择 Customize…
-
在 Commands 下,单击 New… 并配置以下内容:
-
Process:
ShowNetsExtension:ShowNets -
Caption:
ShowNets
-
-
将新命令拖到工具栏上。
-
单击该命令,此时应显示一个包含网络列表的对话框。
后续步骤
-
浏览 Altium Designer SDK 参考文档,了解
SCH、PCB、EDP、DXP、ComponentLib和Embedded命名空间的完整 .NET API。 -
阅读 API 约定 ,了解入口点、GetState/SetState 方法和枚举的组织方式。
-
使用 Altium Designer 扩展模板开始创建下一个扩展,并在 Altium Designer 运行期间使用热重载。