找回密码 注册 QQ登录
一站式解决方案

iCAx开思网

CAD/CAM/CAE/设计/模具 高清视频【积分说明】如何快速获得积分?在线3D打印服务,一键上传,自动报价 
查看: 383|回复: 2

[讨论] 装配图中选中所有实体(solid bodies)的宏

[复制链接]
发表于 2021-11-10 10:11:42 | 显示全部楼层 |阅读模式

马上注册,结交更多同行朋友,交流,分享,学习。

您需要 登录 才可以下载或查看,没有帐号?注册

x
这是一个在装配或者零件环境下选中所有实体(solid bodies)的宏,不知道怎么回事,在我的SW2010下用不了,它总说,变量未定义,大家帮看下,是怎么回事。


This example shows how to select both solid and surface bodies in either a part or an assembly.


'----------------------------------------

'

' Preconditions: Part or assembly is open.

'

' Postconditions: All solid and surface bodies are selected.

'

'----------------------------------------

Option Explicit

Public Enum swDocumentTypes_e

    swDocNONE = 0       '  Used to be TYPE_NONE

    swDocPART = 1       '  Used to be TYPE_PART

    swDocASSEMBLY = 2   '  Used to be TYPE_ASSEMBLY

    swDocDRAWING = 3    '  Used to be TYPE_DRAWING

End Enum

Public Enum swBodyType_e

    swSolidBody = 0

    swSheetBody = 1

    swWireBody = 2

    swMinimumBody = 3

    swGeneralBody = 4

    swEmptyBody = 5

End Enum

Sub SelectBodies _

( _

    swApp As SldWorks.SldWorks, _

    swModel As SldWorks.ModelDoc2, _

    vBody As Variant, _

    sPadStr As String _

)

    Dim swModExt                    As SldWorks.ModelDocExtension

    Dim swBody                      As SldWorks.body2

    Dim sBodySelStr                 As String

    Dim sBodyTypeSelStr             As String

    Dim i                           As Long

    Dim bRet                        As Boolean


    If IsEmpty(vBody) Then Exit Sub

    Set swModExt = swModel.Extension


    For i = 0 To UBound(vBody)

        Set swBody = vBody(i)


        sBodySelStr = swBody.GetSelectionId


        Debug.Print "  " & sPadStr & sBodySelStr


        Select Case swBody.GetType

            Case swSolidBody

                sBodyTypeSelStr = "SOLIDBODY"


            Case swSheetBody

                sBodyTypeSelStr = "SURFACEBODY"


            Case Else

                Debug.Assert False

        End Select


        bRet = swModExt.SelectByID2(sBodySelStr, sBodyTypeSelStr, 0#, 0#, 0#, True, 0, Nothing, swSelectOptionDefault): Debug.Assert bRet

    Next i

End Sub

Sub ProcessComponent _

( _

    swApp As SldWorks.SldWorks, _

    swModel As SldWorks.ModelDoc2, _

    swComp As SldWorks.Component2, _

    nLevel As Long _

)

    Dim vChildComp                  As Variant

    Dim swChildComp                 As SldWorks.Component2

    Dim swCompConfig                As SldWorks.Configuration

    Dim sPadStr                     As String

    Dim vBody                       As Variant

    Dim i                           As Long


    For i = 0 To nLevel - 1

        sPadStr = sPadStr + "  "

    Next i

    Debug.Print sPadStr & swComp.Name2 & " <" & swComp.ReferencedConfiguration & ">"

    ' Solid bodies

    vBody = swComp.GetBodies2(swSolidBody)

    SelectBodies swApp, swModel, vBody, sPadStr


    ' Surface bodies

    vBody = swComp.GetBodies2(swSheetBody)

    SelectBodies swApp, swModel, vBody, sPadStr


    vChildComp = swComp.GetChildren

    For i = 0 To UBound(vChildComp)

        Set swChildComp = vChildComp(i)


        ProcessComponent swApp, swModel, swChildComp, nLevel + 1

    Next i

End Sub

Sub ProcessAssembly _

( _

    swApp As SldWorks.SldWorks, _

    swModel As SldWorks.ModelDoc2 _

)

    Dim swConfigMgr                 As SldWorks.ConfigurationManager

    Dim swConf                      As SldWorks.Configuration

    Dim swRootComp                  As SldWorks.Component2


    Set swConfigMgr = swModel.ConfigurationManager

    Set swConf = swConfigMgr.ActiveConfiguration

    Set swRootComp = swConf.GetRootComponent

    ProcessComponent swApp, swModel, swRootComp, 1

End Sub

Sub main()

    Dim swApp                       As SldWorks.SldWorks

    Dim swModel                     As SldWorks.ModelDoc2

    Dim swPart                      As SldWorks.PartDoc

    Dim vBody                       As Variant

    Dim i                           As Long

    Dim bRet                        As Boolean


    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc


    swModel.ClearSelection2 True


    Debug.Print "File = " & swModel.GetPathName


    Select Case swModel.GetType

        Case swDocPART

            Set swPart = swModel


            ' Solid bodies

            vBody = swPart2.GetBodies(swSolidBody, True)

            SelectBodies swApp, swModel, vBody, ""


            ' Surface bodies

            vBody = swPart.GetBodies(swSheetBody, True)

            SelectBodies swApp, swModel, vBody, ""


        Case swDocASSEMBLY

            ProcessAssembly swApp, swModel


        Case Else

            Exit Sub

    End Select

End Sub

'----------------------------------------





发表于 2021-11-10 10:54:37 | 显示全部楼层
是否是引用的问题
 楼主| 发表于 2021-11-10 13:49:22 | 显示全部楼层
本帖最后由 panliang9 于 2021-11-10 14:28 编辑

重新换了一个2016版本的,问题解决了。h踢踢ps://help.solidworks.com/2016/english/api/sldworksapi/select_bodies_example_vb.htm?verRedirect=1
您需要登录后才可以回帖 登录 | 注册

本版积分规则

3D打印手板模型快速制作服务,在线报价下单!

QQ 咨询|手机版|联系我们|iCAx开思网  

GMT+8, 2022-1-25 22:13 , Processed in 0.022919 second(s), 10 queries , Gzip On, Redis On.

Powered by Discuz! X3.3

© 2002-2022 www.iCAx.org

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