Advertisement
fanqiema

Untitled

Nov 12th, 2023
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. ' 普通模块代码
  3. Option Explicit
  4. ' 主运行方法 Main
  5. Sub Main()
  6.     ' 初始化用户界面,准备数据填充
  7.    UserForm1.Show 0
  8.     InitializeUserForm
  9.     ' 显示用户界面
  10.    CheckMappingAndUpdateLabel
  11.     FillMappingListBoxAndIdentifiers
  12. End Sub
  13.  
  14. ' 功能:获取用于存储设置的文本文件的完整路径。
  15. ' 在这个函数中:
  16. ' 1. 使用 Application.SldWorks 获取 SolidWorks 的应用程序对象。
  17. ' 2. 获取当前运行的宏的路径,并从中提取目录路径。
  18. ' 3. 拼接目标文件名,即存储设置的文本文件名。
  19. ' 4. 返回完整的文件路径。
  20. Function GetFilePath() As String
  21.     Dim swApp As Object
  22.     Dim currentMacroPath As String
  23.     Dim directoryPath As String
  24.     Dim targetFileName As String
  25.    
  26.     ' 获取SolidWorks应用对象
  27.    Set swApp = Application.SldWorks
  28.    
  29.     ' 获取当前宏的路径
  30.    currentMacroPath = swApp.GetCurrentMacroPathName
  31.     directoryPath = Left(currentMacroPath, InStrRev(currentMacroPath, "\"))
  32.     targetFileName = "智能命名系统data.txt"
  33.    
  34.     ' 返回构建的文件路径
  35.    GetFilePath = directoryPath & targetFileName
  36. End Function
  37. ' 此子程序用于读取映射行并填充列表框,同时填充文本框显示完整的代号行内容
  38. Public Sub FillMappingListBoxAndIdentifiers()
  39.     Dim filePath As String
  40.     Dim fileContent As String
  41.     Dim lines As Variant
  42.     Dim i As Integer
  43.    
  44.     filePath = GetFilePath()
  45.     If filePath <> "" And Dir(filePath) <> "" Then
  46.         fileContent = GetFileContent(filePath)
  47.         lines = Split(fileContent, vbCrLf)
  48.        
  49.         ' 清空列表框
  50.        UserForm1.ListBox_映射.Clear
  51.        
  52.         ' 遍历每一行
  53.        For i = LBound(lines) To UBound(lines)
  54.             If InStr(lines(i), "映射|") > 0 Then
  55.                 ' 只向列表框添加以"映射"开头的行
  56.                UserForm1.ListBox_映射.AddItem lines(i)
  57.             ElseIf InStr(lines(i), "钣金件代号|") > 0 Then
  58.                 ' 显示完整的钣金件代号行
  59.                UserForm1.TextBox_钣金件代号.text = lines(i)
  60.             ElseIf InStr(lines(i), "车床件代号|") > 0 Then
  61.                 ' 显示完整的车床件代号行
  62.                UserForm1.TextBox_车床件代号.text = lines(i)
  63.             ElseIf InStr(lines(i), "亚克力代号|") > 0 Then
  64.                 ' 显示完整的亚克力代号行
  65.                UserForm1.TextBox_亚克力代号.text = lines(i)
  66.             ElseIf InStr(lines(i), "机加件标识|") > 0 Then
  67.                 ' 显示完整的机加件标识行
  68.                UserForm1.TextBox_机加件标识.text = lines(i)
  69.             End If
  70.         Next i
  71.     Else
  72.         MsgBox "配置文件不存在,请检查。"
  73.     End If
  74. End Sub
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. Public Function GetFileContent(ByVal filePath As String) As String
  82.     Dim fileNumber As Integer
  83.     Dim lineContent As String
  84.     Dim contentBuilder As String
  85.     fileNumber = FreeFile
  86.  
  87.     On Error GoTo ErrorHandler
  88.     Open filePath For Input As fileNumber
  89.    
  90.     ' 初始化内容构建器
  91.    contentBuilder = ""
  92.    
  93.     ' 循环读取每一行直到文件结束
  94.    Do Until EOF(fileNumber)
  95.         Line Input #fileNumber, lineContent
  96.         contentBuilder = contentBuilder & lineContent & vbCrLf
  97.     Loop
  98.    
  99.     Close fileNumber
  100.     GetFileContent = contentBuilder
  101.     Exit Function
  102.    
  103. ErrorHandler:
  104.     MsgBox "读取文件时发生错误: " & Err.Description, vbCritical, "错误"
  105.     Close fileNumber
  106.     GetFileContent = ""
  107. End Function
  108.  
  109. ' 检查当前装配体名称是否在映射列表中,并更新Label_映射信息的状态
  110. Public Sub CheckMappingAndUpdateLabel()
  111.     Dim swApp As Object
  112.     Dim swModel As Object
  113.     Dim assemblyName As String
  114.     Dim assemblyBaseName As String ' 添加变量来存储基础装配体名称
  115.    Dim foundMapping As Boolean
  116.     Dim mappingEntry As String
  117.     Dim mappingParts As Variant
  118.     Dim i As Integer
  119.    
  120.     Set swApp = Application.SldWorks
  121.     Set swModel = swApp.ActiveDoc
  122.     foundMapping = False
  123.    
  124.     If Not swModel Is Nothing Then
  125.         assemblyName = swModel.GetTitle ' 获取装配体名称
  126.        ' 检查是否存在下划线,并获取下划线之前的名称
  127.        If InStr(assemblyName, "_") > 0 Then
  128.             assemblyBaseName = Left(assemblyName, InStr(assemblyName, "_") - 1)
  129.         Else
  130.             assemblyBaseName = assemblyName
  131.         End If
  132.         Debug.Print "基础装配体名称: " & assemblyBaseName ' 输出基础装配体名称
  133.        
  134.         For i = 0 To UserForm1.ListBox_映射.ListCount - 1
  135.             mappingEntry = UserForm1.ListBox_映射.List(i) ' 获取映射条目
  136.            mappingParts = Split(mappingEntry, "|") ' 分割映射条目
  137.            ' 检查映射条目是否包含基础装配体名称
  138.            If UBound(mappingParts) >= 2 And Trim(mappingParts(1)) = assemblyBaseName Then
  139.                 foundMapping = True
  140.                 UserForm1.TextBox_代号.Value = mappingParts(2) ' 设置映射字符
  141.                Exit For
  142.             End If
  143.         Next i
  144.        
  145.         ' 更新Label_映射信息的显示
  146.        With UserForm1.Label_映射信息
  147.             If foundMapping Then
  148.                 .caption = "映射成功"
  149.                 .ForeColor = RGB(0, 128, 0) ' 绿色
  150.            Else
  151.                 .caption = "暂无映射,请确认"
  152.                 .ForeColor = RGB(255, 0, 0) ' 红色
  153.                UserForm1.TextBox_代号.Value = "" ' 清除之前的值
  154.            End If
  155.         End With
  156.     Else
  157.         MsgBox "未打开装配体文档。"
  158.     End If
  159. End Sub
  160.  
  161.  
  162. ' 将文本追加到文件
  163. Public Sub AppendTextToFile(ByVal filePath As String, ByVal text As String)
  164.     Dim fileNumber As Integer
  165.     fileNumber = FreeFile
  166.     Open filePath For Append As fileNumber
  167.     Print #fileNumber, text
  168.     Close fileNumber
  169. End Sub
  170.  
  171.  
  172.  
  173. ' 这个子程序用于获取当前装配体上级目录下所有文件夹的名称
  174. ' 获取当前装配体所在的上级目录下所有子文件夹的名称,并填充到列表框中
  175. Sub GetFoldersAndFillList()
  176.     Dim swApp As Object
  177.     Dim swModel As Object
  178.     Dim asmPath As String
  179.     Dim parentPath As String
  180.     Dim subFolder As Object
  181.     Dim fso As Object
  182.     Dim folder As Object
  183.  
  184.     Set swApp = Application.SldWorks
  185.     Set swModel = swApp.ActiveDoc
  186.     Set fso = CreateObject("Scripting.FileSystemObject")
  187.  
  188.     If Not swModel Is Nothing Then
  189.         asmPath = swModel.GetPathName
  190.         If asmPath <> "" Then
  191.             ' 获取文件所在的目录
  192.            parentPath = fso.GetParentFolderName(asmPath)
  193.             ' 获取上级目录
  194.            Set folder = fso.GetFolder(parentPath).parentFolder
  195.  
  196.             ' 清空现有列表
  197.            UserForm1.ListBox_文件夹.Clear
  198.  
  199.             ' 获取上级目录下的所有子目录,并添加到列表框中
  200.            For Each subFolder In folder.SubFolders
  201.                 UserForm1.ListBox_文件夹.AddItem subFolder.path
  202.  
  203.             Next subFolder
  204.         Else
  205.             MsgBox "装配体文件没有路径信息。可能是未保存的新文件。"
  206.         End If
  207.     Else
  208.         MsgBox "未打开装配体文档。"
  209.     End If
  210. End Sub
  211.  
  212.  
  213.  
  214. ' 这个子程序用于捕获选中的组件信息并显示在用户窗体控件中
  215. Sub CaptureSelectedComponentInfo()
  216.     Dim swApp As Object
  217.     Dim swModel As Object
  218.     Dim swSelMgr As Object
  219.     Dim swComponent As Object
  220.     Dim path As String
  221.     Dim fileName As String
  222.     Dim fso As Object
  223.     Dim fileExtension As String
  224.    
  225.     Set swApp = Application.SldWorks
  226.     Set swModel = swApp.ActiveDoc
  227.     Set swSelMgr = swModel.SelectionManager
  228.     Set fso = CreateObject("Scripting.FileSystemObject")
  229.    
  230.     If swSelMgr.GetSelectedObjectCount2(-1) > 0 Then
  231.         Set swComponent = swSelMgr.GetSelectedObjectsComponent4(1, -1)
  232.         If Not swComponent Is Nothing Then
  233.             path = swComponent.GetPathName
  234.             fileName = fso.GetBaseName(path)
  235.             fileExtension = fso.GetExtensionName(path)
  236.            
  237.             UserForm1.Label_选中组件路径.caption = path
  238.             UserForm1.TextBox_选中组件名称.Value = fileName
  239.            
  240.             Select Case LCase(fileExtension)
  241.                 Case "sldasm"
  242.                     UserForm1.Label_组件类型.caption = "装配体"
  243.                 Case "sldprt"
  244.                     UserForm1.Label_组件类型.caption = "零件"
  245.                 Case Else
  246.                     UserForm1.Label_组件类型.caption = "未知"
  247.             End Select
  248.            
  249.             If fso.FileExists(Replace(path, fileExtension, "SLDDRW")) Then
  250.                 UserForm1.CheckBox_包含工程图.Value = True
  251.             Else
  252.                 UserForm1.CheckBox_包含工程图.Value = False
  253.             End If
  254.         Else
  255.             'MsgBox "未选中任何组件。"
  256.        End If
  257.     Else
  258.         'MsgBox "未选中任何组件。"
  259.    End If
  260. End Sub
  261.  
  262. ' 这个子程序用于初始化用户窗体,设置初始状态
  263. Sub InitializeUserForm()
  264.     ' 清空所有控件
  265.    UserForm1.TextBox_代号.Value = ""
  266.     UserForm1.ListBox_文件夹.Clear
  267.     UserForm1.Label_选中组件路径.caption = ""
  268.     UserForm1.TextBox_选中组件名称.Value = ""
  269.     'UserForm1.Label_组件类型.caption = ""
  270.    UserForm1.CheckBox_包含工程图.Value = False
  271.    
  272.     ' 调用函数填充数据
  273.    'Call ConvertAssemblyNameToUpper
  274.    Call GetFoldersAndFillList
  275. End Sub
  276. '获取文件夹下所有文件
  277. Public Function GetFilesInDirectory(directory As String) As Collection
  278.     Dim folder As folder
  279.     Dim file As file
  280.     Dim fs As New FileSystemObject
  281.     Dim files As New Collection
  282.  
  283.     Set folder = fs.GetFolder(directory)
  284.  
  285.     For Each file In folder.files
  286.         files.Add file.path
  287.     Next file
  288.  
  289.     Set GetFilesInDirectory = files
  290. End Function
  291. ' 这个子程序用于写入用户修改的钣金件代号、车床件代号、亚克力代号和机加件标识到文本文件中
  292. Public Sub WriteIdentifiers()
  293.     Dim filePath As String
  294.     Dim fileContent As String
  295.     Dim lines As Variant
  296.     Dim i As Integer
  297.     Dim outputLines As String
  298.     Dim identifierFound(1 To 4) As Boolean
  299.  
  300.     filePath = GetFilePath()
  301.     If filePath <> "" And Dir(filePath) <> "" Then
  302.         fileContent = GetFileContent(filePath)
  303.         lines = Split(fileContent, vbCrLf)
  304.  
  305.         ' 初始化标识符是否已找到的数组
  306.        identifierFound(1) = False
  307.         identifierFound(2) = False
  308.         identifierFound(3) = False
  309.         identifierFound(4) = False
  310.  
  311.         ' 遍历文件中的每一行,更新对应的代号行
  312.        For i = LBound(lines) To UBound(lines)
  313.             If InStr(lines(i), "钣金件代号|") > 0 Then
  314.                 lines(i) = UserForm1.TextBox_钣金件代号.text
  315.                 identifierFound(1) = True
  316.             ElseIf InStr(lines(i), "车床件代号|") > 0 Then
  317.                 lines(i) = UserForm1.TextBox_车床件代号.text
  318.                 identifierFound(2) = True
  319.             ElseIf InStr(lines(i), "亚克力代号|") > 0 Then
  320.                 lines(i) = UserForm1.TextBox_亚克力代号.text
  321.                 identifierFound(3) = True
  322.             ElseIf InStr(lines(i), "机加件标识|") > 0 Then
  323.                 lines(i) = UserForm1.TextBox_机加件标识.text
  324.                 identifierFound(4) = True
  325.             End If
  326.             outputLines = outputLines & lines(i) & vbCrLf
  327.         Next i
  328.  
  329.         ' 如果有代号未在文件中找到,则添加它们
  330.        If Not identifierFound(1) Then
  331.             outputLines = outputLines & UserForm1.TextBox_钣金件代号.text & vbCrLf
  332.         End If
  333.         If Not identifierFound(2) Then
  334.             outputLines = outputLines & UserForm1.TextBox_车床件代号.text & vbCrLf
  335.         End If
  336.         If Not identifierFound(3) Then
  337.             outputLines = outputLines & UserForm1.TextBox_亚克力代号.text & vbCrLf
  338.         End If
  339.         If Not identifierFound(4) Then
  340.             outputLines = outputLines & UserForm1.TextBox_机加件标识.text & vbCrLf
  341.         End If
  342.  
  343.         ' 写入文件
  344.        WriteToFile filePath, outputLines
  345.  
  346.         ' 反馈给用户
  347.        MsgBox "代号保存成功!"
  348.     Else
  349.         MsgBox "配置文件不存在,请检查。"
  350.     End If
  351. End Sub
  352.  
  353.  
  354.  
  355.  
  356. ' 这个子程序用于将更新后的全部文本内容写入到指定的文件路径
  357. Private Sub WriteToFile(ByVal filePath As String, ByVal content As String)
  358.     Dim fileNum As Integer
  359.     fileNum = FreeFile
  360.  
  361.     Open filePath For Output As #fileNum
  362.     Print #fileNum, content
  363.     Close #fileNum
  364. End Sub
  365.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement