top of page

How to Extract Dimension Text from a Drawing Sheet Using NXOpen and VB.NET

Mar 23

3 min read

0

19

0

If you're a mechanical design engineer diving into NXOpen customization, one of the best ways to learn is by writing small programs that solve real tasks.

In this post, I'll walk you through a simple yet powerful example — extracting dimension text from a drawing sheet using NXOpen API in VB.NET.


📌 Objective

The goal of this script is to:

  • Access the currently active drawing sheet in Siemens NX.

  • Cycle through all dimension objects.

  • Extract and display the main text of each dimension.

This is especially useful when:

  • Reviewing dimensions programmatically.

  • Building automation around drawing validation.

  • Learning how to work with annotations via NXOpen.


In this video, I will explore how to find hole dimensions in a drawing using UF_OBJ_cycle_objs_in_part in NX Open. UF_OBJ_cycle_objs_in_part is a NX Open API that allows us to iterate through all the objects in a part or assembly.
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Annotations
Imports NXOpen.Utilities
Module report_notes_of_drawing_sheet
    Dim theSession As Session = Session.GetSession()
    Dim theUFSession As UFSession = UFSession.GetUFSession()
    Sub Main(ByVal args() As String)
        Dim displayPart As Part = theSession.Parts.Display
        Dim currentTag As NXOpen.Tag = NXOpen.Tag.Null
        Do
            theUFSession.Obj.CycleObjsInPart(displayPart.Tag, UFConstants.UF_dimension_type, currentTag)
            If currentTag = NXOpen.Tag.Null Then
                Exit Do
            End If
            Try
                Dim dimObj As NXOpen.Annotations.Dimension = _
                    CType(NXObjectManager.Get(currentTag), NXOpen.Annotations.Dimension)
                If dimObj IsNot Nothing Then
                    Dim mainText() As String
                    Dim dualText() As String
                    dimObj.GetDimensionText(mainText, dualText)
                    If mainText.Length > 0 Then
                        theSession.ListingWindow.Open()
                        theSession.ListingWindow.WriteLine("Dimension Text: " & mainText(0))
                    End If
                End If
            Catch ex As System.Exception
                theSession.ListingWindow.Open()
                theSession.ListingWindow.WriteLine("Error: " & ex.Message)
            End Try
        Loop
    End Sub
End Module

Here’s a breakdown of the most important concepts and how they’re implemented in the code:


1. Session and UFSession Initialization

Every NXOpen program starts by connecting to the current NX session.

  • Session.GetSession() gives access to the current NX environment, including active parts, UI, and Listing Window.

  • UFSession.GetUFSession() gives access to the low-level UF (User Function) API, which is used for operations like cycling through objects.

Think of Session as the gateway to high-level NX functionality and UFSession for low-level operations.


2. Accessing the Active Drawing Sheet

This line gets the currently displayed part, which in the case of a drawing file, is the drawing sheet you’re working on.


3. Cycling Through All Dimension Objects

  • CycleObjsInPart() is a loop function that fetches one object at a time of a specified type.

  • UF_dimension_type tells the UF API to find only dimension annotations.

  • The loop ends when currentTag becomes NXOpen.Tag.Null, indicating no more matching objects.

This is a classic pattern for scanning objects in NX using the UF API.


4. Converting Tag to Dimension Object

NX stores objects using internal "tags" (like pointers).To work with these in VB.NET, you must convert them to NXOpen objects using NXObjectManager.Get().

Then, using CType, we safely cast it into a Dimension object.


 5. Reading Dimension Text

  • GetDimensionText() retrieves both the primary and dual text of a dimension.

  • These are returned as arrays because some dimensions have multi-line annotations.

We’re focusing on mainText(0) — the first line of the main dimension.


6. Displaying Output in the Listing Window

NX provides a built-in Listing Window for debugging and displaying custom messages.We open it (if not already open), and print each dimension's text.

This is super helpful for reviewing output without needing file exports.


 7. Error Handling

In case something goes wrong (like an invalid cast or missing data), the program won’t crash. Instead, it logs the error in the listing window — a clean way to handle exceptions during object cycling.

Mar 23

3 min read

0

19

0

Comments

Share Your ThoughtsBe the first to write a comment.

Join the growing community of Mechanical Design Engineers turning into Design Automation Engineers, Today!

  • Whatsapp
  • Facebook
  • X
  • Twitter
  • LinkedIn

©2024 by Design Automation Life

bottom of page