Hello there,
I can excel the zone regions of datum feature symbols on the drawing page with the following journal. As an example DATUME is like on C5 on the page but I want to see this information in the listing window too. so I want to see the window instead of excel. How can I help you how to do it?
' VBA Script to extract all the dimensions in an NX part file to an MS Excel Spreadsheet.
' The details extracted are:
' Sheet No. | Zone | Description\Measurement Direction |
Option Strict Off
Imports System
Imports System.IO
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Annotations
Module ExportDimensionsAndAnnotations
'Declare global variables
Dim theSession as Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession
Sub Main()
' First read the active part file from NX.
' Then, loop through the sheets.
' Then, loop through the dimensions in the sheet
' Grab the zone, text, tolerance type, tolerance and the measurement direction for each.
' Grab the nearest annotation in that zone.
' Build an array or a dictionary (similar to JSON or Python's Dictionary class)
' Then, loop through the text in the document, and do the same steps, except for those that are inapplicable, of course.
' Ask for a savefile location.
' Create an Excel file in that location
' Open the Excel file.
' Write array to the excel file.
If IsNothing(theSession.Parts.Work) Then 'Error handling
MsgBox("This code requires an active part. Please open a drawing first and then try again.")
Exit Sub
End If
Dim workPart as Part
workPart = theSession.Parts.Work
Dim theUISession as UI = UI.getUI
Dim saveFileName as String
Dim saveDialog as New System.Windows.Forms.SaveFileDialog
Dim objExcel as Object
Dim objWorkbook as Object
Dim excelFileExists as Boolean = False
Dim objWorksheet as Object
Dim colSheetNumber as Integer = 1
Dim colZone as Integer = 2
Dim colDescrMatlDir as Integer = 3
Dim valueSheetNumber as String
Dim valueZone as String
Dim valueDescrMatlDir as String
Dim rowNumber as Integer = 1
With saveDialog
.DefaultExt = "xlsx"
.FileName = "Datum_GD&T data"
.Filter = "MS Excel Spreadsheets (*.xlsx)|*.xlsx|All Files (*.*)|*.*"
.FilterIndex = 1
.OverwritePrompt = True
.Title = "Select a file where you'd like to save the exported data."
End With
saveDialog.ShowDialog()
saveFileName = saveDialog.FileName
If saveFileName ="Exported Data" Then
MsgBox("You failed to select a save file. Exitting the macro.")
Exit Sub
End If
objExcel = CreateObject("Excel.Application")
if objExcel is Nothing Then
theUISession.NXMessageBox.Show("Error", theUISession.NXMessageBox.DialogType.Error, "Could not start Excel, journal exiting")
Exit Sub
End If
If File.Exists(saveFileName) Then
excelFileExists = True
objWorkbook = objExcel.Workbooks.Open(saveFileName)
objWorksheet = objWorkbook.Sheets.Add
Else
objWorkbook = objExcel.Workbooks.Add
objWorkbook.saveAs(saveFileName)
objWorksheet = objWorkbook.Sheets(1)
End If
objWorksheet.cells(rowNumber, colSheetNumber).Value = "Sheet Number"
objWorksheet.cells(rowNumber, colZone).Value = "Drawing Zone"
objWorksheet.cells(rowNumber, colDescrMatlDir).Value = "Description Text \ Measurement Direction"
Dim theSheet as Drawings.DrawingSheet
For Each tempDim As Annotations.Gdt In workPart.Gdts
rowNumber = rowNumber + 1
theSheet = AskDrawingSheet(tempDim)
valueSheetNumber = getSheetNumber(theSheet).ToString
valueZone = getZone(theSheet, tempDim)
valueDescrMatlDir = tempDim.GetType.ToString
' Clean up the material direction.
valueDescrMatlDir = Replace(valueDescrMatlDir, "NXOpen.Annotations.","")
objWorksheet.cells(rowNumber, colSheetNumber).Value = valueSheetNumber
objWorksheet.cells(rowNumber, colZone).Value = valueZone
objWorksheet.cells(rowNumber, colDescrMatlDir).Value = valueDescrMatlDir
Next
For Each temp As Annotations.Gdt In workPart.Gdts
theSheet = AskDrawingSheet(temp)
valueSheetNumber = getSheetNumber(theSheet)
objWorksheet.cells(rowNumber, colSheetNumber).Value = valueSheetNumber
objWorksheet.cells(rowNumber, colZone).Value = getZone(theSheet, temp)
Next
objWorksheet.Columns("A:M").EntireColumn.AutoFit
objWorkbook.save()
objWorkbook.close()
objExcel.Quit()
objWorksheet = Nothing
objWorkbook = Nothing
objExcel = Nothing
MsgBox("Completed the extraction successfully! Check " & saveFileName & " for the data.")
End Sub
Function getSheetNumber(ByVal theSheet as Drawings.DrawingSheet) As String
' This function returns the number of a sheet given the DrawingSheet object.
Dim sheetNum as Integer
Dim theSheetBuilder as Drawings.DrawingSheetBuilder
theSheetBuilder = theSession.Parts.Work.DrawingSheets.DrawingSheetBuilder(theSheet)
sheetNum = theSheetBuilder.Number
theSheetBuilder.Destroy()
Return sheetNum.ToString
End Function
Function AskDrawingSheet(ByVal theObject As TaggedObject) As Drawings.DrawingSheet
Dim theView As View = TryCast(theObject, View)
If Not theView Is Nothing Then
Dim sheetTag As Tag = Nothing
Try
theUfSession.Draw.AskDrawingOfView(theView.Tag, sheetTag)
Return Utilities.NXObjectManager.Get(sheetTag) ' the drawing it is on
Catch ex As NXException
Return Nothing ' it is a model view
End Try
End If
Dim viewName As String = Nothing
Dim status As Integer = Nothing
Try
theUfSession.View.AskViewDependentStatus(theObject.Tag, status, viewName)
Catch ex As NXException
Return Nothing
End Try
If status = 0 Then Return Nothing ' it is a model mode object
Dim viewTag As Tag = Nothing
theUfSession.View.AskTagOfViewName(viewName, viewTag)
Dim viewType As Integer = Nothing
Dim viewSubtype As Integer = Nothing
theUfSession.View.AskType(viewTag, viewType, viewSubtype)
If viewType = 0 Then Return Nothing ' it is view dependent in a modeling view
Dim drawingTag As Tag = Nothing
theUfSession.Draw.AskDrawingOfView(viewTag, drawingTag)
Return Utilities.NXObjectManager.Get(drawingTag) ' the drawing it is on!
End Function
Function getZone(ByVal theSheet As Drawings.DrawingSheet, ByVal theAnnotation As Annotations.Annotation) As String
Dim borderBuilder As Drawings.BordersAndZonesBuilder
borderBuilder = theSession.Parts.Work.Drafting.BordersAndZonesObjects.CreateBordersAndZonesBuilder(theSheet.BordersAndZones)
Dim numHorizontalZones As Integer = (theSheet.Length - borderBuilder.LeftMargin - borderBuilder.RightMargin) / borderBuilder.HorizontalSize
Dim numVerticalZones As Integer = (theSheet.Height - borderBuilder.TopMargin - borderBuilder.BottomMargin) / borderBuilder.VerticalSize
'calculate zone wrt bottom left of drawing (ZoneOrigin.Topleft)
Dim Hcell As Double = (theAnnotation.AnnotationOrigin.X - borderBuilder.LeftMargin) / borderBuilder.HorizontalSize
Dim Vcell As Double = (theAnnotation.AnnotationOrigin.Y - borderBuilder.rightMargin) / borderBuilder.VerticalSize
Hcell = Math.Ceiling(Hcell)
Vcell = Math.Ceiling(Vcell)
Dim theZoneOrigin As Drawings.BordersAndZonesBuilder.ZoneOrigin = borderBuilder.Origin
borderBuilder.Destroy()
Dim verticalLetterNum As Integer
Dim verticalLetter As Char
Dim horizontalNum As Integer
If theZoneOrigin = Drawings.BordersAndZonesBuilder.ZoneOrigin.TopLeft Or theZoneOrigin = Drawings.BordersAndZonesBuilder.ZoneOrigin.Bottomleft Then
horizontalNum = Hcell
Else
'origin on left side
horizontalNum = numHorizontalZones - Hcell + 1
End If
If theZoneOrigin = Drawings.BordersAndZonesBuilder.ZoneOrigin.topright Or theZoneOrigin = Drawings.BordersAndZonesBuilder.ZoneOrigin.bottomright Then
verticalLetterNum = Asc("A") + Vcell
verticalLetter = Chr(verticalLetterNum)
Else
'origin on the top
verticalLetterNum = Asc("A") + numVerticalZones - Vcell
verticalLetter = Chr(verticalLetterNum)
End If
Return verticalLetter & horizontalNum.ToString
End Function
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
re: write to listing window
The following article has some information on using the listing window. It shows how to write to the listing window and a text file. If you want to write only to the listing window, do not change the "device" (it defaults to the listing window only).
http://nxjournaling.com/content/write-text-file
listing window
Hello there,
I looked at the form and I saw this page but I did not understand how it would be done because I just started. which commands I have added to which line of the macron which I have added should be entered so that I can see the request in the listing window instead of excel. Can you help me in this matter?
listing window
help me please
re: writing to the information window
Refer to the "listing window overview" section of the article:
http://nxjournaling.com/content/write-text-file
If you want to write to only the information window and not a text file, comment or remove the lines that use the .SelectDevice method.