狮猿社CATIA

CATIA VBA:局部坐标转绝对坐标

2020-04-05  本文已影响0人  锦囊喵

Here is the code, sorry for the delay. I've simplified it to just make the coordinates transformation.

There are two functions and a sub. The two functions are just mathematical stuff, 3x3 matrix inverse and determinant calculation, you can replace them for your preferred ones if you want. Sub Coord_Transform will do the coordinates transformation. The arguments of the sub are:

aRel() As Double , Matrix with a set of coordinates relative to the CATPart's axis system. These are, typically, the ones you get with the GetCoordinates method on a Point. Coordinates are as usual; aRel(0) for X, aRel(1) for Y and aRel(2) for Z. Maybe you need to convert types from variant to double to avoid type mismatch.

aAbs() As Double, Matrix with coordinates with respect to the Axis System of the top-level CATProduct. This is the matrix where you will get the results of the coordinates transformation. Initially blank, pass it to the sub and it will return it to you properly filled; again aAbs(0) for X, aAbs(1) for Y and aAbs(2) for Z

oProduct As Product, This is the CATPart's Product. For example, if you are measuring the coordinates of a "Point.1" in a CATPart named "Part.1", which is the first instance in a CATproduct named "RootProduct", then oProduct=RootProduct.Products.Item(1), and the CATPart itself can be accessed via the reference product, like oProduct.ReferenceProduct.Parent. You need to know the Product object which references to the CATPart in order to get its coordinates with the Position property.

bRecursively As Boolean, True if you want to calculate the coordinates with respect to the top-level Product, no matter what level of depth your CATPart is. False to calculate only with respect to the CATPart's father Product. I guess what you want to try here is "True", with some levels of depth...

Just paste the code in yours and call Coord_Transform with the appropiate arguments. If you need it, a similar routine also calculates coordinates taking into account different Axis Systems in a CATPart, using the same matrix transformations...

Hope it helps!

Regards.

 
'---CODE START------------------------------------------------------
Function Det3x3(dX11 As Double, dX12 As Double, dX13 As Double, _
                dX21 As Double, dX22 As Double, dX23 As Double, _
                dX31 As Double, dX32 As Double, dX33 As Double) As Double
'***********************************************
'*
'* 3x3 matrix determinant calculation (direct)
'*
'***********************************************
                
    Det3x3 = dX11 * dX22 * dX33 + dX12 * dX23 * dX31 + dX21 * dX32 * dX13 - _
             dX13 * dX22 * dX31 - dX12 * dX21 * dX33 - dX23 * dX32 * dX11
End Function
Function Inv3x3(dX11 As Double, dX12 As Double, dX13 As Double, _
           dX21 As Double, dX22 As Double, dX23 As Double, _
           dX31 As Double, dX32 As Double, dX33 As Double, aInv() As Double) As Boolean
'***********************************************
'*
'* 3x3 matrix inverse calculation (direct)
'*
'***********************************************
    Dim dDet As Double
    
    ReDim aInv(8)
    
    Inv3x3 = False
    
    dDet = Det3x3(dX11, dX12, dX13, dX21, dX22, dX23, dX31, dX32, dX33)
    If dDet = 0 Then Exit Function
    
    aInv(0) = (dX22 * dX33 - dX23 * dX32) / Abs(dDet)
    aInv(1) = (dX13 * dX32 - dX12 * dX33) / Abs(dDet)
    aInv(2) = (dX12 * dX23 - dX13 * dX22) / Abs(dDet)
    aInv(3) = (dX23 * dX31 - dX21 * dX33) / Abs(dDet)
    aInv(4) = (dX11 * dX33 - dX13 * dX31) / Abs(dDet)
    aInv(5) = (dX13 * dX21 - dX11 * dX23) / Abs(dDet)
    aInv(6) = (dX21 * dX32 - dX22 * dX31) / Abs(dDet)
    aInv(7) = (dX12 * dX31 - dX11 * dX32) / Abs(dDet)
    aInv(8) = (dX11 * dX22 - dX12 * dX21) / Abs(dDet)
    
    Inv3x3 = True
    
End Function
Sub Coord_Transform(aRel() As Double, aAbs() As Double, oProduct As Product, bRecursively As Boolean)
    
    Dim vProduct As Object, vCoord(11)
    Dim oFatherProduct As Product
    Dim aInv() As Double
    
    'Exit condition, empty object
    If oProduct Is Nothing Then Exit Sub
    
    'Redim absolute coords matrix
    On Error Resume Next
    ReDim aAbs(2)
    On Error GoTo 0
    
    'Calculate product coordinates
    Set vProduct = oProduct
    vProduct.Position.GetComponents vCoord
    
    'Calculate inverse matrix
    If Inv3x3(CDbl(vCoord(0)), CDbl(vCoord(1)), CDbl(vCoord(2)), _
                 CDbl(vCoord(3)), CDbl(vCoord(4)), CDbl(vCoord(5)), _
                 CDbl(vCoord(6)), CDbl(vCoord(7)), CDbl(vCoord(8)), aInv) Then
    Else
        MsgBox "Error, degenerate transformation", vbOKOnly
        Exit Sub
    End If
    
    'Calculate transformation
    aAbs(0) = vCoord(9) + aInv(0) * aRel(0) + aInv(1) * aRel(1) + aInv(2) * aRel(2)
    aAbs(1) = vCoord(10) + aInv(3) * aRel(0) + aInv(4) * aRel(1) + aInv(5) * aRel(2)
    aAbs(2) = vCoord(11) + aInv(6) * aRel(0) + aInv(7) * aRel(1) + aInv(8) * aRel(2)
    
    'If recursive option sepecified, search for parents and applies the transformation again
    If bRecursively Then
        
        'Try to assign parent
        Set oFatherProduct = Nothing
        On Error Resume Next
        Set oFatherProduct = oProduct.Parent.Parent
        On Error GoTo 0
        
        'If OK, recalculate coords
        If oFatherProduct Is Nothing Then
        Else
            aRel(0) = aAbs(0)
            aRel(1) = aAbs(1)
            aRel(2) = aAbs(2)
            Coord_Transform aRel, aAbs, oFatherProduct, True
        End If
        
    End If
    
End Sub

http://www.coe.org/p/fo/et/thread=17535

上一篇下一篇

猜你喜欢

热点阅读