复制
收藏
提问
全网

Dim swApp As Object Dim Part As Object Dim SelMgr As Object Dim boolstatus As Boolean Dim longstatus As Long, longwarnings As Long Dim Feature As Object Dim swmodel As ModelDoc2 Dim swmodelComps As Variant Dim swmodelComp As SldWorks.Component2 Dim swAsm As SldWorks.AssemblyDoc Dim a As Integer '设定变量a为整数 Dim b As String '设定变量b为字符型变量 Dim m As String Dim e As String Dim k As String Dim t As String Dim c As String Dim j As Integer Dim n As String Dim d As Integer Dim strmat As String Dim tempvalue As String Sub main() 'link solidworks Set swApp = Application.SldWorks '定义并获取SolidWorks应用程序对象。 Set Part = swApp.ActiveDoc '获取当前活动文档(模型)。 Set SelMgr = Part.SelectionManager '获取模型的选择管理器。 swApp.ActiveDoc.ActiveView.FrameState = 1 '设置活动视图的帧状态为1,通常用于显示模型的特定视图。 Set swmodel = swApp.ActiveDoc Set swAsm = swmodel swmodelComps = swAsm.GetComponents(True) c = getasmchild(swmodelComps) End Sub Sub main() Private Function getasmchild(swmodelComps As Variant) Dim swmodelComp As SldWorks.Component2 Dim swmodel As ModelDoc2 Dim swAsm As SldWorks.AssemblyDoc For i = 0 To UBound(swmodelComps) Set swmodelComp = swmodelComps(i) Set swmodel = swmodelComp.GetModelDoc2 If swmodel.GetType = 1 Then MsgBox (swmodel.GetTitle) ElseIf swmodel.GetType = 2 Then swAsm = swmodel getasmchild (swAsm.GetComponents(True)) End If Next End Function '设定变量 c = swApp.ActiveDoc.GetTitle() '零件名 strmat = Chr(34) + Trim("SW-Material" + "@") + c + Chr(34) blnretval = Part.DeleteCustomInfo2("", "代号") '删除图样的 "代号" 自定义信息的操作。 blnretval = Part.DeleteCustomInfo2("", "名称") '删除自定义名称内容 a = InStr(c, "_") - 1 '重点:分隔标识符,这里是一个_ If a > 0 Then k = Left(c, a) t = Left(LTrim(e), 3) 'LTrim(e)函数的作用是去除字符串e开头的所有空格字符,Left(LTrim(e), 3)函数的作用是获取经过LTrim处理后的字符串的前3个字符 If t = "GBT" Then e = "GB/T" + Mid(k, 4) '首先使用Mid函数从变量k中提取从第4个字符开始的子字符串,然后将这个子字符串与"GB/T"拼接起来,结果赋值给变量e。 Else e = k End If b = Mid(c, a + 2) '从字符串c的第a + 2个字符开始,提取到字符串末尾的所有字符,并将这部分字符赋值给变量b t = Right(c, 7) If t = ".SLDPRT" Or t = ".SLDASM" Then j = Len(b) - 7 Else j = Len(b) End If m = Left(b, j) d = InStr(m, "_") - 1 '重点:分隔标识符,这里是一个空格(_) If d > 0 Then n = Left(m, d) Else n = m End If End If blnretval = Part.AddCustomInfo3("", "代号", swCustomInfoText, e) '增加自定义代号 blnretval = Part.AddCustomInfo3("", "名称", swCustomInfoText, n) '增加自定义名称 blnretval = Part.AddCustomInfo3("", "表面处理", swCustomInfoText, " ") End Sub

b8d3a622c
3个月前
全网
文档
学术
百科
知识库
时间不限
自动

理解问题Dim swApp As Object Dim Part As Object Dim SelMgr As Object Dim boolstatus As Boolean Dim longstatus As Long, longwarnings As Long Dim Feature As Object Dim swmodel As ModelDoc2 Dim swmodelComps As Variant Dim swmodelComp As SldWorks.Component2 Dim swAsm As SldWorks.AssemblyDoc Dim a As Integer '设定变量a为整数 Dim b As String '设定变量b为字符型变量 Dim m As String Dim e As String Dim k As String Dim t As String Dim c As String Dim j As Integer Dim n As String Dim d As Integer Dim strmat As String Dim tempvalue As String Sub main() 'link solidworks Set swApp = Application.SldWorks '定义并获取SolidWorks应用程序对象。 Set Part = swApp.ActiveDoc '获取当前活动文档(模型)。 Set SelMgr = Part.SelectionManager '获取模型的选择管理器。 swApp.ActiveDoc.ActiveView.FrameState = 1 '设置活动视图的帧状态为1,通常用于显示模型的特定视图。 Set swmodel = swApp.ActiveDoc Set swAsm = swmodel swmodelComps = swAsm.GetComponents(True) c = getasmchild(swmodelComps) End Sub Sub main() Private Function getasmchild(swmodelComps As Variant) Dim swmodelComp As SldWorks.Component2 Dim swmodel As ModelDoc2 Dim swAsm As SldWorks.AssemblyDoc For i = 0 To UBound(swmodelComps) Set swmodelComp = swmodelComps(i) Set swmodel = swmodelComp.GetModelDoc2 If swmodel.GetType = 1 Then MsgBox (swmodel.GetTitle) ElseIf swmodel.GetType = 2 Then swAsm = swmodel getasmchild (swAsm.GetComponents(True)) End If Next End Function '设定变量 c = swApp.ActiveDoc.GetTitle() '零件名 strmat = Chr(34) + Trim("SW-Material" + "@") + c + Chr(34) blnretval = Part.DeleteCustomInfo2("", "代号") '删除图样的 "代号" 自定义信息的操作。 blnretval = Part.DeleteCustomInfo2("", "名称") '删除自定义名称内容 a = InStr(c, "_") - 1 '重点:分隔标识符,这里是一个_ If a > 0 Then k = Left(c, a) t = Left(LTrim(e), 3) 'LTrim(e)函数的作用是去除字符串e开头的所有空格字符,Left(LTrim(e), 3)函数的作用是获取经过LTrim处理后的字符串的前3个字符 If t = "GBT" Then e = "GB/T" + Mid(k, 4) '首先使用Mid函数从变量k中提取从第4个字符开始的子字符串,然后将这个子字符串与"GB/T"拼接起来,结果赋值给变量e。 Else e = k End If b = Mid(c, a + 2) '从字符串c的第a + 2个字符开始,提取到字符串末尾的所有字符,并将这部分字符赋值给变量b t = Right(c, 7) If t = ".SLDPRT" Or t = ".SLDASM" Then j = Len(b) - 7 Else j = Len(b) End If m = Left(b, j) d = InStr(m, "_") - 1 '重点:分隔标识符,这里是一个空格(_) If d > 0 Then n = Left(m, d) Else n = m End If End If blnretval = Part.AddCustomInfo3("", "代号", swCustomInfoText, e) '增加自定义代号 blnretval = Part.AddCustomInfo3("", "名称", swCustomInfoText, n) '增加自定义名称 blnretval = Part.AddCustomInfo3("", "表面处理", swCustomInfoText, " ") End Sub

已完成理解Dim swApp As Object Dim Part As Object Dim SelMgr As Object Dim boolstatus As Boolean Dim longstatus As Long, longwarnings As Long Dim Feature As Object Dim swmodel As ModelDoc2 Dim swmodelComps As Variant Dim swmodelComp As SldWorks.Component2 Dim swAsm As SldWorks.AssemblyDoc Dim a As Integer '设定变量a为整数 Dim b As String '设定变量b为字符型变量 Dim m As String Dim e As String Dim k As String Dim t As String Dim c As String Dim j As Integer Dim n As String Dim d As Integer Dim strmat As String Dim tempvalue As String Sub main() 'link solidworks Set swApp = Application.SldWorks '定义并获取SolidWorks应用程序对象。 Set Part = swApp.ActiveDoc '获取当前活动文档(模型)。 Set SelMgr = Part.SelectionManager '获取模型的选择管理器。 swApp.ActiveDoc.ActiveView.FrameState = 1 '设置活动视图的帧状态为1,通常用于显示模型的特定视图。 Set swmodel = swApp.ActiveDoc Set swAsm = swmodel swmodelComps = swAsm.GetComponents(True) c = getasmchild(swmodelComps) End Sub Sub main() Private Function getasmchild(swmodelComps As Variant) Dim swmodelComp As SldWorks.Component2 Dim swmodel As ModelDoc2 Dim swAsm As SldWorks.AssemblyDoc For i = 0 To UBound(swmodelComps) Set swmodelComp = swmodelComps(i) Set swmodel = swmodelComp.GetModelDoc2 If swmodel.GetType = 1 Then MsgBox (swmodel.GetTitle) ElseIf swmodel.GetType = 2 Then swAsm = swmodel getasmchild (swAsm.GetComponents(True)) End If Next End Function '设定变量 c = swApp.ActiveDoc.GetTitle() '零件名 strmat = Chr(34) + Trim("SW-Material" + "@") + c + Chr(34) blnretval = Part.DeleteCustomInfo2("", "代号") '删除图样的 "代号" 自定义信息的操作。 blnretval = Part.DeleteCustomInfo2("", "名称") '删除自定义名称内容 a = InStr(c, "_") - 1 '重点:分隔标识符,这里是一个_ If a > 0 Then k = Left(c, a) t = Left(LTrim(e), 3) 'LTrim(e)函数的作用是去除字符串e开头的所有空格字符,Left(LTrim(e), 3)函数的作用是获取经过LTrim处理后的字符串的前3个字符 If t = "GBT" Then e = "GB/T" + Mid(k, 4) '首先使用Mid函数从变量k中提取从第4个字符开始的子字符串,然后将这个子字符串与"GB/T"拼接起来,结果赋值给变量e。 Else e = k End If b = Mid(c, a + 2) '从字符串c的第a + 2个字符开始,提取到字符串末尾的所有字符,并将这部分字符赋值给变量b t = Right(c, 7) If t = ".SLDPRT" Or t = ".SLDASM" Then j = Len(b) - 7 Else j = Len(b) End If m = Left(b, j) d = InStr(m, "_") - 1 '重点:分隔标识符,这里是一个空格(_) If d > 0 Then n = Left(m, d) Else n = m End If End If blnretval = Part.AddCustomInfo3("", "代号", swCustomInfoText, e) '增加自定义代号 blnretval = Part.AddCustomInfo3("", "名称", swCustomInfoText, n) '增加自定义名称 blnretval = Part.AddCustomInfo3("", "表面处理", swCustomInfoText, " ") End Sub

展开阅读网页

看起来你正在尝试编写一个VBA宏,用于在SolidWorks中处理模型和装配体的自定义信息。你的代码中有几个问题和不一致之处,我将帮你逐一解决。

  1. 重复的Sub main():你的代码中有两个Sub main(),这会导致编译错误。你需要删除或重命名其中一个。

  2. getasmchild函数的调用:在main子程序中,你调用了getasmchild函数,但没有传递参数。你需要传递swmodelComps作为参数。

  3. 变量e未定义:在处理字符串时,你使用了变量e,但它在之前没有被定义。

  4. strmat变量的使用:你定义了strmat变量,但在代码中没有使用它。

  5. blnretval变量的使用:你使用了blnretval变量来存储DeleteCustomInfo2AddCustomInfo3函数的返回值,但没有检查这些函数是否成功执行。

  6. InStrMid函数的使用:在处理字符串时,你需要确保使用正确的参数和逻辑。

  7. 代码的组织:你的代码中有一些逻辑上的混乱,需要重新组织以提高可读性和可维护性。

以下是修改后的代码:

Dim swApp As Object
Dim Part As Object
Dim SelMgr As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Feature As Object

Dim swmodel As ModelDoc2
Dim swmodelComps As Variant
Dim swmodelComp As SldWorks.Component2
Dim swAsm As SldWorks.AssemblyDoc

Dim a As Integer    '设定变量a为整数
Dim b As String    '设定变量b为字符型变量
Dim m As String
Dim e As String
Dim k As String
Dim t As String
Dim c As String
Dim j As Integer
Dim n As String
Dim d As Integer
Dim strmat As String
Dim tempvalue As String

Sub main()
    'link solidworks
    Set swApp = Application.SldWorks     '定义并获取SolidWorks应用程序对象。
    Set Part = swApp.ActiveDoc  '获取当前活动文档(模型)。
    Set SelMgr = Part.SelectionManager  '获取模型的选择管理器。
    swApp.ActiveDoc.ActiveView.FrameState = 1  '设置活动视图的帧状态为1,通常用于显示模型的特定视图。

    Set swmodel = swApp.ActiveDoc
    Set swAsm = swmodel
    swmodelComps = swAsm.GetComponents(True)
    getasmchild swmodelComps
End Sub

Private Function getasmchild(swmodelComps As Variant)
    Dim swmodelComp As SldWorks.Component2
    Dim swmodel As ModelDoc2
    Dim swAsm As SldWorks.AssemblyDoc
    For i = 0 To UBound(swmodelComps)
        Set swmodelComp = swmodelComps(i)
        Set swmodel = swmodelComp.GetModelDoc2
        If swmodel.GetType = 1 Then
            MsgBox (swmodel.GetTitle)
        ElseIf swmodel.GetType = 2 Then
            swAsm = swmodel
            getasmchild (swAsm.GetComponents(True))
        End If
    Next
End Function

Sub UpdateCustomInfo()
    Dim c As String
    Dim strmat As String
    Dim blnretval As Boolean

    '设定变量
    c = swApp.ActiveDoc.GetTitle() '零件名
    strmat = Chr(34) + Trim("SW-Material" + "@") + c + Chr(34)

    '删除图样的 "代号" 自定义信息的操作。
    blnretval = Part.DeleteCustomInfo2("", "代号")
    If Not blnretval Then
        MsgBox "Failed to delete custom info '代号'."
    End If

    '删除自定义名称内容
    blnretval = Part.DeleteCustomInfo2("", "名称")
    If Not blnretval Then
        MsgBox "Failed to delete custom info '名称'."
    End If

    a = InStr(c, "_") - 1
    If a > 0 Then
        k = Left(c, a)
        e = "GB/T" + Mid(k, 4)  '首先使用Mid函数从变量k中提取从第4个字符开始的子字符串,然后将这个子字符串与"GB/T"拼接起来,结果赋值给变量e。
        b = Mid(c, a + 2)
        t = Right(c, 7)
        If t = ".SLDPRT" Or t = ".SLDASM" Then
更专业一些
转笔记
专业
Dim swApp As Object Dim Part As Object Dim SelMgr As Object Dim boolstatus As Boolean Dim longstatus As Long, longwarnings As Long Dim Feature As Object Dim swmodel As ModelDoc2 Dim swmodelComps As Variant Dim swmodelComp As SldWorks.Component2 Dim swAsm As SldWorks.AssemblyDoc Dim a As Integer '设定变量a为整数 Dim b As String '设定变量b为字符型变量 Dim m As String Dim e As String Dim k As String Dim t As String Dim c As String Dim j As Integer Dim n As String Dim d As Integer Dim strmat As String Dim tempvalue As String Sub main() 'link solidworks Set swApp = Application.SldWorks '定义并获取SolidWorks应用程序对象。 Set Part = swApp.ActiveDoc '获取当前活动文档(模型)。 Set SelMgr = Part.SelectionManager '获取模型的选择管理器。 swApp.ActiveDoc.ActiveView.FrameState = 1 '设置活动视图的帧状态为1,通常用于显示模型的特定视图。 Set swmodel = swApp.ActiveDoc Set swAsm = swmodel swmodelComps = swAsm.GetComponents(True) c = getasmchild(swmodelComps) End Sub Sub main() Private Function getasmchild(swmodelComps As Variant) Dim swmodelComp As SldWorks.Component2 Dim swmodel As ModelDoc2 Dim swAsm As SldWorks.AssemblyDoc For i = 0 To UBound(swmodelComps) Set swmodelComp = swmodelComps(i) Set swmodel = swmodelComp.GetModelDoc2 If swmodel.GetType = 1 Then MsgBox (swmodel.GetTitle) ElseIf swmodel.GetType = 2 Then swAsm = swmodel getasmchild (swAsm.GetComponents(True)) End If Next End Function '设定变量 c = swApp.ActiveDoc.GetTitle() '零件名 strmat = Chr(34) + Trim("SW-Material" + "@") + c + Chr(34) blnretval = Part.DeleteCustomInfo2("", "代号") '删除图样的 "代号" 自定义信息的操作。 blnretval = Part.DeleteCustomInfo2("", "名称") '删除自定义名称内容 a = InStr(c, "_") - 1 '重点:分隔标识符,这里是一个_ If a > 0 Then k = Left(c, a) t = Left(LTrim(e), 3) 'LTrim(e)函数的作用是去除字符串e开头的所有空格字符,Left(LTrim(e), 3)函数的作用是获取经过LTrim处理后的字符串的前3个字符 If t = "GBT" Then e = "GB/T" + Mid(k, 4) '首先使用Mid函数从变量k中提取从第4个字符开始的子字符串,然后将这个子字符串与"GB/T"拼接起来,结果赋值给变量e。 Else e = k End If b = Mid(c, a + 2) '从字符串c的第a + 2个字符开始,提取到字符串末尾的所有字符,并将这部分字符赋值给变量b t = Right(c, 7) If t = ".SLDPRT" Or t = ".SLDASM" Then j = Len(b) - 7 Else j = Len(b) End If m = Left(b, j) d = InStr(m, "_") - 1 '重点:分隔标识符,这里是一个空格(_) If d > 0 Then n = Left(m, d) Else n = m End If End If blnretval = Part.AddCustomInfo3("", "代号", swCustomInfoText, e) '增加自定义代号 blnretval = Part.AddCustomInfo3("", "名称", swCustomInfoText, n) '增加自定义名称 blnretval = Part.AddCustomInfo3("", "表面处理", swCustomInfoText, " ") End Sub
不在提醒

更专业内容正在努力生成中
知识树
Dim swApp As Object Dim Part As Object Dim SelMgr As Object Dim boolstatus As Boolean Dim longstatus As Long, longwarnings As Long Dim Feature As Object Dim swmodel As ModelDoc2 Dim swmodelComps As Variant Dim swmodelComp As SldWorks.Component2 Dim swAsm As SldWorks.AssemblyDoc Dim a As Integer '设定变量a为整数 Dim b As String '设定变量b为字符型变量 Dim m As String Dim e As String Dim k As String Dim t As String Dim c As String Dim j As Integer Dim n As String Dim d As Integer Dim strmat As String Dim tempvalue As String Sub main() 'link solidworks Set swApp = Application.SldWorks '定义并获取SolidWorks应用程序对象。 Set Part = swApp.ActiveDoc '获取当前活动文档(模型)。 Set SelMgr = Part.SelectionManager '获取模型的选择管理器。 swApp.ActiveDoc.ActiveView.FrameState = 1 '设置活动视图的帧状态为1,通常用于显示模型的特定视图。 Set swmodel = swApp.ActiveDoc Set swAsm = swmodel swmodelComps = swAsm.GetComponents(True) c = getasmchild(swmodelComps) End Sub Sub main() Private Function getasmchild(swmodelComps As Variant) Dim swmodelComp As SldWorks.Component2 Dim swmodel As ModelDoc2 Dim swAsm As SldWorks.AssemblyDoc For i = 0 To UBound(swmodelComps) Set swmodelComp = swmodelComps(i) Set swmodel = swmodelComp.GetModelDoc2 If swmodel.GetType = 1 Then MsgBox (swmodel.GetTitle) ElseIf swmodel.GetType = 2 Then swAsm = swmodel getasmchild (swAsm.GetComponents(True)) End If Next End Function '设定变量 c = swApp.ActiveDoc.GetTitle() '零件名 strmat = Chr(34) + Trim("SW-Material" + "@") + c + Chr(34) blnretval = Part.DeleteCustomInfo2("", "代号") '删除图样的 "代号" 自定义信息的操作。 blnretval = Part.DeleteCustomInfo2("", "名称") '删除自定义名称内容 a = InStr(c, "_") - 1 '重点:分隔标识符,这里是一个_ If a > 0 Then k = Left(c, a) t = Left(LTrim(e), 3) 'LTrim(e)函数的作用是去除字符串e开头的所有空格字符,Left(LTrim(e), 3)函数的作用是获取经过LTrim处理后的字符串的前3个字符 If t = "GBT" Then e = "GB/T" + Mid(k, 4) '首先使用Mid函数从变量k中提取从第4个字符开始的子字符串,然后将这个子字符串与"GB/T"拼接起来,结果赋值给变量e。 Else e = k End If b = Mid(c, a + 2) '从字符串c的第a + 2个字符开始,提取到字符串末尾的所有字符,并将这部分字符赋值给变量b t = Right(c, 7) If t = ".SLDPRT" Or t = ".SLDASM" Then j = Len(b) - 7 Else j = Len(b) End If m = Left(b, j) d = InStr(m, "_") - 1 '重点:分隔标识符,这里是一个空格(_) If d > 0 Then n = Left(m, d) Else n = m End If End If blnretval = Part.AddCustomInfo3("", "代号", swCustomInfoText, e) '增加自定义代号 blnretval = Part.AddCustomInfo3("", "名称", swCustomInfoText, n) '增加自定义名称 blnretval = Part.AddCustomInfo3("", "表面处理", swCustomInfoText, " ") End Sub
SolidWorks VBA宏如何获取活动文档对象?
SolidWorks VBA中如何使用SelectionManager?
VBA中如何定义和使用自定义信息?

以上内容由AI搜集生成,仅供参考

在线客服