using System; using System.IO; using System.IO.Pipes; using CommandLine; using SolidWorks.Interop.sldworks; using SolidWorks.Interop.swconst; class Options { [Option('i', "input", Required = true, HelpText = "Input file path")] public string InputFilePath { get; set; } [Option('o', "output", Required = true, HelpText = "Output file path")] public string OutputFilePath { get; set; } [Option('c', "configuration", Required = true, HelpText = "SolidWorks configuration name")] public string ConfigurationName { get; set; } [Option('p', "pipeName", Required = true, HelpText = "Named pipe name for IPC")] public string PipeName { get; set; } [Option('l', "logFilePath", Required = true, HelpText = "File path to redirect SolidWorks stdout")] public string LogFilePath { get; set; } } class Program { static void Main(string[] args) { Parser.Default.ParseArguments(args) .WithParsed(options => Run(options)) .WithNotParsed(errors => HandleParseErrors(errors)); } static void Run(Options options) { try { // Redirect SolidWorks stdout to a file using (StreamWriter sw = new StreamWriter(options.LogFilePath, false)) { Console.SetOut(sw); // Continue with the SolidWorks operation using (ISldWorks swApp = Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application")) as ISldWorks) { if (swApp == null) { Console.WriteLine("Failed to create SolidWorks Application instance."); return; } swApp.Visible = true; // Open the input file with the specified configuration ModelDoc2 swModel = swApp.OpenDoc6(options.InputFilePath, (int)swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", 0, 0); if (swModel == null) { Console.WriteLine($"Failed to open the input file: {options.InputFilePath}"); return; } // Set the active configuration swModel.ShowConfiguration2(options.ConfigurationName); // Save as the output file in a different format (e.g., STEP) swModel.Extension.SaveAs(options.OutputFilePath, (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_Silent, null, null, null); Console.WriteLine($"Conversion successful. Output file saved to: {options.OutputFilePath}"); // Notify Node.js about the completion using named pipe using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", options.PipeName, PipeDirection.Out)) { pipeClient.Connect(); StreamWriter writer = new StreamWriter(pipeClient); writer.WriteLine("SolidWorks operation completed"); writer.Flush(); } // Close the model swApp.CloseDoc(options.InputFilePath); } } } catch (Exception ex) { Console.WriteLine($"An error occurred: {ex.Message}"); } } static void HandleParseErrors(System.Collections.Generic.IEnumerable errors) { // Handle command-line parsing errors Console.WriteLine("Failed to parse command-line arguments."); } }