using System; using System.Runtime.InteropServices; using SolidWorks.Interop.sldworks; using SolidWorks.Interop.swconst; using SolidWorks.Interop.swpublished; namespace PdfExportAddin { /// /// Minimal SOLIDWORKS add-in entry point. /// /// This class is COM-visible because SOLIDWORKS loads add-ins via COM. /// It implements ISwAddin, which is the standard add-in contract used by SOLIDWORKS. /// /// Responsibilities of this class: /// - Connect to and disconnect from SOLIDWORKS. /// - Register a simple command group/menu command. /// - Route the command to the PDF export service. /// - Keep the add-in bootstrap readable and low-risk. /// /// Design intent for future growth: /// - Keep add-in lifecycle code here. /// - Keep drawing export business logic in PdfExportService. /// - Add future commands/services as separate classes rather than bloating this file. /// [ComVisible(true)] [Guid(BuildInfo.AddinGuid)] [ProgId(BuildInfo.ProgId)] public sealed class SwPdfExportAddin : ISwAddin { private SldWorks _swApp; private int _addinCookie; private ICommandManager _commandManager; private PdfExportService _pdfExportService; /// /// SOLIDWORKS calls this when the add-in is loaded. /// /// This is the add-in's main initialization entry point. /// Required tasks performed here: /// - capture the SOLIDWORKS application pointer /// - store the add-in cookie /// - register callback info /// - create the PDF export service /// - register the menu command /// public bool ConnectToSW(object ThisSW, int Cookie) { _swApp = ThisSW as SldWorks; _addinCookie = Cookie; if (_swApp == null) { return false; } _swApp.SetAddinCallbackInfo2(0, this, _addinCookie); _pdfExportService = new PdfExportService(_swApp); _commandManager = _swApp.GetCommandManager(_addinCookie); AddCommandGroup(); if (BuildInfo.ShowLoadMessage) { ShowInfo( "PDF Export Test Add-in loaded.\n\n" + "Use the command 'Save Active Drawing as PDF' from the add-in menu group.\n\n" + "This starter add-in only enables the command when a drawing is active."); } return true; } /// /// SOLIDWORKS calls this when the add-in is being unloaded. /// /// Required cleanup: /// - remove command UI that we created /// - release references so SOLIDWORKS can unload cleanly /// public bool DisconnectFromSW() { try { RemoveCommandGroup(); } catch { // Keep unload robust. During disconnect we do not want a UI cleanup issue // to prevent the add-in from unloading. } finally { _pdfExportService = null; _commandManager = null; _swApp = null; } return true; } /// /// Creates a minimal command group with a single menu command. /// /// Why menu only for the first trial? /// - Fewer moving parts than toolbar/icon setup. /// - Lower risk for the first add-in smoke test. /// - Easy to extend later into a CommandManager tab or toolbar. /// private void AddCommandGroup() { if (_commandManager == null) { return; } int errors = 0; bool ignorePreviousVersion = true; ICommandGroup group = _commandManager.CreateCommandGroup2( BuildInfo.CommandGroupId, BuildInfo.CommandGroupTitle, BuildInfo.CommandTooltip, BuildInfo.CommandTooltip, -1, ignorePreviousVersion, ref errors); if (group == null) { ShowError(string.Format("Failed to create command group. Error code={0}.", errors)); return; } // Menu-only starter command. Once your pipeline is proven, this is the natural place // to add toolbar buttons, tabs, icons, multiple commands, and document-specific UI. group.AddCommandItem2( BuildInfo.CommandTitle, -1, BuildInfo.CommandTooltip, BuildInfo.CommandTooltip, -1, nameof(Command_SaveActiveDrawingAsPdf), nameof(Enable_SaveActiveDrawingAsPdf), 0, (int)swCommandItemType_e.swMenuItem); group.HasMenu = true; group.HasToolbar = false; group.Activate(); } /// /// Removes the command group created by this add-in. /// private void RemoveCommandGroup() { if (_commandManager == null) { return; } _commandManager.RemoveCommandGroup(BuildInfo.CommandGroupId); } /// /// SOLIDWORKS callback method for the command. /// /// IMPORTANT: /// The method name is passed to AddCommandItem2 as a string callback name, /// so renaming this method requires updating that string as well. /// public void Command_SaveActiveDrawingAsPdf() { if (_pdfExportService == null) { ShowError("PDF export service is not initialized."); return; } ExportResult result = _pdfExportService.ExportActiveDrawingToPdf(); if (!result.IsSuccess) { ShowError(result.Message); return; } if (BuildInfo.ShowSuccessMessage) { ShowInfo(result.Message); } } /// /// SOLIDWORKS enable-method callback for the menu command. /// /// Return convention: /// - 1 = enabled /// - 0 = disabled /// /// This starter logic enables the command only when the active document is a drawing. /// public int Enable_SaveActiveDrawingAsPdf() { if (_swApp == null) { return 0; } ModelDoc2 model = _swApp.ActiveDoc as ModelDoc2; if (model == null) { return 0; } return model.GetType() == (int)swDocumentTypes_e.swDocDRAWING ? 1 : 0; } private void ShowInfo(string message) { _swApp?.SendMsgToUser2( message, (int)swMessageBoxIcon_e.swMbInformation, (int)swMessageBoxBtn_e.swMbOk); } private void ShowError(string message) { _swApp?.SendMsgToUser2( message, (int)swMessageBoxIcon_e.swMbStop, (int)swMessageBoxBtn_e.swMbOk); } } }