Hi
below is code which is available in this site for getting zone of drafting datum,
suppose The output will be like
Datum : A
Sheet number : 3
Zone: A10
Datum : A
Sheet number : 2
Zone: A1
Datum : A
Sheet number : 1
Zone: B12
etc
How to get zone for only first occurrence of each datum
Datum : A
Sheet number : 1
Zone: B12
Datum : B
Sheet number : 2
Zone: C5
Datum : C
Sheet number : 1
Zone: D7
etc
Can you please help me in getting zone of first occurrence of each datum, Thanks in Advance,
Below is code :
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Module Module5
Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession
Sub Main()
If IsNothing(theSession.Parts.Work) Then
'active part required
Return
End If
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
For Each temp As NXObject In theSession.Parts.Work.Gdts
'lw.WriteLine(" type: " & temp.GetType.ToString)
If TypeOf (temp) Is Annotations.DraftingDatum Then
Dim myDraftingDatum As Annotations.DraftingDatum = temp
Dim myDraftingDatumZone As New NXJSheetZoneInfo
myDraftingDatumZone = ReportAnnotationSheetZone(AskDrawingSheet(myDraftingDatum), myDraftingDatum)
lw.WriteLine("Datum: " & myDraftingDatum.Label)
lw.WriteLine("Sheet number: " & myDraftingDatumZone.Sheet)
lw.WriteLine("Zone: " & myDraftingDatumZone.VerticalZone & myDraftingDatumZone.HorizontalZone)
lw.WriteLine("")
End If
Next
lw.Close()
End Sub
Function AskDrawingSheet(ByVal theObject As TaggedObject) As Drawings.DrawingSheet
'Code written by Amy Webster of GTAC
' see nx_api4936 or nx_api4937
' This function will work for:
' an object which "Resides on drawing" or is "View Dependent In" a DraftingView
' a DraftingView
' a DrawingSheet.View
' Returns Nothing for all other (ie. model mode) objects
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 ReportAnnotationSheetZone(ByVal theSheet As Drawings.DrawingSheet, ByVal theAnnotation As Annotations.Annotation) As NXJSheetZoneInfo
Dim info As New NXJSheetZoneInfo
Dim borderBuilder As Drawings.BordersAndZonesBuilder
If IsNothing(theSheet.BordersAndZones) Then
Return Nothing
End If
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.BottomMargin - borderBuilder.TopMargin) / borderBuilder.VerticalSize
'calculate zone wrt bottom left of drawing (ZoneOrigin.BottomLeft)
Dim Hcell As Double = (theAnnotation.AnnotationOrigin.X - borderBuilder.LeftMargin) / borderBuilder.HorizontalSize
Dim Vcell As Double = (theAnnotation.AnnotationOrigin.Y - borderBuilder.BottomMargin) / 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.BottomLeft Or theZoneOrigin = Drawings.BordersAndZonesBuilder.ZoneOrigin.TopLeft Then
'origin on left side
horizontalNum = Hcell
Else
'origin on right side
horizontalNum = numHorizontalZones - Hcell + 1
End If
If theZoneOrigin = Drawings.BordersAndZonesBuilder.ZoneOrigin.BottomLeft Or theZoneOrigin = Drawings.BordersAndZonesBuilder.ZoneOrigin.BottomRight Then
'origin on bottom
verticalLetterNum = Asc("A") + Vcell - 1
verticalLetter = Chr(verticalLetterNum)
Else
'origin on the top
verticalLetterNum = Asc("A") + numVerticalZones - Vcell
verticalLetter = Chr(verticalLetterNum)
End If
Dim theSheetNum As String = SheetNumber(theSheet)
info.Sheet = theSheetNum
info.VerticalZone = verticalLetter
info.HorizontalZone = horizontalNum
Return info
End Function
Function SheetNumber(ByVal theSheet As Drawings.DrawingSheet) As String
Dim sheetNum As Integer
Dim theSheetBuilder As Drawings.DrawingSheetBuilder = theSession.Parts.Work.DrawingSheets.DrawingSheetBuilder(theSheet)
sheetNum = theSheetBuilder.Number
theSheetBuilder.Destroy()
Return sheetNum.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
Public Class NXJSheetZoneInfo
Private _sheet As String = ""
Public Property Sheet() As String
Get
Return _sheet
End Get
Set(ByVal value As String)
_sheet = value
End Set
End Property
Private _horizontalZone As String = ""
Public Property HorizontalZone() As String
Get
Return _horizontalZone
End Get
Set(ByVal value As String)
_horizontalZone = value
End Set
End Property
Private _verticalZone As String = ""
Public Property VerticalZone() As String
Get
Return _verticalZone
End Get
Set(ByVal value As String)
_verticalZone = value
End Set
End Property
Public Sub New()
End Sub
End Class
re: drafting datums
Try the code below. It uses a dictionary object to group the datum labels together with their zone location info. It reports each datum and groups the locations it can be found (in alphabetical order - [sheet num/vertical zone/horizontal zone]). If you define the "first location" as the first one alphabetically, you only need to report the first location in each list. If you have a different definition, you will need to adjust the code accordingly.
'NXJournaling.com
'December 20, 2017
'
'http://nxjournaling.com/comment/4935#comment-4935
'Report the sheet zone of the GD&T datum features
'
'updated June 2, 2020
'http://nxjournaling.com/content/datum-zone-first-instance-each-datum
'create a dictionary object to hold the GD&T labels (key) and zone locations as a list (value)
'report locations of each label found, sorted alphabetically (sheetnum/vertical location/horizontal location)
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF
Module Module6
Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession
Sub Main()
If IsNothing(theSession.Parts.Work) Then
'active part required
Return
End If
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
Dim dicDraftingDatums As New Dictionary(Of String, List(Of String))
For Each temp As NXObject In theSession.Parts.Work.Gdts
'lw.WriteLine(" type: " & temp.GetType.ToString)
If TypeOf (temp) Is Annotations.DraftingDatum Then
Dim myDraftingDatum As Annotations.DraftingDatum = temp
Dim myDraftingDatumZone As New NXJSheetZoneInfo
myDraftingDatumZone = ReportAnnotationSheetZone(AskDrawingSheet(myDraftingDatum), myDraftingDatum)
'lw.WriteLine("Datum: " & myDraftingDatum.Label)
'lw.WriteLine("Sheet number: " & myDraftingDatumZone.Sheet)
'lw.WriteLine("Zone: " & myDraftingDatumZone.VerticalZone & myDraftingDatumZone.HorizontalZone)
'lw.WriteLine("")
If dicDraftingDatums.ContainsKey(myDraftingDatum.Label) Then
'add zone info to the existing key
dicDraftingDatums.Item(myDraftingDatum.Label).Add(myDraftingDatumZone.Zone)
Else
'add to dictionary
dicDraftingDatums.Add(myDraftingDatum.Label, New List(Of String)({myDraftingDatumZone.Zone}))
End If
End If
Next
For Each pair As KeyValuePair(Of String, List(Of String)) In dicDraftingDatums
'mySheets.Sort(AddressOf CompareSheetNames)
lw.WriteLine(" Datum label: " & pair.Key & " [" & pair.Value.Count.ToString & " places]")
pair.Value.Sort(AddressOf CompareZones)
For Each temp As String In pair.Value
lw.WriteLine(" zone: " & temp)
Next
lw.WriteLine("")
Next
lw.Close()
End Sub
Function AskDrawingSheet(ByVal theObject As TaggedObject) As Drawings.DrawingSheet
'Code written by Amy Webster of GTAC
' see nx_api4936 or nx_api4937
' This function will work for:
' an object which "Resides on drawing" or is "View Dependent In" a DraftingView
' a DraftingView
' a DrawingSheet.View
' Returns Nothing for all other (ie. model mode) objects
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 ReportAnnotationSheetZone(ByVal theSheet As Drawings.DrawingSheet, ByVal theAnnotation As Annotations.Annotation) As NXJSheetZoneInfo
Dim info As New NXJSheetZoneInfo
Dim borderBuilder As Drawings.BordersAndZonesBuilder
If IsNothing(theSheet.BordersAndZones) Then
Return Nothing
End If
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.BottomMargin - borderBuilder.TopMargin) / borderBuilder.VerticalSize
'calculate zone wrt bottom left of drawing (ZoneOrigin.BottomLeft)
Dim Hcell As Double = (theAnnotation.AnnotationOrigin.X - borderBuilder.LeftMargin) / borderBuilder.HorizontalSize
Dim Vcell As Double = (theAnnotation.AnnotationOrigin.Y - borderBuilder.BottomMargin) / 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.BottomLeft Or theZoneOrigin = Drawings.BordersAndZonesBuilder.ZoneOrigin.TopLeft Then
'origin on left side
horizontalNum = Hcell
Else
'origin on right side
horizontalNum = numHorizontalZones - Hcell + 1
End If
If theZoneOrigin = Drawings.BordersAndZonesBuilder.ZoneOrigin.BottomLeft Or theZoneOrigin = Drawings.BordersAndZonesBuilder.ZoneOrigin.BottomRight Then
'origin on bottom
verticalLetterNum = Asc("A") + Vcell - 1
verticalLetter = Chr(verticalLetterNum)
Else
'origin on the top
verticalLetterNum = Asc("A") + numVerticalZones - Vcell
verticalLetter = Chr(verticalLetterNum)
End If
Dim theSheetNum As String = SheetNumber(theSheet)
info.Sheet = theSheetNum
info.VerticalZone = verticalLetter
info.HorizontalZone = horizontalNum.ToString
info.Zone = theSheetNum.ToString & verticalLetter & horizontalNum.ToString
Return info
End Function
Function SheetNumber(ByVal theSheet As Drawings.DrawingSheet) As String
Dim sheetNum As Integer
Dim theSheetBuilder As Drawings.DrawingSheetBuilder = theSession.Parts.Work.DrawingSheets.DrawingSheetBuilder(theSheet)
sheetNum = theSheetBuilder.Number
theSheetBuilder.Destroy()
Return sheetNum.ToString
End Function
Private Function CompareZones(ByVal x As String, ByVal y As String) As Integer
'case-insensitive sort
Dim myStringComp As StringComparer = StringComparer.CurrentCultureIgnoreCase
'for a case-sensitive sort (A-Z then a-z), change the above option to:
'Dim myStringComp As StringComparer = StringComparer.CurrentCulture
Return myStringComp.Compare(x, y)
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
Public Class NXJSheetZoneInfo
Private _sheet As String = ""
Public Property Sheet() As String
Get
Return _sheet
End Get
Set(ByVal value As String)
_sheet = value
End Set
End Property
Private _horizontalZone As String = ""
Public Property HorizontalZone() As String
Get
Return _horizontalZone
End Get
Set(ByVal value As String)
_horizontalZone = value
End Set
End Property
Private _verticalZone As String = ""
Public Property VerticalZone() As String
Get
Return _verticalZone
End Get
Set(ByVal value As String)
_verticalZone = value
End Set
End Property
Private _zone As String
Public Property Zone() As String
Get
Return _zone
End Get
Set(ByVal value As String)
_zone = value
End Set
End Property
Public Sub New()
End Sub
End Class
Thank You so much, I will
Thank You so much, I will check this code
Hello,
Hello,
Big Thanks for sharing above code,
I need some more information from above code, i.e in above code line
dicDraftingDatums.Add(myDraftingDatum.Label, New List(Of String)({myDraftingDatumZone.Zone})) will add datum label and zone to dictionary and after sorting result will be like Datum Label: A [3 Places]
Zone : A10
Zone : C2
Zone : D4
This fine as sorting in Ascending Order of zone value But it is not considering sheet number, In above case Zone of Datum Label A is A10 in 8th sheet where as D4 is in First sheet, I need fist instance of Datum A first in sheet number ascending order then zone value in ascending order Ex Zone D4-4, A10-8, can you please help me in resolving above issue, Thanks In Advance
re: zone info
The code posted previously adds the sheet number as the first character of the zone information. You should not see "A10" or "D4", they should be "8A10" and "4D4". The code then sorts them alphabetically which should report the first sheet first in the list. However, if you have 10 or more sheets, you might get undesired sorting (such as "11" appearing before "2"). If this is the case, we'll need to change the code to do alphanumeric sorting.
Thanks for reply,
Thanks for reply,
Yes I have more then 10 sheets, even up to 40 sheets, can you please help in alphanumeric sorting.
re: sorting sheets
Here is a version that pads the number of sheets with leading zeroes, they should sort correctly.
'NXJournaling.com
'December 20, 2017
'
'http://nxjournaling.com/comment/4935#comment-4935
'Report the sheet zone of the GD&T datum features
'
'updated June 2, 2020
'http://nxjournaling.com/content/datum-zone-first-instance-each-datum
'create a dictionary object to hold the GD&T labels (key) and zone locations as a list (value)
'report locations of each label found, sorted alphabetically (sheetnum/vertical location/horizontal location)
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF
Module Module6
Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession
Sub Main()
If IsNothing(theSession.Parts.Work) Then
'active part required
Return
End If
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
Dim dicDraftingDatums As New Dictionary(Of String, List(Of String))
'Dim strNumSheets As String = Strings.Right("000" & theSession.Parts.Work.DrawingSheets.ToArray.Length.ToString, 3)
'lw.WriteLine(strNumSheets)
'Return
For Each temp As NXObject In theSession.Parts.Work.Gdts
'lw.WriteLine(" type: " & temp.GetType.ToString)
If TypeOf (temp) Is Annotations.DraftingDatum Then
Dim myDraftingDatum As Annotations.DraftingDatum = temp
Dim myDraftingDatumZone As New NXJSheetZoneInfo
myDraftingDatumZone = ReportAnnotationSheetZone(AskDrawingSheet(myDraftingDatum), myDraftingDatum)
'lw.WriteLine("Datum: " & myDraftingDatum.Label)
'lw.WriteLine("Sheet number: " & myDraftingDatumZone.Sheet)
'lw.WriteLine("Zone: " & myDraftingDatumZone.VerticalZone & myDraftingDatumZone.HorizontalZone)
'lw.WriteLine("")
If dicDraftingDatums.ContainsKey(myDraftingDatum.Label) Then
'add zone info to the existing key
dicDraftingDatums.Item(myDraftingDatum.Label).Add(myDraftingDatumZone.Zone)
Else
'add to dictionary
dicDraftingDatums.Add(myDraftingDatum.Label, New List(Of String)({myDraftingDatumZone.Zone}))
End If
End If
Next
For Each pair As KeyValuePair(Of String, List(Of String)) In dicDraftingDatums
'mySheets.Sort(AddressOf CompareSheetNames)
lw.WriteLine(" Datum label: " & pair.Key & " [" & pair.Value.Count.ToString & " places]")
pair.Value.Sort(AddressOf CompareZones)
For Each temp As String In pair.Value
lw.WriteLine(" zone: " & temp)
Next
lw.WriteLine("")
Next
lw.Close()
End Sub
Function AskDrawingSheet(ByVal theObject As TaggedObject) As Drawings.DrawingSheet
'Code written by Amy Webster of GTAC
' see nx_api4936 or nx_api4937
' This function will work for:
' an object which "Resides on drawing" or is "View Dependent In" a DraftingView
' a DraftingView
' a DrawingSheet.View
' Returns Nothing for all other (ie. model mode) objects
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 ReportAnnotationSheetZone(ByVal theSheet As Drawings.DrawingSheet, ByVal theAnnotation As Annotations.Annotation) As NXJSheetZoneInfo
Dim info As New NXJSheetZoneInfo
Dim borderBuilder As Drawings.BordersAndZonesBuilder
If IsNothing(theSheet.BordersAndZones) Then
Return Nothing
End If
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.BottomMargin - borderBuilder.TopMargin) / borderBuilder.VerticalSize
'calculate zone wrt bottom left of drawing (ZoneOrigin.BottomLeft)
Dim Hcell As Double = (theAnnotation.AnnotationOrigin.X - borderBuilder.LeftMargin) / borderBuilder.HorizontalSize
Dim Vcell As Double = (theAnnotation.AnnotationOrigin.Y - borderBuilder.BottomMargin) / 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.BottomLeft Or theZoneOrigin = Drawings.BordersAndZonesBuilder.ZoneOrigin.TopLeft Then
'origin on left side
horizontalNum = Hcell
Else
'origin on right side
horizontalNum = numHorizontalZones - Hcell + 1
End If
If theZoneOrigin = Drawings.BordersAndZonesBuilder.ZoneOrigin.BottomLeft Or theZoneOrigin = Drawings.BordersAndZonesBuilder.ZoneOrigin.BottomRight Then
'origin on bottom
verticalLetterNum = Asc("A") + Vcell - 1
verticalLetter = Chr(verticalLetterNum)
Else
'origin on the top
verticalLetterNum = Asc("A") + numVerticalZones - Vcell
verticalLetter = Chr(verticalLetterNum)
End If
Dim theSheetNum As String = SheetNumber(theSheet)
info.Sheet = theSheetNum
info.VerticalZone = verticalLetter
info.HorizontalZone = horizontalNum.ToString
info.Zone = Strings.Right("00" & theSheetNum.ToString, 2) & verticalLetter & Strings.Right("00" & horizontalNum.ToString, 2)
Return info
End Function
Function SheetNumber(ByVal theSheet As Drawings.DrawingSheet) As String
Dim sheetNum As Integer
Dim theSheetBuilder As Drawings.DrawingSheetBuilder = theSession.Parts.Work.DrawingSheets.DrawingSheetBuilder(theSheet)
sheetNum = theSheetBuilder.Number
theSheetBuilder.Destroy()
Return sheetNum.ToString
End Function
Private Function CompareZones(ByVal x As String, ByVal y As String) As Integer
'case-insensitive sort
Dim myStringComp As StringComparer = StringComparer.CurrentCultureIgnoreCase
'for a case-sensitive sort (A-Z then a-z), change the above option to:
'Dim myStringComp As StringComparer = StringComparer.CurrentCulture
Return myStringComp.Compare(x, y)
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
Public Class NXJSheetZoneInfo
Private _sheet As String = ""
Public Property Sheet() As String
Get
Return _sheet
End Get
Set(ByVal value As String)
_sheet = value
End Set
End Property
Private _horizontalZone As String = ""
Public Property HorizontalZone() As String
Get
Return _horizontalZone
End Get
Set(ByVal value As String)
_horizontalZone = value
End Set
End Property
Private _verticalZone As String = ""
Public Property VerticalZone() As String
Get
Return _verticalZone
End Get
Set(ByVal value As String)
_verticalZone = value
End Set
End Property
Private _zone As String
Public Property Zone() As String
Get
Return _zone
End Get
Set(ByVal value As String)
_zone = value
End Set
End Property
Public Sub New()
End Sub
End Class
Thanks for your reply,
Thanks for your reply,
above code is not working for me may be because of NX11 I am using or class definition or may be because of drawing templet, I am getting below error
Object reference not set to an instance of an object - Line 57
Line 57 is dicDraftingDatums.Add(myDraftingDatum.Label, New List(Of String)({myDraftingDatumZone.Zone}))
below is modified code from above code and Function ReportAnnotationSheetZone is modified to meet my requirement, now how to get zone in sorted order from this code, can you please help me, Thanks in advance
below is code
'NXJournaling.com
'December 20, 2017
'
'http://nxjournaling.com/comment/4935#comment-4935
'Report the sheet zone of the GD&T datum features
'
'updated June 2, 2020
'http://nxjournaling.com/content/datum-zone-first-instance-each-datum
'create a dictionary object to hold the GD&T labels (key) and zone locations as a list (value)
'report locations of each label found, sorted alphabetically (sheetnum/vertical location/horizontal location)
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF
Module Module6
Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession
Sub Main()
If IsNothing(theSession.Parts.Work) Then
'active part required
Return
End If
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
Dim dicDraftingDatums As New Dictionary(Of String, List(Of String))
For Each temp As NXObject In theSession.Parts.Work.Gdts
'lw.WriteLine(" type: " & temp.GetType.ToString)
If TypeOf (temp) Is Annotations.DraftingDatum Then
Dim myDraftingDatum As Annotations.DraftingDatum = temp
Dim myDraftingDatumZone As string
myDraftingDatumZone = ReportAnnotationSheetZone(AskDrawingSheet(myDraftingDatum), myDraftingDatum)
'lw.WriteLine("Datum: " & myDraftingDatum.Label)
'lw.WriteLine("Sheet number: " & myDraftingDatumZone.Sheet)
'lw.WriteLine("Zone: " & myDraftingDatumZone.VerticalZone & myDraftingDatumZone.HorizontalZone)
'lw.WriteLine("")
If dicDraftingDatums.ContainsKey(myDraftingDatum.Label) Then
'add zone info to the existing key
dicDraftingDatums.Item(myDraftingDatum.Label).Add(myDraftingDatumZone)
Else
'add to dictionary
dicDraftingDatums.Add(myDraftingDatum.Label, New List(Of String)({myDraftingDatumZone}))
End If
End If
Next
For Each pair As KeyValuePair(Of String, List(Of String)) In dicDraftingDatums
'mySheets.Sort(AddressOf CompareSheetNames)
lw.WriteLine(" Datum label: " & pair.Key & " [" & pair.Value.Count.ToString & " places]")
pair.Value.Sort(AddressOf CompareZones)
For Each temp As String In pair.Value
lw.WriteLine(" zone: " & temp)
Next
lw.WriteLine("")
Next
lw.Close()
End Sub
Function AskDrawingSheet(ByVal theObject As TaggedObject) As Drawings.DrawingSheet
'Code written by Amy Webster of GTAC
' see nx_api4936 or nx_api4937
' This function will work for:
' an object which "Resides on drawing" or is "View Dependent In" a DraftingView
' a DraftingView
' a DrawingSheet.View
' Returns Nothing for all other (ie. model mode) objects
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 ReportAnnotationSheetZone(ByVal theSheet As Drawings.DrawingSheet, ByVal theAnnotation As Annotations.Annotation) As String
'sheet number / vertical zone / horizontal zone
Dim borderBuilder As Drawings.BordersAndZonesBuilder
borderBuilder = theSession.Parts.Work.Drafting.BordersAndZonesObjects.CreateBordersAndZonesBuilder(theSheet.BordersAndZones)
borderBuilder.HorizontalSize = 57.45
borderBuilder.VerticalSize = 56.7857
borderBuilder.LeftMargin = 20.0
borderBuilder.RightMargin = 20.0
borderBuilder.BottomMargin = 26.0
borderBuilder.TopMargin = 20.0
Dim numHorizontalZones As Integer = (theSheet.Length - borderBuilder.LeftMargin - borderBuilder.RightMargin) / borderBuilder.HorizontalSize
Dim numVerticalZones As Integer = (theSheet.Height - borderBuilder.BottomMargin - borderBuilder.TopMargin) / borderBuilder.VerticalSize
'calculate zone wrt bottom left of drawing (ZoneOrigin.BottomLeft)
Dim Hcell As Double = (theAnnotation.AnnotationOrigin.X - borderBuilder.LeftMargin) / borderBuilder.HorizontalSize
Dim Vcell As Double = (theAnnotation.AnnotationOrigin.Y - borderBuilder.BottomMargin) / borderBuilder.VerticalSize
Hcell = Math.Ceiling(Hcell)
Vcell = Math.Ceiling(Vcell)
If Vcell > 8 Then
Vcell = Vcell+1
End If
If Vcell > 14 Then
Vcell = Vcell+1
End If
Dim verticalLetterNum As Integer
Dim verticalLetter As Char
Dim horizontalNum As Integer
horizontalNum = numHorizontalZones - Hcell + 1
verticalLetterNum = Asc("A") + Vcell - 1
verticalLetter = Chr(verticalLetterNum)
Dim theSheetNum As String
Return theSheetNum & verticalLetter & horizontalNum.ToString
End Function
Function SheetNumber(ByVal theSheet As Drawings.DrawingSheet) As String
Dim sheetNum As Integer
Dim theSheetBuilder As Drawings.DrawingSheetBuilder = theSession.Parts.Work.DrawingSheets.DrawingSheetBuilder(theSheet)
sheetNum = theSheetBuilder.Number
theSheetBuilder.Destroy()
Return sheetNum.ToString
End Function
Private Function CompareZones(ByVal x As String, ByVal y As String) As Integer
'case-insensitive sort
Dim myStringComp As StringComparer = StringComparer.CurrentCultureIgnoreCase
'for a case-sensitive sort (A-Z then a-z), change the above option to:
'Dim myStringComp As StringComparer = StringComparer.CurrentCulture
Return myStringComp.Compare(x, y)
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
Above code is working fine
Above code is working fine for me and need in Ascending order of zone along with sheet number, and also we can export above date to excel like datum label (column A), zone (column B), sheet number (column C) to excel then sort column B and C In ascending order then remove duplicate from column A, whether we can do same thing without excel since exporting to excel then importing after sorting and removing duplicates, will take time, can you please help in resoling my said issue, Thanks In advance
re: drawing zone of datum
Based on the code that you added, it appears that the borders and zones are not set up on all of your sheets. If so, that would explain the error you were getting. As for the code that you added, the border builder's .Destroy function should be called when you are done with it (before the End Function statement). Failing to destroy the builder within the function can lead to errors in the journal and/or odd behavior in NX after the journal completes.
Beyond that, if you pad the sheet number and horizontal zone number with zeroes as shown in my previous code, it should sort the zone info for you.
Thanks for Reply, I will try
Thanks for Reply, I will try to set up zones and borders and check the code, Thank you so much for your help