找回密码
 立即注册

QQ登录

只需一步,快速开始

微信扫码登录

搜索
查看: 991|回复: 3

[求助] SolidWorks宏

[复制链接]

3

主题

6

回帖

556

积分

三级士官

积分
556
发表于 2023-12-7 19:32:39 | 显示全部楼层 |阅读模式
Dim TopDocPathOnly As String
Dim swModel As SldWorks.ModelDoc2
Dim swApp As SldWorks.SldWorks
Dim longstatus As Long, longwarnings As Long

Sub main()
Set swApp = Application.SldWorks
Set TopDoc = swApp.ActiveDoc '總裝對象
If TopDoc.GetType <> 2 Then
    MsgBox ("Open Assembly")
    Exit Sub '不是裝配=退出
End If
TopDocPathSplit = Split(TopDoc.GetPathName, "") '分割
TopDocName = TopDocPathSplit(UBound(TopDocPathSplit)) '總裝文件名稱
Path_ = TopDoc.GetPathName
TopDocName = Left(TopDocName, Len(TopDocName) - 7) '總裝文件名稱(排除.SLDASM)
TopDocPathOnly = TopDocPathSplit(UBound(TopDocPathSplit) - 1) '總裝目錄名稱
TopConfString = TopDoc.GetActiveConfiguration.Name '總裝配置名稱
SubAsm TopDoc, TopConfString '遍歷

End Sub

Function SubAsm(AsmDoc, ConfString)
Dim name_ay() As String
Set swModel = swApp.ActiveDoc
Set Configuration = AsmDoc.GetConfigurationByName(ConfString)
Set RootComponent = Configuration.GetRootComponent
Components = RootComponent.GetChildren
For Each Child In Components '總裝抓全部零件名稱
    i = i + 1
    ReDim Preserve name_ay(i)
    Set ChildModel = Child.GetModelDoc
    ChildPathSplit = Split(Child.GetPathName, "") '分割
    ChildName = ChildPathSplit(UBound(ChildPathSplit)) '零件文件名稱
    name_ay(i) = Left(ChildName, Len(ChildName) - 7) '編號_名稱
    swModel.DeleteCustomInfo2 "", name_ay(i)
    swModel.AddCustomInfo2 name_ay(i), swCustomInfoText, """SW-Material@" & name_ay(i) & ".SLDPRT"""
Next

'~~~~~~~ parts_property ~~~~~~~
Dim longstatus As Long, longwarnings As Long
Dim retval As String
Set Part = swApp.ActiveDoc
path_name = Part.GetPathName
TopDocPathSplit = Split(path_name, "") '分割
TopDocName = TopDocPathSplit(UBound(TopDocPathSplit))
Path_ = Left(path_name, Len(path_name) - Len(TopDocName))
For n = 1 To i
    Set Part = swApp.OpenDoc6(Path_ & name_ay(n) & ".SLDPRT", 1, 0, "", longstatus, longwarnings)
    swApp.ActivateDoc2 name_ay(n) & ".SLDPRT", False, longstatus
    Set swModel = swApp.ActiveDoc
    '~~~ 注意 L1 設定 ~~~
    L1 = InStrRev(name_ay(n), "_", , 0) '編號_名稱是以 "_" 之符號分隔,可依需要更改所需之符號
    '~~~
    code_part = Left(name_ay(n), L1 - 1) ' 編號
    name_part = Right(name_ay(n), Len(name_ay(n)) - L1) '名稱
    retval = swModel.DeleteCustomInfo("材質")
    retval = swModel.AddCustomInfo3("", "材質", swCustomInfoText, """SW-Material@" & name_ay(n) & ".SLDPRT"""
    retval = swModel.DeleteCustomInfo("名稱")
    retval = swModel.AddCustomInfo3("", "名稱", swCustomInfoText, name_part)
    retval = swModel.DeleteCustomInfo("編號")
    retval = swModel.AddCustomInfo3("", "編號", swCustomInfoText, code_part)
    swModel.Save
    swApp.CloseDoc name_ay(n) & ".SLDPRT"
Next
End Function

1

主题

585

回帖

3998

积分

中尉

积分
3998
发表于 2023-12-8 08:05:25 | 显示全部楼层
66666666666666

0

主题

13

回帖

709

积分

四级士官

积分
709
发表于 2023-12-12 10:55:03 | 显示全部楼层
TopDocPathSplit = Split(TopDoc.GetPathName, "\") '分割

2

主题

89

回帖

781

积分

四级士官

积分
781
发表于 2023-12-24 19:27:32 | 显示全部楼层
没管用,哪里有问题?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

咨询QQ:1359218528|发帖须知!|Archiver|手机版|小黑屋|UG爱好者论坛 ( 京ICP备10217105号-2 )

GMT+8, 2025-1-11 20:49

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表