cancel
Showing results for 
Search instead for 
Did you mean: 

Error in Sample NX Open .NET Visual Basic program : transform copy multiple bodies _nx_api3335

Pioneer
Pioneer

Error.PNG

 Hi

I have used the above code and ended up with the above error.

 

https://solutions.industrysoftware.automation.siemens.com/view.php?sort=desc&q=%22sample%22%22NX%22%...

 

 

Option Strict Off

Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities

Module transform_copy_multiple_bodies

   Public s As Session = Session.GetSession()
   Public ufs As UFSession = UFSession.GetUFSession()
   Public lw As ListingWindow = s.ListingWindow

   Sub Main()

      Dim transformationMatrix(15) As Double

      Dim objects(0) As NXOpen.Tag

      Dim count As Integer = 1
      'Dim move As Integer = 1
      Dim copy As Integer = 2

      Dim layer As Integer = 0 ' original layer
      Dim trace_curves As Integer = 2 ' Off
      Dim returnStatus As Integer = -1
      Dim translation() As Double = {100, 100, 100}

      ufs.Ui.LockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)

      Dim objs() As NXObject = Nothing
      Dim theUI As UI = UI.GetUI

      Dim mask(0) As Selection.MaskTriple
      mask(0) = New Selection.MaskTriple(UFConstants.UF_solid_type, _
                                         UFConstants.UF_solid_body_subtype, _
                                         UFConstants.UF_UI_SEL_FEATURE_BODY)
      Dim cp As Point3d = Nothing

      Dim sel1 As Selection.Response

      Do
         sel1 = theUI.SelectionManager.SelectObjects("Select Bodies", _
             "Select Bodies", Selection.SelectionScope.WorkPart, _
             Selection.SelectionAction.EnableSpecific, _
             False, False, mask, objs)
      Loop While (sel1 = Selection.Response.Cancel) Or _
                  (sel1 = Selection.Response.Back)

      ufs.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)

      Dim counter As Integer = 0

      ' No occurrences should have been selected 
      ' with the selection scope set to work part, 
      ' but this will screen them out anyway

      For Each thisObj As NXObject In objs
         Dim isOcc As Boolean = thisObj.IsOccurrence()
         If isOcc = False Then
            ReDim Preserve objects(counter)
            objects(counter) = thisObj.Tag
            counter += 1
         End If
      Next

      ' the output array must be large enough to hold
      ' all of the objects that will result from
      ' performing the copy operation

      Dim copiedObjects(counter - 1) As NXOpen.Tag

      ufs.Trns.CreateTranslationMatrix(translation, transformationMatrix)

      ufs.Trns.TransformObjects(transformationMatrix, objects, _
                                counter, copy, layer, trace_curves, _
                                  copiedObjects, Nothing, returnStatus)

      ufs.View.FitView(NXOpen.Tag.Null, 0.9)

      If returnStatus <> 0 Then
         lw.WriteLine("Return Status from TransformObjects: " & returnStatus)
      End If

      If counter > 0 Then
         lw.Open()
      End If

      For Each copiedTag As NXOpen.Tag In copiedObjects
         lw.WriteLine("Copied Object Tag: " & copiedTag.ToString())
      Next

   End Sub

   Public Function GetUnloadOption(ByVal dummy As String) As Integer

      Return Session.LibraryUnloadOption.Immediately

   End Function

End Module

 

Please suggest.

 

 

Thanks 

 

11 REPLIES

Re: Error in Sample NX Open .NET Visual Basic program : transform copy multiple bodies _nx_api3335

Phenom
Phenom

The translation matrix used to be a 16 element matrix, it was changed to a 12 element matrix (I'm not sure when the change happened). Change the declaration of your matrix to be a 12 element matrix:

Dim transformationMatrix(11) As Double

Re: Error in Sample NX Open .NET Visual Basic program : transform copy multiple bodies _nx_api3335

Siemens Phenom Siemens Phenom
Siemens Phenom

 

Good catch, cowski1!  That example is almost nine years old, and apparently was initially tested in NX5.

 

I will update it so that it works with the current versions.

 

Thanks!

 

Re: Error in Sample NX Open .NET Visual Basic program : transform copy multiple bodies _nx_api3335

Pioneer
Pioneer

Thanks for the reply.As this pretty old code it is not inline with the new way of code written.

What i am really interested is to copy the selected Component (not the object)and to transform it.

What the above one does is selects the body alone not the component and transforms it.

 

So i have wriiten the below code for copying the component in an assembly and paste it in the same assembly and 

translate it (will be done in the later part of the code).

Option Strict Off 
Imports System
Imports System.IO
Imports System.Collections.Generic
imports System.Windows.Forms
Imports NXOpen
Imports NXOpen.UF
Imports NXOpenUI

Module NXJournal
Sub Main

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work

	Dim selComp As NXOpen.Assemblies.Component = Nothing
        If SelectComponent(selComp) = Selection.Response.Cancel Then
            Exit Sub
        End If

Dim displayPart As Part = theSession.Parts.Display

workPart.componentAssembly().copyComponents()

workPart.PmiManager.RestoreUnpastedObjects()
End Sub

Function SelectComponent(ByRef selObj As TaggedObject) As Selection.Response
 
        Dim theUI As UI = UI.GetUI
        Dim message As String = "Select component to reset position"
        Dim title As String = "Select a Component"
        Dim includeFeatures As Boolean = False
        Dim keepHighlighted As Boolean = False                
        Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
        Dim cursor As Point3d
        Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
        Dim selectionMask_array(0) As Selection.MaskTriple
 
        With selectionMask_array(0)
            .Type = UFConstants.UF_component_type
            .Subtype = UFConstants.UF_all_subtype
        End With
 
        Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(message, _
         title, scope, selAction, _
         includeFeatures, keepHighlighted, selectionMask_array, _
         selObj, cursor)
        If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If
 
    End Function
 
    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        GetUnloadOption = NXOpen.UF.UFConstants.UF_UNLOAD_IMMEDIATELY
    End Function
 
	Public Function ReadLine(lineNumber As Integer, lines As List(Of String)) As String
    Return lines(lineNumber - 1)
End function
End Module

I have wriiten a small piece of code for copying the components on selection.

The error i get on running the code is 

 

12.PNG

 

If you could solve this ,i can go ahead copy the components and paste it in the assembly.

 

Thanks

Re: Error in Sample NX Open .NET Visual Basic program : transform copy multiple bodies _nx_api3335

Phenom
Phenom

If the main assembly is the currently displayed part (which is most likely the case), try changing this code:

workPart.componentAssembly().copyComponents()

to this:

displayPart.componentAssembly.copyComponents({selComp})

The curly braces around the variable name tells the compiler to treat it as a single value array variable instead of a normal variable. That should eliminate the error that you were getting. Also, you can see that I changed "workPart" to "displayPart" as the assembly is almost certainly your currently displayed part; however, I have no idea what your current work part is (it may or may not be the assembly).

Re: Error in Sample NX Open .NET Visual Basic program : transform copy multiple bodies _nx_api3335

Pioneer
Pioneer

Hi Cowski

Thanks for the reply .

Some more extra info.

If i would like to instance the number of objects say the selected objects what would the code for that?

Once instance made .I would like still keep them selected .Say i select a part file and then make  10 copy of it.Then i would like to have 11 parts (1+10) selected.

Please suggest.

 

Re: Error in Sample NX Open .NET Visual Basic program : transform copy multiple bodies _nx_api3335

Pioneer
Pioneer

Hi Cowski

 

If i would like to instance the number of objects say the selected objects what would the code for that?

Please let me know the Pseudo code for that?

Thanks

Re: Error in Sample NX Open .NET Visual Basic program : transform copy multiple bodies _nx_api3335

Phenom
Phenom

You could add a loop to your existing code to copy the component multiple times. Or, you could use the "pattern component" command to get multiple copies. I don't know what the code would look like for the pattern component; I'd suggest recording a journal while using the command to get some code to work with.

Re: Error in Sample NX Open .NET Visual Basic program : transform copy multiple bodies _nx_api3335

Pioneer
Pioneer
Thanks Cowski
That's perfect I could loop with my earlier code would the possible solution.
Yes also let me explore the pattern component one as well.



Sent from my iPhone

Re: Error in Sample NX Open .NET Visual Basic program : transform copy multiple bodies _nx_api3335

Valued Contributor
Valued Contributor
The same error appears from the following example:
https://solutions.industrysoftware.automation.siemens.com/view.php?sort=desc&q=Rotate+a+Component&dt...
NX 9.0.3.4 MP13
VB.net
Visual Studio 2015