Spaces:
Sleeping
Sleeping
Download revit-plugin/RevitCommand.cs from KyrosDev/kstools-license-manager: direct link, hf CLI and curl.
- Browser
- Download file 11.8 kB
-
https://huggingface.co/spaces/KyrosDev/kstools-license-manager/resolve/main/revit-plugin/RevitCommand.cs
- Command line
-
hf download hf://spaces/KyrosDev/kstools-license-manager/revit-plugin/RevitCommand.cs
-
curl -L -o RevitCommand.cs https://huggingface.co/spaces/KyrosDev/kstools-license-manager/resolve/main/revit-plugin/RevitCommand.cs
11.8 kB
| using System; | |
| using System.Threading.Tasks; | |
| using System.Windows.Forms; | |
| using Autodesk.Revit.Attributes; | |
| using Autodesk.Revit.DB; | |
| using Autodesk.Revit.UI; | |
| namespace KSTools.Licensing | |
| { | |
| /// <summary> | |
| /// KSTools Revit 外掛主命令 | |
| /// 整合授權檢查和主要功能執行 | |
| /// </summary> | |
| [] | |
| [] | |
| public class KSToolsCommand : IExternalCommand | |
| { | |
| // 這裡設定你的 API URL,可以是 Hugging Face Spaces 或其他部署位置 | |
| private const string API_BASE_URL = "https://your-username-kstools-license.hf.space/api"; | |
| private static LicenseManager _licenseManager; | |
| /// <summary> | |
| /// 命令執行入口點 | |
| /// </summary> | |
| public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) | |
| { | |
| try | |
| { | |
| // 初始化授權管理器 | |
| if (_licenseManager == null) | |
| { | |
| _licenseManager = new LicenseManager(API_BASE_URL); | |
| } | |
| // 檢查授權狀態 | |
| var licenseCheckResult = CheckLicenseAsync().Result; | |
| if (licenseCheckResult == Result.Succeeded) | |
| { | |
| // 授權有效,執行主要功能 | |
| return ExecuteMainFeatures(commandData); | |
| } | |
| else | |
| { | |
| return licenseCheckResult; | |
| } | |
| } | |
| catch (Exception ex) | |
| { | |
| TaskDialog.Show("KSTools 錯誤", $"執行過程發生錯誤:{ex.Message}"); | |
| return Result.Failed; | |
| } | |
| } | |
| /// <summary> | |
| /// 異步檢查授權狀態 | |
| /// </summary> | |
| /// <returns>檢查結果</returns> | |
| private async Task<Result> CheckLicenseAsync() | |
| { | |
| try | |
| { | |
| // 快速檢查是否有本地授權 | |
| if (_licenseManager.HasValidLicense()) | |
| { | |
| // 進行線上驗證 (不強制,允許使用快取) | |
| var validation = await _licenseManager.ValidateLicenseAsync(false); | |
| if (validation.Valid) | |
| { | |
| // 如果即將到期,顯示提醒 | |
| var licenseInfo = _licenseManager.GetLicenseInfo(); | |
| if (licenseInfo?.IsExpiringSoon == true) | |
| { | |
| TaskDialog.Show("授權提醒", | |
| $"您的授權將在 {licenseInfo.DaysRemaining} 天後到期,請及時聯絡管理員續期。"); | |
| } | |
| return Result.Succeeded; | |
| } | |
| else | |
| { | |
| return ShowLicenseError(validation.Message); | |
| } | |
| } | |
| else | |
| { | |
| // 沒有本地授權,引導用戶進行授權啟用 | |
| return await HandleLicenseActivation(); | |
| } | |
| } | |
| catch (Exception ex) | |
| { | |
| TaskDialog.Show("授權檢查錯誤", $"無法驗證授權狀態:{ex.Message}"); | |
| return Result.Failed; | |
| } | |
| } | |
| /// <summary> | |
| /// 處理授權啟用流程 | |
| /// </summary> | |
| /// <returns>處理結果</returns> | |
| private async Task<Result> HandleLicenseActivation() | |
| { | |
| // 顯示硬體ID和授權啟用對話框 | |
| var hardwareId = _licenseManager.GetHardwareId(); | |
| var dialog = new LicenseActivationDialog(hardwareId); | |
| var dialogResult = dialog.ShowDialog(); | |
| if (dialogResult == DialogResult.OK && !string.IsNullOrEmpty(dialog.LicenseCode)) | |
| { | |
| // 顯示進度對話框 | |
| var progressDialog = new TaskDialog("KSTools") | |
| { | |
| MainInstruction = "正在啟用授權...", | |
| MainContent = "請稍等,正在驗證授權碼有效性。", | |
| AllowCancellation = false | |
| }; | |
| // 非同步啟用授權 | |
| var activationTask = _licenseManager.ActivateLicenseAsync(dialog.LicenseCode); | |
| // 這裡應該顯示進度,但 TaskDialog 不支援異步,所以直接等待 | |
| var result = await activationTask; | |
| if (result.Success) | |
| { | |
| TaskDialog.Show("啟用成功", | |
| $"授權啟用成功!\n\n" + | |
| $"用戶:{result.UserName}\n" + | |
| $"到期時間:{result.ExpiresAt:yyyy-MM-dd HH:mm}\n\n" + | |
| $"感謝使用 KSTools!"); | |
| return Result.Succeeded; | |
| } | |
| else | |
| { | |
| return ShowLicenseError($"授權啟用失敗:{result.Message}"); | |
| } | |
| } | |
| else if (dialogResult == DialogResult.Retry) | |
| { | |
| // 用戶要求重新輸入 | |
| return await HandleLicenseActivation(); | |
| } | |
| else | |
| { | |
| // 用戶取消 | |
| return Result.Cancelled; | |
| } | |
| } | |
| /// <summary> | |
| /// 顯示授權錯誤並提供選項 | |
| /// </summary> | |
| /// <param name="errorMessage">錯誤訊息</param> | |
| /// <returns>用戶選擇結果</returns> | |
| private Result ShowLicenseError(string errorMessage) | |
| { | |
| var dialog = new TaskDialog("KSTools 授權錯誤") | |
| { | |
| MainInstruction = "授權驗證失敗", | |
| MainContent = errorMessage, | |
| CommonButtons = TaskDialogCommonButtons.None | |
| }; | |
| dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink1, "重新輸入授權碼"); | |
| dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink2, "聯絡技術支援"); | |
| dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink3, "取消"); | |
| var result = dialog.Show(); | |
| switch (result) | |
| { | |
| case TaskDialogResult.CommandLink1: | |
| // 清除現有授權並重新啟用 | |
| _licenseManager.ClearLicense(); | |
| return HandleLicenseActivation().Result; | |
| case TaskDialogResult.CommandLink2: | |
| // 顯示技術支援資訊 | |
| ShowSupportInfo(); | |
| return Result.Cancelled; | |
| default: | |
| return Result.Cancelled; | |
| } | |
| } | |
| /// <summary> | |
| /// 顯示技術支援資訊 | |
| /// </summary> | |
| private void ShowSupportInfo() | |
| { | |
| var supportInfo = $"KSTools 技術支援\n\n" + | |
| $"如需協助,請聯絡:\n" + | |
| $"• 電子郵件:support@kstools.com\n" + | |
| $"• 電話:(02) 1234-5678\n\n" + | |
| $"請提供以下資訊:\n" + | |
| $"• 硬體ID:{_licenseManager.GetHardwareId()}\n" + | |
| $"• 錯誤時間:{DateTime.Now:yyyy-MM-dd HH:mm:ss}\n" + | |
| $"• Revit 版本:{commandData.Application.Application.VersionName}"; | |
| TaskDialog.Show("技術支援", supportInfo); | |
| } | |
| /// <summary> | |
| /// 執行主要功能 (這裡實作你的 KSTools 功能) | |
| /// </summary> | |
| /// <param name="commandData">Revit 命令資料</param> | |
| /// <returns>執行結果</returns> | |
| private Result ExecuteMainFeatures(ExternalCommandData commandData) | |
| { | |
| try | |
| { | |
| // 這裡實作你的 KSTools 主要功能 | |
| // 例如: | |
| var dialog = new TaskDialog("KSTools") | |
| { | |
| MainInstruction = "KSTools 功能選單", | |
| MainContent = "請選擇要執行的功能:", | |
| CommonButtons = TaskDialogCommonButtons.Cancel | |
| }; | |
| dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink1, "功能 A", "執行 KSTools 功能 A"); | |
| dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink2, "功能 B", "執行 KSTools 功能 B"); | |
| dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink3, "授權資訊", "查看當前授權狀態"); | |
| var result = dialog.Show(); | |
| switch (result) | |
| { | |
| case TaskDialogResult.CommandLink1: | |
| return ExecuteFeatureA(commandData); | |
| case TaskDialogResult.CommandLink2: | |
| return ExecuteFeatureB(commandData); | |
| case TaskDialogResult.CommandLink3: | |
| ShowLicenseInfo(); | |
| return Result.Succeeded; | |
| default: | |
| return Result.Cancelled; | |
| } | |
| } | |
| catch (Exception ex) | |
| { | |
| TaskDialog.Show("執行錯誤", $"功能執行失敗:{ex.Message}"); | |
| return Result.Failed; | |
| } | |
| } | |
| /// <summary> | |
| /// 執行功能 A | |
| /// </summary> | |
| private Result ExecuteFeatureA(ExternalCommandData commandData) | |
| { | |
| TaskDialog.Show("功能 A", "KSTools 功能 A 執行成功!\n\n這裡實作你的具體功能..."); | |
| return Result.Succeeded; | |
| } | |
| /// <summary> | |
| /// 執行功能 B | |
| /// </summary> | |
| private Result ExecuteFeatureB(ExternalCommandData commandData) | |
| { | |
| TaskDialog.Show("功能 B", "KSTools 功能 B 執行成功!\n\n這裡實作你的具體功能..."); | |
| return Result.Succeeded; | |
| } | |
| /// <summary> | |
| /// 顯示授權資訊 | |
| /// </summary> | |
| private void ShowLicenseInfo() | |
| { | |
| var licenseInfo = _licenseManager.GetLicenseInfo(); | |
| if (licenseInfo != null) | |
| { | |
| var info = $"KSTools 授權資訊\n\n" + | |
| $"授權碼:{licenseInfo.LicenseCode}\n" + | |
| $"用戶:{licenseInfo.UserName}\n" + | |
| $"硬體ID:{licenseInfo.HardwareId}\n" + | |
| $"到期時間:{licenseInfo.ExpiresAt:yyyy-MM-dd HH:mm}\n" + | |
| $"剩餘天數:{licenseInfo.DaysRemaining} 天\n" + | |
| $"最後驗證:{licenseInfo.LastValidation:yyyy-MM-dd HH:mm}"; | |
| if (licenseInfo.IsExpiringSoon) | |
| { | |
| info += "\n\n⚠️ 授權即將到期,請及時續期!"; | |
| } | |
| TaskDialog.Show("授權資訊", info); | |
| } | |
| else | |
| { | |
| TaskDialog.Show("授權資訊", "無法取得授權資訊"); | |
| } | |
| } | |
| } | |
| } |