[MVBA] how to rotate element by transform method

Hello All,

I created some cones by MVBA codes below:

Sub TestCone()
    Dim MySmartSolid As SmartSolidElement
    Dim MyTrans As Transform3d
    
    Set MySmartSolid = SmartSolid.CreateCone(Nothing, 2, 5, 10)
    ActiveModelReference.AddElement MySmartSolid
    
    With MyTrans
         .RowX = Point3dFromXYZ(0, -1, 0)
         .RowY = Point3dFromXYZ(0, 0, 1)
         .RowZ = Point3dFromXYZ(1, 0, 0)
         .TranslationX = 10
    End With
    MySmartSolid.Transform MyTrans
    MySmartSolid.Color = 3
    ActiveModelReference.AddElement MySmartSolid
   
End Sub

The first cone is the black one. then I revised transform, the second cone is red one. I'd like to know how to calculate RowX/Y/Z if I'd like to move it to the yellow one, assuming I know the coordinates of the two centers. Thanks a lot.

Parents
  • Hi,

    I have tested and please find below the VBA code I have put together to transform one such a SmartSolid to the exact position of another Smartsolid with the same definition.
    I have attached a small dgn file containing 2 SmartSolid elements. The green one I am using as source to transform to the position of the other (red) Smartsolid.

    I have used some steps to calculate the rotation of each Smartsolid and combine the required transformations to get from the position of the one SmartSolid to the other SmartSolid.

    There will be a lot of different approaches to get this done....

    For more information on this topic please see:Transforms

    I hope this helps?

    Best regards,

    Artur

    Sub transFormTest()
    
    ' the idea is to transform the source element to the position of the target element
    ' The 2 SmartSolid elements to get the rotation from:
    Dim oSource As SmartSolidElement
    Dim oTarget As SmartSolidElement
    Set oSource = ActiveModelReference.GetElementByID(DLongFromString("1400"))
    Set oTarget = ActiveModelReference.GetElementByID(DLongFromString("1408"))
    
    
    Dim x As Double
    Dim y As Double
    Dim z As Double
    Dim oph As PropertyHandler
    Set oph = CreatePropertyHandler(oSource)
    'Calculate rotation of the Source element:
    If oph.SelectByAccessString("RotationX") Then x = Val(oph.GetValue)
    Dim s1 As Matrix3d
    s1 = Matrix3dFromAxisAndRotationAngle(0, Pi * x / 180)
    If oph.SelectByAccessString("RotationY") Then y = Val(oph.GetValue)
    Dim s2 As Matrix3d
    s2 = Matrix3dFromAxisAndRotationAngle(1, Pi * y / 180)
    If oph.SelectByAccessString("RotationZ") Then z = Val(oph.GetValue)
    Dim s3 As Matrix3d
    s3 = Matrix3dFromAxisAndRotationAngle(2, Pi * z / 180)
    
    Dim s As Matrix3d
    s = Matrix3dFromMatrix3dTimesMatrix3dTimesMatrix3d(s1, s2, s3)
    Dim sInv As Matrix3d
    sInv = Matrix3dInverse(s)
    Dim sTrans As Transform3d
    ' this transformation describes to unrotate oSource:
    sTrans = Transform3dFromMatrix3dAndFixedPoint3d(sInv, oSource.Origin)
    
    'Calculate rotation of Target element:
    Set oph = CreatePropertyHandler(oTarget)
    If oph.SelectByAccessString("RotationX") Then x = Val(oph.GetValue)
    Dim t1 As Matrix3d
    t1 = Matrix3dFromAxisAndRotationAngle(0, Pi * x / 180)
    If oph.SelectByAccessString("RotationY") Then y = Val(oph.GetValue)
    Dim t2 As Matrix3d
    t2 = Matrix3dFromAxisAndRotationAngle(1, Pi * y / 180)
    If oph.SelectByAccessString("RotationZ") Then z = Val(oph.GetValue)
    Dim t3 As Matrix3d
    t3 = Matrix3dFromAxisAndRotationAngle(2, Pi * z / 180)
    
    Dim t As Matrix3d
    t = Matrix3dFromMatrix3dTimesMatrix3dTimesMatrix3d(t1, t2, t3)
    t = Matrix3dFromMatrix3dTimesMatrix3d(t, sInv)
    
    ' Calculate transformation describing the translation from Source origin to target origin:
    Dim moveTrans As Transform3d
    moveTrans = Transform3dFromPoint3d(Point3dSubtract(oTarget.Origin, oSource.Origin))
    
    Dim tTrans As Transform3d
    
    'this describes the rotation ogf the target element
    tTrans = Transform3dFromMatrix3dAndFixedPoint3d(t, oSource.Origin)
    
    'combine rotation with translation to one single translation:
    tTrans = Transform3dFromTransform3dTimesTransform3d(moveTrans, tTrans)
    
    'Transform source element to target element
    oSource.Transform tTrans
    oSource.Rewrite
    
    End Sub

    3835.transformation.dgn

Reply
  • Hi,

    I have tested and please find below the VBA code I have put together to transform one such a SmartSolid to the exact position of another Smartsolid with the same definition.
    I have attached a small dgn file containing 2 SmartSolid elements. The green one I am using as source to transform to the position of the other (red) Smartsolid.

    I have used some steps to calculate the rotation of each Smartsolid and combine the required transformations to get from the position of the one SmartSolid to the other SmartSolid.

    There will be a lot of different approaches to get this done....

    For more information on this topic please see:Transforms

    I hope this helps?

    Best regards,

    Artur

    Sub transFormTest()
    
    ' the idea is to transform the source element to the position of the target element
    ' The 2 SmartSolid elements to get the rotation from:
    Dim oSource As SmartSolidElement
    Dim oTarget As SmartSolidElement
    Set oSource = ActiveModelReference.GetElementByID(DLongFromString("1400"))
    Set oTarget = ActiveModelReference.GetElementByID(DLongFromString("1408"))
    
    
    Dim x As Double
    Dim y As Double
    Dim z As Double
    Dim oph As PropertyHandler
    Set oph = CreatePropertyHandler(oSource)
    'Calculate rotation of the Source element:
    If oph.SelectByAccessString("RotationX") Then x = Val(oph.GetValue)
    Dim s1 As Matrix3d
    s1 = Matrix3dFromAxisAndRotationAngle(0, Pi * x / 180)
    If oph.SelectByAccessString("RotationY") Then y = Val(oph.GetValue)
    Dim s2 As Matrix3d
    s2 = Matrix3dFromAxisAndRotationAngle(1, Pi * y / 180)
    If oph.SelectByAccessString("RotationZ") Then z = Val(oph.GetValue)
    Dim s3 As Matrix3d
    s3 = Matrix3dFromAxisAndRotationAngle(2, Pi * z / 180)
    
    Dim s As Matrix3d
    s = Matrix3dFromMatrix3dTimesMatrix3dTimesMatrix3d(s1, s2, s3)
    Dim sInv As Matrix3d
    sInv = Matrix3dInverse(s)
    Dim sTrans As Transform3d
    ' this transformation describes to unrotate oSource:
    sTrans = Transform3dFromMatrix3dAndFixedPoint3d(sInv, oSource.Origin)
    
    'Calculate rotation of Target element:
    Set oph = CreatePropertyHandler(oTarget)
    If oph.SelectByAccessString("RotationX") Then x = Val(oph.GetValue)
    Dim t1 As Matrix3d
    t1 = Matrix3dFromAxisAndRotationAngle(0, Pi * x / 180)
    If oph.SelectByAccessString("RotationY") Then y = Val(oph.GetValue)
    Dim t2 As Matrix3d
    t2 = Matrix3dFromAxisAndRotationAngle(1, Pi * y / 180)
    If oph.SelectByAccessString("RotationZ") Then z = Val(oph.GetValue)
    Dim t3 As Matrix3d
    t3 = Matrix3dFromAxisAndRotationAngle(2, Pi * z / 180)
    
    Dim t As Matrix3d
    t = Matrix3dFromMatrix3dTimesMatrix3dTimesMatrix3d(t1, t2, t3)
    t = Matrix3dFromMatrix3dTimesMatrix3d(t, sInv)
    
    ' Calculate transformation describing the translation from Source origin to target origin:
    Dim moveTrans As Transform3d
    moveTrans = Transform3dFromPoint3d(Point3dSubtract(oTarget.Origin, oSource.Origin))
    
    Dim tTrans As Transform3d
    
    'this describes the rotation ogf the target element
    tTrans = Transform3dFromMatrix3dAndFixedPoint3d(t, oSource.Origin)
    
    'combine rotation with translation to one single translation:
    tTrans = Transform3dFromTransform3dTimesTransform3d(moveTrans, tTrans)
    
    'Transform source element to target element
    oSource.Transform tTrans
    oSource.Rewrite
    
    End Sub

    3835.transformation.dgn

Children