Revit二次开发_使用InnoSetup打包插件

InnoSetup是一个开源的安装包制作工具。

如果是简单的安装,例如安装流程只需要用户选一个安装路径,那么直接按引导操作就行,甚至不需要写代码。

如果有更复杂的需求,通常需要使用Inno支持的Pascal脚本进行自定义。

Inno打包程序脚本引导部分比较简单,资料网上也非常多,这里不再赘述。

对于Revit插件安装,特别的是最后要把addin文件复制到合适的位置,并且修改内容,指向对应文件,这部分网上内容相对少,记录一下。

下面三个示例,第一个是常规打包,

第二个与第一个类似,但增加了一个自定义页面,让用户设置一个资源存放的文件夹,计划用于存放依赖资源,

第三个则是通过调用一个外部程序来执行addin修改操作——如果对Pascal脚本不熟悉,可以用熟悉的语言写一个程序,然后在脚本中进行调用,从而执行类似操作

示例一(常规打包)

; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName "MyRevitPlugin"
#define MyAppVersion "1.0"
#define MyAppPublisher "My Company, Inc."
#define MyAppURL "https://www.example.com/"

[Setup]
; NOTE: The value of AppId uniquely identifies this application. Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{782BAC7F-2B51-4378-B9CC-8559EB6E2CE7}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={autopf64}\{#MyAppName}
DefaultGroupName={#MyAppName}
DisableProgramGroupPage=yes
; Uncomment the following line to run in non administrative install mode (install for current user only.)
;PrivilegesRequired=lowest
OutputDir=F:\packet_ex
OutputBaseFilename=mysetup
Compression=lzma
SolidCompression=yes
WizardStyle=modern

[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"

// 调整dll路径;addin.template其实是常规的addin文件改了下文件名
[Files]
Source: "C:\Users\lisiting01\source\repos\MyRevitPlugin\MyRevitPlugin\bin\x64\Debug\MyRevitPlugin.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "F:\addin\2019.addin.template"; DestDir: "{app}\addin"; Flags: ignoreversion

[UninstallDelete]
Type: files; Name: "C:\ProgramData\Autodesk\Revit\Addins\2019\RevitPlugin.addin"

[Code]
var
  InstallPath: string;

// CurStepChanged事件,安装阶段变化时触发
procedure CurStepChanged(CurStep: TSetupStep);
var
  AddinTemplateFile: string;
  AddinTargetFile: string;
  AddinContentANSI: AnsiString;
  AddinContentUCS: string;
begin
  // 到达 ssPostInstall 阶段时(安装后)触发事件
  if CurStep = ssPostInstall then
  begin
    // 安装路径
    InstallPath := ExpandConstant('{app}');
    
    // .addin 模板文件和目标文件路径
    AddinTemplateFile := InstallPath + '\addin\2019.addin.template';
    AddinTargetFile := 'C:\ProgramData\Autodesk\Revit\Addins\2019\RevitPlugin.addin';
   
    // 注册 .addin 文件并指向到安装目录
    if LoadStringFromFile(AddinTemplateFile, AddinContentANSI) then
    begin
      AddinContentUCS := String(AddinContentANSI);
      StringChangeEx(AddinContentUCS, '{InstallPath}', InstallPath, True);
      SaveStringToFile(AddinTargetFile, AddinContentUCS, False);
    end;
  end;
end;

示例二(自定义页面)

; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName "MyRevitPlugin"
#define MyAppVersion "1.0"
#define MyAppPublisher "My Company, Inc."
#define MyAppURL "https://www.example.com/"

[Setup]
; NOTE: The value of AppId uniquely identifies this application. Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{782BAC7F-2B51-4378-B9CC-8559EB6E2CE7}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={autopf64}\{#MyAppName}
DefaultGroupName={#MyAppName}
DisableProgramGroupPage=yes
; Uncomment the following line to run in non administrative install mode (install for current user only.)
;PrivilegesRequired=lowest
OutputDir=F:\packet_ex
OutputBaseFilename=mysetup
Compression=lzma
SolidCompression=yes
WizardStyle=modern

[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"

// 调整dll路径;addin.template其实是常规的addin文件改了下文件名
[Files]
Source: "C:\Users\lisiting01\source\repos\MyRevitPlugin\MyRevitPlugin\bin\x64\Debug\MyRevitPlugin.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "F:\addin\2019.addin.template"; DestDir: "{app}\addin"; Flags: ignoreversion

[UninstallDelete]
Type: files; Name: "{app}\Settings.ini"
Type: files; Name: "C:\ProgramData\Autodesk\Revit\Addins\2019\RevitPlugin.addin"

[Code]
var
  InstallPath: string;
  InputDirWizardPage: TInputDirWizardPage;

// InitializeWizard事件,初始化安装向导时触发
procedure InitializeWizard;
begin
  // 创建自定义页面
  InputDirWizardPage := CreateInputDirPage(wpSelectDir, 
    '设置资源文件夹路径', '这个文件夹将用于存放相关资源(族、贴图、渲染资源等)', 
    '设置资源文件夹,然后点击下一步',
    True, 'BIMResource');
  InputDirWizardPage.Add('');
  InputDirWizardPage.Values[0] := ExpandConstant('{userdocs}')+'\BIMResource';

end;

// CurStepChanged事件,安装阶段变化时触发
procedure CurStepChanged(CurStep: TSetupStep);
var
  AddinTemplateFile: string;
  AddinTargetFile: string;
  AddinContentANSI: AnsiString;
  AddinContentUCS: string;
  ConfigFile: string;
begin
  // 到达 ssPostInstall 阶段时(安装后)触发事件
  if CurStep = ssPostInstall then
  begin
    // 安装路径
    InstallPath := ExpandConstant('{app}');
    
    // .addin 模板文件和目标文件路径
    AddinTemplateFile := InstallPath + '\addin\2019.addin.template';
    AddinTargetFile := 'C:\ProgramData\Autodesk\Revit\Addins\2019\RevitPlugin.addin';
    
    // 配置文件路径
    ConfigFile := InstallPath + '\Settings.ini';
    
    // 注册 .addin 文件并指向到安装目录
    if LoadStringFromFile(AddinTemplateFile, AddinContentANSI) then
    begin
      AddinContentUCS := String(AddinContentANSI);
      StringChangeEx(AddinContentUCS, '{InstallPath}', InstallPath, True);
      SaveStringToFile(AddinTargetFile, AddinContentUCS, False);
    end;
    
    // 创建用户指定的资源文件夹
    CreateDir(InputDirWizardPage.Values[0]);
    
    // 设置配置文件
    SaveStringToFile(ConfigFile, 'BIMResource=' + InputDirWizardPage.Values[0] , False);
  end;
end;

// 更新准备安装页的信息
function UpdateReadyMemo(Space, NewLine, MemoUserInfoInfo, MemoDirInfo, MemoTypeInfo, MemoComponentsInfo, MemoGroupInfo, MemoTasksInfo: String): String;
var
  S: String;
begin

  S := '';
  S := S + '安装路径:' + NewLine;
  S := S + Space + ExpandConstant('{app}') + NewLine;
  S := S + NewLine;
  S := S + '资源路径:' + NewLine;
  S := S + Space + InputDirWizardPage.Values[0] + NewLine;

  Result := S;
end;

示例三(调用程序执行)

; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName "MyRevitPlugin"
#define MyAppVersion "1.0"
#define MyAppPublisher "My Company, Inc."
#define MyAppURL "https://www.example.com/"

[Setup]
; NOTE: The value of AppId uniquely identifies this application. Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{782BAC7F-2B51-4378-B9CC-8559EB6E2CE7}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={autopf64}\{#MyAppName}
DefaultGroupName={#MyAppName}
DisableProgramGroupPage=yes
; Uncomment the following line to run in non administrative install mode (install for current user only.)
;PrivilegesRequired=lowest
OutputDir=F:\packet_ex
OutputBaseFilename=mysetup
Compression=lzma
SolidCompression=yes
WizardStyle=modern

[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"

// 调整dll路径;addin.template其实是常规的addin文件改了下文件名
[Files]
Source: "C:\Users\lisiting01\source\repos\MyRevitPlugin\MyRevitPlugin\bin\x64\Debug\MyRevitPlugin.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "F:\SetupProgram.exe"; DestDir: "{app}"; Flags: ignoreversion

[UninstallDelete]
Type: files; Name: "C:\ProgramData\Autodesk\Revit\Addins\2019\RevitPlugin.addin"

[Code]
var
  InstallPath: string;

// CurStepChanged事件,安装阶段变化时触发
procedure CurStepChanged(CurStep: TSetupStep);
begin
  // 到达 ssPostInstall 阶段时(安装后)触发事件
  if CurStep = ssPostInstall then
  begin
    // 安装路径
    InstallPath := ExpandConstant('{app}');
    // 执行 SetupProgram.exe
    Exec(ExpandConstant(InstallPath + '\SetupProgram.exe'), '', '', SW_SHOW, ewNoWait, ResultCode);
  end;
end;

资料:

InnoSetup文档:https://jrsoftware.org/ishelp.php

中文安装语言:https://github.com/kira-96/Inno-Setup-Chinese-Simplified-Translation

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值