Change translucency of the selected part

I am new user and this is my first journal.
I'd like to have a button to set the translucency of the selected part to 70%. I have recorded the following journal, but it basically does it only for the part that I selected at during the record procedure.


using System;
using NXOpen;

public class NXJournal
{
public static void Main(string[] args)
{
NXOpen.Session theSession = NXOpen.Session.GetSession();
NXOpen.Part workPart = theSession.Parts.Work;
NXOpen.Part displayPart = theSession.Parts.Display;
// ----------------------------------------------
// Menu: Edit->Object Display...
// ----------------------------------------------
NXOpen.Session.UndoMarkId markId1;
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Edit Object Display");

NXOpen.DisplayModification displayModification1;
displayModification1 = theSession.DisplayManager.NewDisplayModification();

displayModification1.ApplyToAllFaces = true;

displayModification1.ApplyToOwningParts = false;

displayModification1.NewTranslucency = 79;

NXOpen.DisplayableObject[] objects1 = new NXOpen.DisplayableObject[1];
NXOpen.Assemblies.Component component1 = (NXOpen.Assemblies.Component)workPart.ComponentAssembly.RootComponent.FindObject("COMPONENT model3 1");
objects1[0] = component1;
displayModification1.Apply(objects1);

displayModification1.Dispose();
// ----------------------------------------------
// Menu: Tools->Journal->Stop Recording
// ----------------------------------------------

}
public static int GetUnloadOption(string dummy) { return (int)NXOpen.Session.LibraryUnloadOption.Immediately; }
}

Could you please guide me on how I can change it so that it validates the variable component1 to the selected part instead of the part named "model3"?

Thanks in advance.

i think you should have the part selected prior to start recording your macro.

Great, thank you very much!

Did you get it working then?

Yes, I recorded another journal with the component selected before before. It then gave me all of the necessary methods to get access to the selected component. Thanks again. Can I set your comment as answer here in this forum?

We don't currently have the option to mark a post as an "answer"; perhaps in a future site update...

Anyway, even if you could, Wouter should get the credit, not me.

You're welcome, I made the same journal couple of weeks ago ;)

Now i notice that when you select multiple parts (to set the translucency), it only applies it to the first selected component.
Does anyone know how to improve this script so it works with more components?


Option Strict Off
Imports System
Imports NXOpen

Module NXJournal
Sub Main (ByVal args() As String)

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

Dim displayPart As Part = theSession.Parts.Display

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

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

displayModification1.ApplyToAllFaces = False

displayModification1.ApplyToOwningParts = False

displayModification1.NewTranslucency = 50

Dim objects1(0) As DisplayableObject
Dim theUI As UI = UI.GetUI()

objects1(0) = CType(theUI.SelectionManager.GetSelectedObject(0), Assemblies.Component)
displayModification1.Apply(objects1)

displayModification1.Dispose()
' ----------------------------------------------
' Menu: Tools->Journal->Stop Recording
' ----------------------------------------------

End Sub
End Module

You'll need some sort of loop to process all of the preselected objects. The code below sorts the components out of the preselected objects and applies the display change to the components only.

Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpenUI

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUI As UI = UI.GetUI

If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If

Dim numsel As Integer = theUI.SelectionManager.GetNumSelectedObjects()
Dim theComps As New List(Of Assemblies.Component)

For i As Integer = 0 To numsel - 1
Dim selObj As TaggedObject = theUI.SelectionManager.GetSelectedTaggedObject(i)
If TypeOf (selObj) Is Assemblies.Component Then
theComps.Add(selObj)
End If
Next

If theComps.Count = 0 Then
'no components found among the preselected objects
Return
End If

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Edit Object Display")

Dim displayModification1 As DisplayModification
displayModification1 = theSession.DisplayManager.NewDisplayModification()
With displayModification1
.ApplyToAllFaces = False
.ApplyToOwningParts = False
.NewTranslucency = 50
.Apply(theComps.ToArray)
End With
displayModification1.Dispose()

End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer

'Unloads the image immediately after execution within NX
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

End Function

End Module

Thankyou!! works perfectly.

Very interesting!! I want to change translucency of parts in Model, not in assembly, so how can i select and loop in model, please help me, thank you.

Each part has a collection named .Bodies that contains all the solid and sheet bodies in the part. If you are working with a part (not an assembly), you can iterate through the .Bodies collection to find and update the desired bodies.

HI, so thanks your responding. i am a beginer so i don't know written codes, i mean i want to change the translucency of the bodies.

If you want to change the translucency of all the bodies in the current work part, the code might look something like the following (note that component bodies will not be affected, the bodies must be defined in the work part):

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpenUI

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUI As UI = UI.GetUI

If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Edit Object Display")

Dim displayModification1 As DisplayModification
displayModification1 = theSession.DisplayManager.NewDisplayModification()
With displayModification1
.ApplyToAllFaces = False
.ApplyToOwningParts = False
.NewTranslucency = 50
.Apply(theSession.Parts.Work.Bodies.ToArray)
End With
displayModification1.Dispose()

End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer

'Unloads the image immediately after execution within NX
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

End Function

End Module

Thank you to much, but i still can not modify the code, i have an idea when i select some body and want to change it's translucency into 2 case, if the translucency value <> 50% then change to 50%, else if translucency value = 50% then change to 0%
But i don't know how to change just some bodies selected and don't know how to deal with the cases
Please help me in next time, i am looking for solution

hi, i don't know how to change Dim theComps As New List(Of Assemblies.Component) to the list of body in workpart.
please tell me the way, thank you alot

Hi, i written a code, but it very lag when i call it from Journal button shortcut
i also can not select more bodies, please help me to enhance it

Imports System
Imports NXOpen
Imports NXOpen.Selection ' Added to make selection code simpler
Imports NXOpen.UF
Module NXJournal
Sub Main(ByVal args() As String)
Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
Dim theUFSession As UFSession = UFSession.GetUFSession
Dim workPart As NXOpen.Part = theSession.Parts.Work
Dim displayPart As NXOpen.Part = theSession.Parts.Display
' ----------------------------------------------
' Menu: Edit->Object Display...
' ----------------------------------------------
Dim markId1 As NXOpen.Session.UndoMarkId = Nothing
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Edit Object Display")
Dim objectArray1(0) As NXOpen.DisplayableObject

'Dim body1 As NXOpen.Body = CType(workPart.Bodies.FindObject("CYLINDER(2)"), NXOpen.Body)
' Ask user to select object
Dim body1 As Body = SelectBody()
If body1 Is Nothing Then
Return
End If
Dim percentTranslucent As Integer
theUFSession.Obj.AskTranslucency(body1.Tag, percentTranslucent)

Dim displayModification1 As DisplayModification = theSession.DisplayManager.NewDisplayModification()
objectArray1(0) = body1

If percentTranslucent.ToString <> 50 Then
displayModification1.NewTranslucency = 50
displayModification1.Apply(objectArray1)
Else
displayModification1.NewTranslucency = 0
displayModification1.Apply(objectArray1)
End If

'displayModification1.Dispose()
' ----------------------------------------------
' Menu: Tools->Journal->Stop Recording
' ----------------------------------------------
End Sub
' Function to ask user to select a body
Function SelectBody() As Body
Dim body1 As Body = Nothing
' Get UI object
Dim theUI As NXOpen.UI = NXOpen.UI.GetUI()
Dim sel As NXOpen.Selection = theUI.SelectionManager
Dim objects1(0) As NXOpen.DisplayableObject
Dim selObj As TaggedObject
Dim cursor As Point3d
Dim message As String = "Select Body"
Dim title As String = "Selection"
Dim resp As Response = sel.SelectTaggedObject(message, title, SelectionScope.UseDefault,
False, False, selObj, cursor)
If resp <> Response.Back And resp <> Response.Cancel Then
If TypeOf selObj Is Body Then
body1 = CType(selObj, Body)
End If
End If
Return body1
End Function
End Module

If you want to select multiple objects, you will need to use the .SelectTaggedObjects method (note the "s" at the end). An example can be found here:
http://nxjournaling.com/comment/5416#comment-5416

Also, this line of your code may be causing an, as yet undetected, issue:

If percentTranslucent.ToString <> 50 Then

The percentTranslucent variable is declared as an integer; in the line of code above, that value is converted to a string and then compared to an integer value. This particular comparison should always return TRUE, because a string value will never equal an integer value. I'd suggest eliminating the .ToString conversion and just comparing the values directly:

If percentTranslucent <> 50 Then

Hi, Thanks for your Respone, are there have examples to deal with the sitiation is
if i already selected bodies before run the code then the code will apply to selected bodies, if not the selection prompt will appear.

http://nxjournaling.com/comment/553#comment-553

The code in the link above works on "pre-selected" objects; select one or more objects before running the code. The journal will report the object type and tag for each selected object.

In your case, you'd want to look for "body" objects and apply your desired display modification to only those objects. If no objects are selected (or no body objects are selected), you can prompt the user to select objects with the .SelectTaggedObjects command.

Hi,
Your Code working perfectly to assign Colors and layers to components in assembly level, Thanks for that,
but i need a small change that instead of selecting color code i would like to add through RGB for example R=255, G=255, B=9,
How to add these in code.

Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpenUI

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUI As UI = UI.GetUI

If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If

Dim numsel As Integer = theUI.SelectionManager.GetNumSelectedObjects()
Dim theComps As New List(Of Assemblies.Component)

For i As Integer = 0 To numsel - 1
Dim selObj As TaggedObject = theUI.SelectionManager.GetSelectedTaggedObject(i)
If TypeOf (selObj) Is Assemblies.Component Then
theComps.Add(selObj)
End If
Next

If theComps.Count = 0 Then
'no components found among the preselected objects
Return
End If

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Edit Object Display")

Dim displayModification1 As DisplayModification
displayModification1 = theSession.DisplayManager.NewDisplayModification()
With displayModification1
.ApplyToAllFaces = False
.ApplyToOwningParts = False
.NewColor = 6
.NewLayer = 110
.Apply(theComps.ToArray)
End With
displayModification1.Dispose()

End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer

'Unloads the image immediately after execution within NX
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

End Function

End Module

-[]-

NX uses an indexed palette, this means that it uses a limited number of pre-defined colors. If you need to specify a specific RGB value, you would need to customize the color definition file (CDF) that the part uses. Alternatively, there is a function that, given an RGB value, will return the closest color defined in the CDF (.AskClosestColor).

You might find some interesting information in the following thread:
http://nxjournaling.com/content/nxcolor-class

How to Load CDF File From a journal in a part or assembly??

-[]-

I don't know of an API command that will load a CDF file directly, but there is some code in the link below that may be of use to you.

http://www.eng-tips.com/viewthread.cfm?qid=388210