Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations MintJulep on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro / Script to modify all NX files in a folder one by one 1

Status
Not open for further replies.

Lennoluinge

Marine/Ocean
May 27, 2009
4
Hi,

I have to change the color of a large number of JT files in a folder. I recorded a journal in NX5 to change the color, but I can't find a way to let NX open all files in the folder one by one. Now it only opens Part1, but I like to create some kind of a batch proces.

Any ideas?


Thanks...





' NX 5.0.4.1
' Journal created by llu on Wed May 27 11:11:17 2009 W. Europe Daylight Time
'
Option Strict Off
Imports System
Imports NXOpen

Module NXJournal
Sub Main

Dim theSession As Session = Session.GetSession()
' ----------------------------------------------
' Menu: File->Open...
' ----------------------------------------------
Dim basePart1 As BasePart
Dim partLoadStatus1 As PartLoadStatus
basePart1 = theSession.Parts.OpenBaseDisplay("C:\Documents and Settings\llu\Desktop\FOLDER1\FILE1.jt", partLoadStatus1)

Dim workPart As Part = theSession.Parts.Work

Dim displayPart As Part = theSession.Parts.Display

partLoadStatus1.Dispose()
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Enter Gateway")

Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Open Part Navigator")

Dim markId3 As Session.UndoMarkId
markId3 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Enter Gateway")

' ----------------------------------------------
' Menu: View->Operation->Fit
' ----------------------------------------------
workPart.ModelingViews.WorkView.Fit()

' ----------------------------------------------
' Menu: Edit->Object Display...
' ----------------------------------------------
Dim markId4 As Session.UndoMarkId
markId4 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Start")

theSession.SetUndoMarkName(markId4, "Class Selection")

' Refer to the sample NXOpen application, Selection for "Select All" alternatives.
Dim markId5 As Session.UndoMarkId
markId5 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Class Selection")

theSession.DeleteUndoMark(markId5, Nothing)

theSession.DeleteUndoMark(markId4, Nothing)

' ----------------------------------------------
' Dialog Begin Edit Object Display
' ----------------------------------------------
Dim markId6 As Session.UndoMarkId
markId6 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Edit Object Display")

Dim displayModification1 As DisplayModification
displayModification1 = theSession.DisplayManager.NewDisplayModification()

displayModification1.ApplyToAllFaces = False

displayModification1.NewColor = 128

Dim objects1(0) As DisplayableObject
Dim facetedBody1 As Facet.FacetedBody = CType(workPart.FacetedBodies.FindObject("HANDLE R-96"), Facet.FacetedBody)

objects1(0) = facetedBody1
displayModification1.Apply(objects1)

displayModification1.Dispose()
' ----------------------------------------------
' Menu: File->Save
' ----------------------------------------------
Dim partSaveStatus1 As PartSaveStatus
partSaveStatus1 = workPart.Save(BasePart.SaveComponents.True, BasePart.CloseAfterSave.False)

partSaveStatus1.Dispose()
Dim partCloseResponses1 As PartCloseResponses
partCloseResponses1 = theSession.Parts.NewPartCloseResponses()

workPart.Close(BasePart.CloseWholeTree.False, BasePart.CloseModified.UseResponses, partCloseResponses1)

workPart = Nothing
displayPart = Nothing
partCloseResponses1.Dispose()
' ----------------------------------------------
' Menu: File->Open...
' ----------------------------------------------
' ----------------------------------------------
' Menu: Tools->Journal->Stop
' ----------------------------------------------

End Sub
End Module
 
Replies continue below

Recommended for you

You can process all files in the directory by passing in the folder name as an argument. You should run the journal from the ug command prompt. Modify your code to accept argument from your main method.

This is part of an example.

Sub Main(ByVal args As String())

Try

Dim theDirectory As String = args(0)

For Each fileName As String In My.Computer.FileSystem.GetFiles(theDirectory)

Dim loadStatus As NXOpen.PartLoadStatus = Nothing

Dim extension As String = Right$(fileName, 4)

If extension = ".prt" Or _
extension = ".PRT" Or _
extension = ".Prt" Then

Dim currentPart As Part = _
s.Parts.OpenBaseDisplay(fileName,loadStatus)

.....//and your code here.



Suresh
 
Thanks..ufsure, Do you know a way to make this work for all JT files in a folder as well?

grtz

Lenno
 
Ow, it is working for JT files as well ofcourse...

I will try it.

thanks!
 
Are you trying to change the color of a single face, single faceted body or all the faceted bodies in the jt file? If it is a single body or a single face you have to do more programming than the journal you have recorded.

Even otherwise still you need to cycle objects of 'faceted body' type and assign them color. Please look at examples in UGAnswer. 'uganswer.ugs.com'



Suresh
 
Its working!

thanks!



***************


' NX 5.0.6.3
' Journal created by cwei on Thu May 28 10:21:44 2009 Westeuropäische Normalzeit
'
Option Strict Off
Imports System
Imports NXOpen

Module NXJournal
Sub Main

For Each fileName As String In My.Computer.FileSystem.GetFiles("Y:\NX\Makro Change Color\JTs")

Dim theSession As Session = Session.GetSession()
' ----------------------------------------------
' Menu: File->Open...
' ----------------------------------------------
Dim basePart1 As BasePart
Dim partLoadStatus1 As PartLoadStatus
basePart1 = theSession.Parts.OpenBaseDisplay(fileName, partLoadStatus1)

Dim workPart As Part = theSession.Parts.Work

Dim displayPart As Part = theSession.Parts.Display

partLoadStatus1.Dispose()

' ----------------------------------------------
' Dialog Begin Edit Object Display
' ----------------------------------------------
Dim displayModification1 As DisplayModification
displayModification1 = theSession.DisplayManager.NewDisplayModification()

displayModification1.ApplyToAllFaces = False

displayModification1.NewColor = 150

Dim objects1(0) As DisplayableObject
Dim facetedBody1 As Facet.FacetedBody = CType(workPart.FacetedBodies.FindObject("HANDLE R-96"), Facet.FacetedBody)

objects1(0) = facetedBody1
displayModification1.Apply(objects1)

displayModification1.Dispose()
' ----------------------------------------------
' Menu: File->Save
' ----------------------------------------------
Dim partSaveStatus1 As PartSaveStatus
partSaveStatus1 = workPart.Save(BasePart.SaveComponents.True, BasePart.CloseAfterSave.False)

partSaveStatus1.Dispose()
Dim partCloseResponses1 As PartCloseResponses
partCloseResponses1 = theSession.Parts.NewPartCloseResponses()

workPart.Close(BasePart.CloseWholeTree.False, BasePart.CloseModified.UseResponses, partCloseResponses1)

workPart = Nothing
displayPart = Nothing
partCloseResponses1.Dispose()
' ----------------------------------------------
' Menu: Tools->Journal->Stop
' ----------------------------------------------
Next
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor