steve_suich 发表于 2023-6-9 13:46:29

模型改名同时改工程图

先在模型树选中所要修改的模型,再运行宏。宏内容如下:
Dim swApp As Object
Dim Part As Object
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set swSelMgr = Part.SelectionManager
Set swComp = swSelMgr.GetSelectedObject(1)
oldpathname = swComp.GetPathName
Path = Left(oldpathname, InStrRev(oldpathname, "\"))
ntype = Mid(oldpathname, InStrRev(oldpathname, "."))
oldfi = Mid(oldpathname, InStrRev(oldpathname, "\") + 1)
oldname = Left(oldfi, InStrRev(oldfi, ".") - 1)
   mip = InputBox("changename", "name", oldname)
If mip <> "" Then
Part.Extension.RenameDocument mip
Part.Save
tmpfi = Dir(Path & "*.SLDDRW")
Do Until tmpfi = ""
    vDepend = swApp.GetDocumentDependencies(Path & tmpfi, False, False)
    If Mid(vDepend(1), InStrRev(vDepend(1), "\") + 1) = oldfi Then
   Name Path & tmpfi As Path & mip & ".SLDDRW"
    bl = swApp.ReplaceReferencedDocument(Path & mip & ".SLDDRW", vDepend(1), Path & mip & ntype)
   Exit Do
   End If
tmpfi = Dir
Loop
End If
End Sub

steve_suich 发表于 2023-6-10 09:20:42

本帖最后由 steve_suich 于 2023-6-10 09:21 编辑

shentu 发表于 2023-6-9 22:21
同样运行出错。。。。。Dim swApp As Object
Dim Part As Object
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set swSelMgr = Part.SelectionManager
Set swComp = swSelMgr.GetSelectedObject(1)
oldpathname = swComp.GetPathName
Path = Left(oldpathname, InStrRev(oldpathname, "\"))
ntype = Mid(oldpathname, InStrRev(oldpathname, "."))
oldfi = Mid(oldpathname, InStrRev(oldpathname, "\") + 1)
oldname = Left(oldfi, InStrRev(oldfi, ".") - 1)
   mip = InputBox("changename", "name", oldname)
If mip <> "" Then
Part.Extension.RenameDocument mip
Part.Save
tmpfi = Dir(Path & "*.SLDDRW")
Do Until tmpfi = ""
    vDepend = swApp.GetDocumentDependencies(Path & tmpfi, False, False)
    If Mid(vDepend(1), InStrRev(vDepend(1), "") + 1) = oldfi Then
   Name Path & tmpfi As Path & mip & ".SLDDRW"
    bl = swApp.ReplaceReferencedDocument(Path & mip & ".SLDDRW", vDepend(1), Path & mip & ntype)
   Exit Do
   End If
tmpfi = Dir
Loop
End If
End Sub




happilly 发表于 2023-6-9 14:14:20

sw嘛?.

da 发表于 2023-6-9 14:24:40

你是chatgpt搞的?

steve_suich 发表于 2023-6-9 14:28:38

happilly 发表于 2023-6-9 14:14
sw嘛?.

sw的宏

行云亦 发表于 2023-6-9 15:21:17

能说一下怎么用吗?

steve_suich 发表于 2023-6-9 17:21:12

行云亦 发表于 2023-6-9 15:21
能说一下怎么用吗?

1、sw 新建一个宏文件,内容按上面的代码。2、打开一模型文件(装配体或者零件)
3、在打开的模型界面的模型树结构里面用鼠标点选所要改名的零件(或者子装配体)

4、运行刚才建好的宏文件,---弹出输入框--输入新的名字--点确定---完成。


顺子93 发表于 2023-6-9 21:52:39

运行出错,08行有问题。错误91,对象变量或with 块变量未设置

shentu 发表于 2023-6-9 22:21:02

同样运行出错。。。。。

人外有人 发表于 2023-6-19 10:59:33

steve_suich 发表于 2023-6-10 09:20
Dim swApp As Object
Dim Part As Object
Sub main()


试了下,只改了part文件名,图纸没变,问题出在哪呢?

页: [1] 2 3
查看完整版本: 模型改名同时改工程图