Hi! I’ve cleaned up the code. Now even the variables are in English, so it’s easier to understand.
The height is calculated automatically based on the selected face (which now must be the bottom of the fluid volume, not the top anymore).
Once the calculation is done, the code deletes the temporary features so it doesn’t clutter the model tree if they’re not needed.
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
' === THRESHOLD SETUP (3 inches ≈ 7.62 cm) ===
Dim threshold_cm As Double = 7.62
' === BASE REFERENCES ===
Dim oDoc As PartDocument = ThisDoc.Document
Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition
Dim oBody As SurfaceBody = oDef.SurfaceBodies.Item(1)
Dim oRange As Box = oBody.RangeBox
' === SELECT BASE FACE ===
Dim baseFace As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select the reference face (bottom of fluid)")
Dim basePlane As WorkPlane = oDef.WorkPlanes.AddByPlaneAndOffset(baseFace, 0)
basePlane.Visible = False
' === NORMAL VECTOR OF FACE ===
Dim faceEval As SurfaceEvaluator = baseFace.Evaluator
Dim origin As Point = baseFace.PointOnFace
Dim points() As Double = {origin.X, origin.Y, origin.Z}
Dim normal(2) As Double
faceEval.GetNormalAtPoint(points, normal)
Dim normalVec As Vector = oTG.CreateVector(normal(0), normal(1), normal(2))
' === CALCULATE MAX HEIGHT ALONG NORMAL ===
Dim hMax_cm As Double = 0
For Each corner As Point In {oRange.MinPoint, oRange.MaxPoint, _
oTG.CreatePoint(oRange.MinPoint.X, oRange.MinPoint.Y, oRange.MaxPoint.Z), _
oTG.CreatePoint(oRange.MinPoint.X, oRange.MaxPoint.Y, oRange.MinPoint.Z), _
oTG.CreatePoint(oRange.MaxPoint.X, oRange.MinPoint.Y, oRange.MinPoint.Z), _
oTG.CreatePoint(oRange.MaxPoint.X, oRange.MaxPoint.Y, oRange.MinPoint.Z), _
oTG.CreatePoint(oRange.MaxPoint.X, oRange.MinPoint.Y, oRange.MaxPoint.Z), _
oTG.CreatePoint(oRange.MinPoint.X, oRange.MaxPoint.Y, oRange.MaxPoint.Z)}
Dim vec As Vector = corner.VectorTo(origin)
Dim projLen As Double = vec.DotProduct(normalVec) ' cm
If projLen > hMax_cm Then
hMax_cm = projLen
End If
Next
' === INITIALIZATION ===
Dim currentHeight As Double = 0
' === TXT FILE SETUP ===
Dim folderPath As String = ThisDoc.Path
Dim filePath As String = folderPath & "\VolumesEvery3Inches.txt"
Dim sw As System.IO.StreamWriter = System.IO.File.CreateText(filePath)
sw.WriteLine("Height (cm) | Volume (cm³) | Volume (in³) | Gallons")
' === LOOP ===
While currentHeight <= hMax_cm
' Create a temporary work plane at the current height
Dim workPlane As WorkPlane = oDef.WorkPlanes.AddByPlaneAndOffset(basePlane, -currentHeight)
workPlane.Visible = False
' Split body
Dim newSplit As SplitFeature = oDef.Features.SplitFeatures.TrimSolid(workplane, oBody, True)
Dim newBody As SurfaceBody = newSplit.SurfaceBodies.Item(1)
Dim volume_cm3 As Double = newBody.Volume(0.01)
' Convert
Dim volume_in3 As Double = volume_cm3 * 0.0610237
Dim volume_gallons As Double = volume_cm3 * 0.000264172
' Write
sw.WriteLine(Math.Round(currentHeight, 2) & " | " & _
Math.Round(volume_cm3, 2) & " | " & _
Math.Round(volume_in3, 2) & " | " & _
Math.Round(volume_gallons, 2))
newSplit.Delete
workplane.Delete
currentHeight += threshold_cm
End While
basePlane.Delete
sw.Close()
' === DONE ===
MessageBox.Show("Calculation completed!" & vbCrLf & _
"File saved to:" & vbCrLf & filePath, "Done")