[英]SOLIDWORKS VBA open drawing of active part
我只需要使用活動部分的 VBA 打開圖紙。 繪圖始終具有與零件完全相同的文件名和位置。 我得到的是
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swDocSpecification As SldWorks.DocumentSpecification
Dim sName As String
Dim longstatus As Long, longwarnings As Long
Sub main()
Set swApp = Application.SldWorks
Set swDocSpecification = swApp.GetOpenDocSpec("C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS
2017\tutorial\AutoCAD\7550-021.slddrw")
sName = swDocSpecification.FileName
swDocSpecification.DocumentType = swDocDRAWING
swDocSpecification.ReadOnly = True
swDocSpecification.Silent = False
Set swModel = swApp.OpenDoc7(swDocSpecification)
longstatus = swDocSpecification.Error
longwarnings = swDocSpecification.Warning
End Sub
但它不起作用可能是因為文件位置可能總是不同,具體取決於活動部件的命名方式和活動部件的位置。
有人可以分享一個 function 來簡單地打開零件的相關圖紙嗎?
嘗試這個:
Option Explicit
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.ModelDoc2
Dim swDocSpecification As SldWorks.DocumentSpecification
Dim FilePath As String
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then MsgBox "Open an assembly or part": Exit Sub
If swModel.GetType <> swDocumentTypes_e.swDocASSEMBLY And swModel.GetType <> swDocumentTypes_e.swDocPART Then MsgBox "Open an assembly or part": Exit Sub
Set swDocSpecification = swApp.GetOpenDocSpec(swModel.GetPathName)
FilePath = LCase(swModel.GetPathName)
FilePath = Replace(FilePath, ".sldprt", ".slddrw")
FilePath = Replace(FilePath, ".sldasm", ".slddrw")
swDocSpecification.FileName = FilePath
swDocSpecification.DocumentType = swDocumentTypes_e.swDocDRAWING
swDocSpecification.ReadOnly = True
swDocSpecification.Silent = True
Set swDraw = swApp.OpenDoc7(swDocSpecification)
swApp.ActivateDoc3 FilePath, False, swRebuildOnActivation_e.swRebuildActiveDoc, Empty
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.