【问题标题】:SOLIDWORKS VBA open drawing of active partSOLIDWORKS VBA 打开活动零件的工程图
【发布时间】:2021-03-16 09:01:04
【问题描述】:

我只需要使用活动部件的 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

但它不起作用可能是因为文件位置可能总是不同,具体取决于活动部件的命名方式和活动部件的位置。

有人可以分享一个功能来简单地打开零件的相关图纸吗?

【问题讨论】:

  • 所以基本上,你的问题真的是:“我怎样才能使文件名变量取决于 X”? "C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS 2017\tutorial\AutoCAD\7550-021.slddrw" - 这是7550-021 改变的部分吗?这些信息从何而来?
  • 我的问题是如何从字面上使 VBA 代码打开当前打开的零件的关联图形。该图形与活动零件共享相同的文件名和位置。该位置和部件名称可能因部件而异。是的,在那个例子中,我提供的文件名是“7550-021”。另一部分显然没有那个文件名
  • 您是否尝试过将您想要的文本放入字符串变量中?那将是第 1 步。
  • 没有“我想要的文字”。在solidworks中,有一个按钮可以打开零件的图纸,并且该功能不会记录为宏。所以我想知道它在 VBA 代码中的功能是什么。我想出了如何通过定位不基于任何东西的确切事物来打开绘图,这是不正确的。

标签: vba solidworks


【解决方案1】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 2017-01-16
    • 2016-01-03
    • 2022-11-10
    • 1970-01-01
    • 2021-09-02
    • 2023-04-01
    • 2017-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多