Showing results for 
Search instead for 
Do you mean 
Solved! Go to solution

How to pass the workpart/displayedpart as parameter to AskBoundingBoxExact ?

I want to create multiple boxes around all parts in an assembly, the example code shows that I can pass the selection as 




However, I would like to pass part instead of getting from the selection. 


I replaced 




but they do not work (errors return invalid parameter)


The working code with selection option

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UI
Imports NXOpen.Utilities
Imports NXOpen.UF
Module make_bounding_block_of_selected_body_relative_to_wcs
    Dim s As Session = Session.GetSession()
    Dim ufs As UFSession = UFSession.GetUFSession()
    Dim lw As ListingWindow = s.ListingWindow()
    Sub Main()
        Dim a_body As NXOpen.Tag = NXOpen.Tag.Null
        Dim csys As NXOpen.Tag = NXOpen.Tag.Null
        Dim target As NXOpen.Tag = NXOpen.Tag.Null
        Dim blockFeature As NXOpen.Tag = NXOpen.Tag.Null
        Dim min_corner(2) As Double
        Dim directions(2, 2) As Double
        Dim distances(2) As Double
        Dim edge_len(2) As String
        While select_a_body(a_body) = Selection.Response.Ok
            ufs.Modl.AskBoundingBoxExact(a_body, csys, min_corner, directions, _
            lw.WriteLine("Min_corner: " & _
                min_corner(0).ToString & ", " & _
                min_corner(1).ToString & ", " & _
                min_corner(2).ToString & ", ")
            lw.WriteLine("X direction: " & _
                directions(0, 0).ToString & ", " & _
                directions(0, 1).ToString & ", " & _
                directions(0, 2).ToString & ", ")
            lw.WriteLine("X distance: " & _
            lw.WriteLine("Y direction: " & _
                directions(1, 0).ToString & ", " & _
                directions(1, 1).ToString & ", " & _
                directions(1, 2).ToString & ", ")
            lw.WriteLine("Y distance: " & _
            lw.WriteLine("Z direction: " & _
                directions(2, 0).ToString & ", " & _
                directions(2, 1).ToString & ", " & _
                directions(2, 2).ToString & ", ")
            lw.WriteLine("Z distance: " & _
            edge_len(0) = distances(0).ToString()
            edge_len(1) = distances(1).ToString()
            edge_len(2) = distances(2).ToString()
            ufs.Modl.CreateBlock(FeatureSigns.Nullsign, _
                                 target, min_corner, edge_len, blockFeature)
        End While
    End Sub
    Function select_a_body(ByRef a_body As NXOpen.Tag) As Selection.Response
        Dim message As String = "Select a body"
        Dim title As String = "Select a body"
        Dim scope As Integer = UFConstants.UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY
        Dim response As Integer
        Dim view As NXOpen.Tag
        Dim cursor(2) As Double
        Dim ip As UFUi.SelInitFnT = AddressOf body_init_proc
            ufs.Ui.SelectWithSingleDialog(message, title, scope, ip, _
                         Nothing, response, a_body, cursor, view)
        End Try
        If response <> UFConstants.UF_UI_OBJECT_SELECTED And _
           response <> UFConstants.UF_UI_OBJECT_SELECTED_BY_NAME Then
            Return Selection.Response.Cancel
            ufs.Disp.SetHighlight(a_body, 0)
            Return Selection.Response.Ok
        End If
    End Function
    Function body_init_proc(ByVal select_ As IntPtr, _
                           ByVal userdata As IntPtr) As Integer
        Dim num_triples As Integer = 1
        Dim mask_triples(0) As UFUi.Mask
        mask_triples(0).object_type = UFConstants.UF_solid_type
        mask_triples(0).object_subtype = UFConstants.UF_solid_body_subtype
        mask_triples(0).solid_type = UFConstants.UF_UI_SEL_FEATURE_BODY
        ufs.Ui.SetSelMask(select_, _
                           UFUi.SelMaskAction.SelMaskClearAndEnableSpecific, _
                           num_triples, mask_triples)
        Return UFConstants.UF_UI_SEL_SUCCESS
    End Function
    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY
    End Function
Accepted by topic author Secret
‎05-31-2017 07:27 PM

Re: How to pass the workpart/displayedpart as parameter to AskBoundingBoxExact ?

You need to read the description of the UF wrapper in the .NET programmers reference to understand which objects can be used as the source.

Returns the bounding box information of wireframe and solid type objects
aligned to a CSYS.

Wireframe objects include lines, arcs, splines, and conics.  Solid type
objects include bodies, faces, and edges.  Bounding box values are
returned in absolute coordinate values according to where the object
exists in the part file and aligned to the input CSYS.

If you call this function with an occurrence, the bounding box
of the underlying geometry is transformed into assembly space.

Use occurrence object tags when working in an assembly context and prototype
object tags when working in non-assembly situations. Passing in a prototype
object tag when in an assembly may produce undesired results.

The csys_tag should always be in the context of the current work part.

To derive the corner points, use the X,Y,X components of the
min_corner  and then add the X,Y,Z components of the directions multiplied
by the X,Y,Z distances. For Example, to derive the 2 corner points:

  corner_pts[0][0] = min_corner[0]
  corner_pts[0][1] = min_corner[1]
  corner_pts[0][2] = min_corner[2]
  for i = 0 -> 2 inclusive
    corner_pts[1][i] = min_corner[i]
    for j = 0 -> 2 inclusive
      corner_pts[1][i] += directions[j][i] * distances[j]

While a more accurate box is produced, processing time may be increased

Environment: Internal  and  External
See Also: 
History: Originally released in NX V4.0
extern UFUNEXPORT int UF_MODL_ask_bounding_box_exact(
tag_t   object ,          /* <I>
                             Object identifier of object to ask bounding box.
tag_t   csys_tag,         /* <I>
                             CSYS to use for box alignment.
                             NULL_TAG - Use Work CSYS
double  min_corner[3],    /* <O>
                             Minimum corner of box of object.
                             [0] - minimum x value
                             [1] - minimum y value
                             [2] - minimum z value
double  directions[3][3], /* <O>: direction vectors of bounding box
                             [0][] - X Direction
                             [1][] - Y Direction
                             [2][] - Z Direction */
double  distances[3]      /* <O>
                             Distances to move along directions of CSYS
                             to derive all points of the bounding box.
                             [0] - X distance value
                             [1] - Y distance value
                             [2] - Z distance value

For the UF wrappers it is best to check the C header file for a detailed description if there are still doubts about their usage.

Stefan Pendl, Systemmanager CAx, HAIDLMAIR GmbH
Production: NX10.0.3, VERICUT 8.0, FBM, MRL 3.1.4 | TcUA 10.1 MP7 Patch 0 ( | TcVis 10.1
Development: VB.NET, Tcl/Tk    Testing: NX11.0 EAP, NX12.0 EAP

How to Get the Most from Your Signature in the Community