178 lines
6.8 KiB
PowerShell
178 lines
6.8 KiB
PowerShell
# 家具导出插件 - 一键安装脚本(PowerShell)
|
||
# 目标:
|
||
# 1) 复制 bundle 到当前用户:%AppData%\Autodesk\ApplicationPlugins
|
||
# 2) 写入 AutoCAD 2024 自启动注册表(避免每次 NETLOAD)
|
||
# 3) 追加可信路径 TRUSTEDPATHS(SECURELOAD=1 时必须,否则不自动加载)
|
||
|
||
$ErrorActionPreference = "Stop"
|
||
|
||
function Write-Step {
|
||
param([string]$Text)
|
||
Write-Host $Text -ForegroundColor Cyan
|
||
}
|
||
|
||
function Test-IsAdmin {
|
||
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
|
||
$principal = New-Object Security.Principal.WindowsPrincipal($identity)
|
||
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
|
||
}
|
||
|
||
function Add-TrustedPath {
|
||
param(
|
||
[AllowNull()][string]$Existing,
|
||
[Parameter(Mandatory = $true)][string]$PathToAdd
|
||
)
|
||
|
||
$normalizedAdd = $PathToAdd.Trim().TrimEnd('\')
|
||
$items = @()
|
||
|
||
if ($Existing) {
|
||
$items = $Existing.Split(';') | ForEach-Object { $_.Trim() } | Where-Object { $_ -ne "" }
|
||
}
|
||
|
||
if (-not $items -or $items.Count -eq 0) {
|
||
# 保留 AutoCAD 常见默认值,避免误删用户原本设置
|
||
$items = @("APP_PATH")
|
||
}
|
||
|
||
$exists = $false
|
||
foreach ($it in $items) {
|
||
if ($it.Trim().TrimEnd('\').Equals($normalizedAdd, [StringComparison]::OrdinalIgnoreCase)) {
|
||
$exists = $true
|
||
break
|
||
}
|
||
}
|
||
|
||
if (-not $exists) {
|
||
$items += $PathToAdd
|
||
}
|
||
|
||
# 去重(忽略大小写 + 末尾反斜杠)
|
||
$seen = New-Object "System.Collections.Generic.HashSet[string]" ([StringComparer]::OrdinalIgnoreCase)
|
||
$dedup = New-Object System.Collections.Generic.List[string]
|
||
foreach ($it in $items) {
|
||
$k = $it.Trim().TrimEnd('\')
|
||
if ($seen.Add($k)) {
|
||
$dedup.Add($it.Trim())
|
||
}
|
||
}
|
||
|
||
return ($dedup -join ";")
|
||
}
|
||
|
||
Write-Step "[0/3] 环境检查..."
|
||
|
||
if (Get-Process acad -ErrorAction SilentlyContinue) {
|
||
Write-Host ""
|
||
Write-Host "注意:检测到 AutoCAD 正在运行 (acad.exe)。" -ForegroundColor Yellow
|
||
Write-Host "为避免 DLL 被占用导致复制/删除失败,建议先完全关闭 AutoCAD 再安装。" -ForegroundColor Yellow
|
||
Write-Host ""
|
||
}
|
||
|
||
if (Test-IsAdmin) {
|
||
Write-Host ""
|
||
Write-Host "注意:你正在以管理员身份运行。" -ForegroundColor Yellow
|
||
Write-Host "本插件按“当前 Windows 用户”安装(%AppData% + HKCU 注册表)。" -ForegroundColor Yellow
|
||
Write-Host "如果不是在设计师账号下运行,安装后可能在设计师账号下不生效。" -ForegroundColor Yellow
|
||
Write-Host ""
|
||
}
|
||
|
||
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||
$bundleSrc = Join-Path $scriptDir "FurnitureExporter.bundle"
|
||
|
||
# 兼容“解压覆盖旧目录”导致的套娃结构:FurnitureExporter.bundle\FurnitureExporter.bundle\...
|
||
if (-not (Test-Path (Join-Path $bundleSrc "PackageContents.xml"))) {
|
||
$nested = Join-Path $bundleSrc "FurnitureExporter.bundle"
|
||
if (Test-Path (Join-Path $nested "PackageContents.xml")) {
|
||
$bundleSrc = $nested
|
||
}
|
||
}
|
||
|
||
if (-not (Test-Path (Join-Path $bundleSrc "PackageContents.xml"))) {
|
||
Write-Host "未找到 FurnitureExporter.bundle(请先解压安装包,再运行 安装.bat)。" -ForegroundColor Red
|
||
exit 1
|
||
}
|
||
|
||
$appPlugins = Join-Path $env:APPDATA "Autodesk\ApplicationPlugins"
|
||
$bundleDst = Join-Path $appPlugins "FurnitureExporter.bundle"
|
||
$dllPath = Join-Path $bundleDst "Contents\FurnitureExporter.dll"
|
||
$trustedDir = Join-Path $bundleDst "Contents"
|
||
|
||
Write-Step "[1/3] 复制插件..."
|
||
New-Item -ItemType Directory -Path $appPlugins -Force | Out-Null
|
||
|
||
if (Test-Path $bundleDst) {
|
||
try {
|
||
Remove-Item -Path $bundleDst -Recurse -Force
|
||
}
|
||
catch {
|
||
Write-Host ""
|
||
Write-Host "删除旧版本失败:$bundleDst" -ForegroundColor Red
|
||
Write-Host "常见原因:AutoCAD 未完全关闭/文件被占用/安全软件拦截。" -ForegroundColor Yellow
|
||
Write-Host "请关闭 AutoCAD 后重试,或手动删除上述目录。" -ForegroundColor Yellow
|
||
Write-Host ""
|
||
throw
|
||
}
|
||
}
|
||
|
||
Copy-Item -Path $bundleSrc -Destination $bundleDst -Recurse -Force
|
||
|
||
if (-not (Test-Path $dllPath)) {
|
||
Write-Host "安装失败:未找到 DLL:$dllPath" -ForegroundColor Red
|
||
exit 1
|
||
}
|
||
|
||
Write-Step "[2/3] 注册自启动/可信路径..."
|
||
|
||
$acadRoot = "HKCU:\Software\Autodesk\AutoCAD\R24.3"
|
||
$curVerObj = Get-ItemProperty -Path $acadRoot -Name CurVer -ErrorAction SilentlyContinue
|
||
$acadCur = $curVerObj.CurVer
|
||
|
||
if (-not $acadCur) {
|
||
Write-Host "未检测到 AutoCAD 2024 注册表信息:$acadRoot\CurVer" -ForegroundColor Yellow
|
||
Write-Host "已完成文件复制,但未写入自启动注册。请确认已至少启动过 AutoCAD 2024 一次。" -ForegroundColor Yellow
|
||
}
|
||
else {
|
||
$acadBase = Join-Path $acadRoot $acadCur
|
||
|
||
# 1) Applications 自启动(.NET 插件标准方式)
|
||
$appKey = Join-Path $acadBase "Applications\FurnitureExporter"
|
||
New-Item -Path $appKey -Force | Out-Null
|
||
New-ItemProperty -Path $appKey -Name "DESCRIPTION" -PropertyType String -Value "家具导出插件 v$Version" -Force | Out-Null
|
||
New-ItemProperty -Path $appKey -Name "LOADCTRLS" -PropertyType DWord -Value 2 -Force | Out-Null
|
||
New-ItemProperty -Path $appKey -Name "MANAGED" -PropertyType DWord -Value 1 -Force | Out-Null
|
||
New-ItemProperty -Path $appKey -Name "LOADER" -PropertyType String -Value $dllPath -Force | Out-Null
|
||
|
||
# 2) 可信路径:只加入本插件 Contents,避免把整个 ApplicationPlugins 目录设为可信
|
||
$profilesRoot = Join-Path $acadBase "Profiles"
|
||
$profiles = Get-ChildItem -Path $profilesRoot -ErrorAction SilentlyContinue
|
||
|
||
foreach ($p in $profiles) {
|
||
$varsKey = Join-Path $p.PSPath "Variables"
|
||
if (-not (Test-Path $varsKey)) { continue }
|
||
|
||
$vars = Get-ItemProperty -Path $varsKey -ErrorAction SilentlyContinue
|
||
$existingTrusted = $null
|
||
if ($vars.PSObject.Properties.Name -contains "TRUSTEDPATHS") {
|
||
$existingTrusted = [string]$vars.TRUSTEDPATHS
|
||
}
|
||
|
||
$newTrusted = Add-TrustedPath -Existing $existingTrusted -PathToAdd $trustedDir
|
||
if ($existingTrusted -ne $newTrusted) {
|
||
New-ItemProperty -Path $varsKey -Name "TRUSTEDPATHS" -PropertyType String -Value $newTrusted -Force | Out-Null
|
||
}
|
||
|
||
# 如果 SECURELOAD=2,会导致即使加入可信路径也不加载(尽量只从 2 降到 1)
|
||
if ($vars.PSObject.Properties.Name -contains "SECURELOAD") {
|
||
if ([string]$vars.SECURELOAD -eq "2") {
|
||
New-ItemProperty -Path $varsKey -Name "SECURELOAD" -PropertyType String -Value "1" -Force | Out-Null
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
Write-Step "[3/3] 完成"
|
||
Write-Host ""
|
||
Write-Host "安装完成。请完全关闭 AutoCAD 后重新启动。" -ForegroundColor Green
|
||
Write-Host "启动后命令行输入:FEXPORT(单资产)或 FEXPORTPRODUCT(整套兼容)" -ForegroundColor Green
|
||
Write-Host "" |