Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Assign the same material to all component accross the assembly 2

Status
Not open for further replies.

PSI-CAD

Computer
Feb 13, 2009
997
0
0
FR
I know that there is no way to assign the same material to all component accross the assembly in one operation.

Does anybody have a journal to do this ?

Thanks in advance

I am using NX7.5 and NX8


Regards
Didier Psaltopoulos
 
Replies continue below

Recommended for you

Can't you just drawn a rectangular around the assembly so you have selected everything and drag the material to it? In the Assign Material menu you see beneath Type, Select Body(s), doesn't that mean you can pick multiple components?

Best regards,

Michaël.

NX7.5.4.4 + TC Unified 8.3
Win 7 64 bit (Intel(R) Xeon(R) CPU X5650 @2.67GHz)
24.0 GB
NVIDIA Quadro 4000 + NVIDIA Tesla C2050

 
Didier,

Please see if this meets your needs. Edit the desired material accordingly (two places).

HTH, Joe

Code:
' This program will walk an assembly structure.  For each component it will:
' Assign physical materials to the bodies in the components.
' This program changes the displayed part instead of simply changing the work part.
' Changing displayed part is slower than just changing work part, but it is safer.  If you
' have mixed unit parts (english / metric) then the program will error when trying to change
' work parts.

Option Strict Off
Imports System
Imports System.IO
Imports System.Collections

Imports NXOpen
Imports NXOpen.Assemblies
Imports NXOpen.UF
Imports NXOpenUI
Imports NXOpen.UIStyler

Module NXJournal
    ' Declare module variables.  These are available to all subroutines.
    Dim theSession As Session = Session.GetSession()
    Dim alreadyProcessed As Hashtable
    Dim knt As Integer = 0
    Dim partLoadStat As PartLoadStatus
    Dim LW As ListingWindow = theSession.ListingWindow
    Dim displayPart As Part = theSession.Parts.Display
    Dim workPart As Part

    Sub Main()  ' Program entry point
        LW.Open()
        LW.WriteLine("Listing child components")

        Try

            Dim part1 As Part = theSession.Parts.Work
            LW.WriteLine("Work part: " & part1.FullPath)
            LW.WriteLine(part1.Leaf)
            LW.WriteLine(CType(TimeOfDay(), String))

            ' Initialize a hash table to store components that have been processed.
            ' This will prevent components from being processed twice
            alreadyProcessed = New Hashtable

            Dim c As ComponentAssembly = part1.ComponentAssembly
		LW.WriteLine("first walk: ")
            Walk(c.RootComponent, 0)

            ' When the program is done "walking" the assembly it will return here.
            ' Set the work part back to what it was
			LW.WriteLine("Done: ")
            theSession.Parts.SetDisplay(part1, False, False, partLoadStat)

            LW.WriteLine(CType(TimeOfDay(), String))
        Catch e As Exception
            LW.WriteLine("Error running application: " & e.Message)
        End Try
    End Sub

    Sub Walk(ByVal c As Component, ByVal level As Integer)
	  LW.WriteLine("Walking: ")
        Dim ufs As UFSession = UFSession.GetUFSession()

        'Dim wrap As String = Chr(10)
        'Dim dwrap As String = wrap & wrap

        'Dim title As String
        'Dim value As String
        'Dim inx As Integer = 0
	
        Dim prototype As Part
        Dim children As Component() = c.GetChildren()

        Dim child As Component
        prototype = CType(c.Prototype, Part)

		'LW.WriteLine("Component: " & child.ToString)
             LW.WriteLine("Component: " & c.ToString)
		
        If Not alreadyProcessed.Contains(prototype.Leaf) Then

            ' Add this prototype to the hash table so that we don't process it again
            alreadyProcessed.Add(prototype.Leaf, prototype.Leaf)
            knt = knt + 1
            Dim msgText As String
            msgText = knt.ToString & " " & New String(" "c, (level * 4)) & prototype.Leaf
            'LW.WriteLine(msgText.ToString)
            theSession.Parts.SetDisplay(prototype, False, False, partLoadStat)
 '           displayPart = theSession.Parts.Display
             '==============
             Dim workPart As Part = theSession.Parts.Work


             Dim physicalMaterial1 As PhysicalMaterial

             Try
                 'load from library in case it is not used in the part yet
                  physicalMaterial1 = workPart.MaterialManager.PhysicalMaterials.LoadFromNxmatmllibrary("Aluminum_6061")
             Catch ex as Exception
                 ' the material is already known in the part so use it
                 physicalMaterial1 = CType(workPart.MaterialManager.PhysicalMaterials.FindObject("PhysicalMaterial[Aluminum_6061]"), PhysicalMaterial)
             End Try

             Dim bodies As BodyCollection = workPart.Bodies
             Dim solidBodies(0) As NXObject
             Dim counter As Integer = 0

             Dim bodyCount As Integer = bodies.ToArray.Length
      
             LW.WriteLine("Total Bodies in Work Part: " & bodyCount.ToString())
      
             If bodyCount > 0 Then

                 For Each thisBody As Body In bodies

                     If thisBody.IsSheetBody.Equals(False) Then
                         ReDim Preserve solidBodies(counter)
                         solidBodies(counter) = thisBody
                         LW.WriteLine("Solid Body: " & thisBody.ToString())
                         counter += 1
                     End If

                 Next

                 Dim solidBodyCount As Integer = solidBodies.Length()

                 LW.WriteLine("Solid Bodies in Work Part: " & _
                                solidBodyCount.ToString())

                 ' At this point, all solid bodies 
                 ' should be in an array called solidBodies
 
                 If (solidBodies.Length > 0) Then
                     physicalMaterial1.AssignObjects(solidBodies)
                 End If
                End If
          Else
              LW.WriteLine("Already processsed")
              'theSession.DisplayManager.MakeUpToDate()
        End if ' not already processed

        '==================
        For Each child In children
           LW.Writeline("next child = " & child.ToString)
            Walk(child, level + 1)
        Next
    End Sub


End Module
 
Hi Joe,

After some tests I found that if a material already exist in the part I have to confirm each time (attached jpeg file). could you add a command to delete all assigned material ?

TIA


Regards
Didier Psaltopoulos
 
Olaf,

At the risk of abbetting thread hijacking... ;-)

Here is a modified version of code that is available to all customers from the GTAC support site
[URL unfurl="true"]http://gtac.industrysoftware.automation.siemens.com/view.php?sort=desc&p=1&q=.DisplayName+.Net+Sample&i=nx_api3555&k=2&o=0[/url]
that reports part name, path and component names to get you started.

HTH, Joe

Code:
Imports System
Imports NXOpen
Imports NXOpen.Assemblies

Module NXJournal

    Dim s As session = session.GetSession()
    Dim lw As ListingWindow = s.ListingWindow
       Dim c_part As Part = Nothing

    Sub ShowAssemblyTree(ByVal c As Component, ByVal indent As String)
        Dim children As Component() = c.GetChildren()
        Dim newIndent As String
        For Each child As Component In children
            If indent.Length = 0 Then
                newIndent = " "
            Else
                newIndent = indent & " "
            End If

            c_Part = ctype(child.Prototype(),part)

            lw.WriteLine(newIndent & "Display Name: " & child.DisplayName)
            lw.WriteLine(newIndent & "Full Path: " & c_Part.FullPath)
            lw.WriteLine(newIndent & "Component  Name: " &child.Name)
            ShowAssemblyTree(child, newIndent)
        Next

    End Sub

    Sub Main(ByVal args As String())

        lw.Open()

        If args.Length = 0 And s.Parts.Work Is Nothing Then
            lw.WriteLine("Part file argument expected or work part required")
            Return
        End If

        Try
            Dim part1 As Part
            If args.Length = 0 Then
                part1 = s.Parts.Work
            Else
                Dim loadStatus1 As PartLoadStatus
                part1 = s.Parts.OpenDisplay(args(0), loadStatus1)
                loadStatus1.Dispose()
            End If

            Dim c As Component = part1.ComponentAssembly.RootComponent
            lw.WriteLine("*Display name: " & c.DisplayName)

            ShowAssemblyTree(c, " ")

        Catch e As Exception
            lw.WriteLine("Failed: " & e.Message)
        End Try
    End Sub
End Module
 
Didier,

Regarding:

Didier said:
After some tests I found that if a material already exist in the part I have to confirm each time (attached jpeg file). could you add a command to delete all assigned material ?

I understand the request for deleting any/all existing materials to avoid the dialog for "Multiple materials are assigned to Bodies, Please select the one to represent the part material".

This happens even when (re) assigning a new material interactively.

The function call to do that would be

Code:
physicalMaterial1.UnassignAllObjects()

I can't promise when/if I will be able to find time to supply tested code.

Happy New Year,

Joe
 
Didier,

Please the three lines, at the location shown.

Regards,

Joe

Code:
[COLOR=#3465A4]           Dim workPart As Part = theSession.Parts.Work[/color]

        For Each aMaterial As PhysicalMaterial In workPart.MaterialManager.PhysicalMaterials.ToArray()
            aMaterial.UnassignAllObjects()
        Next

             [COLOR=#3465A4]Dim physicalMaterial1 As PhysicalMaterial

             Try
                 'load from library in case it is not used in the part yet[/color]
 
Hi,
I tried to run this Journal in NX6 but it is giving error as mentioned below. Please help on this.
'LoadFromNxmatmllibrary' is not a member of NXOpen.PhysicalMaterialCollection.

Bambila


Bambila
Mechanical Engineer
 
Is it possible to convert this Journal such that it can be run in NX6? Actually this journal very usefull for us.


Bambila
Mechanical Engineer
 
Status
Not open for further replies.
Back
Top