Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

UG NX automated code for Save-As assembly to new number and replace some part with new ones

Status
Not open for further replies.

bhushan12382

Mechanical
Sep 10, 2010
2
0
0
IN
Hello All,
I am new to programming and customisation in UG NX.

I have a task to Save as existing UG NX Assembly (Teamcentre integrated) and in this newly created assembly replace some part numbers that I have in txt or xls.

Can anyone help me to get NXopen or any code to automate this task. I have 100 Assembly parts to perform same task in it.

Thansk
 
Replies continue below

Recommended for you

I assume it can be automated with VB code.
There are good people that come on here that
should be able to guide you in the right direction.

Jerry J.
UGV5-NX1899
 
As Jerry says, the function is called "Clone".
and , one can run this clone tool by a logfile, i.e pretend that you are about to do a clone ( do a "dry run") , save the logfile .

Edit that logfile in an editor such as notepad, you can then use the logfile to clone another assembly .



The more you know about a subject, the more you know how little you know about that subject.
 
Hello,

First - You can do it with Clone. And if You have NX version bellow NX1947, You can copy data from clone window to Excel, change its in Excel and paste in the same place.
Second - If You need code in Vb, a long time ago, I "writed" some code for save as single (active) file in Native and TC:

Code:
' NX 12.0.2.9
' Journal created by MANok on Sat Jun  8 13:15:54 2019 Środkowoeuropejski czas letni
Option Strict Off
Imports System
Imports System.IO
Imports NXOpen
Imports NXOpenUI
Imports NXOpen.UI
Imports NXOpen.UF
Imports NXOpen.Utilities
Imports System.Math
Imports NXOpen.Features
Imports System.Collections.Generic
'Imports System.Windows.Forms
'Imports System.Reflection

Module Saver_as

    Public theSession As NXOpen.Session = NXOpen.Session.GetSession()
    Public workPart As NXOpen.Part = theSession.Parts.Work
    Public displayPart As NXOpen.Part = theSession.Parts.Display
    Public currentFile, currentPath As String
    Public Length As String
    Public check_save As Boolean = True
    Public name_length As Integer
	Public IsTcEng As Boolean

    Sub Main(ByVal args() As String)


        theSession.EnableRedo(False)
        Dim markId2 As Session.UndoMarkId
        markId2 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Change length and save")
        Threading.Thread.CurrentThread.CurrentCulture = New Globalization.CultureInfo("en-US")
        Threading.Thread.CurrentThread.CurrentUICulture = New Globalization.CultureInfo("en-US")
        'Check TC or native
        Dim theUFS As NXOpen.UF.UFSession = NXOpen.UF.UFSession.GetUFSession()
        IsTcEng = False
        theUFS.UF.IsUgmanagerActive(IsTcEng)



        Dim nazwa As String
        If IsTcEng Then
            nazwa = workPart.GetStringAttribute("DB_PART_NO")
        Else
            currentPath = Path.GetDirectoryName(workPart.FullPath)
            currentFile = Path.GetFileNameWithoutExtension(workPart.FullPath)

            nazwa = currentFile
        End If

        Dim EndName As String
		Dim FullEndName as String


        EndName = NXInputBox.GetInputString("Podaj nazwę pliku", "Save... the Queen...", nazwa)

		If IsTcEng = false then 
            FullEndName = currentPath & "\" & EndName
        End if
		
			
		Try
            If IsTcEng Then

				If check_save = True Then
                Dim partOperationCopyBuilder1 As NXOpen.PDM.PartOperationCopyBuilder = Nothing
                partOperationCopyBuilder1 = theSession.PdmSession.CreateCopyOperationBuilder(NXOpen.PDM.PartOperationBuilder.OperationType.SaveAs)
                partOperationCopyBuilder1.SetOperationSubType(NXOpen.PDM.PartOperationCopyBuilder.OperationSubType.Default)
                partOperationCopyBuilder1.DependentFilesToCopyOption = NXOpen.PDM.PartOperationCopyBuilder.CopyDependentFiles.All
                partOperationCopyBuilder1.ReplaceAllComponentsInSession = True
                partOperationCopyBuilder1.SetDialogOperation(NXOpen.PDM.PartOperationBuilder.OperationType.SaveAs)
                partOperationCopyBuilder1.DefaultDestinationFolder = partOperationCopyBuilder1.DefaultDestinationFolder = ":Normalia:Modyfikowane"

                Dim selectedparts3(0) As NXOpen.BasePart
                selectedparts3(0) = workPart
                Dim failedparts3() As NXOpen.BasePart
                partOperationCopyBuilder1.SetSelectedPartsToCopy(selectedparts3, failedparts3)

                Dim logicalobjects3() As NXOpen.PDM.LogicalObject
                partOperationCopyBuilder1.CreateLogicalObjects(logicalobjects3)

                Dim sourceobjects8() As NXOpen.NXObject
                sourceobjects8 = logicalobjects3(0).GetUserAttributeSourceObjects()

                Dim objects2(-1) As NXOpen.NXObject
                Dim attributePropertiesBuilder1 As NXOpen.AttributePropertiesBuilder = Nothing
                attributePropertiesBuilder1 = theSession.AttributeManager.CreateAttributePropertiesBuilder(workPart, objects2, NXOpen.AttributePropertiesBuilder.OperationType.None)

                Dim objects3(0) As NXOpen.NXObject
                objects3(0) = sourceobjects8(0)
                attributePropertiesBuilder1.SetAttributeObjects(objects3)

                attributePropertiesBuilder1.Title = "DB_PART_NO"
                attributePropertiesBuilder1.Category = "Item"
                attributePropertiesBuilder1.StringValue = EndName

                Dim changed1 As Boolean = Nothing
                changed1 = attributePropertiesBuilder1.CreateAttribute()

                attributePropertiesBuilder1.Title = "DB_PART_NAME"
                Dim changed2 As Boolean = Nothing
                changed2 = attributePropertiesBuilder1.CreateAttribute()

                partOperationCopyBuilder1.ValidateLogicalObjectsToCommit()
                Dim nXObject1 As NXOpen.NXObject = Nothing
                nXObject1 = partOperationCopyBuilder1.Commit()

                partOperationCopyBuilder1.Destroy()
                attributePropertiesBuilder1.Destroy()
				End If
				
            Else
                If check_save = True Then
                    Dim partSaveStatus1 As NXOpen.PartSaveStatus = Nothing
                    partSaveStatus1 = workPart.SaveAs(FullEndName & ".prt")
                    partSaveStatus1.Dispose()
                End If
            End If
        Catch ex As NXException
            MsgBox("Dany plik istnieje w określonej lokalizacji, nie mnóż bytów")
            theSession.UndoToMark(markId2, "Change length And save")

        Catch e As Exception
            theSession.UndoToMark(markId2, "Change length And save")
            MsgBox(e.Message)
            MsgBox(e.HResult)
        End Try

End1:
    End Sub


    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        'Unloads the image when the NX session terminates
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

    End Function
End Module

Here You can find steper by all component in assembly:

Read file from Excel, and create list to save (p.ex Dictionary (of String, String) (oldName, newName)
And for each component You must active and save as...
(and remove from list to save).

Sometimes the best helper is record You operation to journal and modificate it.

Try and if You will need detailed help - write.

Best regards

MANok
NX2006
TC14
 
Note that the cloning feature does a lot of things which might be difficult to note at a quick look,
for example, if you do a save as of a component in the cloned assembly during the clone operation, any assembly constraints , Interpart expressions, related drawings etc will be remapped to the new file-s.
Writing a program from scratch to replace the clone function, I guess there will be a long development time .

Regards,
Tomas

The more you know about a subject, the more you know how little you know about that subject.
 
Status
Not open for further replies.
Back
Top