'=============================================================================
' DutchAero
' Patrick Delisse - 2008-05-11 - <a href="mailto:patrick.delisse@dutchaero.nl"> </a>
' rev 2 - 2012-05-16 - Added the option to add extra material on all sides
'=============================================================================
Option Strict Off
Imports NXOpen
Imports NXOpen.UF
Imports NXOpenUI
Imports NXOpen.Utilities
Imports System
Imports System.Math
Module BoxVolume
Dim s As Session = Session.GetSession()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim workPart As Part = s.Parts.Work
Dim ui As UI = ui.GetUI()
Dim body As NXObject = Nothing
Dim markId1 As Session.UndoMarkId
Dim nullFeatures_Feature As Features.Feature = Nothing
Dim MyForm As New xxx
Sub Main()
markId1 = s.SetUndoMark(Session.MarkVisibility.Visible, "Start")
Dim message As String = "Select a Block"
Dim title1 As String = "Selection"
Dim min_corner() As Double = {0.0, 0.0, 0.0}
Dim directions(,) As Double = {{1.0, 0.0, 0.0}, _
{0.0, 1.0, 0.0}, _
{0.0, 0.0, 1.0}}
Dim distances() As Double = {0.0, 0.0, 0.0}
Dim csysTag As NXOpen.Tag = Tag.Null
Dim workviewtag As NXOpen.Tag = Tag.Null
start1:
SelectABody("Select Solid", body)
On Error GoTo EndingIt
If body.Tag <> Tag.Null Then
ufs.Modl.AskBoundingBoxExact(body.Tag, csysTag, min_corner, directions, distances)
ufs.Disp.SetHighlight(body.Tag, 0)
DisplayBoundingBox(min_corner, directions, distances)
End If
EndingIt:
End Sub
Public Sub SelectABody(ByVal prompt As String, ByRef obj As NXObject)
Dim mask(0) As Selection.MaskTriple
With mask(0)
.Type = UFConstants.UF_solid_type
.Subtype = UFConstants.UF_solid_body_subtype
.SolidBodySubtype = 0
End With
Dim cursor As Point3d = Nothing
ui.SelectionManager.SelectTaggedObject(prompt, "Select a body", _
Selection.SelectionScope.AnyInAssembly, _
Selection.SelectionAction.ClearAndEnableSpecific, _
False, False, mask, obj, cursor)
End Sub
Private Sub DisplayBoundingBox(ByRef min_corner As Double(), ByRef directions As Double(,), _
ByRef distances As Double())
Dim partUnits As Integer = 1
ufs.Part.AskUnits(workPart.Tag, partUnits)
Dim Unit As String
Dim OtherUnit As String
Dim Factor As Decimal
Dim xp, xm, yp, ym, zp, zm As String
If partUnits = 1 Then
Unit = "mm"
Factor = 1 / 25.4
OtherUnit = "Inch"
Else
Unit = "inch"
Factor = 25.4
OtherUnit = "mm"
End If
Try
If MyForm.ShowDialog = Windows.Forms.DialogResult.OK Then
xp = Abs(CDec(Replace(MyForm.TextBox1.Text, ".", ",")))
xm = Abs(CDec(Replace(MyForm.TextBox2.Text, ".", ",")))
yp = Abs(CDec(Replace(MyForm.TextBox3.Text, ".", ",")))
ym = Abs(CDec(Replace(MyForm.TextBox4.Text, ".", ",")))
zp = Abs(CDec(Replace(MyForm.TextBox5.Text, ".", ",")))
zm = Abs(CDec(Replace(MyForm.TextBox6.Text, ".", ",")))
Else
xp = 0
yp = 0
zp = 0
xm = 0
ym = 0
zm = 0
End If
MyForm.Close()
MyForm.Dispose()
MyForm = Nothing
Dim blockFeatureBuilder1 As Features.BlockFeatureBuilder
blockFeatureBuilder1 = workPart.Features.CreateBlockFeatureBuilder(nullFeatures_Feature)
blockFeatureBuilder1.BooleanOption.Type = GeometricUtilities.BooleanOperation.BooleanType.Create
Dim targetBodies1(0) As Body
Dim nullBody As Body = Nothing
targetBodies1(0) = nullBody
blockFeatureBuilder1.BooleanOption.SetTargetBodies(targetBodies1)
s.SetUndoMarkName(markId1, "Block Dialog")
Dim markId2 As Session.UndoMarkId
markId2 = s.SetUndoMark(Session.MarkVisibility.Invisible, "Block")
blockFeatureBuilder1.Type = Features.BlockFeatureBuilder.Types.OriginAndEdgeLengths
Dim point1 As Point
point1 = blockFeatureBuilder1.OriginPoint
blockFeatureBuilder1.OriginPoint = point1
'As the block only accepts dimensions as strings we need to convert our number to a string and replace decimal comma by decimal point.
Dim Lengte As String
Dim Breedte As String
Dim Hoogte As String
Lengte = Replace(CStr(distances(0) + xp + xm), ",", ".")
Breedte = Replace(CStr(distances(1) + yp + ym), ",", ".")
Hoogte = Replace(CStr(distances(2) + zp + zm), ",", ".")
Dim originPoint1 As Point3d = New Point3d(min_corner(0) - xm, min_corner(1) - ym, min_corner(2) - zm)
blockFeatureBuilder1.SetOriginAndLengths(originPoint1, Lengte, Breedte, Hoogte)
blockFeatureBuilder1.SetBooleanOperationAndTarget(Features.Feature.BooleanType.Create, nullBody)
Dim feature1 As Features.Feature
feature1 = blockFeatureBuilder1.CommitFeature()
Dim displayModification1 As DisplayModification
displayModification1 = s.DisplayManager.NewDisplayModification()
displayModification1.ApplyToAllFaces = True
displayModification1.NewTranslucency = 70
Dim objects1(0) As DisplayableObject
Dim body1 As Body = nullBody
Dim p As Integer = 0
Dim i As Integer = 0
Do While i = 0
Try
body1 = CType(workPart.Bodies.FindObject("BLOCK(" & p & ")"), Body)
i = 1
Catch ex As Exception
p = p + 1
End Try
Loop
objects1(0) = body1
displayModification1.Apply(objects1)
displayModification1.Dispose()
Dim cornX, cornY, cornZ As Decimal
Dim lX, lY, lZ As Decimal
cornX = min_corner(0) - xm
cornY = min_corner(1) - ym
cornZ = min_corner(2) - zm
lX = distances(0) + xm + xp
lY = distances(1) + ym + yp
lZ = distances(2) + zm + zp
s.ListingWindow.Open()
s.ListingWindow.WriteLine("=================================================================")
s.ListingWindow.WriteLine("* Part boundaries (Box) *")
s.ListingWindow.WriteLine("=================================================================")
s.ListingWindow.WriteLine(" ")
s.ListingWindow.WriteLine(" Box Origin in X (" & Unit & ") = " & Math.Round(cornX, 3, MidpointRounding.AwayFromZero))
s.ListingWindow.WriteLine(" Box Origin in Y (" & Unit & ") = " & Math.Round(cornY, 3, MidpointRounding.AwayFromZero))
s.ListingWindow.WriteLine(" Box Origin in Z (" & Unit & ") = " & Math.Round(cornZ, 3, MidpointRounding.AwayFromZero))
s.ListingWindow.WriteLine(" ")
s.ListingWindow.WriteLine(" Box Length (X) (" & Unit & ") = " & Math.Round(lX, 2, MidpointRounding.AwayFromZero))
s.ListingWindow.WriteLine(" Box Width (Y) (" & Unit & ") = " & Math.Round(lY, 2, MidpointRounding.AwayFromZero))
s.ListingWindow.WriteLine(" Box Height (Z) (" & Unit & ") = " & Math.Round(lZ, 2, MidpointRounding.AwayFromZero))
s.ListingWindow.WriteLine(" ")
s.ListingWindow.WriteLine(" Box Length (X) (" & OtherUnit & ") = " & Math.Round(lX * Factor, 3, MidpointRounding.AwayFromZero))
s.ListingWindow.WriteLine(" Box Width (Y) (" & OtherUnit & ") = " & Math.Round(lY * Factor, 3, MidpointRounding.AwayFromZero))
s.ListingWindow.WriteLine(" Box Height (Z) (" & OtherUnit & ") = " & Math.Round(lZ * Factor, 3, MidpointRounding.AwayFromZero))
s.ListingWindow.WriteLine(" ")
s.ListingWindow.WriteLine(" Box Volume (" & Unit & "^3) = " & Math.Round(lX * lY * lZ, 3, MidpointRounding.AwayFromZero))
s.ListingWindow.WriteLine(" (Cm^3) = " & Math.Round(lX * lY * lZ / 1000, 2, MidpointRounding.AwayFromZero))
s.ListingWindow.WriteLine(" ")
s.ListingWindow.WriteLine(" ")
If partUnits = 1 Then
s.ListingWindow.WriteLine("=================================================================")
s.ListingWindow.WriteLine(" Approximate Mass")
s.ListingWindow.WriteLine(" ---------------------------------------------------------------")
s.ListingWindow.WriteLine(" Aluminium (Kg) = " & Math.Round(lX * lY * lZ * 0.00000271, 3, MidpointRounding.AwayFromZero))
s.ListingWindow.WriteLine(" Steel (Kg) = " & Math.Round(lX * lY * lZ * 0.00000785, 3, MidpointRounding.AwayFromZero))
s.ListingWindow.WriteLine(" Titanium (Kg) = " & Math.Round(lX * lY * lZ * 0.0000045, 3, MidpointRounding.AwayFromZero))
s.ListingWindow.WriteLine(" Inconel (Kg) = " & Math.Round(lX * lY * lZ * 0.00000819, 3, MidpointRounding.AwayFromZero))
End If
s.ListingWindow.WriteLine("-----------------------------------------------------------------")
s.ListingWindow.WriteLine(" ")
s.DeleteUndoMark(markId2, Nothing)
s.SetUndoMarkName(markId1, "Block")
blockFeatureBuilder1.Destroy()
workPart.FacetedBodies.DeleteTemporaryFacesAndEdges()
Catch ex As Exception
MsgBox("Check input!" & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Critical, "Cannot Calculate Bounding Box")
End Try
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
'Unloads the image immediately after execution within NX
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
End Function
Public Function SetSolidDisplay(ByVal Kleur As Integer, ByVal Nr As Integer) As Integer
On Error Resume Next
End Function
End Module
Partial Class xxx
Inherits System.Windows.Forms.Form
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
Friend WithEvents TextBox4 As System.Windows.Forms.TextBox
Friend WithEvents TextBox5 As System.Windows.Forms.TextBox
Friend WithEvents TextBox6 As System.Windows.Forms.TextBox
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents Label6 As System.Windows.Forms.Label
Friend WithEvents Label7 As System.Windows.Forms.Label
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button()
Me.Button2 = New System.Windows.Forms.Button()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.TextBox2 = New System.Windows.Forms.TextBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.Label2 = New System.Windows.Forms.Label()
Me.TextBox3 = New System.Windows.Forms.TextBox()
Me.TextBox4 = New System.Windows.Forms.TextBox()
Me.TextBox5 = New System.Windows.Forms.TextBox()
Me.TextBox6 = New System.Windows.Forms.TextBox()
Me.Label3 = New System.Windows.Forms.Label()
Me.Label4 = New System.Windows.Forms.Label()
Me.Label5 = New System.Windows.Forms.Label()
Me.Label6 = New System.Windows.Forms.Label()
Me.Label7 = New System.Windows.Forms.Label()
Me.SuspendLayout()
''Button1
'
Me.Button1.DialogResult = System.Windows.Forms.DialogResult.OK
Me.Button1.Location = New System.Drawing.Point(12, 168)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(57, 23)
Me.Button1.TabIndex = 6
Me.Button1.Text = "Ok"
Me.Button1.UseVisualStyleBackColor = True
''Button2
'
Me.Button2.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.Button2.Location = New System.Drawing.Point(178, 168)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(57, 23)
Me.Button2.TabIndex = 7
Me.Button2.Text = "Cancel"
Me.Button2.UseVisualStyleBackColor = True
''TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(52, 62)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(63, 20)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = "0"
''TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(152, 62)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(63, 20)
Me.TextBox2.TabIndex = 1
Me.TextBox2.Text = "0"
''Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(26, 65)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(20, 13)
Me.Label1.TabIndex = 6
Me.Label1.Text = "X+"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight
''Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(129, 65)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(17, 13)
Me.Label2.TabIndex = 7
Me.Label2.Text = "X-"
Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight
''TextBox3
'
Me.TextBox3.Location = New System.Drawing.Point(52, 88)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.Size = New System.Drawing.Size(63, 20)
Me.TextBox3.TabIndex = 2
Me.TextBox3.Text = "0"
''TextBox4
'
Me.TextBox4.Location = New System.Drawing.Point(152, 88)
Me.TextBox4.Name = "TextBox4"
Me.TextBox4.Size = New System.Drawing.Size(63, 20)
Me.TextBox4.TabIndex = 3
Me.TextBox4.Text = "0"
''TextBox5
'
Me.TextBox5.Location = New System.Drawing.Point(52, 114)
Me.TextBox5.Name = "TextBox5"
Me.TextBox5.Size = New System.Drawing.Size(63, 20)
Me.TextBox5.TabIndex = 4
Me.TextBox5.Text = "0"
''TextBox6
'
Me.TextBox6.Location = New System.Drawing.Point(152, 114)
Me.TextBox6.Name = "TextBox6"
Me.TextBox6.Size = New System.Drawing.Size(63, 20)
Me.TextBox6.TabIndex = 5
Me.TextBox6.Text = "0"
''Label3
'
Me.Label3.AutoSize = True
Me.Label3.Location = New System.Drawing.Point(26, 91)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(20, 13)
Me.Label3.TabIndex = 12
Me.Label3.Text = "Y+"
Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight
''Label4
'
Me.Label4.AutoSize = True
Me.Label4.Location = New System.Drawing.Point(26, 117)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(20, 13)
Me.Label4.TabIndex = 13
Me.Label4.Text = "Z+"
Me.Label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight
''Label5
'
Me.Label5.AutoSize = True
Me.Label5.Location = New System.Drawing.Point(129, 91)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(17, 13)
Me.Label5.TabIndex = 14
Me.Label5.Text = "Y-"
Me.Label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight
''Label6
'
Me.Label6.AutoSize = True
Me.Label6.Location = New System.Drawing.Point(129, 117)
Me.Label6.Name = "Label6"
Me.Label6.Size = New System.Drawing.Size(17, 13)
Me.Label6.TabIndex = 15
Me.Label6.Text = "Z-"
Me.Label6.TextAlign = System.Drawing.ContentAlignment.MiddleRight
''Label7
'
Me.Label7.AutoSize = True
Me.Label7.Location = New System.Drawing.Point(26, 27)
Me.Label7.Name = "Label7"
Me.Label7.Size = New System.Drawing.Size(130, 13)
Me.Label7.TabIndex = 16
Me.Label7.Text = "Extra material to be added"
Me.Label7.TextAlign = System.Drawing.ContentAlignment.MiddleRight
''xxx
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center
Me.ClientSize = New System.Drawing.Size(247, 203)
Me.ControlBox = False
Me.Controls.Add(Me.Label7)
Me.Controls.Add(Me.Label6)
Me.Controls.Add(Me.Label5)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.TextBox6)
Me.Controls.Add(Me.TextBox5)
Me.Controls.Add(Me.TextBox4)
Me.Controls.Add(Me.TextBox3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "xxx"
Me.ShowInTaskbar = False
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Bounding Box"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
End Class