Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations IDS on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to set default unit by NXopen

Status
Not open for further replies.

niedzviedz

Mechanical
Apr 1, 2012
307
Hello,

Can someone can help with this question - how to change / set default unit by Nxopen? I found new code in NX 12:

Code:
SetDefaultDataEntryUnits¶

UnitCollection.SetDefaultDataEntryUnits

    Set a choice of default set of units for data entry in the given part.

    Signature SetDefaultDataEntryUnits(defaults)
    Parameters:	defaults (NXOpen.UnitCollectionUnitDefaults) –

    New in version NX12.0.0.

    License requirements: None.

But how to use it?
I have something like this:

Code:
	Dim myMeasure As MeasureManager = theSession.Parts.work.MeasureManager()
	Dim massUnits(7) As Unit
	
	lw.open()
	
	massUnits(0) = theSession.Parts.Display.UnitCollection.GetBase("Area")
	massUnits(1) = theSession.Parts.Display.UnitCollection.GetBase("Volume")
	massUnits(2) = theSession.Parts.Display.UnitCollection.GetBase("Mass")
	massUnits(3) = theSession.Parts.Display.UnitCollection.GetBase("Length")
	massUnits(4) = theSession.Parts.Display.UnitCollection.GetBase("Angle")
	massUnits(5) = theSession.Parts.Display.UnitCollection.GetBase("Force")
	massUnits(6) = theSession.Parts.Display.UnitCollection.GetBase("Temperature")

	mass_ = massUnits(2).symbol
	Length_ = massUnits(3).symbol
	Angle_ = massUnits(4).symbol
	Force_ = massUnits(5).symbol
	tempr_ = massUnits(6).symbol

	lw.writeline(mass_)
	lw.writeline(Length_)	
	lw.writeline(Angle_)
	lw.writeline(Force_)	
	lw.writeline(tempr_)

	massUnits(4) = theSession.Parts.Display.UnitCollection.SetDefaultDataEntryUnits("radian")

But I receive an error, that last sentence doesn't give an output.




With best regards
Michael
 
Replies continue below

Recommended for you

Thanks @Cowski for link. I tried something like this but an error occured:

Code:
	theSession.Parts.Display.UnitCollection.SetDefaultDataEntryUnits("KgMmNDegC")

System.InvalidCastException: Conversion from "KgMmNDegC" to Intiger type is incorect.

I also tried without "" and there was an error that element KgMmNDegC is n ot declared. Placing numbers (0,4,5) nothing changes, I receive:

Code:
kg
mm
degrees
mN
C


With best regards
Michael
 
A string value won't work, you need to use a value from the enumeration.

Code:
theSession.Parts.Display.UnitCollection.SetDefaultDataEntryUnits(UnitCollection.UnitDefaults.KgMmNDegC)

www.nxjournaling.com
 
Thanks @cowski, I will try and write if it helped.

With best regards
Michael
 
Hello,

I tested this command. It changing measure units, but in info I still have old ones.
Below is my code:

Code:
Option Strict Off

Imports System
Imports NXOpen
Imports NXOpen.UF
Imports System.Collections.Generic
Imports System.Windows.Forms

Module NXJournal

	Dim theSession As Session = Session.GetSession()
	Dim theUFSession As UFSession = UFSession.GetUFSession()
	Dim workPart As Part = theSession.Parts.Work

	Public lw As ListingWindow = theSession.ListingWindow

	Public Angle_ As String
	Public Force_ As String

	Public Mass_ As String
	Public Length_ As String
	Public Tempr_ As String

Public Sub Main(ByVal args As String())
 
	Dim myMeasure As MeasureManager = theSession.Parts.work.MeasureManager()
	Dim massUnits(7) As Unit
	
	lw.open()
	
	massUnits(0) = theSession.Parts.Display.UnitCollection.GetBase("Area")
	massUnits(1) = theSession.Parts.Display.UnitCollection.GetBase("Volume")
	massUnits(2) = theSession.Parts.Display.UnitCollection.GetBase("Mass")
	massUnits(3) = theSession.Parts.Display.UnitCollection.GetBase("Length")
	massUnits(4) = theSession.Parts.Display.UnitCollection.GetBase("Angle")
	massUnits(5) = theSession.Parts.Display.UnitCollection.GetBase("Force")
	massUnits(6) = theSession.Parts.Display.UnitCollection.GetBase("Temperature")
	
	'Dim totalVolume As Double = 0
	
	lw.writeline("Area -> " & massUnits(0).Abbreviation)
	lw.writeline("Volume -> " & massUnits(1).Abbreviation)
	lw.writeline("Mass -> " & massUnits(2).Abbreviation)
	lw.writeline("Length -> " & massUnits(3).Abbreviation)
	lw.writeline("Angle -> " & massUnits(4).Abbreviation)
	lw.writeline("Force > " & massUnits(5).Abbreviation)
	lw.writeline("Temperature -> " & massUnits(6).Abbreviation)
	
	mass_ = massUnits(2).symbol
	Length_ = massUnits(3).symbol
	Angle_ = massUnits(4).symbol
	Force_ = massUnits(5).symbol
	tempr_ = massUnits(6).symbol
	
	lw.writeline(mass_)
	lw.writeline(Length_)	
	lw.writeline(Angle_)
	lw.writeline(Force_)	
	lw.writeline(tempr_)

	lw.writeline("")
	
	'theSession.Parts.Display.UnitCollection.SetDefaultDataEntryUnits(UnitCollection.UnitDefaults.KgMmNDegC)
	'theSession.Parts.Display.UnitCollection.SetDefaultObjectInformationUnits(UnitCollection.UnitDefaults.KgMmNDegC) 
	theSession.Parts.Display.UnitCollection.SetDefaultDataEntryUnits(UnitCollection.UnitDefaults.KgMNRadK) 
	theSession.Parts.Display.UnitCollection.SetDefaultObjectInformationUnits(UnitCollection.UnitDefaults.KgMNRadK)
	
	massUnits(0) = theSession.Parts.Display.UnitCollection.GetBase("Area")
	massUnits(1) = theSession.Parts.Display.UnitCollection.GetBase("Volume")
	massUnits(2) = theSession.Parts.Display.UnitCollection.GetBase("Mass")
	massUnits(3) = theSession.Parts.Display.UnitCollection.GetBase("Length")
	massUnits(4) = theSession.Parts.Display.UnitCollection.GetBase("Angle")
	massUnits(5) = theSession.Parts.Display.UnitCollection.GetBase("Force")
	massUnits(6) = theSession.Parts.Display.UnitCollection.GetBase("Temperature")

	mass_ = massUnits(2).symbol
	Length_ = massUnits(3).symbol
	Angle_ = massUnits(4).symbol
	Force_ = massUnits(5).symbol
	tempr_ = massUnits(6).symbol

	lw.writeline(mass_)
	lw.writeline(Length_)	
	lw.writeline(Angle_)
	lw.writeline(Force_)	
	lw.writeline(tempr_)
	
	lw.close()
	
End Sub
 
Public Function GetUnloadOption(ByVal arg As String) As Integer

	Return Session.LibraryUnloadOption.Immediately
		
End Function
 
End Module

With best regards
Michael
 
.GetBase will return the part's base units, the returned results will depend on whether the part is "inch" or "mm". Your code does not convert the part units, so it will always give the same answer, no matter what you set for the "entry" and "informational" units. If you want to see what the entry and/or informational units are set to, try the .GetDefaultDataEntryUnits and/or the .GetDefaultObjectInformationUnits methods.

www.nxjournaling.com
 
Thank @Cowski for Your support. Finally I found some time to finish it. Below is my code, maybe it will be useful to someone:

Code:
Option Strict Off

Imports System
Imports NXOpen
Imports NXOpen.UF
Imports System.Collections.Generic
Imports System.Windows.Forms

Module NXJournal

	Dim theSession As Session = Session.GetSession()
	Dim theUFSession As UFSession = UFSession.GetUFSession()
	Dim workPart As Part = theSession.Parts.Work

	Public lw As ListingWindow = theSession.ListingWindow
	Public Unit_1 As Integer
	Public Unit_2 As Integer

Public Sub Main(ByVal args As String())

	Dim myForm1 As New Form1
 	
	'lw.open()

	'lw.writeline("")

	Unit_1 = theSession.Parts.Display.UnitCollection.GetDefaultDataEntryUnits() 
	Unit_2 = theSession.Parts.Display.UnitCollection.GetDefaultObjectInformationUnits()
		
	myform1.Unit_1 = Unit_1
	myform1.Unit_2 = Unit_2
	
	myForm1.ShowDialog()

	If myForm1.Canceled Then

		'Exit sub	
		Return

	Else

	End If
	
	Unit_1 = myform1.Unit_1
	Unit_2 = myform1.Unit_2

	theSession.Parts.Display.UnitCollection.SetDefaultDataEntryUnits(Unit_1) 
	theSession.Parts.Display.UnitCollection.SetDefaultObjectInformationUnits(Unit_2)


	'lw.close()
	
End Sub
 
Public Function GetUnloadOption(ByVal arg As String) As Integer

	Return Session.LibraryUnloadOption.Immediately
		
End Function
 
End Module

'----------------------------------------------------------------------------------------------------------------
' FORM1
'----------------------------------------------------------------------------------------------------------------

Public Class Form1

	Public Unit_1 As Integer
	Public Unit_2 As Integer
	Public Units as String
	Public X as integer = 0
	
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
	
	For Each c As Control In Controls

		AddHandler c.MouseClick, AddressOf ClickHandler

	Next
	
	if unit_2 = 0 then
	
		Units = "lbm/in/lbf/deg/F"
		
	elseif Unit_2 = 1 then
		
		Units = "lbm/ft/lbf/deg/F"
		
	elseif Unit_2 = 2 then
		
		Units = "g/mm/N/deg/C"	
	
	elseif Unit_2 = 3 then
		
		Units = "g/cm/N/deg/C"
	
	elseif Unit_2 = 4 then
		
		Units = "kg/m/N/rad/K"

	elseif Unit_2 = 5 then
		
		Units = "kg/mm/N/deg/C"
		
	end if
	
	'LbmInLbfDegF 	Usual choice for inch parts.  	   - 0
	'LbmFtLbfDegF 	Not supported for data entry units - 1
	'GMmNDegC 	Not supported for data entry units - 2
	'GCmNDegC 	Not supported for data entry units - 3
	'KgMNRadK 	SI compatibility                   - 4
	'KgMmNDegC 	Usual choice for mm parts	   - 5 	
	
	Label4.Text = Units
	ComboBox3.Visible = false

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

	If ComboBox1.Text = vbNullString Then
	
		_canceled = True
		Me.Close()
		
	End If

	If ComboBox2.Text = vbNullString Then
	
		_canceled = True
		Me.Close()
		
	End If


	if x = 1 then
		
		Units = ComboBox2.text
		
	elseif x = 2 then 
		
		Units = ComboBox3.text
		
	End if
	
	
	if Units = "lbm/in/lbf/deg/F" then
	
		Unit_1 = 0 
		Unit_2 = 0

	elseif Units = "lbm/ft/lbf/deg/F" then
	
		Unit_1 = 0 
		Unit_2 = 1

	elseif Units = "g/mm/N/deg/C" then
	
		Unit_1 = 5 
		Unit_2 = 2

	elseif Units = "g/cm/N/deg/C" then
	
		Unit_1 = 5 
		Unit_2 = 3
		
	elseif Units = "kg/m/N/rad/K" then
	
		Unit_1 = 5 
		Unit_2 = 4
		
	elseif Units = "kg/mm/N/deg/C" then
	
		Unit_1 = 5 
		Unit_2 = 5
	
	end if	

	
	Me.Close()

End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
	
	Dim intSelectedIndex As Integer
	intSelectedIndex = ComboBox1.SelectedIndex
	Dim objSelectedItem As Object
	objSelectedItem = ComboBox1.SelectedItem

	if intSelectedIndex = 0 then
	
		ComboBox2.Visible = true
		ComboBox3.Visible = false
		x = 1
		
	else 
	
		ComboBox2.Visible = false
		ComboBox3.Visible = true
		x = 2 
		
	end if
	
End Sub

Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
	
	Dim intSelectedIndex2 As Integer
	intSelectedIndex2 = ComboBox2.SelectedIndex
	Dim objSelectedItem2 As Object
	objSelectedItem2 = ComboBox2.SelectedItem

End Sub

Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
	
	Dim intSelectedIndex3 As Integer
	intSelectedIndex3 = ComboBox2.SelectedIndex
	Dim objSelectedItem3 As Object
	objSelectedItem3 = ComboBox3.SelectedItem

End Sub


	
Private _canceled As Boolean = False

Public ReadOnly Property Canceled() As Boolean

	Get

		Return _canceled

	End Get

End Property

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
	
	_canceled = True
	Me.Close()
		
End Sub
	
'	Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
   
'		_canceled = True
'		me.close()
		
'	End Sub

Private Sub ClickHandler(sender As Object, e As MouseEventArgs) Handles Me.MouseClick

	'Label1.Text = String.Format("{0}", e.Button.ToString.ToLower)
	
   if e.Button = MouseButtons.Middle then
	
		'Label2.Text = "Middle"
		SendKeys.Send("{ENTER}")
		
	end if
	
End Sub

End Class


'----------------------------------------------------------------------------------------------------------------
' FORM1 -  IKONY
'----------------------------------------------------------------------------------------------------------------

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form

'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

Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
	Get
		Dim param As System.Windows.Forms.CreateParams = MyBase.CreateParams
		param.ClassStyle = param.ClassStyle Or &H200
		Return param
	End Get
End Property

'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.Label1 = New System.Windows.Forms.Label()
	Me.Label2 = New System.Windows.Forms.Label()
	Me.Label3 = New System.Windows.Forms.Label()
	Me.Label4 = New System.Windows.Forms.Label()
	Me.ComboBox1 = New System.Windows.Forms.ComboBox()
	Me.ComboBox2 = New System.Windows.Forms.ComboBox()
	Me.ComboBox3 = New System.Windows.Forms.ComboBox()
	Me.Button1 = New System.Windows.Forms.Button()
	Me.Button2 = New System.Windows.Forms.Button()
	Me.SuspendLayout()
	'
	'Label1
	'
	Me.Label1.AutoSize = True
	Me.Label1.Location = New System.Drawing.Point(16, 13)
	Me.Label1.Name = "Label1"
	Me.Label1.Size = New System.Drawing.Size(78, 16)
	Me.Label1.TabIndex = 0
	Me.Label1.Text = "Units in part:"
	'
	'Label2
	'
	Me.Label2.AutoSize = True
	Me.Label2.Location = New System.Drawing.Point(16, 45)
	Me.Label2.Name = "Label2"
	Me.Label2.Size = New System.Drawing.Size(65, 16)
	Me.Label2.TabIndex = 0
	Me.Label2.Text = "Choose template:"
	'
	'Label3
	'
	Me.Label3.AutoSize = True
	Me.Label3.Location = New System.Drawing.Point(16, 77)
	Me.Label3.Name = "Label3"
	Me.Label3.Size = New System.Drawing.Size(61, 16)
	Me.Label3.TabIndex = 0
	Me.Label3.Text = "Choose units:"
	'
	'Label4
	'
	Me.Label4.AutoSize = True
	Me.Label4.Location = New System.Drawing.Point(130, 13)
	Me.Label4.Name = "Label4"
	Me.Label4.Size = New System.Drawing.Size(86, 16)
	Me.Label4.TabIndex = 0
	Me.Label4.Text = ""
	'
	'ComboBox1
	'
	Me.ComboBox1.FormattingEnabled = True
	Me.ComboBox1.Items.AddRange(New Object() {"Metric", "Imperial"})
	Me.ComboBox1.Location = New System.Drawing.Point(134, 38)
	Me.ComboBox1.Margin = New System.Windows.Forms.Padding(2, 2, 2, 2)
	Me.ComboBox1.Name = "ComboBox1"
	Me.ComboBox1.Size = New System.Drawing.Size(231, 24)
	Me.ComboBox1.TabIndex = 1
	Me.ComboBox1.Text = "Metric"
	'
	'ComboBox2
	'
	Me.ComboBox2.FormattingEnabled = True
	Me.ComboBox2.Items.AddRange(New Object() {"g/mm/N/deg/C", "g/cm/N/deg/C", "kg/m/N/rad/K", "kg/mm/N/deg/C"})
	Me.ComboBox2.Location = New System.Drawing.Point(134, 69)
	Me.ComboBox2.Margin = New System.Windows.Forms.Padding(2, 2, 2, 2)
	Me.ComboBox2.Name = "ComboBox2"
	Me.ComboBox2.Size = New System.Drawing.Size(231, 24)
	Me.ComboBox2.TabIndex = 2
	Me.ComboBox2.Text = "g/mm/N/deg/C"
	'
	'ComboBox3
	'
	Me.ComboBox3.FormattingEnabled = True
	Me.ComboBox3.Items.AddRange(New Object() {"lbm/in/lbf/deg/F", "lbm/ft/lbf/deg/F"})
	Me.ComboBox3.Location = New System.Drawing.Point(134, 69)
	Me.ComboBox3.Margin = New System.Windows.Forms.Padding(2, 2, 2, 2)
	Me.ComboBox3.Name = "ComboBox3"
	Me.ComboBox3.Size = New System.Drawing.Size(231, 24)
	Me.ComboBox3.TabIndex = 2
	Me.ComboBox3.Text = "lbm/in/lbf/deg/F"
	'
	'Button1
	'
	Me.Button1.Location = New System.Drawing.Point(54, 110)
	Me.Button1.Margin = New System.Windows.Forms.Padding(2, 2, 2, 2)
	Me.Button1.Name = "Button1"
	Me.Button1.Size = New System.Drawing.Size(103, 32)
	Me.Button1.TabIndex = 3
	Me.Button1.Text = "Ok"
	Me.Button1.UseVisualStyleBackColor = True
	'
	'Button2
	'
	Me.Button2.Location = New System.Drawing.Point(222, 110)
	Me.Button2.Margin = New System.Windows.Forms.Padding(2, 2, 2, 2)
	Me.Button2.Name = "Button2"
	Me.Button2.Size = New System.Drawing.Size(103, 32)
	Me.Button2.TabIndex = 4
	Me.Button2.Text = "Cancel"
	Me.Button2.UseVisualStyleBackColor = True
	'
	'Form1
	'
	Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 16.0!)
	Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
	Me.ClientSize = New System.Drawing.Size(377, 280)
	'Me.ControlBox = False
	Me.ControlBox = True
	Me.Controls.Add(Me.Button2)
	Me.Controls.Add(Me.Button1)
	Me.Controls.Add(Me.ComboBox3)
	Me.Controls.Add(Me.ComboBox2)
	Me.Controls.Add(Me.ComboBox1)
	Me.Controls.Add(Me.Label4)
	Me.Controls.Add(Me.Label3)
	Me.Controls.Add(Me.Label2)
	Me.Controls.Add(Me.Label1)
	Me.AcceptButton = Button1	
	Me.CancelButton = Button2 
	Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(238, Byte))
	Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable
	Me.MaximumSize = New System.Drawing.Size(393, 200)
	Me.MinimumSize = New System.Drawing.Size(393, 200)
	Me.MaximizeBox = False
	Me.MinimizeBox = False
	Me.Name = "Form1"
	Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
	Me.Text = "Part units change"
	me.KeyPreview = true
	me.cancelbutton = Button2
	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 Label4 As System.Windows.Forms.Label
	Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
	Friend WithEvents ComboBox2 As System.Windows.Forms.ComboBox
	Friend WithEvents ComboBox3 As System.Windows.Forms.ComboBox
	Friend WithEvents Button1 As System.Windows.Forms.Button
	Friend WithEvents Button2 As System.Windows.Forms.Button
	Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
	Friend WithEvents CheckBox2 As System.Windows.Forms.CheckBox
	Friend WithEvents ToolTip1 As System.Windows.Forms.ToolTip
	
End Class


With best regards
Michael
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor