Need help in Inventor vba to calculate distance between 2 points.

Need help in Inventor vba to calculate distance between 2 points.

Anonymous
Not applicable
3,189 Views
9 Replies
Message 1 of 10

Need help in Inventor vba to calculate distance between 2 points.

Anonymous
Not applicable

Am very new to inventor customisation through vba and don't know from where to start. I need to find distance between two selected points of a part for doing tolerance stack up. I intend to form to message box so that it return the values in it.

Thanks.

0 Likes
Accepted solutions (1)
3,190 Views
9 Replies
Replies (9)
Message 2 of 10

ekinsb
Alumni
Alumni

This is probably a good program to get started on because it's relatively simple.  The first question I have though is where do the 2 points come from.  Are they selected by the user or discovered in some way?


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 3 of 10

Anonymous
Not applicable
Yes,they will be selected by user. The thing is the user will select any two points on the geometry or say part and a messagebox should pop out calculating the distance between that two points.
0 Likes
Message 4 of 10

Anonymous
Not applicable
I have prepared a Vba script in Microsoft Excel but am seriously getting some problems while learning Inventor API. Please guide me to prepare my first task.
0 Likes
Message 5 of 10

ekinsb
Alumni
Alumni
Accepted solution

Here's a VBA program that I believe does what you want.  It's written as simple as possible so it's not fancy at all.

 

Public Sub MeasurePointDistance()
    ' Have the two points selected.
    Dim points(1) As Object
    Set points(0) = ThisApplication.CommandManager.Pick(kAllPointEntities, "Select Point 1")
    Set points(1) = ThisApplication.CommandManager.Pick(kAllPointEntities, "Select Point 2")
    
    ' Use some code that gets the 3D coordinate from each point.
    ' This varies depending on what was selected.
    Dim resultPoints(1) As Point
    Dim i As Integer
    For i = 0 To 1
        If TypeOf points(i) Is WorkPoint Then
            Dim wkPnt As WorkPoint
            Set wkPnt = points(i)
            Set resultPoints(i) = wkPnt.Point
        ElseIf TypeOf points(i) Is SketchPoint Then
            Dim skPnt As SketchPoint
            Set skPnt = points(i)
            Set resultPoints(i) = skPnt.Geometry3d
        ElseIf TypeOf points(i) Is SketchPoint3D Then
            Dim skPnt3D As SketchPoint3D
            Set skPnt3D = points(i)
            Set resultPoints(i) = skPnt3D.Geometry
        ElseIf TypeOf points(i) Is Vertex Then
            Dim vert As Vertex
            Set vert = points(i)
            Set resultPoints(i) = vert.Point
        End If
    Next
    
    ' Display the distance between the points using the current document units.
    Dim uom As UnitsOfMeasure
    Set uom = ThisApplication.ActiveDocument.UnitsOfMeasure
    
    Dim result As String
    result = "Distance: " & uom.GetStringFromValue(resultPoints(0).DistanceTo(resultPoints(1)), kDefaultDisplayLengthUnits)
    
    Call MsgBox(result, vbOKOnly, "Distance Between Points")
End Sub

 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 6 of 10

Anonymous
Not applicable
Am very thankful to you. I have started learning Vba for inventor few days back and understanding this script would help me gain knowledge. Am looking for some more exhaustive material for the same.Thanks again.
0 Likes
Message 7 of 10

Anonymous
Not applicable
What changes should be made in the above code if we want the vertex co-ordinates,like in X,Y and Z components of the selected point?
0 Likes
Message 8 of 10

ekinsb
Alumni
Alumni

The resultPoints array contains Point objects.  The Point object is a wrapper over X,Y,Z coordinates.  For example, if you add this line at the bottom it will show you the x,y,z coordinates of the first point.  In addition to just the X,Y,Z coordintes, the Point also provides some utilities for working with a point.  One of these is the DistanceTo method which I used in the previous program.

 

Call MsgBox("Coords: " & resultPoints(0).X & ", " & resultPoints(0).Y & ", " & resultPoints(0).Z)


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 9 of 10

Anonymous
Not applicable

I am struck with the thing that if I select the point-1 to point-2  from left to right it should give me positive dimension and if I select point 1 to point 2 from right to left it should give me negative dimension. Is it possible?

 

 

Also Is it possible to have a message box that asks for a value after each dimension displayed when user selects two points?

I want the inventor to ask the value and then store in it and then displays those all values collectively.

 

0 Likes
Message 10 of 10

Anonymous
Not applicable

Ekin , I have a doubt over here.

 

In the above script you have mentioned the " Result" as string and so in the next line the command GetValueFromString is used.

So for further mathematical calculations, am not able to do any thing as it is string and for any mathematical functions I have to use Integer or double.

 

Do you have any alternative?

Please tell me.

 

0 Likes