乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 83|回复: 0

[综合讨论] Solidworks api另存为问题

[复制链接]

1

主题

1

帖子

0

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-8 22:15:17 | 显示全部楼层 |阅读模式
你好
我有一个宏,从图纸中复制所有图纸,打开新模板并粘贴图纸。然后关闭旧图形并另存为与原始图形同名的新图形以覆盖它。
 
问题是粘贴完成后无法保存。它工作了一段时间,但不再有效,我什么也没改变。
 
你有什么想法吗?这是我的代码。。它与零件和装配部分配合良好。。非常感谢。
 
Dim vSheetName作为变体
将swView设置为SldWorks。看法
Dim swDraw作为SldWorks。绘图文档
Dim swAnn作为SldWorks。注释
将swSelMgr设置为SldWorks。SelectionMgr
将SWNOTE设置为SldWorks。注释
将S变暗为字符串
将swCustPropMgr设置为SldWorks。CustomPropertyManager
Dim SheetCount为整数
Dim DOC作为ModelDoc2
布尔型模糊布尔状态
Dim longstatus As Long,longwarnings As Long
将零件变暗为对象
Dim PARTTITLE作为字符串
尺寸X为字符串
公共Z作为字符串
公共Q作为字符串
Dim交换为SldWorks。SldWorks公司
Dim swModel作为ModelDoc2
昏暗的可怕的长
 
  1. Sub main()
  2. Dim Answer As String
  3. Dim MyNote As String
  4.    'Place your text here
  5.    MyNote = "DO YOU REALLY WISH TO REFRESH" & Chr(13) & "ACTUAL DOCUMENT AUTHOR AND DATE?"
  6.    'Display MessageBox
  7.    Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "???")
  8.    If Answer = vbNo Then
  9.        'Code for No button Press
  10.        MsgBox "OPERATION ABORT BY USER!"
  11.        Exit Sub
  12.        'Code for Yes button Press
  13.    End If
  14. Z = 0
  15. A = 0
  16. Set SWAPP = Application.SldWorks
  17. Set DOC = SWAPP.ACTIVEDOC
  18. If DOC Is Nothing Then MsgBox "A SOLIDWORKS DOCUMENT MUST BE OPEN" & Chr(13) & "TO PERFORM REFRESH THIS WAY!!": End
  19. Dim swDocTypeLong As Long
  20. Set PART = SWAPP.ACTIVEDOC
  21. EXT = Right(PART.GetPathName, 7)
  22. swDocTypeLong = Switch(EXT = ".SLDPRT", swDocPART, EXT = ".SLDDRW", swDocDRAWING, EXT = ".SLDASM", swDocASSEMBLY, True, -1)
  23. X = PART.GetPathName
  24. PARTTITLE = PART.GetTitle
  25.    If swDocTypeLong = swDocDRAWING Then GoTo 2
  26. UserForm3.Show
  27. If Z = 1 Then Exit Sub
  28. Set SWAPP = Application.SldWorks
  29. Set DOC = SWAPP.ACTIVEDOC
  30. 'boolstatus = swApp.CloseAllDocuments(True)
  31.    'Debug.Print boolstatus
  32. 'If swDocTypeLong = swDocPART Then GoTo 4
  33. 'If swDocTypeLong = swDocASSEMBLY Then GoTo 4
  34. Set PART = SWAPP.ACTIVEDOC
  35. Set swModel = SWAPP.ACTIVEDOC
  36. Set swCustPropMgr = swModel.Extension.CustomPropertyManager("")
  37. swCustPropMgr.Add2 "DESIGN DATE", swCustomInfoText, " "
  38. swCustPropMgr.Set "DESIGN DATE", Q
  39. PART.DeleteAllRelations
  40. Dim swEquationMgr As Object
  41. Set swEquationMgr = PART.GetEquationMgr()
  42. swEquationMgr.add -1, Chr(34) & "Autorun""" & "=" & "Application.SldWorks.RunMacro" & "(" & """" & "C" & ":" & "" & "SOLIDWORKS" & " " & "MACRO" & "" & "MACRO4.swp" & """,""" & "MACRO41" & """,""main" & """)"
  43. swModel.SummaryInfo(SwConst.swSumInfoAuthor) = "PAT LAFONTAINE"
  44. GoTo 6
  45. 2 Set PART = SWAPP.ACTIVEDOC
  46. Set swModel = SWAPP.ACTIVEDOC
  47. Set SWDWG = swModel
  48. Set swDraw = swModel
  49. vSheetName = swDraw.GetSheetNames
  50. 'For i = 0 To UBound(vSheetName)
  51. SheetCount = PART.GetSheetCount
  52. SWDWG.ActivateSheet vSheetName(SheetCount - (SheetCount))
  53. PARTTITLE = PART.GetTitle
  54. boolstatus = PART.Extension.SelectByID2(vSheetName(SheetCount - 1), "SHEET", 0, 0, 0, False, 0, Nothing, 0)
  55. If SheetCount - 1 = 0 Then GoTo 8
  56. boolstatus = PART.Extension.SelectByID2(vSheetName(SheetCount - 2), "SHEET", 0, 0, 0, True, 0, Nothing, 0)
  57. If SheetCount - 2 = 0 Then GoTo 8
  58. boolstatus = PART.Extension.SelectByID2(vSheetName(SheetCount - 3), "SHEET", 0, 0, 0, True, 0, Nothing, 0)
  59. If SheetCount - 3 = 0 Then GoTo 8
  60. boolstatus = PART.Extension.SelectByID2(vSheetName(SheetCount - 4), "SHEET", 0, 0, 0, True, 0, Nothing, 0)
  61. If SheetCount - 4 = 0 Then GoTo 8
  62. boolstatus = PART.Extension.SelectByID2(vSheetName(SheetCount - 5), "SHEET", 0, 0, 0, True, 0, Nothing, 0)
  63. If SheetCount - 5 = 0 Then GoTo 8
  64. boolstatus = PART.Extension.SelectByID2(vSheetName(SheetCount - 6), "SHEET", 0, 0, 0, True, 0, Nothing, 0)
  65. If SheetCount - 6 = 0 Then GoTo 8
  66. boolstatus = PART.Extension.SelectByID2(vSheetName(SheetCount - 7), "SHEET", 0, 0, 0, True, 0, Nothing, 0)
  67. If SheetCount - 7 = 0 Then GoTo 8
  68. boolstatus = PART.Extension.SelectByID2(vSheetName(SheetCount - , "SHEET", 0, 0, 0, True, 0, Nothing, 0)
  69. If SheetCount - 8 = 0 Then GoTo 8
  70. boolstatus = PART.Extension.SelectByID2(vSheetName(SheetCount - 9), "SHEET", 0, 0, 0, True, 0, Nothing, 0)
  71. If SheetCount - 9 = 0 Then GoTo 8
  72. boolstatus = PART.Extension.SelectByID2(vSheetName(SheetCount - 10), "SHEET", 0, 0, 0, True, 0, Nothing, 0)
  73. If SheetCount - 10 > 0 Then MsgBox "DRAWING COUNTAIN MORE THAN 10 SHEETS," & Chr(13) & "ONLY 10 FIRST WILL BE COPY," & Chr(13) & "SO CHECK TO MANUALLY COPY MISSING SHEETS."
  74. 8 PART.EditCopy
  75. 'If Right(M, 6) = "SLDASM" Then Set PART = swApp.NewDocument("s:\aaatemplates\solidworks 2010 template\fond de plan\ASSY-D_Orientech.slddrt", 12, 0.2794, 0.4318)
  76. Set PART = SWAPP.NewDocument("s:\aaatemplates\solidworks 2010 template\DRAWING.drwdot", 12, 0.2794, 0.4318)
  77. SWAPP.ActivateDoc2 "Draw7 - Sheet1", False, longstatus
  78. 'Y = Mid(X, 1, Len(X) - 7) & "1" & Right(X, 7)
  79. 'PARTTITLE2 = PART.GetTitle
  80. 'SWAPP.CloseDoc PARTTITLE
  81. 'Set PART = SWAPP.ActivateDoc2(PARTTITLE2, 0, 0)
  82. 'longstatus = PART.SaveAs3(Y, 0, 0)
  83. 'PARTTITLE3 = PART.GetTitle
  84. 'SWAPP.CloseDoc PARTTITLE3
  85. 'Set swModel = SWAPP.OpenDoc6(Y, swDocTypeLong, swOpenDocOptions_Silent, "", nErrors, NWARNINGS)
  86. Set PART = SWAPP.ACTIVEDOC
  87. Dim myDrawingSheet As Object
  88. Set myDrawingSheet = PART.GetCurrentSheet()
  89. myDrawingSheet.SetName "SHEET TO DELETE"
  90. Set PART = SWAPP.ACTIVEDOC
  91. boolstatus = PART.Extension.SelectByID2("SHEET TO DELETE", "SHEET", 0, 0, 0, False, 0, Nothing, 0)
  92. PART.Paste
  93. Set swModel = SWAPP.ACTIVEDOC
  94.    Set SWDWG = swModel
  95. Set swDraw = swModel
  96. vSheetName = swDraw.GetSheetNames
  97. SWDWG.ActivateSheet vSheetName(SheetCount - (SheetCount - 1))
  98. Set swModel = SWAPP.ACTIVEDOC
  99.    Set swDraw = swModel
  100.    Set swSheet = swDraw.GetCurrentSheet
  101.    Set swSelMgr = swModel.SelectionManager
  102. Set swView = swDraw.GetFirstView
  103. Set swView = swView.GetNextView
  104. Set swModel = SWAPP.ACTIVEDOC
  105.    Set SWDWG = swModel
  106. SWDWG.ActivateSheet "SHEET TO DELETE"
  107. M = swView.ReferencedDocument.GetPathName
  108. Set PART = SWAPP.ACTIVEDOC
  109. Dim MYView As Object
  110. Set MYiew = PART.CreateDrawViewFromModelView3(M, "*Front", 0.1097457655955, 0.1648856124764, 0)
  111. Set swModel = SWAPP.ACTIVEDOC
  112.    Set SWDWG = swModel
  113.    sSheetNames = SWDWG.GetSheetCount
  114. Set swSelMgr = swModel.SelectionManager
  115. Set swModel = SWAPP.ACTIVEDOC
  116. Set PART = SWAPP.ACTIVEDOC
  117. boolstatus = PART.Extension.SelectByID2("DetailItem346@Sheet Format1", "NOTE", 0.4080223743143, -0.001548983140407, 0, False, 0, Nothing, 0)
  118. Set SWNOTE = swSelMgr.GetSelectedObject6(1, 0)
  119. Set swAnn = SWNOTE.GetAnnotation
  120. S = SWNOTE.GetText
  121. SWDWG.ActivateSheet vSheetName(SheetCount - (SheetCount - 1))
  122. Set myDrawingSheet = PART.GetCurrentSheet()
  123. Set swDraw = swModel
  124. Set swSheet = swDraw.GetCurrentSheet
  125. myDrawingSheet.SetName "Sheet1"
  126. boolstatus = PART.Extension.SelectByID2("SET AUTHOR NAME & PROPERTY1", "SHEET", 0, 0, 0, False, 0, Nothing, 0)
  127. 'part.DeleteSelection (False)
  128. If boolstatus = True Then GoTo 9
  129. boolstatus = SWAPP.RunMacro2("c:\SOLIDWORKS MACRO\DWG.swp", "MACROFEATURE_MODULE1", "main", swRunMacroUnloadAfterRun, nErrors)
  130. 9 vSheetProps = swSheet.GetProperties
  131. 'Set swCustPropMgr = swModel.Extension.CustomPropertyManager("")
  132. 'swCustPropMgr.Add2 "DOCTYPE", swCustomInfoText, " "
  133. 'swCustPropMgr.Set "DOCTYPE", "$PRPSHEET" & ":" & Chr(34) & "DOCTYPE"""
  134. 'Set part = swApp.ACTIVEDOC
  135. 'S = swCustPropMgr.Get("DOCTYPE")
  136. If S = " " Then boolstatus = PART.SetupSheet5("Sheet1", 12, 12, vSheetProps(2), vSheetProps(3), False, "PART-B_Orientech.slddrt", 0.4318, 0.2794, "Default", True)
  137. If S = " " Then boolstatus = PART.SetupSheet5("Sheet1", 12, 12, vSheetProps(2), vSheetProps(3), False, "ASSY-D_Orientech.slddrt", 0.4318, 0.2794, "Default", True)
  138. If S = "" Then boolstatus = PART.SetupSheet5("Sheet1", 12, 12, vSheetProps(2), vSheetProps(3), False, "ASSY-D_Orientech.slddrt", 0.4318, 0.2794, "Default", True)
  139. If S = "" Then boolstatus = PART.SetupSheet5("Sheet1", 12, 12, vSheetProps(2), vSheetProps(3), False, "PART-B_Orientech.slddrt", 0.4318, 0.2794, "Default", True)
  140. D = 2
  141. 3 If sSheetNames = D Then GoTo 5
  142. SWDWG.ActivateSheet vSheetName(SheetCount - (SheetCount - D))
  143. Set PART = SWAPP.ACTIVEDOC
  144. Set myDrawingSheet = PART.GetCurrentSheet()
  145. Set swDraw = swModel
  146. Set swSheet = swDraw.GetCurrentSheet
  147. vSheetProps = swSheet.GetProperties
  148. If A = 1 Then If S = " " Then boolstatus = PART.SetupSheet5("Sheet" & D - 1, 12, 12, vSheetProps(2), vSheetProps(3), False, "PART-B_Orientech.slddrt", 0.4318, 0.2794, "Default", True)
  149. If A = 1 Then If S = " " Then boolstatus = PART.SetupSheet5("Sheet" & D - 1, 12, 12, vSheetProps(2), vSheetProps(3), False, "ASSY-D_Orientech.slddrt", 0.4318, 0.2794, "Default", True)
  150. If A = 0 Then If S = " " Then boolstatus = PART.SetupSheet5("Sheet" & D, 12, 12, vSheetProps(2), vSheetProps(3), False, "PART-B_Orientech.slddrt", 0.4318, 0.2794, "Default", True)
  151. If A = 0 Then If S = " " Then boolstatus = PART.SetupSheet5("Sheet" & D, 12, 12, vSheetProps(2), vSheetProps(3), False, "ASSY-D_Orientech.slddrt", 0.4318, 0.2794, "Default", True)
  152. If A = 1 Then If S = "" Then boolstatus = PART.SetupSheet5("Sheet" & D - 1, 12, 12, vSheetProps(2), vSheetProps(3), False, "ASSY-D_Orientech.slddrt", 0.4318, 0.2794, "Default", True)
  153. If A = 1 Then If S = "" Then boolstatus = PART.SetupSheet5("Sheet" & D - 1, 12, 12, vSheetProps(2), vSheetProps(3), False, "PART-B_Orientech.slddrt", 0.4318, 0.2794, "Default", True)
  154. If A = 0 Then If S = "" Then boolstatus = PART.SetupSheet5("Sheet" & D, 12, 12, vSheetProps(2), vSheetProps(3), False, "ASSY-D_Orientech.slddrt", 0.4318, 0.2794, "Default", True)
  155. If A = 0 Then If S = "" Then boolstatus = PART.SetupSheet5("Sheet" & D, 12, 12, vSheetProps(2), vSheetProps(3), False, "PART-B_Orientech.slddrt", 0.4318, 0.2794, "Default", True)
  156. If A = 1 Then A = 0
  157. 'myDrawingSheet.SetName "Sheet" & D
  158.    Dim bRet                    As Boolean
  159.    Set SWAPP = CreateObject("SldWorks.Application")
  160.    Set swModel = SWAPP.ACTIVEDOC
  161.    Set swDraw = swModel
  162.    Set swSheet = swDraw.GetCurrentSheet
  163.    Set swView = swDraw.GetFirstView
  164.    Debug.Print "File = " & swModel.GetPathName
  165.    Debug.Print "  " & swSheet.GetName
  166.    While Not swView Is Nothing
  167.        Debug.Print "    " & swView.GetName2 & " [" & swView.Type & "]"
  168.        Set swView = swView.GetNextView
  169. While swView Is Nothing
  170. boolstatus = PART.Extension.SelectByID2("Sheet" & D, "SHEET", 0, 0, 0, False, 0, Nothing, 0)
  171. PART.DeleteSelection (False)
  172. A = 1
  173. GoTo 4
  174. Wend
  175. GoTo 4
  176.    Wend
  177. 4 D = D + 1
  178. GoTo 3
  179. 5 'swDwg.ActivateSheet "SHEET TO DELETE"
  180. boolstatus = PART.Extension.SelectByID2("SHEET TO DELETE", "SHEET", 0, 0, 0, False, 0, Nothing, 0)
  181. PART.DeleteSelection (False)
  182. 'part.EditDelete
  183. swModel.SummaryInfo(SwConst.swSumInfoAuthor) = "PAT LAFONTAINE"
  184. PARTTITLE2 = PART.GetTitle
  185. SWAPP.CloseDoc PARTTITLE
  186. Set PART = SWAPP.ActivateDoc2(PARTTITLE2, 0, 0)
  187. 'PART.Save2 (silent)
  188. Set PART = SWAPP.ACTIVEDOC
  189. 'Dim i As Integer
  190.   ' Set SWAPP = Application.SldWorks
  191.   ' SendKeys "%{F}" 'invoke file menu
  192.   ' For i = 0 To 3 'go down to the saveas dialog
  193.   ' SendKeys "{down}"
  194.   ' Next i
  195.    'SendKeys "{enter}" 'enter
  196. longstatus = PART.SaveAs3(X, 0, 0)
  197. If swDocTypeLong = swDocDRAWING Then GoTo 11
  198. 6  longstatus = PART.SaveAs3(X, 0, 0)
  199. Set PART = Nothing
  200. Dim Answer3 As String
  201. Dim MyNote3 As String
  202.    'Place your text here
  203.    MyNote3 = "DO YOU WISH TO CLOSE DOCUMENT?"
  204.    'Display MessageBox
  205.    Answer3 = MsgBox(MyNote3, vbQuestion + vbYesNo, "???")
  206.    If Answer3 = vbNo Then
  207.        'Code for No button Press
  208.        GoTo 10
  209.        'Code for Yes button Press
  210.    End If
  211. SWAPP.CloseDoc PARTTITLE
  212. GoTo 10
  213. 11 Set PART = SWAPP.ACTIVEDOC
  214. PARTTITLE = PART.GetTitle
  215. Set PART = Nothing
  216. Dim Answer2 As String
  217. Dim MyNote2 As String
  218.    'Place your text here
  219.    MyNote2 = "DO YOU WISH TO CLOSE DOCUMENT?"
  220.    'Display MessageBox
  221.    Answer2 = MsgBox(MyNote2, vbQuestion + vbYesNo, "???")
  222.    If Answer2 = vbNo Then
  223.        'Code for No button Press
  224.        GoTo 10
  225.        'Code for Yes button Press
  226.    End If
  227. SWAPP.CloseDoc PARTTITLE
  228. 10 MsgBox "REFRESH DONE!"   ' Define title.
  229. End
  230. End Sub
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2024-11-22 06:58 , Processed in 0.204482 second(s), 65 queries .

© 2020-2024 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表