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

iCAx开思网

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

[原创] 宏--拉伸切除特征定义修改为给定深度

[复制链接]
发表于 2018-4-27 11:22:49 | 显示全部楼层 |阅读模式
  1. ' ******************************************************************************
  2. '镜向阵列时,由于拉伸切除特征中定义是拉伸到某点或到某线或到某面,出现报错。通过宏将定义改为给定深度,避免报错  04/25/2018 by PYCZT
  3. '预选:选择一个拉伸或切除特征
  4. '结果:根据特征的定义进行计算出给定深度,并修改定义
  5. ' ******************************************************************************
  6. Dim swApp                      As SldWorks.SldWorks
  7. Dim swModel                     As SldWorks.ModelDoc2
  8. Dim swSelMgr                    As SldWorks.SelectionMgr
  9. Dim swFeat                      As SldWorks.Feature
  10. Dim SwSketch                    As SldWorks.Sketch
  11.    
  12. Dim swExtrusionData             As ExtrudeFeatureData2       '定义拉伸切除特征参数
  13. Dim boolstatus                  As Boolean
  14. Dim Forward                     As Boolean
  15. Dim EndCondvalue(1)             As Integer
  16. Dim SwEndConRef                 As Object
  17. Dim FromEntity                  As Object
  18. Dim FromEntityType              As Long
  19. Dim Sketchplane                 As SldWorks.Entity
  20. Dim nEntType                    As Long

  21. Dim vPoint1, vPoint2, vPoint3, vPoint4   As Variant
  22. Dim EndCondition1, EndCondition2 As Long
  23. Dim Depth                       As Double
  24.    
  25. Sub main()
  26.     Set swApp = CreateObject("SldWorks.Application")
  27.     Set swModel = swApp.ActiveDoc
  28.     Set swSelMgr = swModel.SelectionManager
  29.     Set swFeat = swSelMgr.GetSelectedObject5(1)
  30.        Debug.Print swFeat.Name & " [" & swFeat.GetTypeName & "]"     '特征名
  31.     Set SwSketch = swFeat.GetFirstSubFeature.GetSpecificFeature2 'GetChildren
  32.        Debug.Print "Sketch Name = 草图名称为 " + SwSketch.Name
  33.      
  34.     Set swExtrusionData = swFeat.GetDefinition         '取得特征参数
  35.     boolstatus = swExtrusionData.AccessSelections(swModel, Nothing)    '获取访问特征参数,此条必须!
  36.   

  37. Dim Ref1 As Object
  38. Dim Type1 As Long
  39. Dim Ref2 As Object
  40. Dim Type2 As Long
  41. Dim DirectNumValue As Long
  42. DirectNumValue = swExtrusionData.GetDirectionReference(Ref1, Type1, Ref2, Type2)
  43.   Debug.Print " DirectNumValue =  " & DirectNumValue
  44. If DirectNumValue >= 1 Then
  45. MsgBox "暂不支持存在方向参考,执行退出"
  46. Exit Sub
  47. End If
  48.    
  49. Forward = True  '方向初值
  50. For I = 0 To 1
  51.          
  52. EndCondvalue(I) = swExtrusionData.GetEndCondition(Forward)
  53. Debug.Print "第" & I & " 终止条件为EndConditionvalue " & EndCondvalue(I)
  54. Select Case EndCondvalue(I)
  55.    Case 0
  56.       Debug.Print "swEndCondBlind 给定深度"
  57.    Case 1
  58.       Debug.Print "swEndCondThroughAll完全贯穿"
  59.    Case 2
  60.       Debug.Print "swEndCondThroughNext成形到下个面"
  61.    Case 3
  62.       Debug.Print "swEndCondUpToVertex成形到顶点 "
  63.    Case 4
  64.       Debug.Print "swEndCondUpToSurface成形到一面 "
  65.    Case 5
  66.       Debug.Print "swEndCondOffsetFromSurface成形到离指定面指定的距离"
  67.    Case 6
  68.       Debug.Print "'swEndCondMidPlane两侧对称"
  69.    Case 7
  70.       Debug.Print "'swEndCondUpToBody成形到实体"
  71.   End Select
  72. If EndCondvalue(I) = 0 Or EndCondvalue(I) = 1 Or EndCondvalue(I) = 2 Or EndCondvalue(I) = 5 Or EndCondvalue(I) = 6 Or EndCondvalue(I) = 7 Then
  73.   Debug.Print "终止条件已是拉伸或切除深度或其它不合适项,无需转换"
  74.   GoTo nextdo
  75. End If

  76. Dim ReferenceType As Long
  77. Set SwEndConRef = swExtrusionData.GetEndConditionReference(Forward, ReferenceType) '获得终止对象
  78.      ' Dim EndConRefValue As String
  79.      '  EndConRefValue = swModel.GetEntityName(SwEndConRef)
  80.      '  Debug.Print " SwEndConRefName = " & EndConRefValue
  81.    
  82.   swExtrusionData.GetFromEntity FromEntity, FromEntityType '获得开始对象,只有在曲面/面/基准面有效,否则为nothing值

  83.         Debug.Print " FromEntityType拉伸或切除是从哪里开始的实体的几何类型 = " & FromEntityType
  84.   
  85.          '注意上面几何类型值FromEntityType与下面的类型值swExtrusionData.FromType不一致
  86.       
  87.    '以下四种从哪里开始情况分别计算实际深度
  88.       
  89.        Select Case swExtrusionData.FromType
  90.         
  91.             Case SwConst.swExtrudeFrom_e.swExtrudeFrom_SketchPlane
  92.                Debug.Print "  from: sketchplane 从草图基准面"
  93.                
  94.                Set Sketchplane = SwSketch.GetReferenceEntity(nEntType)  '因为原来FromEntity为空值,所以通过草图得到基准面或平面或曲面
  95.                Depth = swModel.ClosestDistance(SwEndConRef, Sketchplane, vPoint1, vPoint2)  '计算终止对象与草图平面的距离
  96.                               
  97.             Case SwConst.swExtrudeFrom_e.swExtrudeFrom_Offset
  98.                 Debug.Print "  from: offset 从等距"
  99.                 Debug.Print "  distance等距距离 = " & swExtrusionData.FromOffsetDistance
  100.                 Debug.Print "  reverse等距方向  = " & swExtrusionData.FromOffsetReverse
  101.                  
  102.                  Set Sketchplane = SwSketch.GetReferenceEntity(nEntType)  '因为原来FromEntity为空值,所以通过草图得到基准面或平面或曲面
  103.          
  104.                If swExtrusionData.FromOffsetReverse Then
  105.                   Depth = swModel.ClosestDistance(SwEndConRef, Sketchplane, vPoint1, vPoint2) + swExtrusionData.FromOffsetDistance '计算终止对象与草图平面的距离再加等距距离
  106.                Else
  107.                   Depth = swModel.ClosestDistance(SwEndConRef, Sketchplane, vPoint1, vPoint2) - swExtrusionData.FromOffsetDistance '计算终止对象与草图平面的距离再减等距距离
  108.                End If
  109.                         
  110.             Case SwConst.swExtrudeFrom_e.swExtrudeFrom_SurfaceFacePlane
  111.                 Debug.Print "  from: surface 从曲面/面/基准面"
  112.                 Depth = swModel.ClosestDistance(SwEndConRef, FromEntity, vPoint1, vPoint2) '计算终止对象与开始对象的距离
  113.                
  114.             Case SwConst.swExtrudeFrom_e.swExtrudeFrom_Vertex
  115.                 Debug.Print "  from: vertex 从顶点"
  116.                 Set Sketchplane = SwSketch.GetReferenceEntity(nEntType)  '因为原来FromEntity为空值,所以通过草图得到基准面或平面或曲面
  117.                 Depth = swModel.ClosestDistance(SwEndConRef, Sketchplane, vPoint1, vPoint2) - swModel.ClosestDistance(FromEntity, Sketchplane, vPoint3, vPoint4)  '两个距离计算值相差
  118.                   
  119.         End Select

  120.         If Depth = -1# Then
  121.            Debug.Print "无法计算距离起始曲面与终点对象的距离,执行退出no solution"
  122.            GoTo nextdo
  123.          End If
  124.   
  125.     Debug.Print " Depth = " & Depth * 1000# & " mm"

  126.     swExtrusionData.SetEndCondition Forward, swEndCondBlind   '改终止条件为"拉伸深度"
  127.     swExtrusionData.SetDepth Forward, Depth              '赋拉伸值
  128. nextdo:
  129. Forward = False
  130. Next I

  131. boolstatus = swFeat.ModifyDefinition(swExtrusionData, swModel, Nothing) '修改参数重建

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

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

  135. End Sub
复制代码
论坛冷清,抛砖引玉

本帖子中包含更多资源

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

x
发表于 2018-4-27 11:49:22 | 显示全部楼层

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

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

x
发表于 2018-5-8 20:09:54 | 显示全部楼层

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

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

x
发表于 2018-5-9 08:01:57 | 显示全部楼层

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

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

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

本版积分规则

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

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

GMT+8, 2024-4-18 12:24 , Processed in 0.024952 second(s), 10 queries , Gzip On, Redis On.

Powered by Discuz! X3.3

© 2002-2024 www.iCAx.org

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