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!

centerline, centermark color change by journal

Status
Not open for further replies.

niedzviedz

Mechanical
Apr 1, 2012
307
Hello everybody,

I would like to change every centerline (2D, 3D etc) and center mark to specific color, for example 186. I have found some example here Link but there is a problem. For example when I add:

Code:
centerline3dBuilder1.Settings.Color = 186

I receive an error: "cannot convert integer to NXOpen.NXcolor". Any one can help with this part?

I tried:

Dim color as NXcolor = (255,0,0) and also have some errors, so I changed my code to this:

Code:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.Annotations

Module NXJournal

Sub Main (ByVal args() As String) 

Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
Dim workPart As NXOpen.Part = theSession.Parts.Work
Dim displayPart As NXOpen.Part = theSession.Parts.Display
Dim lw As ListingWindow = theSession.ListingWindow
Dim count As Integer = 0
Dim Color as Integer = 186

lw.open

For each cline As Centerline in workPart.Annotations.Centerlines
    count = count+1
    'lw.WriteLine(count.ToString() & ". " & cline.ToString() & " Type: " & cline.GetType().ToString())

    If TypeOf cline Is NXOpen.Annotations.Centerline3d Then

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

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

		displayModification1.ApplyToAllFaces = True
		displayModification1.ApplyToOwningParts = False
		displayModification1.NewColor = Color

	Dim objects1(0) As NXOpen.DisplayableObject
	for each centerline2d1 As NXOpen.Annotations.Centerline2d in workPart.annotations.Centerlines

		objects1(0) = centerline2d1
		displayModification1.Apply(objects1)
		displayModification1.Dispose()

	next


    Else If TypeOf cline Is NXOpen.Annotations.Centerline2d Then

    Else If TypeOf cline Is NXOpen.Annotations.SymmetricalCenterline Then

    Else If TypeOf cline Is NXOpen.Annotations.CenterMark Then

    Else If TypeOf cline Is NXOpen.Annotations.BoltCircleCenterline Then

    Else 
        lw.WriteLine(" -> Unknown Centerline Type")
    End If

Next

Return

lw.close

End Sub
End Module

but i receive an error (memory access violation) in this line:
displayModification1.Apply(objects1)

any help will be appreciated.

With best regards
Michael
 
Replies continue below

Recommended for you

Your code creates a display modification object then sets up a loop to process all the centerlines; in the loop it calls .Apply and .Dispose. The first time through the loop, the display modification object is destroyed. If your file has multiple centerlines, the code will fail on the 2nd time through the loop. If you want to process all the centerlines, I'd suggest something like the code below:

Code:
Dim displayModification1 As NXOpen.DisplayModification
displayModification1 = theSession.DisplayManager.NewDisplayModification()

displayModification1.ApplyToAllFaces = True
displayModification1.ApplyToOwningParts = False
displayModification1.NewColor = 186

displayModification1.Apply(workPart.Annotations.Centerlines.ToArray)
displayModification1.Dispose()

www.nxjournaling.com
 
Thank @Cowski, it's work like a charm. I also figured it out first problem with NXcolor. I have to use
Code:
centerline3dBuilder1.Settings.Color = workPart.Colors.Find("Red")

It's a pity, that developers didn't implemented "color code" in NXopen.NXColor. I have found some mentions about NXcolor.RGB, but unfortunately I was unable to do it work.


With best regards
Michael
 
NX uses a limited palette of colors, you cannot assign your own arbitrary RGB values. If you have RGB values, you can find the closest color in the NX palette with .AskClosestColor or you can query the RGB values of an NX color with the .GetRgb method. The RGB values it reports are normalized, so you will need to multiply them by 255 to get the normal RGB values.

Code:
Option Strict Off
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports NXOpen
Imports NXOpen.UF

Module Module1

    Sub Main()

        Dim theSession As Session = Session.GetSession()
        Dim theUFSession As UFSession = UFSession.GetUFSession
        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()

        Dim myObj As NXObject
        Dim myDispObj As DisplayableObject

        Do Until SelectAnObject(myObj) = Selection.Response.Cancel

            lw.WriteLine("object type: " & myObj.GetType.ToString)
            myDispObj = DirectCast(myObj, DisplayableObject)

            lw.WriteLine("color number: " & myDispObj.Color)
            Dim myColor As NXColor
            myColor = theSession.Parts.Work.Colors.Find(myDispObj.Color)
            lw.WriteLine("color label: " & myColor.Label)
            lw.WriteLine("color rgb: " & myColor.GetRgb.ToString)
            Dim red As Integer = myColor.GetRgb.R * 255
            Dim green As Integer = myColor.GetRgb.G * 255
            Dim blue As Integer = myColor.GetRgb.B * 255

            lw.WriteLine("red: " & red.ToString)
            lw.WriteLine("green: " & green.ToString)
            lw.WriteLine("blue: " & blue.ToString)
            lw.WriteLine("")

        Loop

    End Sub

    Function SelectAnObject(ByRef selObj As NXObject) As Selection.Response

        Dim prompt As String = "Select an object"
        Dim theUI As UI = UI.GetUI
        Dim cursor As Point3d
        Dim typeArray() As Selection.SelectionType = _
            {Selection.SelectionType.All, _
                Selection.SelectionType.Faces, _
                Selection.SelectionType.Edges}

        Dim resp As Selection.Response = theUI.SelectionManager.SelectObject( _
                prompt, "Selection", _
                Selection.SelectionScope.AnyInAssembly, _
                False, typeArray, selObj, cursor)

        If resp = Selection.Response.ObjectSelected Or _
                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

        'Unloads the image when the NX session terminates
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

    End Function

End Module

www.nxjournaling.com
 
I know, NX only use 216 colors. What I wanna do is convert for example color 186 to fit NXcolor -> workPart.Colors.Find("Red") or to RGB values.

With best regards
Michael
 
The code above takes the color number and reports the NX color name and RGB values.

The .AskClosestColor method will accept RGB values and return the closest color in the NX palette. I know there is at least one example of it on eng-tips, if you want to search for it.

www.nxjournaling.com
 
Thanks Cowski, I will dig it closely.

With best regards
Michael
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor