我想测量孔重心 (COG)。
我写了代码。它可以工作,但默认情况下 COG 值是从局部轴获取的。
我想提取相对于绝对轴系统的COG。
Sub main()
Dim oPartDoc As document
Set oPartDoc = CATIA.ActiveDocument
Dim selection2 As Selection
Set selection2 = oPartDoc.Selection
selection2.Search ("Type=Hole,all")
For i = 1 To selection2.Count
Dim ocircle As Variant
Set ocircle = selection2.Item(i).Value
Set TheSPAWorkbench = CATIA.ActiveDocument.GetWorkbench("SPAWorkbench")
Set TheMeasurable = TheSPAWorkbench.GetMeasurable(ocircle)
Dim Coordinates(2)
TheMeasurable.GetCOG Coordinates
x1 = Coordinates(0)
y1 = Coordinates(1)
z1 = Coordinates(2)
MsgBox "Center of gravity : X = " & _
CStr(Coordinates(0)) & ", Y = " + CStr(Coordinates(1)) & ", Z = " + CStr(Coordinates(2))
Next
selection2.Clear
End Sub
从 R28 开始有新方法,可让您轻松地在装配环境中进行测量。 不再需要执行转换或玩字符串游戏来制作产品参考。
SPAWorkbench.GetMeasurableInContext(partReference,instanceProduct) 就是执行 COG 所需的全部内容。
这是一个例子
Dim oRoot As Product
Set oRoot = CATIA.ActiveDocument.Product
Dim oPartProd As Product
Set oPartProd = oRoot.Products.Item("Part2.1") 'this is a specific instance product
Dim oPart As Part
Set oPart = oPartProd.ReferenceProduct.Parent.Part 'get the part from the instance product
Dim oBodyRef As Reference
Set oBodyRef = oPart.CreateReferenceFromObject(oPart.Bodies.Item(1)) 'part reference to the PartBody
Dim oSPA As SPAWorkbench
Set oSPA = CATIA.ActiveDocument.GetWorkbench("SPAWorkbench") 'get the SPA workbench from the root assembly
Dim oRefMeasurable As Measurable
Set oRefMeasurable = oSPA.GetMeasurableInContext(oBodyRef, oPartProd) 'measurable in assembly context *NEW in R29*
Dim cog(2)
Dim vMeas As Variant
Set vMeas = oRefMeasurable
Call vMeas.GetCOG(cog)
Debug.Print "PRODUCT COG = " & cog(0) & " " & cog(1) & " " & cog(2)