GetExistingFacetTolerances error when converting VBA to VB.NET

GetExistingFacetTolerances error when converting VBA to VB.NET

basnederveen
Advocate Advocate
683 Views
3 Replies
Message 1 of 4

GetExistingFacetTolerances error when converting VBA to VB.NET

basnederveen
Advocate
Advocate

Hello


I am trying to convert some code from VBA to VB.NET. It's some code from the 'Drawing automation' pack (http://modthemachine.typepad.com/my_weblog/2009/10/drawing-automation.html). I've edited it a bit, but the part that gives the error is original.

 

A type mismatch error (Screenshot attached) occurrs while the method SurfaceBody.GetExistingFacetTolerances() requests a double array and an integer.. Code that errors:

 

        ' Get the faceted representation of the model.
        Dim lFacetCount As Integer
        Dim adFacetTolerances() As Double
        Call body.GetExistingFacetTolerances(lFacetCount, adFacetTolerances)

 

In VBA there are no problems at all,  does anyone know what this means and how I can fix it?

 

Thanks in advance!

 

0 Likes
Accepted solutions (1)
684 Views
3 Replies
Replies (3)
Message 2 of 4

BrianEkins
Mentor
Mentor
Accepted solution

Arrays are handled a bit differently in Visual Basic than in VBA.  In the declaration, you need to initialize as an empty array.  The code below does this for both the GetExistingFacetTolerances and GetExistingFacets calls.

 

        Dim tolsCount As Integer
        Dim existingTols() As Double = {}
        body.GetExistingFacetTolerances(tolsCount, existingTols)

        Dim bestTol As Double = existingTols(0)
        For i As Integer = 1 To tolsCount - 1
            If existingTols(i) < bestTol Then
                bestTol = existingTols(i)
            End If
        Next

        Dim vertCount As Integer
        Dim facetCount As Integer
        Dim coords() As Double = {}
        Dim verts() As Double = {}
        Dim indices() As Integer = {}
        body.GetExistingFacets(bestTol, vertCount, facetCount, coords, verts, indices)
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 3 of 4

basnederveen
Advocate
Advocate

Had a feeling it was something like this! Thanks a lot Brian!

 

 

0 Likes
Message 4 of 4

fbeaupreNFXB9
Explorer
Explorer

Thank you Brian for all this beautiful code, I have been "borrowing" a lot.  I found that running that code can be very slow when the part as several faces, it seems that sorting was the issue. Below is a code alteration:

 

Public Function calculateTightBoundingBox(body As SurfaceBody, Optional Tolerance As Double = 0) As Box


Dim vertCount As Long
Dim facetCount As Long
Dim vertCoords() As Double = {}
Dim normVectors() As Double = {}
Dim vertInds() As Integer = {}

' If the tolerance is zero, use the best display mesh available.
If Tolerance <= 0 Then
' Get the best display mesh available.
Dim tolCount As Integer
Dim tols() As Double = {}

Call body.GetExistingFacetTolerances(tolCount, tols)
Dim i As Integer
Dim bestTol As Double
bestTol = tols(0)
For i = 1 To tolCount - 1
If tols(i) < bestTol Then
bestTol = tols(i)
End If
Next

Call body.GetExistingFacets(bestTol, vertCount, facetCount, vertCoords, normVectors, vertInds)
Else
' Calculate a new mesh based on the input tolerance.
Call body.CalculateFacets(Tolerance, vertCount, facetCount, vertCoords, normVectors, vertInds)
End If

Dim tg As TransientGeometry
tg = _invApp.TransientGeometry

' Calculate the range of the mesh.
Dim smallPnt As Point
Dim largePnt As Point
smallPnt = tg.CreatePoint(vertCoords(0), vertCoords(1), vertCoords(2))
largePnt = tg.CreatePoint(vertCoords(0), vertCoords(1), vertCoords(2))


Dim vertCoordsX(((vertCoords.Length) / 3) - 1) As Double
Dim vertCoordsY(((vertCoords.Length) / 3) - 1) As Double
Dim vertCoordsZ(((vertCoords.Length) / 3) - 1) As Double
Dim w As Integer = 0

For x_ = 0 To vertCoords.Count - 1
vertCoordsX(w) = vertCoords(x_)
x_ = x_ + 2
w = w + 1
Next
Array.Sort(vertCoordsX)


w = 0
For y = 1 To vertCoords.Count - 1
vertCoordsY(w) = vertCoords(y)
y = y + 2
w = w + 1
Next
Array.Sort(vertCoordsY)

w = 0
For z = 2 To vertCoords.Count - 1
vertCoordsZ(w) = vertCoords(z)
z = z + 2
w = w + 1
Next
Array.Sort(vertCoordsZ)

smallPnt.X = vertCoordsX(0)
smallPnt.Y = vertCoordsY(0)
smallPnt.Z = vertCoordsZ(0)

largePnt.X = vertCoordsX(UBound(vertCoordsX))
largePnt.Y = vertCoordsY(UBound(vertCoordsY))
largePnt.Z = vertCoordsZ(UBound(vertCoordsZ))


' Create and return a Box as the result.
calculateTightBoundingBox = tg.CreateBox()
calculateTightBoundingBox.MinPoint = smallPnt
calculateTightBoundingBox.MaxPoint = largePnt

 

End Function

  

0 Likes