70 lines
4.2 KiB
C#
70 lines
4.2 KiB
C#
using Microsoft.Win32;
|
|
using Launcher.Next.Core;
|
|
namespace Launcher.Next.Autodesk;
|
|
public interface IRegistryReader { object? GetValue(string path, string name); }
|
|
public sealed class WindowsRegistryReader : IRegistryReader { public object? GetValue(string path, string name) => Registry.LocalMachine.OpenSubKey(path)?.GetValue(name); }
|
|
public sealed class PlantProductDiscovery(IRegistryReader registry) : IProductDiscovery
|
|
{
|
|
public ProductInstallation? DiscoverPlant3D(int year)
|
|
{
|
|
if (year != 2026) return null;
|
|
const string plantInstall = @"SOFTWARE\Autodesk\AutoCAD\R25.1\ACAD-9117\Install";
|
|
var install = registry.GetValue(plantInstall, "InstallDir") as string;
|
|
var exe = string.IsNullOrWhiteSpace(install) ? null : Path.Combine(install, "acad.exe");
|
|
if (exe is null || !File.Exists(exe) || !Directory.Exists(Path.Combine(install!, "PLNT3D"))) return null;
|
|
return new(exe, "2026", "x64", []);
|
|
}
|
|
}
|
|
public sealed class StartupScriptBuilder : IStartupScriptBuilder
|
|
{
|
|
public string BuildNetloadScript(string root, string entry)
|
|
{ var full = Path.GetFullPath(Path.Combine(root, entry)); if (!full.StartsWith(Path.GetFullPath(root) + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase) || !File.Exists(full)) throw new InvalidOperationException("UNTRUSTED_ENTRY_PATH"); return $"_.NETLOAD\n\"{full.Replace("\"", "\\\"")}\"\n"; }
|
|
public string BuildAutomationScript(ResolvedAutomationScriptPaths paths)
|
|
{
|
|
var fixture = Path.GetFullPath(paths.FixtureOrthoDwg);
|
|
if (!File.Exists(fixture)) throw new InvalidOperationException("AUTOMATION_FIXTURE_NOT_FOUND");
|
|
var quote = (string value) => "\"" + value.Replace("\"", "\\\"") + "\"";
|
|
var dialogFreeOpen = "_.FILEDIA\n0\n_.CMDDIA\n0\n";
|
|
var diagnostic = paths.Diagnostic
|
|
? "_.CMDECHO\n1\n_.NOMUTT\n0\n_.LOGFILEMODE\n1\n"
|
|
: string.Empty;
|
|
var commands = paths.Selection is null ? BuildDefaultCommands(paths) : BuildSelectedEntityCommands(paths);
|
|
return dialogFreeOpen + "_.OPEN\n" + quote(fixture) + "\n" + diagnostic + BuildNetloadScript(paths.ValidatedPackageRoot, paths.EntryAssembly) +
|
|
"PHP_REC_AUTOMATION_PREPARE\n" + commands + "PHP_REC_AUTOMATION_FINALIZE\n";
|
|
}
|
|
|
|
private static string BuildDefaultCommands(ResolvedAutomationScriptPaths paths) => paths.Apply
|
|
? "_.UNDO\n_MARK\nPXP-OA\nPXP-OA\n_.UNDO\n_BACK\n"
|
|
: paths.Diagnostic
|
|
? "PHP_REC_AUTOMATION_BEFORE_PXP_1\nPXP-OA\nPHP_REC_AUTOMATION_AFTER_PXP_1\nPHP_REC_AUTOMATION_BEFORE_PXP_2\nPXP-OA\nPHP_REC_AUTOMATION_AFTER_PXP_2\n"
|
|
: "PXP-OA\nPXP-OA\n";
|
|
|
|
private static string BuildSelectedEntityCommands(ResolvedAutomationScriptPaths paths)
|
|
{
|
|
var selection = paths.Selection!;
|
|
selection.Validate();
|
|
var invoke = "(setq P_REC_ANNOT_SELECTED_ENTITY (handent \"" + selection.ObjectHandle.ToUpperInvariant() + "\"))\n" +
|
|
"(if (and P_REC_ANNOT_SELECTED_ENTITY (= (cdr (assoc 0 (entget P_REC_ANNOT_SELECTED_ENTITY))) \"" + selection.ExpectedDxfName.ToUpperInvariant() + "\")) " +
|
|
"(command-s \"" + selection.CommandName.ToUpperInvariant() + "\" P_REC_ANNOT_SELECTED_ENTITY) " +
|
|
"(progn (prompt \"\\nP_REC_ANNOT_SELECTION_REJECTED\") (quit)))\n";
|
|
return paths.Apply ? "_.UNDO\n_MARK\n" + invoke + invoke + "_.UNDO\n_BACK\n" : invoke + invoke;
|
|
}
|
|
}
|
|
public sealed class DryRunAutodeskProcessLauncher : IAutodeskProcessLauncher
|
|
{
|
|
public PreparedLaunch Prepare(LaunchRequest r)
|
|
{
|
|
if (!r.DryRun) throw new InvalidOperationException("PROCESS_START_DISABLED_IN_FOUNDATION");
|
|
var startInfo = new System.Diagnostics.ProcessStartInfo
|
|
{
|
|
FileName = r.Product.ExecutablePath,
|
|
WorkingDirectory = Path.GetDirectoryName(r.Product.ExecutablePath)!,
|
|
UseShellExecute = false
|
|
};
|
|
startInfo.ArgumentList.Add("/product"); startInfo.ArgumentList.Add("PLNT3D");
|
|
startInfo.ArgumentList.Add("/p"); startInfo.ArgumentList.Add(r.Profile);
|
|
startInfo.ArgumentList.Add("/b"); startInfo.ArgumentList.Add(r.StartupScriptPath);
|
|
return new(startInfo.FileName, startInfo.ArgumentList.ToArray(), startInfo.WorkingDirectory, false);
|
|
}
|
|
}
|