GetPartBox Vba Correct --> VB.Net 2010 doesn't works
roberto gennari Feb 11, 2016 2:33 AMHallo,
I have a macro that works very well in vba.
The macro creates the bounding box of the part using a tessellation.
Now I wanted to bring the macro in VB.Net 2010, but no longer works correctly.
In the Immediate window returns to me:
PartBox =
( 724.168060463243, 339.974691869806,-20) mm
( 895.47730198881, 550.582432487882, 66.0561904007547) mm
Vol = 3104,82579461169 cm^3
TessBox =
( +Infinito, +Infinito, +Infinito) mm
(-Infinito,-Infinito,-Infinito) mm
Vol = -Infinito cm^3
This is the code in VB.net(the same of the macro) and annexed place the macro .swp:
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swcommands
Imports SolidWorks.Interop.swconst
Public
Enum swBodyType_e
swSolidBody = 0
swSheetBody = 1
swWireBody = 2
swMinimumBody = 3
swGeneralBody = 4
swEmptyBody = 5
End
Enum
Public Class Form1
Function GetMax(ByVal Val1 As Double, ByVal Val2 As Double, ByVal Val3 As Double, ByVal Val4 As Double) As Double
' finds maximum of four values
GetMax = Val1
If Val2 > GetMax Then
GetMax = Val2
End If
If Val3 > GetMax Then
GetMax = Val3
End If
If Val4 > GetMax Then
GetMax = Val4
End If
End Function
Function GetMin(ByVal Val1 As Double, ByVal Val2 As Double, ByVal Val3 As Double, ByVal Val4 As Double) As Double
' finds minimum of four values
GetMin = Val1
If Val2 < GetMin Then
GetMin = Val2
End If
If Val3 < GetMin Then
GetMin = Val3
End If
If Val4 < GetMin Then
GetMin = Val4
End If
End Function
Sub ProcessTessTriangles( ByVal vTessTriangles As Object, ByVal X_max As Double, ByVal X_min As Double, ByVal Y_max As Double,ByVal Y_min As Double, ByVal
Z_max As Double,
_ ByVal
Z_min As Double)
Dim i As Long
For i =0 To UBound(vTessTriangles) / (1 * 9) - 1
X_max = GetMax((vTessTriangles(9 * i + 0)),(vTessTriangles(9 * i + 3)), (vTessTriangles(9 * i + 6)), X_max)
X_min = GetMin((vTessTriangles(9 * i + 0)), (vTessTriangles(9 * i + 3)),(vTessTriangles(9 * i + 6)), X_min)
Y_max= GetMax((vTessTriangles(9 * i + 1)), (vTessTriangles(9 * i + 4)),vTessTriangles(9 * i + 7)), Y_max)
Y_min = GetMin((vTessTriangles(9 * i + 1)), (vTessTriangles(9 * i + 4)),(vTessTriangles(9 * i + 7)), Y_min)
Z_max = GetMax((vTessTriangles(9 * i + 2)), (vTessTriangles(9 * i + 5)),(vTessTriangles(9 * i + 8)), Z_max)
Z_min = GetMin((vTessTriangles(9 * i + 2)), (vTessTriangles(9 * i + 5)),(vTessTriangles(9 * i + 8)), Z_min)
Next i
End Sub
Sub ProcessBodies ( ByVal vBodies As Object,ByVal X_max As Double,ByVal X_min As Double,ByVal Y_max As Double, ByVal Y_min As Double, _
ByVal Z_max As Double,ByVal Z_min As Double)
Dim swBody As Body2
Dim swFace As Face2
Dim vTessTriangles As Object
Dim i As Long
' probably empty if no reference surfaces
If IsNothing(vBodies) Then Exit Sub
For i = 0 To UBound(vBodies)
swBody = vBodies(i)
swFace = swBody.GetFirstFace
While Not swFace Is Nothing
vTessTriangles = swFace.GetTessTriangles(True)
ProcessTessTriangles(vTessTriangles, X_max, X_min, Y_max, Y_min, Z_max, Z_min)
swFace = swFace.GetNextFace
End While
Next i
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Const MaxDouble As Double = 1.79769313486231E+308
Const MinDouble As Double = -1.79769313486231E+308
Dim swApp As SldWorks
Dim swModel As ModelDoc2
Dim swPart As PartDoc
Dim vBodies As Object
Dim vBoundBox As Object
Dim X_max As Double
Dim X_min As Double
Dim Y_max As Double
Dim Y_min As Double
Dim Z_max As Double
Dim Z_min As Double
Dim swSketchPt(8) As SketchPoint
Dim swSketchSeg(12) As SketchSegment
Dim i As Long
swApp = CreateObject("SldWorks.Application")
swModel = swApp.ActiveDoc
swPart = swModel
' initialiseto large/small values
X_max = MinDouble
X_min = MaxDouble
Y_max = MinDouble
Y_min = MaxDouble
Z_max = MinDouble
Z_min = MaxDouble
' solid body
vBodies = swPart.GetBodies(swBodyType_e.swSolidBody)
ProcessBodies(vBodies, X_max, X_min, Y_max, Y_min, Z_max, Z_min)
' reference surfaces
vBodies = swPart.GetBodies(swBodyType_e.swSheetBody)
ProcessBodies(vBodies, X_max, X_min, Y_max, Y_min, Z_max, Z_min)
' approximate bounding box
vBoundBox = swPart.GetPartBox(True)
Debug.Print("Tessellation Quality = " + Str(swModel.GetTessellationQuality))
Debug.Print("")
Debug.Print("PartBox = ")
Debug.Print(" (" + _
Str(vBoundBox(0) *1000.0#) + "," + _
Str(vBoundBox(1) *1000.0#) + "," + _
Str(vBoundBox(2) *1000.0#) + ") mm")
Debug.Print(" (" + Str(vBoundBox(3) *1000.0#) + "," + _
Str(vBoundBox(4) * 1000.0#) + "," + _
Str(vBoundBox(5) *1000.0#) + ") mm")
Debug.Print(" Vol = " & ((vBoundBox(3) - vBoundBox(0)) * (vBoundBox(4) - vBoundBox(1)) * (vBoundBox(5) - vBoundBox(2))) * 1000000.0# & " cm^3")
Debug.Print("")
Debug.Print("TessBox = ")
Debug.Print(" ("+ Str(X_min * 1000) + "," + Str(Y_min * 1000) + "," + Str(Z_min * 1000) + ") mm")
Debug.Print(" ("+ Str(X_max * 1000) + "," + Str(Y_max * 1000) + "," + Str(Z_max * 1000) + ") mm")
Debug.Print(" Vol = " & ((X_max - X_min) * (Y_max - Y_min) * (Z_max - Z_min)) * 1000000.0# & " cm^3")
Debug.Print("")
swModel.Insert3DSketch2(True)
swModel.SetAddToDB(True)
' draw points at each corner of bounding box
swSketchPt(0) =swModel.CreatePoint2(X_min, Y_min, Z_min)
swSketchPt(1) = swModel.CreatePoint2(X_min, Y_min, Z_max)
swSketchPt(2) =swModel.CreatePoint2(X_min, Y_max, Z_min)
swSketchPt(3) =swModel.CreatePoint2(X_min, Y_max, Z_max)
swSketchPt(4) =swModel.CreatePoint2(X_max, Y_min, Z_min)
swSketchPt(5) =swModel.CreatePoint2(X_max, Y_min, Z_max)
swSketchPt(6) =swModel.CreatePoint2(X_max, Y_max, Z_min)
swSketchPt(7) =swModel.CreatePoint2(X_max, Y_max, Z_max)
' now draw bounding box
swSketchSeg(0) =swModel.CreateLine2(X_min, Y_min, Z_min, X_max, Y_min, Z_min)
swSketchSeg(1) =swModel.CreateLine2(X_max, Y_min, Z_min, X_max, Y_min, Z_max)
swSketchSeg(2) =swModel.CreateLine2(X_max, Y_min, Z_max, X_min, Y_min, Z_max)
swSketchSeg(3) =swModel.CreateLine2(X_min, Y_min, Z_max, X_min, Y_min, Z_min)
swSketchSeg(4) =swModel.CreateLine2(X_min, Y_min, Z_min, X_min, Y_max, Z_min)
swSketchSeg(5) =swModel.CreateLine2(X_min, Y_min, Z_max, X_min, Y_max, Z_max)
swSketchSeg(6) =swModel.CreateLine2(X_max, Y_min, Z_min, X_max, Y_max, Z_min)
swSketchSeg(7) =swModel.CreateLine2(X_max, Y_min, Z_max, X_max, Y_max, Z_max)
swSketchSeg(8) =swModel.CreateLine2(X_min, Y_max, Z_min, X_max, Y_max, Z_min)
swSketchSeg(9) =swModel.CreateLine2(X_max, Y_max, Z_min, X_max, Y_max, Z_max)
swSketchSeg(10) =swModel.CreateLine2(X_max, Y_max, Z_max, X_min, Y_max, Z_max)
swSketchSeg(11) =swModel.CreateLine2(X_min, Y_max, Z_max, X_min, Y_max, Z_min)
swModel.SetAddToDB(False)
swModel.Insert3DSketch2(True)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
-
BoundingBox on XY.swp.zip 14.1 KB