【问题标题】:Convert all Solidworks files in folder to step files macro将文件夹中的所有 Solidworks 文件转换为步骤文件宏
【发布时间】:2018-12-08 13:05:37
【问题描述】:

我正在四处搜索并寻找一个宏,该宏在运行时会将位置中的文件转换为 .stp 文件,我遇到了以下内容。我如何操纵它来抓取文件夹中的下一个文件并继续下一个文件并转换它们,直到所有文件都被转换。

Dim swApp               As Object

将零件调暗为对象

将文件路径变暗为字符串

将 sFilePath 设为字符串

将路径尺寸变暗

将 PathNoExtention 变暗为字符串

将 NewFilePath 调暗为字符串

将文件位置变暗为字符串

将 sPath 变暗为字符串

暗淡无光

Dim bRebuild 为布尔值

将 bRet 设为布尔值

将 sRev 调暗为字符串

Dim nErrors As Long

将 nWarnings 变暗

子主()

设置 swApp = Application.SldWorks

设置部分 = swApp.ActiveDoc

FilePath = Part.GetPathName

PathSize = Strings.Len(FilePath)

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

sRev = Part.CustomInfo("re") '在此处更改配置属性名称

FileLocation = "C:"

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

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

结束子

【问题讨论】:

    标签: macros solidworks converters step


    【解决方案1】:

    如果您不使用 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;
                }
            }
        }
    }
    

    【讨论】:

    • 谢谢杰里米,我会试一试。如果我想使用不同的文件类型 let's parasolid,我所要做的就是更改 step 函数并使其适合 parasolid?
    • 您可以更改函数以接受作为文件扩展名的参数。例如,将方法更改为 static void CreateStepFile(string filename, int docType, string extension) { string stepFile = Path.Combine(Path.GetDirectoryName(fileName), Path.GetFileNameWithoutExtension(fileName), ".", extension); }
    • 你能告诉我如何运行这个文件吗?我的意思是如何设置开发环境并运行功能,对它的任何引用等等。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多