diff --git a/SolidWorks.Interop.sldworks.dll b/SolidWorks.Interop.sldworks.dll new file mode 100644 index 0000000..566bcad Binary files /dev/null and b/SolidWorks.Interop.sldworks.dll differ diff --git a/export-file.cmd b/export-file.cmd new file mode 100644 index 0000000..5ab9b7d --- /dev/null +++ b/export-file.cmd @@ -0,0 +1,12 @@ +ECHO OFF +SET inputFilePath=%~dp0%1".SLDPRT" + +SET outFilePath=%~dp0%1".step" + +PowerShell -NoProfile -ExecutionPolicy Bypass -File "%~dp0export-file.ps1" %inputFilePath% %outFilePath% + +SET inputFilePath=%~dp0%1".SLDDRW" +SET outFilePath=%~dp0%1".pdf" +PowerShell -NoProfile -ExecutionPolicy Bypass -File "%~dp0export-file.ps1" %inputFilePath% %outFilePath% +SET outFilePath=%~dp0%1".dxf" +PowerShell -NoProfile -ExecutionPolicy Bypass -File "%~dp0export-file.ps1" %inputFilePath% %outFilePath% diff --git a/export-file.ps1 b/export-file.ps1 new file mode 100644 index 0000000..be26228 --- /dev/null +++ b/export-file.ps1 @@ -0,0 +1,102 @@ +$inputFilePath=$args[0] +$outFilePath=$args[1] + +$ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path + +$Assem = ( + $ScriptDir + "\SolidWorks.Interop.sldworks.dll" + ) + +$Source = @" +using SolidWorks.Interop.sldworks; +using System; + + namespace CodeStack + { + public static class Exporter + { + #region Libraries + + static Exporter() + { + AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve; + } + + public static void LoadLibrary(params object[] libs) + { + foreach(string lib in libs) + { + Console.WriteLine(string.Format("Loading library: {0}", lib)); + System.Reflection.Assembly assm = System.Reflection.Assembly.LoadFrom(lib); + Console.WriteLine(assm.GetName().ToString()); + } + } + + private static System.Reflection.Assembly OnAssemblyResolve(object sender, ResolveEventArgs args) + { + foreach (System.Reflection.Assembly assm in AppDomain.CurrentDomain.GetAssemblies()) + { + if(assm.GetName().ToString() == args.Name) + { + return assm; + } + }; + + return null; + } + + #endregion + + public static void ExportFile(string filePath, string outFilePath) + { + Console.WriteLine("Connecting to SOLIDWORKS..."); + + ISldWorks app = Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application")) as ISldWorks; + + if (app != null) + { + Console.WriteLine(string.Format("Opening file '{0}'...", filePath)); + + IDocumentSpecification docSpec = app.GetOpenDocSpec(filePath) as IDocumentSpecification; + docSpec.ReadOnly = true; + docSpec.Silent = true; + IModelDoc2 model = app.OpenDoc7(docSpec); + + if (model != null) + { + const int swSaveAsCurrentVersion = 0; + const int swSaveAsOptions_Silent = 1; + int err = -1; + int warn = -1; + + Console.WriteLine(string.Format("Exporting file '{0}' to '{1}'...", filePath, outFilePath)); + + if (!model.Extension.SaveAs(outFilePath, swSaveAsCurrentVersion, + swSaveAsOptions_Silent, null, ref err, ref warn)) + { + Console.WriteLine(string.Format("Failed to export '{0}' to '{1}'. Error code: {2}", filePath, outFilePath, err)); + } + + Console.WriteLine(string.Format("Closing file '{0}'...", filePath)); + + app.CloseDoc(model.GetTitle()); + } + else + { + Console.WriteLine(string.Format("Failed to open document: '{0}'. Error code: {1}", + filePath, docSpec.Error)); + } + } + else + { + Console.WriteLine("Failed to connect to SOLIDWORKS instance"); + } + } + } +} +"@ + +Add-Type -TypeDefinition $Source -ReferencedAssemblies $Assem -Language CSharp + +[CodeStack.Exporter]::LoadLibrary($Assem) +[CodeStack.Exporter]::ExportFile($inputFilePath, $outFilePath) diff --git a/export-pdf-dxf.ps1 b/export-pdf-dxf.ps1 new file mode 100644 index 0000000..002543b --- /dev/null +++ b/export-pdf-dxf.ps1 @@ -0,0 +1,110 @@ +$inputFilePath=$args[0]+".SLDDRW" +$outFilePath=$args[0] + +$ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path + +$Assem = ( + $ScriptDir + "\SolidWorks.Interop.sldworks.dll" + ) + +$Source = @" +using SolidWorks.Interop.sldworks; +using System; + + namespace CodeStack + { + public static class Exporter + { + #region Libraries + + static Exporter() + { + AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve; + } + + public static void LoadLibrary(params object[] libs) + { + foreach(string lib in libs) + { + Console.WriteLine(string.Format("Loading library: {0}", lib)); + System.Reflection.Assembly assm = System.Reflection.Assembly.LoadFrom(lib); + Console.WriteLine(assm.GetName().ToString()); + } + } + + private static System.Reflection.Assembly OnAssemblyResolve(object sender, ResolveEventArgs args) + { + foreach (System.Reflection.Assembly assm in AppDomain.CurrentDomain.GetAssemblies()) + { + if(assm.GetName().ToString() == args.Name) + { + return assm; + } + }; + + return null; + } + + #endregion + + public static void ExportFile(string filePath, string outFilePath) + { + Console.WriteLine("Connecting to SOLIDWORKS..."); + + ISldWorks app = Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application")) as ISldWorks; + + if (app != null) + { + Console.WriteLine(string.Format("Opening file '{0}'...", filePath)); + + IDocumentSpecification docSpec = app.GetOpenDocSpec(filePath) as IDocumentSpecification; + docSpec.ReadOnly = true; + docSpec.Silent = true; + IModelDoc2 model = app.OpenDoc7(docSpec); + + if (model != null) + { + const int swSaveAsCurrentVersion = 0; + const int swSaveAsOptions_Silent = 1; + int err = -1; + int warn = -1; + + Console.WriteLine(string.Format("Exporting file '{0}' to '{1}'...", filePath, outFilePath+".pdf")); + + if (!model.Extension.SaveAs(outFilePath+".pdf", swSaveAsCurrentVersion, + swSaveAsOptions_Silent, null, ref err, ref warn)) + { + Console.WriteLine(string.Format("Failed to export '{0}' to '{1}'. Error code: {2}", filePath, outFilePath+".pdf", err)); + } + + Console.WriteLine(string.Format("Exporting file '{0}' to '{1}'...", filePath, outFilePath+".dxf")); + + if (!model.Extension.SaveAs(outFilePath+".dxf", swSaveAsCurrentVersion, + swSaveAsOptions_Silent, null, ref err, ref warn)) + { + Console.WriteLine(string.Format("Failed to export '{0}' to '{1}'. Error code: {2}", filePath, outFilePath+".dfx", err)); + } + + Console.WriteLine(string.Format("Closing file '{0}'...", filePath)); + + app.CloseDoc(model.GetTitle()); + } + else + { + Console.WriteLine(string.Format("Failed to open document: '{0}'. Error code: {1}", + filePath, docSpec.Error)); + } + } + else + { + Console.WriteLine("Failed to connect to SOLIDWORKS instance"); + } + } + } +} +"@ + +Add-Type -TypeDefinition $Source -ReferencedAssemblies $Assem -Language CSharp + +[CodeStack.Exporter]::LoadLibrary($Assem) +[CodeStack.Exporter]::ExportFile($inputFilePath, $outFilePath) diff --git a/export-step.ps1 b/export-step.ps1 new file mode 100644 index 0000000..6aedabb --- /dev/null +++ b/export-step.ps1 @@ -0,0 +1,102 @@ +$inputFilePath=$args[0]+".SLDPRT" +$outFilePath=$args[0] + +$ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path + +$Assem = ( + $ScriptDir + "\SolidWorks.Interop.sldworks.dll" + ) + +$Source = @" +using SolidWorks.Interop.sldworks; +using System; + + namespace CodeStack + { + public static class Exporter + { + #region Libraries + + static Exporter() + { + AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve; + } + + public static void LoadLibrary(params object[] libs) + { + foreach(string lib in libs) + { + Console.WriteLine(string.Format("Loading library: {0}", lib)); + System.Reflection.Assembly assm = System.Reflection.Assembly.LoadFrom(lib); + Console.WriteLine(assm.GetName().ToString()); + } + } + + private static System.Reflection.Assembly OnAssemblyResolve(object sender, ResolveEventArgs args) + { + foreach (System.Reflection.Assembly assm in AppDomain.CurrentDomain.GetAssemblies()) + { + if(assm.GetName().ToString() == args.Name) + { + return assm; + } + }; + + return null; + } + + #endregion + + public static void ExportFile(string filePath, string outFilePath) + { + Console.WriteLine("Connecting to SOLIDWORKS..."); + + ISldWorks app = Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application")) as ISldWorks; + + if (app != null) + { + Console.WriteLine(string.Format("Opening file '{0}'...", filePath)); + + IDocumentSpecification docSpec = app.GetOpenDocSpec(filePath) as IDocumentSpecification; + docSpec.ReadOnly = true; + docSpec.Silent = true; + IModelDoc2 model = app.OpenDoc7(docSpec); + + if (model != null) + { + const int swSaveAsCurrentVersion = 0; + const int swSaveAsOptions_Silent = 1; + int err = -1; + int warn = -1; + + Console.WriteLine(string.Format("Exporting file '{0}' to '{1}'...", filePath, outFilePath+".step")); + + if (!model.Extension.SaveAs(outFilePath+".step", swSaveAsCurrentVersion, + swSaveAsOptions_Silent, null, ref err, ref warn)) + { + Console.WriteLine(string.Format("Failed to export '{0}' to '{1}'. Error code: {2}", filePath, outFilePath+".step", err)); + } + + Console.WriteLine(string.Format("Closing file '{0}'...", filePath)); + + app.CloseDoc(model.GetTitle()); + } + else + { + Console.WriteLine(string.Format("Failed to open document: '{0}'. Error code: {1}", + filePath, docSpec.Error)); + } + } + else + { + Console.WriteLine("Failed to connect to SOLIDWORKS instance"); + } + } + } +} +"@ + +Add-Type -TypeDefinition $Source -ReferencedAssemblies $Assem -Language CSharp + +[CodeStack.Exporter]::LoadLibrary($Assem) +[CodeStack.Exporter]::ExportFile($inputFilePath, $outFilePath) diff --git a/gui.py b/gui.py new file mode 100644 index 0000000..c4b1b26 --- /dev/null +++ b/gui.py @@ -0,0 +1,16 @@ +import PySimpleGUI as sg +import sys + +if len(sys.argv) == 1: + fname = sg.Window('My Script', + [[sg.Text('Document to open')], + [sg.In(), sg.FileBrowse()], + [sg.Open(), sg.Cancel()]]).read(close=True)[1][0] +else: + fname = sys.argv[1] + +if not fname: + sg.popup("Cancel", "No filename supplied") + raise SystemExit("Cancelling: no filename supplied") +else: + sg.popup('The filename you chose was', fname) \ No newline at end of file diff --git a/run.py b/run.py new file mode 100644 index 0000000..b62a833 --- /dev/null +++ b/run.py @@ -0,0 +1,22 @@ +# example: .\run.py path\Part1 +import os +import sys +import pathlib +import subprocess +from rich.progress import track + +# folder input +if(sys.argv[1]): + pass + + + +files = sys.argv[1:] # take files given in input +currentPath = str(pathlib.Path().resolve()) + +for i in track(files, description="Exporting...", total=len(files)): + input = str(i) + commandstep = "PowerShell -NoProfile -ExecutionPolicy Bypass -File " + currentPath + "\export-step.ps1 " + input + commandPdfDxf = "PowerShell -NoProfile -ExecutionPolicy Bypass -File " + currentPath + "\export-pdf-dxf.ps1 " + input + subprocess.run(commandstep, capture_output=True) + subprocess.run(commandPdfDxf, capture_output=True) diff --git a/sample part/Part1.SLDDRW b/sample part/Part1.SLDDRW new file mode 100644 index 0000000..9665d40 Binary files /dev/null and b/sample part/Part1.SLDDRW differ diff --git a/sample part/Part1.SLDPRT b/sample part/Part1.SLDPRT new file mode 100644 index 0000000..cb8f832 Binary files /dev/null and b/sample part/Part1.SLDPRT differ