还剩23页未读,继续阅读
本资源只提供10页预览,全部文档请下载后查看!喜欢就下载吧,查找使用更方便
文本内容:
二次开发入门AltiumDesigner二次开发概述
1.Altium Designer什么是二次开发
1.1二次开发是指在现有的软件基础上,通过编写额外的代码或使用提供的开发工具,实现对软件功能的扩展、优化或定制在设计软件领域,提供了PC Altium Designer一套强大的二次开发工具和技术,使用户能够根据自己的需求定制软件,提高设计效率二次开发的应用场景
1.2二次开发在中的应用场景非常广泛,包括但不限于-自定义脚本Altium Designer实现自动化任务,如批量生成文件、自动布局等-扩展功能添加新的功能模块,如特定的仿真工具、检查工具等-集成第三方工具将与其他设计Altium Designer工具或管理系统集成,实现数据互通-优化工作流程通过脚本或插件,优化设计流程,减少重复性工作二次开发的优势13通过二次开发,用户可以获得以下优势-提高效率自动化重复任务,节省时间-增加灵活性根据具体需求定制功能,满足特定设计要求-增强协作:与其他工具和系统集成,提升团队协作效率-提升质量通过自定义检查和验证工具,提高设计质量二次开发环境搭建
2.Altium Designer安装开发工具
2.1提供了多种二次开发工具,其中最常用的是和Altium DesignerAltium ScripterAdd-Ino以下是安装这些工具的步骤安装
1.Altium Designerpublic void OnCustomCommandIDocumentPCBpcbDoc=PCBServer.PCBEditServer.GetCurrentPCBDocument;I Board board=pcbDoc.Board;〃执行自定义命令MessageBox.ShowCustom CommandExecuted;}}添加工具栏按钮using System;using Altium.Drivers;using Altium.UI;[ComVisibletrue][GuidnB8DlC560-0E2D-4E0C-924B-lC2D4E0C924B]public classCustomToolbarAddln:lAltiumAddln public void OnStartup;AltiumMenu.RegisterCommandCustomToolbarAddIn,Custom Button,OnCustomButto npublic void OnShutdownAltiumMenu.UnregisterCommandCustomToolbarAddln,Custom Button;public voidOnCustomButtonIDocumentPCB pcbDoc=PCBServer.PCBEditServer.GetCurrentPCBDocument;I Board board=pcbDoc.Board;〃执行自定义命令MessageBox.ShowCustom ButtonClicked;事件处理和监听
3.6监听板变化事件361PCBusing System;using Altium.Drivers;using Altium.UI;[ComVisibletrue][GuidB8DlC560-0E2D-4E0C-924B-lC2D4E0C924B]public classPCBEventAddln:lAltiumAddln,IPCBEventNotifypublic void OnStartupOAltiumMenu.RegisterCommandPCBEventAddln,Start Listening,OnStartListening;PCBServer.PCBEditServer.AddPCBEventNotifythis;public void OnShutdownAltiumMenu.UnregisterCommandPCBEventAddln,Start Listening;PCBServer.PCBEditServer.RemovePCBEventNotifythis;public voidOnStartListeningMessageBox.ShowListening toPCB eventsstarted;public voidOnPCBEventIPCBBoard Board,PCBEvent Event,int EventDataif Event==PCBEvent.PCBEventComponentMovedMessageBox.ShowComponent Moved;监听原理图变化事件using System;using Altium.Drivers;using Altium.UI;[ComVisibletrue][Guid,,B8DlC560-0E2D-4E0C-924B-lC2D4E0C924B]public classSchEventAddln:lAltiumAddln,ISchEventNotifypublic voidOnStartupOAltiumMenu.RegisterCommandSchEventAddln,Start Listening,OnStartListening;SchServer.SchEditServer.AddSchematicEventNotifythis;public voidOnShutdownAltiumMenu.UnregisterCommandSchEventAddln,Start Listening;SchServer.SchEditServer.RemoveSchematicEventNotifythis;public voidOnStartListeningMessageBox.ShowListening toSchematic eventsstarted;public voidOnSchematicEventISchematicDocument SchDoc,SchEvent Event,int EventDataifEvent==SchEvent.SchEventComponentMovedMessageBox.ShowComponent Moved;}交互式设计工具
3.7创建自定义工具371using System;using Altium.Drivers;using Altium.UI;[ComVisibletrue][GuidB8DlC560-0E2D-4E0C-924B-lC2D4E0C924B]public classCustomToolAddln:lAltiumAddln,IlnteractiveTool public void OnStartupAltiumMenu.RegisterCommandCustomToolAddln,Custom Tool,OnCustomTool;public voidOnShutdownAltiumMenu.UnregisterCommandCustomToolAddln,Custom Tool;public voidOnCustomToolPCBServer.PCBEditServer.RunlnteractiveToolthis;public voidOnStartMessageBox.ShowCustom ToolStarted;public voidOnEndMessageBox.ShowCustom ToolEnded;public voidOnActionIDocumentPCB pcbDoc=PCBServer.PCBEditServer.GetCurrentPCBDocument;I Board board=pcbDoc.Board;〃获取用户选择的元器件IPCBComponent selectedComponent=board.GetSelectedComponent;if selectedComponent!=null〃执行自定义操作selectedComponent.Location.X+=1000;selectedComponent.Location.Y+=1000;board.Refresh;}数据验证和优化
3.8刍定义设计规则检查,自定义设计规则检查Sub CustomDRCDimPCBServer As IPCBServerSet PCBServer=GetPCBServerDim BoardAs I BoardDim DRCAs IDRCSetDRC=Board.DRC添加自定义规则Dim CustomRuleAs IDRCRuleSetCustomRule=DRC.AddRuleCustom Rule,Custom RuleDescription,设置规则参数CustomRule.AddParameter MinDistance,1000运行检查DRCDRC.ExecuteEnd Sub优化布局优化布局#Sub OptimizeLayoutDimPCBServer As IPCBServerSet PCBServer=GetPCBServerDim BoardAs I Board.常用开发技术Set##3数据验证和优化###
3.8自定义设计规则检查#vbscript1自定义设计规则检查Sub CustomDRCDimPCBServer As IPCBServerSet PCBServer=GetPCBServerDim BoardAs I BoardDim DRCAs IDRCSetDRC=Board.DRC‘添加自定义规则Dim CustomRuleAs IDRCRuleSetCustomRule=DRC.AddRuleCustom Rule*1,Custom RuleDescription设置规则参数CustomRule.Add ParameterMinDistance,1000运行检查DRCDRC.ExecuteEnd Sub优化布局*优化布局Sub OptimizeLayoutDimPCBServer As IPCBServerSet PCBServer=GetPCBServerDim BoardAs IBoard,获取所有元器件Dim Components As IComponentsSet Components=Board.ComponentList计算元器件的中心位置Dim centerXAs DoubleDimcenterY AsDoublecenterX=0centerY=0Dim iAs IntegerFor i=0To Components.Count-1centerX=centerX+Components.ltemi.Location.XcenterY=centerY+Components.ltemi.Location.YNext icenterX=centerX/Components.CountcenterY=centerY/Components.Count,重新布局元器件For i=0To Components.Count-1Dim ComponentAs IPCBComponentSetComponent=Components.Itemi计算新的位置Dim newXAs DoubleDimnewY AsDoublenewX=centerX+i Mod5*2000newY=centerY+i\5*2000,设置新的位置Component.Location.X=newXComponent.Location.Y=newYNext i,刷新板PCBBoa rd.RefreshEnd Sub创建自定义报告高级功能开发
3.9’创建自定义报告Sub CreateCustomReportDimPCBServer AsIPCBServerSet PCBServer=GetPCBServerDim BoardAsIBoardDim ComponentsAs IComponentsSetComponents=Board.ComponentList,创建报告文件Dim ReportFileName As StringReportFileName=CustomReport.txtDim FileSystemAs ObjectSetFileSystem=CreateObjectHScripting.FileSystemObjectDim ReportFileAs ObjectSetReportFile=FileSystem.CreateTextFileReportFileName,True’写入报告内容Dim iAs IntegerFori=0To Components.Count-1Dim ComponentAs IPCBComponentSetComponent=Components.ItemiReportFile.WriteLine Component Name:Component.NameReportFile.WriteLine Location:Component.Location.X,Component.Location.Y1111ReportFile.WriteLine Footprint:Component.FootprintReportFile.WriteLine-------------------------------Next iReportFile.CloseMsgBox HCustomReport Created:ReportFileNameEnd Sub创建自定义属性392创建自定义属性Sub AddCustomPropertyDimPCBServer AsIPCBServerSet PCBServer=GetPCBServerDim BoardAsIBoardDim ComponentsAs IComponentsSetComponents=Board.ComponentList,遍历所有元器件并添加自定义属性Dim iAs IntegerFori=0To Components.Count-1Dim ComponentAs IPCBComponentSetComponent=Components.Itemi检查属性是否存在If NotComponent.PropertyExistsCCustomProperty Then添加自定义属性Component.AddProperty CustomProperty,CustomValue End IfNext i,刷新板PCBBoa rd.RefreshMsgBox CustomProperty Addedto AllComponentsEnd Sub集成第三方工具
3.10集成数据管理系统
3.
10.1using System;using Altium.Drivers;using Altium.UI;[ComVisibletrue][GuidB8DlC560-0E2D-4E0C-924B-lC2D4E0C924B]public classDataManagementAddln:lAltiumAddln public void OnStartupAltiumMenu.RegisterCommandDataManagementAddln,Export to Data ManagementSystern,OnExportToDMS;public voidOnShutdownAltiumMenu.UnregisterCommandDataManagementAddln,Export toData Management System;public voidOnExportToDMSIDocumentPCB pcbDoc=PCBServer.PCBEditServer.GetCurrentPCBDocument;IBoard board=pcbDoc.Board;〃获取所有元器件Components components=board.ComponentList;〃构建数据包string data=PCB DesignData:\n;foreach IPCBComponentcomponent incomponentsdata+=$Component Name:{component.Name},X:{component.Location.X},Y:{component.Location.Y}\n;〃调用第三方数据管理系统APT假设第三方系统提供了一个//HTTP APIstringurl=;HttpPostDataurl,data;〃显示成功消息MessageBox.ShowData exportedtoDataManagementSystem;从官方网站下载并安装最新版本的o AltiumAltium Designer安装
2.Altium Scripter是一个脚本开发工具,支持和Altium ScripterVBScript JavaScriptoo在中,通过“工具”-”扩展和自定义”-“脚本管O Altium Designer理器”来安装和管理脚本安装开发工具
3.Add-In开发工具允许用户使用等语言开发插件Add-In C#o需要安装并配置的Visual Studio,Altium DesignerSDKo O配置开发环境
2.2配置
1.Altium Scripter打开进入“工具”-“扩展和自定义”-“脚本管O Altium Designer,理器”确保脚本语言支持已启用,如和VBScript JavaScriptoo创建一个新的脚本文件,选择合适的语言进行编写O配置开发环境
2.Add-In安装Visual StudiOoo下载并解压的Altium DesignerSDKo O在中创建一个新的项目,引用的库Visual StudioC#Altium DesignerSDKo配置项目属性,确保能够正确编译和部署插件O创建第一个脚本
2.3示例
2.
3.1VBScript创建一个新的文档PCBSub CreateNewPCBDocDimNewPCBDoc As IDocumentPCBSet NewPCBDoc=PCBServer.PCBEditServer.CreatePCBDocumentNewPCB.PcbDocNewPCBDoc.Saveprivate voidHttpPostDatastring url,string data使用或其他库发送请求//HttpClient HTTPPOST〃以下是一个简单的示例using varclient=new System.Net.WebClient client.Headers[Content-Type]=application/x-www-form-urlencoded;string response=client.UploadStringurl,data=+data;if response==Success MessageBox.ShowfData uploadedsuccessfully;elseMessageBox.ShowData uploadfailed;}}集成仿真工具using System;using Altium.Drivers;using Altium.UI;[ComVisibletrue][GuidB8DlC560-0E2D-4E0C-924B-lC2D4E0C924B]public classSimulationAddln:lAltiumAddlnpublic voidOnStartupOAltiumMenu.RegisterCommandCSimulationAddln,Run Simulation,OnRunSimulation;public voidOnShutdownAltiumMenu.UnregisterCommandCSimulationAddln,Run Simulation;public voidOnRunSimulationISchematicServer schServer=SchServer.SchEditServer;ISchematicDocument schDoc=schServer.GetCurrentSchematicDocument;〃获取所有元器件Components components=schDoc.ComponentList;构建仿真数据//string simulationData=Simulation Data:\n;foreach ISchematicComponentcomponent incomponents simulationData+=$Component Name:{component.Name},Value:{component.Value}\n〃调用第三方仿真工具API假设第三方仿真工具提供了一个命令行接口//string command=C:\\Path\\To\\SimulationTool.exe;string arguments=-input+simulationData+-output C:\\Path\\To\\SimulationResults.txtRunSimulationToolcommand,arguments;〃显示成功消息MessageBox.ShowSimulation completed;}private voidRunSimulationToolstring command,string arguments使用类启动仿真工具//Processusing varprocess=new System.Diagnostics.ProcessO process.Startlnfo.FileName=command;process.Startinfo.Arguments=arguments;process.Startinfo.UseShellExecute=false;process.Startlnfo.RedirectStandardOutput=true;process.Startlnfo.CreateNoWindow=true;process.Start;process.WaitForExit;if process.ExitCode==0MessageBox.ShowSimulation toolexecuted successfully;elseMessageBox.ShowSimulation toolexecution failed;}最佳实践
3.代码组织和模块化
3.
11.1•使用类和模块将功能相关的代码组织到类和模块中,提高代码的可读性和可维护性•遵循命名规范使用清晰、一致的命名规范,便于理解和维护•注释和文档为代码添加注释,撰写文档,帮助其他开发者理解和使用错误处理和日志记录
3.
11.2•错误处理使用语句处理可能的异常,确保程序的稳定性try-catch•日志记录记录关键操作和错误信息,便于调试和问题追踪using System;using Altium.Drivers;using Altium.UI;[ComVisibletrue][Guid,,B8DlC560-0E2D-4E0C-924B-lC2D4E0C924B]public classErrorHandlingAddln:lAltiumAddlnpublic voidOnStartupOAltiumMenu.RegisterCommandErrorHandlingAddln,Run Task,OnRunTask;public voidOnShutdownAltiumMenu.UnregisterCommandErrorHandlingAddln,Run Task;publicvoidOnRunTasktry〃执行任务RunTasklnternal;MessageBox.ShowTask completedsuccessfully;}catch Exceptionex〃记录错误日志LogErrorex.Message;MessageBox.ShowTask failed:+ex.Message;}private voidRunTasklnternal〃示例任务获取当前板并创建一个新的元器件:PCBIDocumentPCB pcbDoc=PCBServer.PCBEditServer.GetCurrentPCBDocument;IBoardboard=pcbDoc.Board;〃创建新的元器件IPCBComponent newComponent=board.CreateComponentNewComponent;newComponent.Location.X=1000;newComponent.Location.Y=1000;board.AddComponentnewComponent;〃刷新灰PCBboard.Refresh;}private voidLogErrorstring message;string logFilePath=C:\\Path\\To\\Logs\\ErrorLog.txt”using StreamWritersw=new StreamWriterlogFilePath,true sw.WriteLine${DateTime.Now}:{message};常见问题和解决方案
3.12脚本无法运行
3.
12.1•检查脚本语言是否启用确保中已启用脚本语言支持Altium Designer•检查脚本语法确保脚本没有语法错误•检查权限确保脚本有权限访问所需的对象和方法插件无法加载
3.
12.2•检查项目配置确保项目属性配置正确,能够正确编译和部署•检查注册表确保插件的和路径已正确注册到注册表GUID Windows•检查依赖库确保所有依赖库已正确引用和安装进一步学习资源
3.13•官方文档官方文档提供了详细的参考和开发指南Altium DesignerAPI•社区论坛社区论坛是获取帮助和交流经验的好地方Altium Designer•示例代码官方提供的示例代码可以帮助你更好地理解各种开发技术.结论4通过二次开发,用户可以极大地提升的使用体验和设计效率无论Altium Designer是简单的脚本编写还是复杂的插件开发,都提供了强大的工具和技AltiumDesigner术支持希望本文档能帮助你快速入门的二次开发,开启新的设计AltiumDesigner之旅NewPCBDoc.LoadEnd Sub示例
2.
3.2JavaScript〃创建一个新的文档PCBfunction CreateNewPCBDoc{var NewPCBDoc=RCBServer.PCBEditServer.CreatePCBDocumentCNewRCB.PcbDoc;NewPCBDoc.Save;NewPCBDoc.Load;}创建第一个插件
2.4示例
2.
4.1C#using System;using System.Runtime.InteropServices;using Altium.Drivers;using Altium.UI;[ComVisibletrue][Guid,,B8DlC560-0E2D-4E0C-924B-lC2D4E0C924B,*]public classMyAddln:lAltiumAddlnpublic voidOnStartup〃注册菜单项AltiumMenu.RegisterCommandMyAddln,My Command,OnMyCommand;}publicvoidOnShutdown注销菜单项//AltiumMenu.UnregisterCommandMyAddln,My Command;}publicvoidOnMyCommand创建一个新的文档//PCBIDocumentPCB newPCBDoc=newPCBDoc.Save;newPCBDoc.Load;脚本和插件的调试
2.5脚本调试
1.在中,通过“工具”-“扩展和自定义”-“脚本管o AltiumDesigner理器”来运行和调试脚本使用内置的调试工具,如断点、单步执行等o插件调试
2.在中,通过键启动调试模式o VisualStudio F5确保作为调试目标,可以在“项目属性”中设置o AltiumDesigner使用的调试工具,如断点、单步执行等o VisualStudio常用开发技术
3.脚本语言基础
3.1基础
3.
1.1VBScript•变量声明Dim variableNameAs DataType•条件语句If conditionThen,执行代码Else,执行代码EndIf•循环语句Fori=1To10,执行代码Next i基出
3.
1.2JavaScript ii•变量声明var variableName=value;•条件语句if condition{〃执行代码}else{〃执行代码•循环语句for vari=1;i=10;i++{〃执行代码__}_______________________________________________________________对象模型
3.2AltiumDesigner主要对象
3.
2.1提供对编辑器的访问•PCBServer PCB表示板•Board PCB表示板上的各种对象,如元器件、网络等•PCBObject PCB提供对原理图编辑器的访问•SchServer表示文档对象,如文档、原理图文档等•Document PCB对象方法和属性
3.
2.2•创建板PCBDim NewBoardAs IBoard•获取板上的元器件PCBDim BoardAs IBoardDim ComponentsAs IComponentsSetComponents=Board.ComponentList•创建原理图:var NewSchDoc=SchServer.SchEditServer.CreateSchematicDocumentC^ewSchematic.SchDoc;•获取原理图上的元器件:var SchDoc=SchServer.SchEditServer.GetCurrentSchematicDocument;var Components=SchDoc.ComponentList;自动化任务示例
3.3批量生成文件331,批量生成文件Sub BatchGenerateFilesDimPCBServer AsIPCBServerSet PCBServer=GetPCBServerDim BoardAsIBoardDim PCBDocAsIDocumentPCBSetPCBDoc=Board.DocumentDim FileNamesAs VariantFileNames=ArrayFilel.txt,File
2.txt,File
3.txtDim iAs IntegerFori=0To UBoundFileNamesDimFileContent AsStringFileContent=This is the contentofFileNamesi,写入文件Dim FileSystemAs ObjectSetFileSystem=CreateObjectCScripting.FileSystemObjectDim FileAs ObjectSetFile=FileSystem.CreateTextFilefFileNamesfi,TrueFile.WriteLine FileContentFile.CloseNext iEndSub自动布局*自动布局Sub AutoPlaceComponentsDimPCBServer AsIPCBServerSet PCBServer=GetPCBServerDim BoardAsIBoardDim ComponentsAsIComponentsSetComponents=Board.ComponentListDim iAs IntegerFori=0To Components.Count-1DimComponentAs IPCBComponentSetComponent=Components.Itemi设置元器件位置Component.Location.X=i*1000Component.Location.Y=0Next iEndSub数据处理和交换
3.4读取和写入数据文件341,读取数据文件Sub ReadDataFileDimFileName AsStringFileName=Data.txt’读取文件Dim FileSystemAs ObjectSetFileSystem=CreateObjectScripting.FileSystemObjectDim FileAs ObjectSetFile=FileSystem.OpenTextFileFileName,1,FalseDim LineAs StringWhileNot File.AtEndOfStreamLine=File.ReadLineMsgBox LineWendFile.CloseEnd Sub,写入数据文件Sub WriteDataFileDimFileNameAsStringFileName=Data.txt,写入文件Dim FileSystemAs ObjectSetFileSystem=CreateObjectScripting.FileSystemObjectDim FileAs ObjectSetFile=FileSystem.CreateTextFileFileName,TrueFile.WriteLine This isthefirst lineFile.WriteLine“Thisisthe secondline,File.CloseEnd Sub与其他工具的数据交换using System;using System.IO;using Altium.Drivers;using Altium.UI;[ComVisibletrue][GuidB8DlC560-0E2D-4E0C-924B-lC2D4E0C924Bn]public classDataExchangeAddln:lAltiumAddln publicvoidOnStartupOAltiumMenu.RegisterCommandDataExchangeAddln,Export Data,OnExportData;publicvoidOnShutdownAltiumMenu.UnregisterCommandDataExchangeAddln,Export Data;publicvoidOnExportDataIDocumentPCB pcbDoc=PCBServer.PCBEditServer.GetCurrentPCBDocument;IBoardboard=pcbDoc.Board;string filePath=C:\\Path\\To\\Output\\Data.txt;using StreamWritersw=new StreamWriterfilePathforeach IPCBComponentcomponent inboard.ComponentListsw.WriteLine$ComponentName:{component.Name},X:{component.Location.X},Y:{component.Location.Y};}用户界面定制
3.5添加菜单项
3.
5.1using System;using Altium.Drivers;using Altium.UI;[ComVisibletrue][GuidB8DlC560-0E2D-4E0C-924B-lC2D4E0C924B]public classCustomUIAddln:lAltiumAddlnpublic voidOnStartupO;AltiumMenu.RegisterCommandfCustomUIAddln,Custom Command,OnCustomComman dpublicvoidOnShutdownAltiumMenu.UnregisterCommandCustomUIAddln,Custom Command;。
个人认证
优秀文档
获得点赞 0