Compute distance between objects

The script below shows how you can calculate the distance between two objects using the scripting language. 

The script works by using the TransformPosition command, which takes a starting coordinate system, a new coordinate system and a position vector defined by its x, y, and z components. The x, y and z values, input to the TransformPosition command, are modified in place (by reference) to convert the vector from the starting coordinate system to the new coordinate system. 

In this manner you can transform the vertex location of a surface (as an example) to global coordinates by using the syntax below, where n is the node number of the surface:

Option Explicit

Type T_POINT
    n As Long
    x As Double
    y As Double
    z As Double
End Type


Sub Main

    Dim p1 As T_POINT, p2 As T_POINT

    'Node number and point on the first object
    p1.n = FindFullName( "Geometry.Plane" )
    p1.x = 0
    p1.y = 0
    p1.z = 0

    'Node number and point on the second object
    p2.n = FindFullName( "Geometry.Plane 1" )
    p2.x = 0
    p2.y = 0
    p2.z = 0

    Print "Distance between objects is " & CalcDistance(p1, p2) & " " & GetUnits()

End Sub


Function CalcDistance( ByVal p1 As Variant, _
                       ByVal p2 As Variant ) As Double

    'Convert the first point from local to global
    TransformPosition(p1.n, -1, p1.x, p1.y, p1.z)

    'Convert the second point from local to global
    TransformPosition(p2.n, -1, p2.x, p2.y, p2.z)

    'Compute the length of the vector
    Dim dist As Double
    GetLength3D(p2.x-p1.x, p2.y-p1.y, p2.z-p1.z, dist)

    'Return the length
    Return dist

End Function<br>

Example File: ComputeDistanceExample.frd

Still need help? Contact Us Contact Us