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

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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,362评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,330评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,247评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,560评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,580评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,569评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,929评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,587评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,840评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,596评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,678评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,366评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,945评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,929评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,165评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,271评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,403评论 2 342

推荐阅读更多精彩内容

  • By clicking to agree to this Schedule 2, which is hereby ...
    qaz0622阅读 1,427评论 0 2
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,442评论 0 13
  • 本文转载自知乎 作者:季子乌 笔记版权归笔记作者所有 其中英文语句取自:英语流利说-懂你英语 ——————————...
    Danny_Edward阅读 43,848评论 4 38
  • 纵有一千只手 那也抓不住所有 周而复始 生死轮流 不如,生来就空尤
    李大侠_阅读 179评论 0 0
  • 秋天像一块巨大的明矾,一日日沉淀所有的时光与悲哀。秋天很有灵气。 回想校园一条路上载满的法国梧桐,漂亮极了。走在上...
    贪吃的土拨鼠阅读 703评论 2 7