iCAx开思网

标题: 宏--所选草图拉伸成形到所选实体 [打印本页]

作者: pyczt    时间: 2016-8-11 23:44
标题: 宏--所选草图拉伸成形到所选实体
  1. ' ******************************************************************************
  2. 'PYCZT原创作品
  3. '条件:在特征中预先一个草图和在实体或曲面实体文件夹中选择一个实件
  4. '结果:增加一个凸台-拉伸特征,由所选草图拉伸到所选实体
  5. ' ******************************************************************************
  6. Option Explicit
  7. Dim swApp As SldWorks.SldWorks
  8. Dim part As SldWorks.PartDoc
  9. Dim swSelMgr As SldWorks.SelectionMgr
  10. Dim selsketch As SldWorks.Sketch
  11. Dim swFeat As SldWorks.Feature
  12. Dim SketchName As String
  13. Dim swBody As SldWorks.Body2
  14. Dim ngetSel             As Integer
  15. Dim ntype             As Long
  16. Dim i As Integer
  17. Dim bodyname As String
  18. Dim boolstatus As Boolean
  19. Dim longstatus As Long, longwarnings As Long

  20. Sub main()

  21. Set swApp = Application.SldWorks
  22. Set part = swApp.ActiveDoc

  23. Set swSelMgr = part.SelectionManager     '定义选择管理器
  24. ngetSel = swSelMgr.GetSelectedObjectCount2(0)        '选择数量
  25. Debug.Print "GetSelectedObjectCount     = " & ngetSel
  26. If ngetSel <> 2 Then
  27.     MsgBox "应选择草图和成形到实体二项"
  28.      Exit Sub
  29. End If
  30. For i = 1 To ngetSel
  31.     ntype = swSelMgr.GetSelectedObjectType3(i, 0)
  32.     Debug.Print "ntype" & i; "= " & ntype
  33.    
  34.    Select Case ntype
  35.       Case swSelSKETCHES     '草图9
  36.         Dim featType As String
  37.         Set swFeat = swSelMgr.GetSelectedObject5(i)
  38.         Set selsketch = swFeat.GetSpecificFeature    '获取特定功能类型的接口
  39.         SketchName = swFeat.GetNameForSelection(featType)
  40.         Debug.Print "SketchName草图名 " & "= " & SketchName

  41.      Case swSelSOLIDBODIES    '实体76
  42.        Set swBody = swSelMgr.GetSelectedObject5(i)
  43.        bodyname = swBody.Name
  44.        Debug.Print "BodyName实体名 " & "= " & bodyname
  45.       
  46.      Case swSelSURFACEBODIES    '曲面实体75
  47.        Set swBody = swSelMgr.GetSelectedObject5(i)
  48.        bodyname = swBody.Name
  49.        Debug.Print "SurfacebodyName曲面实体名 " & "= " & bodyname
  50.      
  51.      Case swSelFACES    '面2
  52.        Set swBody = swSelMgr.GetSelectedObject5(i).GetBody    '由面得到实体
  53.        bodyname = swBody.Name
  54.        Debug.Print "FaceGetbodyName面的实体名 " & "= " & bodyname
  55.      
  56.      Case swSelEXTSKETCHSEGS   '草图线条24
  57.         Dim selsegment As ISketchSegment
  58.         Set selsegment = swSelMgr.GetSelectedObject5(i)
  59.         Set selsketch = selsegment.GetSketch  '由草图线条得到草图
  60.        Set swFeat = selsketch
  61.        SketchName = swFeat.Name
  62.        Debug.Print "SketchName草图名 " & "= " & SketchName
  63.     End Select
  64.   Next i
  65.   
  66. part.ClearSelection2 True

  67. '以下判断草图合法性
  68. Dim nRetVal                 As Long
  69. Dim nOpenCount              As Long
  70. Dim nClosedCount            As Long
  71. nRetVal = selsketch.CheckFeatureUse(swSketchCheckFeature_BOSSEXTRUDE, nOpenCount, nClosedCount)

  72.   Debug.Print "     nRetVal       = " & nRetVal
  73.    'SW2012出现再次判定时会出错,与上次判定不一样现象,困惑
  74.   Debug.Print "    OpenCount    = " & nOpenCount
  75.   Debug.Print "    ClosedCount  = " & nClosedCount
  76.   Debug.Print

  77. If nRetVal <> 0 Then       '选择草图中局部范围

  78.     Dim mySelectData As SldWorks.SelectData
  79.     Dim myFeature2 As SldWorks.Feature
  80.     Dim regionCount As Long
  81.     Dim vSkRegions As Variant
  82.     Dim skRegion As SketchRegion
  83.     Dim Ri As Integer
  84.       Set mySelectData = swSelMgr.CreateSelectData
  85.    Set myFeature2 = part.FeatureByName(SketchName)
  86.    Set selsketch = myFeature2.GetSpecificFeature2()
  87.       If Not selsketch Is Nothing Then
  88.         regionCount = selsketch.GetSketchRegionCount()
  89.         Debug.Print regionCount & " regions in sketch " & myFeature2.Name
  90.         vSkRegions = selsketch.GetSketchRegions()
  91.         For Ri = LBound(vSkRegions) To UBound(vSkRegions)
  92.             Set skRegion = vSkRegions(Ri)
  93.             If Not skRegion Is Nothing Then
  94.                 Debug.Print "  region " & Ri & ":"
  95.               boolstatus = skRegion.Select2(True, mySelectData)  '选择草图中局部范围
  96.                 If boolstatus = 0 Then
  97.                     Debug.Print "Selection of region failed局部范围选择失败."
  98.                     Exit Sub
  99.                 End If
  100.             End If
  101.         Next Ri
  102.     End If
  103. Else
  104. boolstatus = part.Extension.SelectByID2(SketchName, "SKETCH", 0, 0, 0, False, 0, Nothing, 0) '选草图
  105. End If

  106. Dim myFeature As SldWorks.Feature     '定义要增加的拉伸特征
  107. Set myFeature = part.FeatureManager.FeatureExtrusion2(True, False, False, _
  108. 0, 0, 0.1, 0, False, False, False, False, 1.74532925199433E-02, 1.74532925199433E-02, False, False, False, False, True, True, True, 0, 0, False)  '增加拉伸特征
  109. Debug.Print
  110. Debug.Print myFeature.Name & " [" & myFeature.GetTypeName & "]"     '特征名
  111.         
  112. Dim swExtrusionData  As ExtrudeFeatureData2              '定义拉伸特征参数
  113. Set swExtrusionData = myFeature.GetDefinition         '取得特征参数
  114. boolstatus = swExtrusionData.AccessSelections(part, Nothing)   '获取访问特征参数,此条必须!

  115. swExtrusionData.SetEndCondition True, swEndCondUpToBody     '改终止条件为成形到实体7
  116. swExtrusionData.SetEndConditionReference True, swBody     '实体附值

  117. boolstatus = myFeature.ModifyDefinition(swExtrusionData, part, Nothing)   '修改参数重建

  118. If boolstatus = False Then              '错误说明实体方向不对
  119.     boolstatus = swExtrusionData.AccessSelections(part, Nothing)   '获取访问特征参数,此条必须!
  120.     swExtrusionData.ReverseDirection = Not swExtrusionData.ReverseDirection    '反向
  121.      boolstatus = myFeature.ModifyDefinition(swExtrusionData, part, Nothing)   '修改参数重建
  122. End If

  123. swExtrusionData.ReleaseSelectionAccess    '释放特征参数

  124. part.Rebuild (1)    '重建以免合并结果未更新错误
  125. part.ClearSelection2 True    '清除选择

  126. End Sub
复制代码



作者: xiabulai    时间: 2016-8-12 09:22
SolidWorks2016使用正常,谢谢分享。
作者: gt.adan    时间: 2016-8-17 13:33
謝謝前輩分享~
作者: Trouble12138    时间: 2018-11-25 19:12
谢谢楼主  太厉害了
作者: 岁寒叁友    时间: 2018-12-12 09:18
怎么用法  给个GIF说明下





欢迎光临 iCAx开思网 (https://www.icax.org/) Powered by Discuz! X3.3