首页 > 解决方案 > 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

标签: macrossolidworksconvertersstep

解决方案


如果您不使用 VB6 宏,您可以通过几种不同的方式执行此操作。如果您使用 .NET 宏(Visual Basic 或 C#),它们支持 .NET 库,这使得此过程非常简单。我在 C# 中创建了以下控制台应用程序。您可以在 SolidWorks 中创建与 .NET 宏相同的东西。添加到您提供的示例中的重要内容是 foreach 语句,它将遍历目录中的所有文件并仅在 SolidWorks 零件或装配体上执行翻译。

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;
            }
        }
    }
}

推荐阅读