macrossolidworksconvertersstep

Convert all Solidworks files in folder to step files macro


I was searching around and looking for a macro that will when run it will convert the files in the location into .stp files and I came across the below. how can i manipulate it to grab the next file in the folder and continue the next files and convert them until all the files have been converted.

Dim swApp               As Object

Dim Part As Object

Dim FilePath As String

Dim sFilePath As String

Dim PathSize As Long

Dim PathNoExtention As String

Dim NewFilePath As String

Dim FileLocation As String

Dim sPath As String

Dim i As Long

Dim bRebuild As Boolean

Dim bRet As Boolean

Dim sRev As String

Dim nErrors As Long

Dim nWarnings As Long

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

FilePath = Part.GetPathName

PathSize = Strings.Len(FilePath)

sPath = Left(Part.GetPathName, InStrRev(Part.GetPathName, "\"))

sRev = Part.CustomInfo("re") 'Change Configuration Property name here

FileLocation = "C:"

PathNoExtension = Strings.Left(FilePath, PathSize - 7)

Part.SaveAs (PathNoExtension & "rev" & sRev & ".step")

End Sub


Solution

  • You could do this a few different ways if you're not using a VB6 Macro. If you use a .NET Macro (Visual Basic or C#), they support .NET libraries which makes this process quite simple. I've created the following Console Application in C#. You could create the same thing as a .NET Macro in SolidWorks. The important thing to add to the example you provided is the foreach statement which will iterate over all of the files in the directory and only perform the translation on SolidWorks Parts or Assemblies.

    using SolidWorks.Interop.sldworks;
    using System;
    using System.IO;
    
    namespace CreateStepFiles
    {
        class Program
        {
            static SldWorks swApp;
    
            static void Main(string[] args)
            {
                string directoryName = GetDirectoryName();
    
                if (!GetSolidWorks())
                {
                    return;
                }
    
                int i = 0;
    
                foreach (string fileName in Directory.GetFiles(directoryName))
                {
                    if (Path.GetExtension(fileName).ToLower() == ".sldprt")
                    {
                        CreateStepFile(fileName, 1);
                        i += 1;
                    }
                    else if (Path.GetExtension(fileName).ToLower() == ".sldasm")
                    {
                        CreateStepFile(fileName, 2);
                        i += 1;
                    }
                }
    
                Console.WriteLine("Finished converting {0} files", i);
    
            }
    
            static void CreateStepFile(string fileName, int docType)
            {
                int errors = 0;
                int warnings = 0;
    
                ModelDoc2 swModel = swApp.OpenDoc6(fileName, docType, 1, "", ref errors, ref warnings);
    
                string stepFile = Path.Combine(Path.GetDirectoryName(fileName), Path.GetFileNameWithoutExtension(fileName), ".STEP");
    
                swModel.Extension.SaveAs(stepFile, 0, 1, null, ref errors, ref warnings);
    
                Console.WriteLine("Created STEP file: " + stepFile);;
    
                swApp.CloseDoc(fileName);
            }
    
            static string GetDirectoryName()
            {
                Console.WriteLine("Directory to Converty");
                string s = Console.ReadLine();
    
                if (Directory.Exists(s))
                {
                    return s;
                }
    
                Console.WriteLine("Directory does not exists, try again");
                return GetDirectoryName();
            }
    
            static bool GetSolidWorks()
            {
                try
                {
                    swApp = (SldWorks)Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application"));
    
                    if (swApp == null)
                    {
                        throw new NullReferenceException(nameof(swApp));
                    }
    
                    if (!swApp.Visible)
                    {
                        swApp.Visible = true;
                    }
    
                    Console.WriteLine("SolidWorks Loaded");
                    return true;
                }
                catch (Exception)
                {
                    Console.WriteLine("Could not launch SolidWorks");
                    return false;
                }
            }
        }
    }