Dimensions from a drawing

I've had a play and used the Journal which takes the manual dimensions from a drawing and places them in a text file but is it possible to take all dimensions from the drawing and place them in an excel spreadsheet?

Many thanks in advance.

The short answer is: Yes, that's possible.

Unfortunately, I don't have time to work up an example right now. How familiar are you with programming macros in Excel? I ask because that knowledge will come in handy when working with the Excel object model through your journal code.

Many thanks. I've done a bit with Macro's in Excel but wouldn't consider myself to be experienced. I'll have a play but just knowing it is possible is very useful.

Thank you

Below is a very quick example I put together today. It will export all of the dimensions in the work part to a new Excel file (change the Excel file path in the journal to something that will work for you before running the code).

This simple example does no error checking and only lists the main dim text (no tolerance, dual text, appended text, etc).

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpenUI

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUISession As UI = UI.GetUI
Dim workPart As Part = theSession.Parts.Work

Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "journal")

'change excelFileName to meet your needs
Const excelFileName As String = "C:\Temp\part_dimensions.xlsm"
Dim row As Long = 1
Dim column As Long = 1

'create Excel object
Dim objExcel = CreateObject("Excel.Application")
If objExcel Is Nothing Then
theUISession.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, "Could not start Excel, journal exiting")
theSession.UndoToMark(markId1, "journal")
Exit Sub
End If

'open Excel file
Dim objWorkbook = objExcel.Workbooks.Open(excelFileName)
If objWorkbook Is Nothing Then
theUISession.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, "Could not open Excel file: " & excelFileName & ControlChars.NewLine & "journal exiting.")
theSession.UndoToMark(markId1, "journal")
Exit Sub
End If

objExcel.visible = True

objExcel.Cells(row, 1) = workPart.FullPath

Dim myDimText() As String
Dim myDimDualText() As String
For Each myDimension As Annotations.Dimension In workPart.Dimensions
row += 1
myDimension.GetDimensionText(myDimText, myDimDualText)
objExcel.Cells(row, column) = myDimText(0)
Next

'objExcel.Quit()
objWorkbook = Nothing
objExcel = Nothing

lw.Close()

End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer

'Unloads the image when the NX session terminates
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

'----Other unload options-------
'Unloads the image immediately after execution within NX
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

'Unloads the image explicitly, via an unload dialog
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Explicitly
'-------------------------------

End Function

End Module

What type of variable is the excel objectworkbook? I want to pass it into a function but I dont know how.

It is an object of type: System.__ComObject

I determined that by adding the following line to the journal above:

lw.WriteLine("objWorkbook type: " & objWorkbook.GetType.ToString)

Thank you for the information & macro provided for export all the dimensions in excel. I tried it by copying in Journal, but it is C++ or ASCI code and not possible to run.
Pl inform where to copy this macro code? I have little bit familiar with macro coding in excel.
Whether I have to create a new excel with this macro and paste this file in my work part folder?

To run the journal above, copy the text and paste it into a new, plain text file; notepad works well for this, do not use Word or Wordpad as they will add extra formatting characters that will interfere with the interpreter. Give this new text file a name and save it in a convenient location; be sure to change the file's extension from ".txt" to ".vb". Start NX, open a file of interest and go to {menu} -> tools -> journal -> play... Now use the browse button and select the text file that you saved previously then click the "run" button.

Thank you for the journal as well, is there any way I can get tolerances in a seperate column as well?
i tried searching for more information but this was the closest i could get..

http://www.nxjournaling.com/content/extracting-correct-upperlower-tolerance

any help would be deeply appreciated!

You will need to check the dimension's .ToleranceType property along with the .LowerToleranceValue and .UpperToleranceValue properties. The .ToleranceType will inform you as to which of the tolerance values apply to the dimension in question. For example, the type ".LimitOneLine" uses both the upper and lower values, but ".BilateralOneLine" uses only the upper value (the lower value is ignored).

If you create a dimension in drafting and change the tolerance style, the settings dialog will provide some feedback as to whether the upper, lower, or both values are used.

thank you very much, sorry I have another question..
where can I find the name of these properties?

You can get to the NXOpen API reference documentation through the NX help system. The NX help is a separate install from NX itself; when installing the NX help, be sure to turn on the option to install the programming help files as well (it is turned off by default). Start the NX help and you will see a list of topics on the left side of the page; scroll to the bottom of the list and click on "programming tools". If the programming help files have been installed already, click the link at the top of the page, otherwise, it shows instructions on how to install the necessary help files. After clicking the link, the programming main help page appears. Take some time and peruse the topics here, there is a lot of good information. When you are ready to look at the API reference, go to NX Open -> Open for .NET -> NX Open for .NET reference guide. This will open a new page showing how to download the file to your local drive. Save it to a convenient location and open the file when it is done transferring to your desired location. When the file opens, change to the "index" tab and type in the name of an object such as "part"; double click the "part class" entry in the list of results. In the window on the right will be listed all of the properties and methods available through the part class. When you have a reference to a part object in your journal code, you can use any of the displayed properties and methods to get information about the part and/or make changes to the part.

Maybe this could be an option to get the stuff into some other colums?

Option Strict Off
Imports System
Imports NXOpen

Module Module1

' Explicit Activation
' This entry point is used to activate the application explicitly
Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUISession As UI = UI.GetUI
Dim workPart As Part = theSession.Parts.Work

Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "journal")

'change excelFileName to meet your needs
Const excelFileName As String = "C:\temp\part_dimensions.xlsm"
Dim row As Long = 1
Dim column As Long = 1

'create Excel object
Dim objExcel = CreateObject("Excel.Application")
If objExcel Is Nothing Then
theUISession.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, "Could not start Excel, journal exiting")
theSession.UndoToMark(markId1, "journal")
Exit Sub
End If

'open Excel file
Dim objWorkbook = objExcel.Workbooks.Open(excelFileName)
If objWorkbook Is Nothing Then
theUISession.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, "Could not open Excel file: " & excelFileName & ControlChars.NewLine & "journal exiting.")
theSession.UndoToMark(markId1, "journal")
Exit Sub
End If

objExcel.visible = True

objExcel.Cells(row, 1) = workPart.FullPath

Dim myDimText() As String
Dim myDimDualText() As String
Dim myToleranceType As String
Dim myToleranceLower As String
Dim myToleranceUpper As String
For Each myDimension As Annotations.Dimension In workPart.Dimensions
row += 1
myDimension.GetDimensionText(myDimText, myDimDualText)
myToleranceType = myDimension.ToleranceType.ToString
objExcel.Cells(row, column) = myDimText(0)
objExcel.Cells(row, column + 1) = myToleranceType
Select Case myDimension.ToleranceType.ToString
Case Is = "UnilateralBelow"
myToleranceLower = myDimension.LowerMetricToleranceValue.ToString
objExcel.Cells(row, column + 2) = myToleranceLower
Case Is = "LimitsAndFits"
myToleranceLower = myDimension.LimitFitDeviation
myToleranceUpper = myDimension.LimitFitGrade
objExcel.Cells(row, column + 2) = myToleranceLower & myToleranceUpper
Case Is = "BilateralOneLine"
myToleranceUpper = myDimension.UpperMetricToleranceValue.ToString
objExcel.Cells(row, column + 2) = myToleranceUpper
Case Is = "BilateralTwoLines"
myToleranceLower = myDimension.LowerMetricToleranceValue.ToString
myToleranceUpper = myDimension.UpperMetricToleranceValue.ToString
objExcel.Cells(row, column + 3) = myToleranceLower
objExcel.Cells(row, column + 2) = myToleranceUpper
Case Is = "UnilateralAbove"
myToleranceUpper = myDimension.UpperMetricToleranceValue.ToString
objExcel.Cells(row, column + 2) = myToleranceUpper
End Select
Next

'objExcel.Quit()
objWorkbook = Nothing
objExcel = Nothing

lw.Close()

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

Chris2u,
Thanks for posting the code. That's pretty much how I'd do it as well. The only thing that I would suggest would be to use the tolerance type directly in the select case block instead of converting the values to strings first. I'm not 100% sure, but using .ToString on the tolerance type may result in different values depending on the language NX is setup to use. By using the tolerance type enumeration, you can avoid any potential problems with languages other than english.

Select Case myDimension.ToleranceType
Case Is = Annotations.ToleranceType.UnilateralBelow
myToleranceLower = myDimension.LowerMetricToleranceValue.ToString
objExcel.Cells(row, column + 2) = myToleranceLower
Case Is = Annotations.ToleranceType.LimitsAndFits
myToleranceLower = myDimension.LimitFitDeviation
myToleranceUpper = myDimension.LimitFitGrade
objExcel.Cells(row, column + 2) = myToleranceLower & myToleranceUpper
Case Is = Annotations.ToleranceType.BilateralOneLine
myToleranceUpper = myDimension.UpperMetricToleranceValue.ToString
objExcel.Cells(row, column + 2) = myToleranceUpper
Case Is = Annotations.ToleranceType.BilateralTwoLines
myToleranceLower = myDimension.LowerMetricToleranceValue.ToString
myToleranceUpper = myDimension.UpperMetricToleranceValue.ToString
objExcel.Cells(row, column + 3) = myToleranceLower
objExcel.Cells(row, column + 2) = myToleranceUpper
Case Is = Annotations.ToleranceType.UnilateralAbove
myToleranceUpper = myDimension.UpperMetricToleranceValue.ToString
objExcel.Cells(row, column + 2) = myToleranceUpper
End Select

Hi Chris2u,
Inside myDimtext. how can i distinguish whether it is linear or radial or the other?
Because I want to add a column to denote it as Radital text.
Thank you

Kien

You can use the .GetType method to determine the type of an object.

For Each myDimension As Annotations.Dimension In theSession.Parts.Work.Dimensions

lw.WriteLine("dim type: " & myDimension.GetType.ToString)
Next

You can take action based on the object type:

For Each myDimension As Annotations.Dimension In theSession.Parts.Work.Dimensions
lw.WriteLine("dim type: " & myDimension.GetType.ToString)
Select Case myDimension.GetType
Case GetType(NXOpen.Annotations.RadiusDimension)
lw.WriteLine(" radius dimension found")
Case GetType(NXOpen.Annotations.HorizontalDimension)
lw.WriteLine(" horizontal dimension (linear) found")
Case GetType(NXOpen.Annotations.MinorAngularDimension)
lw.WriteLine(" angular dimension found")
Case Else
'dimension type not handled by this journal
End Select
Next

Thank you.
From your support, I got what I needed.

Kien

That's perfect for me because we are working in an multilanguage environment :-)
I copied it :-)

Hello guys,
Could you please help me get the below task done.
I want a code that will do below stuff:
1. It should create an excel sheet with the name of dimension as different columns
2. Export dimensions from each drawing sheet under the suitable column (matching text in the dimension)
3. I should be able to run a number of drawing files in team center with part ID. Need to use an input excel sheet to feed all part numbers
4. The output excel sheet needs to have rows with part number and sub rows (for each part) with drawing sheet names

Many thanks in advance,
Aman Jha