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

iCAx开思网

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

[原创] 宏--所选草图拉伸成形到所选实体

[复制链接]
跳转到指定楼层
1
发表于 2016-8-11 23:44:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  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
复制代码


本帖子中包含更多资源

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

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏3 分享淘帖 赞一下!赞一下!
2
发表于 2016-8-12 09:22:46 | 只看该作者

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

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

x
3
发表于 2016-8-17 13:33:05 | 只看该作者

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

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

x
4
发表于 2018-11-25 19:12:52 | 只看该作者

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

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

x
5
发表于 2018-12-12 09:18:07 | 只看该作者

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

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

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

本版积分规则

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

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

GMT+8, 2024-4-27 07:14 , Processed in 0.024499 second(s), 10 queries , Gzip On, Redis On.

Powered by Discuz! X3.3

© 2002-2024 www.iCAx.org

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