Altium Designer SDK 快速入门指南

概览

按照这份循序渐进的 SDK 指南,构建你的第一个自定义 Altium Designer 扩展。从配置开发工具、创建 C# 项目,到添加并运行用于显示原理图或项目网络名称的命令,本指南将带你走完整个流程。你还将了解支持热重载的命令行模板方案。继续阅读,迈出扩展 Altium Designer 功能的第一步。

- 了解 SDK 扩展开发的先决条件和环境配置步骤。
- 学习如何在 Altium Designer 中实现、注册和运行“显示网络”命令。
- 查找命令行模板和参考资源,为开发下一个扩展做好准备。
使用 AI 深入了解:

Altium Designer SDK 目前处于封闭测试阶段,仅向少数获选的抢先体验客户开放。

Altium Designer SDK 可让您构建直接集成到 Altium Designer 中的自定义扩展,通过托管 .NET API 添加命令、自动执行工作流程并访问设计数据。

本指南将引导您安装开发工具、创建扩展项目、添加命令,并在 Altium Designer 中运行该命令。

Prefer the command line? Altium Designer 扩展模板 无需使用应用内向导或 Visual Studio,即可创建包含一个命令的完整 C# 扩展,hot-reloads your code while Altium Designer keeps running.

dotnet new install Altium.Designer.Extension.Templates
dotnet new ad-extension -n MyTools --altium-system-dir "C:\Program Files\Altium\AD26\System"

它需要 Altium Designer 24 或更高版本以及 .NET SDK 8 或更高版本(Altium Designer 26.12 及更高版本需要 .NET SDK 10)。下方 Step 3 中的命令代码在基于模板的项目中同样适用。

前提条件

第 1 步:安装 Altium Developer 扩展

Altium Developer 扩展可将项目模板和 SDK 工具直接添加到 Altium Designer 中。

  1. 在 Altium Designer 中,转到 Extensions and Updates → Available。

  2. 在 Software Extensions 部分,搜索 Altium Developer。

  3. 安装扩展,然后根据提示重新启动 Altium Designer。

第 2 步:创建新的扩展项目

  1. 在 Altium Designer 中,转到 File → New → Other → Extension。

    • Extension ID:ShowNetsExtension

    • Extension Kind:Altium Designer Server

    • Development Language:C#

    • IDE Version:latest

    • SDK Version: latest

  2. 扩展创建完成后,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 中运行命令

  1. 启动 Altium Designer。

  2. 右键单击任意工具栏,然后选择 Customize…

  3. 在 Commands 下,单击 New… 并配置以下内容:

    • Process:ShowNetsExtension:ShowNets

    • Caption:ShowNets

  4. 将新命令拖到工具栏上。

  5. 单击该命令,此时应显示一个包含网络列表的对话框。

后续步骤

AI-LocalizedAI 翻译
如您发现任何问题,请选中相关文本/图片,并按 Ctrl + Enter 键向我们提交反馈。
Contents
Contents