Solidworks C# 插件 - 将字符串发送到宏

Solidworks C# Addin - Sending a string to a macro

我目前正在开发一个新的 Solidworks 任务窗格,主要是实现我以更方便的格式编写的一些 "old" 宏。其中一些需要用户通过文本框输入,我想将其包含在任务窗格中。

问题是,我找不到编写临时文件或将这些字符串导入我的宏的方法(我不想花时间重写)

有没有其他方法可以将这些字符串发送到我的宏? Addin 在 C# 中,我目前使用的是 Solidworks "RunMacro2" 方法。

谢谢!

编辑: 在下面添加一些代码片段。有问题的主要宏是为了在整个程序集中传播自定义 属性 文件。

namespace Efficiency_Interface
{
    [ComVisible(true)]
    [ProgId(ProjMan_Tab_PROGID)]
    public partial class Project_Management_Tab : UserControl
    {

        SwAddin SolidRun = new SwAddin();
        public const string ProjMan_Tab_PROGID = "Proj Management";
        public const string scratchFile = "C:\keyStoneAddinScratch.txt";
        StreamWriter writeText = new StreamWriter(scratchFile);

        public Project_Management_Tab()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (var textBox in this.Controls.OfType<TextBox>())
            {
                writeText.WriteLine(textBox.Text);
            }
            writeText.Close();
            SolidRun.runGen("proc");
        }
    }
}

以上是我的任务窗格的基本代码。 下面是我的主要插件 class 的代码片段,它还包含我的其他按钮之一的代码。

public void runCreate4Pack()
{
    iSwApp.RunMacro2(macroPath + "CREATE 4 SIZE PACKAGE.swp", "", "", 0, out runMacroError);
    return;
}

public void runGen(string procName)
{
    iSwApp.RunMacro2(macroPath + "Efficiency Interface.swp", "hitButton", procName, 0, out runMacroError);
    File.Delete("C:\Users\Public\keyStoneAddinScratch.txt");
    return;
}

最后,是我尝试与之交互的宏的主要代码。

Private Sub fillProps_Click()
    Dim doneParts() As Variant
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set part = swApp.ActiveDoc

    If firstAssem = "" Then
        referenceList (0)
    End If

    i = 0
    ReDim Preserve doneParts(1)
    Set custPropMgr = part.Extension.CustomPropertyManager(part.GetActiveConfiguration.name)
    setProps "", "", ""

    While i <= 1000 And Not assemComps(i, j) = ""
        parentAssemNum = Left(assemComps(i, 0), InStrRev(assemComps(i, 0), ".") - 1)
        parentAssemDesc = assemComps(i, 1)
        j = 2
        While j <= 10000 And Not assemComps(i, j) = ""
            k = 0
            While k < UBound(doneParts())
                If doneParts(k) = assemComps(i, j) Then
                    GoTo skipEntry
                End If
                k = k + 1
            Wend
            Set part = swApp.ActivateDoc3(assemComps(i, j), True, 1, 1)
            If part.GetType = 1 Then
                Set swpartdoc = part
            End If

            If InStr(part.IGetActiveConfiguration.name, "Default") > 0 Then
                Set custPropMgr = part.Extension.CustomPropertyManager("")
            End If
            Debug.Print custPropMgr.Get("Description")
            setProps parentAssemNum + "", parentAssemDesc + "", ""

            ReDim Preserve doneParts(UBound(doneParts) + 1)
            doneParts(UBound(doneParts)) = assemComps(i, j)
            If i > 0 Or j > 0 Then
                swApp.QuitDoc (assemComps(i, j))
            End If
skipEntry:
            j = j + 1
        Wend
        i = i + 1
    Wend   
End Sub

Sub setProps(parentAssemNum As String, parentAssemDesc As String, stockSize  As String)
    If custPropMgr.Get("StockSize") = "" Then
        If part.GetType = swDocPART Then
            If swpartdoc.IsWeldment = False Then
                Dim sizeArray(2)
                vboundbox = swpartdoc.GetPartBox(False)
                size1 = Round(Abs(vboundbox(0) - vboundbox(3)), 2)
                size2 = Round(Abs(vboundbox(1) - vboundbox(4)), 2)
                size3 = Round(Abs(vboundbox(2) - vboundbox(5)), 2)
                sizeArray(0) = size1
                sizeArray(1) = size2
                sizeArray(2) = size3
                rectVol = size1 * size2 * size3
                Set swmass = part.Extension.CreateMassProperty
                swmass.UseSystemUnits = False
                swVol = swmass.Volume
                QuickSort sizeArray, LBound(sizeArray), UBound(sizeArray)
                eq = 0
                If size1 = size2 Then
                    eq = 1
                    cylVol = 3.14159 * ((size1 / 2) * (size1 / 2)) * size3
                End If
                If size1 = size3 Then
                    eq = 1
                    cylVol = 3.14159 * ((size1 / 2) * (size1 / 2)) * size2
                End If
                If size3 = size2 Then
                    eq = 1
                    cylVol = 3.14159 * ((size2 / 2) * (size2 / 2)) * size1
                End If
                If eq = 1 Then
                    If Abs(swVol - cylVol) < Abs(swVol - rectVol) Then
                        boolstatus = custPropMgr.Set("StockSize", "ROUND BAR, " & size2 & " OD X " & size3 & "LG")
                    End If
                    If Abs(swVol - rectVol) < Abs(swVol - cylVol) Then
                        'If size3 > size2 Then
                            boolstatus = custPropMgr.Set("StockSize", size2 & " SQ X " & size3 & "LG")
                        'End If
                        'If size3 < size2 Then
                        '    boolstatus = custPropMgr.Set("StockSize", "PLATE," & size2 & " SQ X " & size3 & "LG")
                        'End If
                    End If
                    If Abs(swVol - cylVol) = Abs(swVol - rectVol) Then
                        'If size3 > size2 Then
                            boolstatus = custPropMgr.Set("StockSize", "BAR, " & size2 & " X " & size3 & "LG")
                        'End If
                        'If size3 < size2 Then
                        '    boolstatus = custPropMgr.Set("StockSize", "PLATE," & size2 & " SQ X " & size3 & "LG")
                        'End If
                    End If
                End If
                If eq = 0 Then
                    boolstatus = custPropMgr.Set("StockSize", sizeArray(2) & " X " & sizeArray(1) & " X " & sizeArray(0))
                End If
            End If
        End If
    End If
    If Len(custPropMgr.Get("DATE")) = 0 Then
        boolstatus = custPropMgr.Set("Date", Left(Now, InStrRev(Now, "/") + 4))
    End If

    boolstatus = custPropMgr.Set("Workorder", Interface.workorder.Text)
    boolstatus = custPropMgr.Set("Plant", Interface.plantBox.Text)
    boolstatus = custPropMgr.Set("AssemblyNumber", parentAssemNum)
    boolstatus = custPropMgr.Set("Line2", parentAssemDesc)
    boolstatus = custPropMgr.Set("Line3", Interface.projBox.Text)

    If Len(custPropMgr.Get("Finish")) = 0 Then
        'boolstatus = custPropMgr.Set("Finish", Interface.finish.Text)
    End If
    If Len(custPropMgr.Get("DesignBy")) = 0 Then
        'boolstatus = custPropMgr.Set("DesignBy", Interface.creator.Text)
    End If
    If Len(custPropMgr.Get("DrawnBy")) = 0 Then
        'boolstatus = custPropMgr.Set("DrawnBy", Interface.creator.Text)
    End If
End Sub

我希望这会有所帮助,并且不会太混乱。

Solidworks 支持 2 种类型的 VB 宏文件

  • swp - 较新版本
  • swb - 较旧的纯文本宏,这里是空 swb 宏的示例:

    Dim swApp As Object  
    Dim swModel  As SldWorks.ModelDoc2  
      Sub main()  
        Set swApp = Application.SldWorks 
        Set swModel = swApp.ActiveDoc  
      End Sub  
    

如果您可以访问宏的源代码,您可以将它们转换为纯文本并保留占位符。

MsgBox "--MyParameter1--"

用户在您的任务窗格中填写参数后,将 swb 宏中的占位符替换为实际值,然后运行它。