⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 functions.bas

📁 CATIA二次开发
💻 BAS
字号:
Attribute VB_Name = "functions"
Option Explicit

'寻找零件文档中,直径为15mm的孔特征
Sub FindHoles()

    Dim i As Long, j As Long
    Dim myBody As Object, myShape As Object
    Dim Holes() As Object, HoleCount As Long
    Dim L As Length
    
    '如果有多个实体特征,则分别搜索
    For i = 1 To oPartDoc.Part.Bodies.Count
        
        '把myBody设为实体集合中的元素以便搜索
        Set myBody = oPart.Bodies.Item(i)
        
        '分别检查实体中的每个特征
        For j = 1 To myBody.Shapes.Count
            
            '把myShape设为集合中的特征
            Set myShape = myBody.Shapes.Item(j)
            
            '如果是孔特征则检查孔的大小
            If TypeName(myShape) = "Hole" Then
                
                '设L为孔的直径,并检查L的值
                '如果直径为15mm则把它加入到Holes数组中
                Set L = myShape.Diameter
                If L.Value = 15 Then
                    HoleCount = HoleCount + 1
                    ReDim Preserve Holes(HoleCount)
                    Set Holes(HoleCount) = myShape
                End If
            End If
        Next
    Next
    
    '为每个孔创建参考特征以便添加约束
    Set oHBody = AddHBody("RefElements")
    For i = 1 To HoleCount
        CreateRefElemts Holes(i)
    Next
    
    '更新零件文档
    oPart.Update

End Sub

'在产品文档中插入Plate文档和bolt文档作为组件
Sub InsertPlate()
    
    '定义产品根节点和子节点,并定义文件数组
    '这三者都应为变体类型,否则程序会报错
    Dim oRootProduct 'As Product
    Dim oRootChildren 'As Products
    Dim CompList(1)
   
    '获取产品的根节点和子节点
    Set oRootProduct = oProductDoc.Product
    Set oRootChildren = oRootProduct.Products
    
    '为文件数组赋值
    CompList(0) = App.Path + "\plate.CATPart"
    CompList(1) = App.Path + "\bolt.CATPart"
    
    '将两个零件文档作为组件加入到产品中
    oRootChildren.AddComponentsFromFiles CompList, "*"
    
End Sub

'为孔特征创建参考元素,传入的参数为变体类型
Sub CreateRefElemts(oHole)
    
    '定义孔的方向数组,变体类型
    Dim HoleDirArr(2), HoleDir
    Dim HoleCenterArr(2)
    Dim oPt, refPt, oLine
    
    '创建孔的中心点并隐藏
    oHole.GetOrigin HoleCenterArr
    Set oPt = oHSF.AddNewPointCoord(HoleCenterArr(0), HoleCenterArr(1), HoleCenterArr(2))
    oHBody.AppendHybridShape oPt
    HideShow oPt
    
    '获取孔的方向
    oHole.GetDirection HoleDirArr
    Set HoleDir = oHSF.AddNewDirectionByCoord(HoleDirArr(0), HoleDirArr(1), HoleDirArr(2))
    
    '创建孔方向的参考线并隐藏
    Set refPt = oPart.CreateReferenceFromObject(oPt)
    Set oLine = oHSF.AddNewLinePtDir(refPt, HoleDir, 0, 20, False)
    oHBody.AppendHybridShape oLine
    HideShow oLine

End Sub

'根据孔的数量实例化螺栓,并装配到相应位置
Sub AddConstraint()
    
    Dim i As Long
    
    '定义产品根节点和子节点
    '以下操作没有数组参数,可以定义为明确的类型
    Dim oRootProduct As Product
    Dim oRootChildren As Products
    
    '获取产品的根节点和子节点
    Set oRootProduct = oProductDoc.Product
    Set oRootChildren = oRootProduct.Products
    
    '获取名为"plate.1"的钢板组件
    Dim oPlate As Product
    Set oPlate = oRootChildren.Item("plate.1")
    
    '获取名为"bolt.1"的螺栓组件,把它命名为"MainBolt"
    '后面的程序要用这个组件进行实例化
    Dim oMainBolt As Product, refBolt As Reference
    Set oMainBolt = oRootChildren.Item("bolt.1")
    Set refBolt = oMainBolt.ReferenceProduct
    oMainBolt.Name = "MainBolt"
    
    '获取钢板组件中放置参考元素的几何图形集
    '计算孔的数量
    Dim HoleCount As Long
    Set oHBody = oPart.HybridBodies.GetItem("RefElements")
    HoleCount = oHBody.HybridShapes.Count / 2
    
    '定义螺栓组件变量
    Dim oBolts As Product, oBolt As Product
    
    '定义螺栓上的参考元素变量
    Dim refPtPub As Publication, refPt As Reference
    Dim refAxisPub As Publication, refAxis As Reference
    
    '定义钢板上的参考元素变量
    Dim oDirLine, oCoodPt
    Dim refDirLine As Reference, refCoodPt As Reference

    '获取Constraints集合
    Dim oConstraints As Constraints, oConstraint As Constraint
    Set oConstraints = oRootProduct.Connections("CATIAConstraints")
    
    '为钢板组件创建固定约束
    Dim refPlate As Reference
    Set refPlate = oRootProduct.CreateReferenceFromName _
        (oRootProduct.Name & "/plate.1/!" & oRootProduct.Name & "/plate.1/")
    Set oConstraint = oConstraints.AddMonoEltCst(catCstTypeReference, refPlate)
    
    '根据孔的数量建立循环
    For i = 1 To HoleCount
        
        '实例化螺栓组件
        Set oBolt = oRootChildren.AddComponent(refBolt)
        
        '获取螺栓组件上的参考元素
        Set refPtPub = oBolt.Publications.Item("refPt")
        Set refAxisPub = oBolt.Publications.Item("Axis")
        Set refPt = refPtPub.Valuation
        Set refAxis = refAxisPub.Valuation
        
        '获取钢板组件上的参考元素
        Set oCoodPt = oHBody.HybridShapes.Item(i * 2 - 1)
        Set oDirLine = oHBody.HybridShapes.Item(i * 2)
        Set refCoodPt = oRootProduct.CreateReferenceFromName(oRootProduct.Name & "/plate.1/!" & oCoodPt.Name)
        Set refDirLine = oRootProduct.CreateReferenceFromName(oRootProduct.Name & "/plate.1/!" & oDirLine.Name)
        
        '为相应的孔和螺栓添加方向约束和位置约束
        Set oConstraint = oConstraints.AddBiEltCst(catCstTypeOn, refPt, refCoodPt)
        Set oConstraint = oConstraints.AddBiEltCst(catCstTypeOn, refAxis, refDirLine)
    
    Next
    
    '移除用作实例化的螺栓组件
    oRootChildren.Remove "MainBolt"
    
    '更新产品文档
    oRootProduct.Update

End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -