Sample NX Open .NET Visual Basic program : create tabnote with all 3d points coordinates
작성자k2ice작성시간09.09.05조회수941 목록 댓글 0Date: 2-APR-2009
Subject: Sample NX Open .NET Visual Basic program : create tabnote with all 3d points coordinates
Note: GTAC provides programming examples for illustration only, and
assumes that you are familiar with the programming language being
demonstrated and the tools used to create and debug procedures. GTAC
support professionals can help explain the functionality of a particular
procedure, but we will not modify these examples to provide added
functionality or construct procedures to meet your specific needs.
��Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.Annotations
Imports NXOpen.UI
Imports NXOpen.UF
Imports NXOpen.Utilities
Module create_tabnote_with_all_3d_points_coordinates
' Creates a tabular note which lists all Points of the current work part.
' Additionaly it creates little ID notes at these points in the selected
' drawing member view.
' For testing, create a part with a simple PointSet and a TOP drawing view.
' Note: The tabular note and the ID notes are not associative yet...
Dim s As Session = Session.GetSession()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim theUI As UI = UI.GetUI()
Dim lw As ListingWindow = s.ListingWindow
Dim workPart As Part = s.Parts.Work
Sub Main()
Try
' Get all points in part
Dim pcol As PointCollection = workPart.Points
If pcol.ToArray().Length = 0 Then
lw.Open()
lw.WriteLine("No Points found. Exit.")
return
End If
' Get a view for the IDs
Dim dwgview As Drawings.BaseView
If select_a_drawing_member_view(dwgview) <> Selection.Response.Ok Then
return
End If
' Get a location for the tabular note
Dim cursor As Point3d
Dim response As Selection.DialogResponse = SelectScreenPos(cursor)
If response <> Selection.DialogResponse.Pick Then
return
End If
' Create the tabular note
Dim n_new_columns As Integer = 4
Dim tabnote As NXOpen.Tag = CreateTabnoteWithSize(0,n_new_columns,cursor)
' Get the column tags
Dim columns(n_new_columns-1) As NXOpen.Tag
For ii As Integer = 0 To n_new_columns-1
ufs.Tabnot.AskNthColumn(tabnote, ii, columns(ii))
Next
' Add Header Row
Dim headerrow As NXOpen.Tag
ufs.Tabnot.CreateRow(30, headerrow )
ufs.Tabnot.AddRow(tabnote, headerrow, UFConstants.UF_TABNOT_APPEND)
Dim cell As NXOpen.Tag
ufs.Tabnot.AskCellAtRowCol(headerrow, columns(0), cell)
ufs.Tabnot.SetCellText(cell, "ID")
ufs.Tabnot.AskCellAtRowCol(headerrow, columns(1), cell)
ufs.Tabnot.SetCellText(cell, "X")
ufs.Tabnot.AskCellAtRowCol(headerrow, columns(2), cell)
ufs.Tabnot.SetCellText(cell, "Y")
ufs.Tabnot.AskCellAtRowCol(headerrow, columns(3), cell)
ufs.Tabnot.SetCellText(cell, "Z")
' Add one row for each point
Dim letteringPrefs As LetteringPreferences = Nothing
Dim userSymPrefs As UserSymbolPreferences = Nothing
Dim jj As Integer = 0
For each pt As Point in pcol
Dim id As Integer = jj+1
' Get the Coordinates
Dim pt3d as Point3D = pt.Coordinates
' Add a row for each point
Dim row As NXOpen.Tag
ufs.Tabnot.CreateRow(30, row )
ufs.Tabnot.AddRow(tabnote, row, UFConstants.UF_TABNOT_APPEND)
ufs.Tabnot.AskCellAtRowCol(row, columns(0), cell)
ufs.Tabnot.SetCellText(cell, id.ToString())
' Set the cell text
ufs.Tabnot.AskCellAtRowCol(row, columns(1), cell)
ufs.Tabnot.SetCellText(cell, pt3d.X.ToString())
ufs.Tabnot.AskCellAtRowCol(row, columns(2), cell)
ufs.Tabnot.SetCellText(cell, pt3d.Y.ToString())
ufs.Tabnot.AskCellAtRowCol(row, columns(3), cell)
ufs.Tabnot.SetCellText(cell, pt3d.Z.ToString())
' Add ID notes to the points
AddNoteToPoint(id, pt3d, dwgview)
jj = jj+1
Next
Catch ex As Exception
lw.Open()
lw.WriteLine(ex.GetBaseException.ToString())
Catch ex As NXOpen.NXException
lw.Open()
lw.WriteLine(ex.Message)
End Try
End Sub
Public Function SelectScreenPos(ByRef pos As Point3d) As Selection.DialogResponse
Dim view As NXOpen.View = Nothing
Dim letteringPrefs As LetteringPreferences = Nothing
Dim userSymPrefs As UserSymbolPreferences = Nothing
return theUI.SelectionManager.SelectScreenPosition("Select location for tabnote", view, pos)
End Function
Function select_a_drawing_member_view(ByRef dwgview As Drawings.BaseView)
Dim ui As UI = GetUI()
Dim mask(0) As Selection.MaskTriple
With mask(0)
.Type = UFConstants.UF_view_type
.Subtype = UFConstants.UF_view_imported_subtype
.SolidBodySubtype = 0
End With
Dim cursor As Point3d = Nothing
Dim vw As View = nothing
Dim resp As Selection.Response = _
ui.SelectionManager.SelectObject("Select a drawing member view", _
"Select a drawing member view", _
Selection.SelectionScope.AnyInAssembly, _
Selection.SelectionAction.ClearAndEnableSpecific, _
False, False, mask, vw, cursor)
If resp = Selection.Response.ObjectSelected Or _
resp = Selection.Response.ObjectSelectedByName Then
dwgview = CType(vw, Drawings.BaseView)
return Selection.Response.Ok
Else
return Selection.Response.Cancel
End If
End Function
Public Function CreateTabnoteWithSize( _
ByVal nRows As Integer, ByVal nColumns As Integer, ByVal loc As Point3d) As NXOpen.Tag
' Create the tabular note
Dim secPrefs As UFTabnot.SectionPrefs
ufs.Tabnot.AskDefaultSectionPrefs(secPrefs)
Dim cellPrefs As UFTabnot.CellPrefs
ufs.Tabnot.AskDefaultCellPrefs(cellPrefs)
cellPrefs.zero_display = UFTabnot.ZeroDisplay.ZeroDisplayZero
ufs.Tabnot.SetDefaultCellPrefs(cellPrefs)
Dim origin(2) As Double
origin(0) = loc.X
origin(1) = loc.Y
origin(2) = loc.Z
Dim tabnote As NXOpen.Tag
ufs.Tabnot.Create(secPrefs, origin, tabnote)
' Delete all existing columns and rows (we create them as needed)
Dim nmRows As Integer = 0
ufs.Tabnot.AskNmRows(tabnote, nmRows)
For ii As Integer = 0 To nmRows-1
Dim row As NXOpen.Tag
ufs.Tabnot.AskNthRow(tabnote, 0, row)
ufs.Tabnot.RemoveRow(row)
ufs.Obj.DeleteObject(row)
Next
Dim nmColumns As Integer = 0
ufs.Tabnot.AskNmColumns(tabnote, nmColumns)
For ii As Integer = 0 To nmColumns-1
Dim column As NXOpen.Tag
ufs.Tabnot.AskNthColumn(tabnote, 0, column)
ufs.Tabnot.RemoveColumn(column)
ufs.Obj.DeleteObject(column)
Next
' Now add our columns as needed
Dim columns(nColumns-1) As NXOpen.Tag
For ii As Integer = 0 To nColumns-1
ufs.Tabnot.CreateColumn(30, columns(ii))
ufs.Tabnot.AddColumn(tabnote, columns(ii), UFConstants.UF_TABNOT_APPEND)
Next
' Now add our rows as needed
Dim rows(nRows-1) As NXOpen.Tag
For ii As Integer = 0 To nRows-1
ufs.Tabnot.CreateRow(30, rows(ii))
ufs.Tabnot.AddRow(tabnote, rows(ii), UFConstants.UF_TABNOT_APPEND)
Next
return tabnote
End Function
Function AddNoteToPoint(ByVal id As Integer, ByVal pt As Point3d, ByVal dwgview As Drawings.BaseView)
Dim nullAnnotations_SimpleDraftingAid As Annotations.SimpleDraftingAid = Nothing
Dim draftingNoteBuilder1 As Annotations.DraftingNoteBuilder
draftingNoteBuilder1 = workPart.Annotations.CreateDraftingNoteBuilder(nullAnnotations_SimpleDraftingAid)
Dim text1(0) As String
text1(0) = id.ToString()
draftingNoteBuilder1.Text.TextBlock.SetText(text1)
draftingNoteBuilder1.Origin.AnnotationView.Value = dwgview
draftingNoteBuilder1.Origin.Origin.SetValue(Nothing, Nothing, pt)
draftingNoteBuilder1.Commit()
draftingNoteBuilder1.Destroy()
End Function
Public Function GetUnloadOption(ByVal dummy As String) As Integer
Return Session.LibraryUnloadOption.Immediately
End Function
End Module