create Library Material

Hello Everyone,

Below is the journal for adding new material to the NX library material.

This journal creates new material by receiving user inputs for,
1). Material Name, 2). Material type and 3). Density

New material will be created as local material and then exported to the Material library automatically by the Macro and the local material will be deleted.

This macro is created only to add density as our requirements are only to find the weight and update in the parts list.

' NX 12.0.1.7
' Journal created by Balaji_Sampath03 on Fri Apr 17 11:07:51 2020 India Standard Time
'

Imports System
Imports NXOpen
Imports System.Collections.Generic
Imports NXOpen.UF
Imports NXOpenUI

Module NXJournal

Sub Main (ByVal args() As String)
Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
Dim workPart As NXOpen.Part = theSession.Parts.Work
Dim displayPart As NXOpen.Part = theSession.Parts.Display

' ----------------------------------------------
' Menu: Tools->Materials->Manage Materials ...
' ----------------------------------------------

Dim physicalMaterialListBuilder1 As NXOpen.PhysMat.PhysicalMaterialListBuilder = Nothing
physicalMaterialListBuilder1 = workPart.MaterialManager.PhysicalMaterials.CreateListBlockBuilder()

Dim physicalMaterialBuilder1 As NXOpen.PhysicalMaterialBuilder = Nothing
physicalMaterialBuilder1 = workPart.MaterialManager.PhysicalMaterials.CreatePhysicalMaterialBuilder(NXOpen.PhysicalMaterial.Type.Isotropic)

Dim propertyTable1 As NXOpen.CAE.PropertyTable = Nothing
propertyTable1 = physicalMaterialBuilder1.ItemPropertyTable

' ----------------------------------------------
' Dialog Begin Isotropic Material
' ----------------------------------------------

Dim MaterialToCreate As String = Nothing
Dim MaterialType As String = Nothing
Dim DensityToAdd As Integer = Nothing

'create new form object
Dim myForm As New AddLibMatl
'set form object properties (current part attribute title and value)
myForm.MaterialToCreate = MaterialToCreate
myForm.MaterialType = MaterialType
myForm.DensityToAdd = DensityToAdd
'display our form
myForm.ShowDialog()

If myForm.Canceled Then
'user pressed cancel, exit journal
Return
Else
'user pressed OK, assign value from form to part attribute
MaterialToCreate = myForm.MaterialToCreate
MaterialType = myForm.MaterialType
DensityToAdd = myForm.DensityToAdd
End If

'Create Material from Form Input
physicalMaterialBuilder1.ItemName = MaterialToCreate

Dim propertyTable2 As NXOpen.CAE.PropertyTable = Nothing
propertyTable2 = physicalMaterialBuilder1.ItemPropertyTable

'Select MaterialType from Form Input
propertyTable2.SetStringPropertyValue("Category", MaterialType)

Dim scalarFieldWrapper1 As NXOpen.Fields.ScalarFieldWrapper = Nothing
scalarFieldWrapper1 = propertyTable2.GetScalarFieldWrapperPropertyValue("MassDensity")

Dim expression1 As NXOpen.Expression = Nothing
expression1 = scalarFieldWrapper1.GetExpression()

Dim unit1 As NXOpen.Unit = CType(workPart.UnitCollection.FindObject("KilogramPerCubicMeter"), NXOpen.Unit)

'Assign Density from FORM Input
workPart.Expressions.EditWithUnits(expression1, unit1, DensityToAdd)

Dim nXObject1 As NXOpen.NXObject = Nothing
nXObject1 = physicalMaterialBuilder1.Commit()

physicalMaterialBuilder1.Destroy()
physicalMaterialListBuilder1.Destroy()

' ----------------------------------------------
' Menu: Tools->Materials->Manage Library Materials...
' ----------------------------------------------

Dim physicalMaterialListBuilder2 As NXOpen.PhysMat.PhysicalMaterialListBuilder = Nothing
physicalMaterialListBuilder2 = workPart.MaterialManager.PhysicalMaterials.CreateListBlockBuilder()

Dim physicalMaterialLibMgrBuilder1 As NXOpen.PhysMat.PhysicalMaterialLibMgrBuilder = Nothing
physicalMaterialLibMgrBuilder1 = workPart.MaterialManager.PhysicalMaterials.CreateMaterialLibmgrBuilder()

' ----------------------------------------------
' Dialog Begin Export Material(s) To Library
' ----------------------------------------------

Dim pcmatlnames1(0) As String
pcmatlnames1(0) = MaterialToCreate
Dim pclibnames1(0) As String
pclibnames1(0) = ""
Dim bupdatelibraryreference1(0) As Boolean
bupdatelibraryreference1(0) = True
workPart.MaterialManager.PhysicalMaterials.ExportMaterialsToLibrary(pcmatlnames1, pclibnames1, "D:\Macro\FINAL\physicalmateriallibrary.xml", bupdatelibraryreference1)

physicalMaterialLibMgrBuilder1.Destroy()
physicalMaterialListBuilder2.Destroy()

' ----------------------------------------------
' Menu: Tools->Materials->Manage Materials ...
' ----------------------------------------------

Dim physicalMaterialListBuilder3 As NXOpen.PhysMat.PhysicalMaterialListBuilder = Nothing
physicalMaterialListBuilder3 = workPart.MaterialManager.PhysicalMaterials.CreateListBlockBuilder()

Dim physicalMaterial1 As NXOpen.PhysicalMaterial = CType(nXObject1, NXOpen.PhysicalMaterial)
physicalMaterial1.Delete()
physicalMaterialListBuilder3.Destroy()

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
End Module

Public Class AddLibMatl

Private _Material As String
Private _MaterialType As String
Private _Density As Integer

Public Property MaterialToCreate() As String
Get
Return _Material
End Get
Set(ByVal value As String)
_Material= value
End Set
End Property

Public Property MaterialType() As String
Get
Return _MaterialType
End Get
Set(ByVal value As String)
_MaterialType = value
End Set
End Property

Public Property DensityToAdd() As Integer
Get
Return _Density
End Get
Set(ByVal value As Integer)
_Density = value
End Set
End Property

Private _canceled As Boolean = False
Public ReadOnly Property Canceled() As Boolean
Get
Return _canceled
End Get
End Property

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = _Material
ComboBox1.Text = _MaterialType
TextBox2.Text = _Density
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ButtonOK.Click
_Material = TextBox1.Text.ToUpper
_MaterialType = ComboBox1.Text.ToUpper
_Density = TextBox2.Text.ToUpper
Me.Close()
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles ButtonCancel.Click
_canceled = True
Me.Close()
End Sub

End Class

_
Partial Class AddLibMatl
Inherits System.Windows.Forms.Form

'Form overrides dispose to clean up the component list.
_
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

'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.
_
Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label()
Me.Label2 = New System.Windows.Forms.Label()
Me.Label3 = New System.Windows.Forms.Label()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.ComboBox1 = New System.Windows.Forms.ComboBox()
Me.TextBox2 = New System.Windows.Forms.TextBox()
Me.ButtonOK = New System.Windows.Forms.Button()
Me.ButtonCancel = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label1.Location = New System.Drawing.Point(12, 20)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(58, 17)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Material"
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label2.Location = New System.Drawing.Point(12, 50)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(94, 17)
Me.Label2.TabIndex = 1
Me.Label2.Text = "Material Type"
'
'Label3
'
Me.Label3.AutoSize = True
Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label3.Location = New System.Drawing.Point(12, 81)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(55, 17)
Me.Label3.TabIndex = 2
Me.Label3.Text = "Density"
'
'TextBox1
'
Me.TextBox1.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.TextBox1.Location = New System.Drawing.Point(136, 16)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(100, 23)
Me.TextBox1.TabIndex = 3
'
'ComboBox1
'
Me.ComboBox1.AllowDrop = True
Me.ComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.ComboBox1.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.ComboBox1.FormattingEnabled = True
Me.ComboBox1.Items.AddRange(New Object() {"Select Material Type:", "Metal", "Plastic", "Others"})
Me.ComboBox1.Location = New System.Drawing.Point(136, 45)
Me.ComboBox1.Name = "ComboBox1"
Me.ComboBox1.Size = New System.Drawing.Size(121, 24)
Me.ComboBox1.TabIndex = 4
'
'TextBox2
'
Me.TextBox2.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.TextBox2.Location = New System.Drawing.Point(136, 81)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(100, 23)
Me.TextBox2.TabIndex = 5
'
'ButtonOK
'
Me.ButtonOK.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.ButtonOK.Location = New System.Drawing.Point(51, 111)
Me.ButtonOK.Name = "ButtonOK"
Me.ButtonOK.Size = New System.Drawing.Size(42, 23)
Me.ButtonOK.TabIndex = 6
Me.ButtonOK.Text = "OK"
Me.ButtonOK.UseVisualStyleBackColor = True
'
'ButtonCancel
'
Me.ButtonCancel.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.ButtonCancel.Location = New System.Drawing.Point(136, 111)
Me.ButtonCancel.Name = "ButtonCancel"
Me.ButtonCancel.Size = New System.Drawing.Size(75, 23)
Me.ButtonCancel.TabIndex = 7
Me.ButtonCancel.Text = "CANCEL"
Me.ButtonCancel.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(275, 147)
Me.Controls.Add(Me.ButtonCancel)
Me.Controls.Add(Me.ButtonOK)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.ComboBox1)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Name = "Form1"
Me.Text = "Create Material"
Me.TransparencyKey = System.Drawing.Color.FromArgb(CType(CType(128, Byte), Integer), CType(CType(255, Byte), Integer), CType(CType(255, Byte), Integer))
Me.ResumeLayout(False)
Me.PerformLayout()

End Sub

Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents ButtonOK As System.Windows.Forms.Button
Friend WithEvents ButtonCancel As System.Windows.Forms.Button
End Class

Thanks, B

Thanks for sharing your code!