Cancel
Showing results for 
Search instead for 
Did you mean: 

Re: journal to check density applied in assembly components

Siemens Phenom Siemens Phenom
Siemens Phenom

Hi @FORZINDI ,

 

Try adding a line like below and check it. Check for different scenarios. There is still room to improve journal like consider default part density as it varies as per units and remove duplicate entries if assembly is of multi-instance type.

 

Dim workPart As Part
on error resume next

 

@cowski1 , any suggestion why same component is still processed though below line gives unique names?

 

                'lw.WriteLine("Unique part names:" & workPart.Name)

Also Material attribute checking condition not working correctly.

 

Code to process only unique components:

 

Option Strict Off

Imports System
Imports NXOpen
Imports NXOpen.UF
Imports System.Collections.Generic

Module Apply_Default_Density

	Private Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
	Private Dim ufs As UFSession = UFSession.GetUFSession()
    Dim lw As ListingWindow = theSession.ListingWindow
	Dim workPart As Part
	
    Sub Main()
	''' https://community.plm.automation.siemens.com/t5/NX-Programming-Customization-Forum/journal-to-check-density-applied-in-assembly-components/m-p/593816#M17389
		Dim sessionParts As PartCollection = theSession.Parts
		Dim parts() As NXOpen.BasePart = sessionParts.ToArray()
        Dim originalPart as NXOpen.Part = theSession.Parts.Work
		Dim loadStatus As PartLoadStatus
		Dim b As Body
		Dim bodies() As Body
		Dim density As Double 
		lw.Open
        on error resume next
        lw.WriteLine("List of components with density applied:")
        Dim i as string
        
        Dim PartDictionary As New Dictionary(Of String, Integer)        
        
        For Each workPart In parts
		    theSession.Parts.SetWork(workPart)
			bodies = GetBodiesInReferenceSet("MODEL")
            
            Const attributeName As String = "Material"
            Dim attributeInfo As NXObject.AttributeInformation
            ''' Check duplicate entries of part names
            If PartDictionary.ContainsKey(workPart.Name) Then
                i = PartDictionary(workPart.Name)
                'lw.WriteLine("ersererL:" & workPart.Name)
            Else
                'lw.WriteLine("Unique part names:" & workPart.Name)
                For Each b In bodies
                    ufs.Modl.AskBodyDensity(b.Tag, UFModl.DensityUnits.KilogramsMeters, density)
                    If density = 0 Then
                        If Not theSession.Parts.Work Is Nothing Then
                            lw.WriteLine("change body density from 0 to 1")
                            ufs.Modl.SetBodyDensity(b.Tag, UFModl.DensityUnits.KilogramsMeters, 1.0)
                        End If
                    Else
                        If density <> 7830640 Then
                        ''' http://nxjournaling.com/content/part-attribute
                            If workPart.HasUserAttribute(attributeName, NXObject.AttributeType.String, -1) Then ''' This evaluates to False, why???
                            attributeInfo = workPart.GetUserAttribute(attributeName, NXObject.AttributeType.String, -1)
                            lw.WriteLine("Part: " & workPart.name & "-> body density: " & density & " kg/m3 | " & attributeInfo.StringValue)
                            Else
                            attributeInfo = workPart.GetUserAttribute(attributeName, NXObject.AttributeType.String, -1)
                            lw.WriteLine("Part: " & workPart.name & "-> body density: " & density & " kg/m3 | " & attributeInfo.StringValue)  
                            End if
                        End If
                    End If
                Next                
            End If
		Next
        theSession.Parts.SetWork(originalPart)
		lw.Close()
    End Sub

    Sub Echo(ByVal output As String)
        theSession.ListingWindow.Open()
        theSession.ListingWindow.WriteLine(output)
        theSession.LogFile.WriteLine(output)
    End Sub
	
	Function GetBodiesInReferenceSet(ByVal refSetName As String) As Body()
        Dim theRefset As ReferenceSet = GetReferenceSet(refSetName)
        Dim theMembers As NXObject() = theRefset.AskMembersInReferenceSet()

        Dim bodyList As System.Collections.ArrayList = New System.Collections.ArrayList
        For Each anObject As NXObject In theMembers
            Try
                Dim aBody As Body = CType(anObject, Body)
                bodyList.Add(aBody)
            Catch ex As Exception
                ' the cast failed so this one is NOT a Body
            End Try
        Next

        Return bodyList.ToArray(GetType(Body))

    End Function

    Function GetReferenceSet(ByVal refSetName As String) As ReferenceSet

        Dim refSetTag As Tag = Tag.Null

        ufs.Obj.CycleByNameAndType(workPart.Tag, refSetName, _
            UFConstants.UF_reference_set_type, False, refSetTag)

        If (refSetTag = Tag.Null) Then Return Nothing

        Dim theRefSet As ReferenceSet = _
            CType(Utilities.NXObjectManager.Get(refSetTag), ReferenceSet)

        Return theRefSet

    End Function
	
   Public Function GetUnloadOption(ByVal dummy As String) As Integer
        Return Session.LibraryUnloadOption.Immediately
   End Function


End Module

 

Regards,
Samadhan

GTAC | NX Help: NX1102 | NX1202 | NX1847
Please mark post as an "Accepted Solution", if it answers your question/is more helpful!

Re: journal to check density applied in assembly components

Pioneer
Pioneer

@SamadhanGaikwad thanks now its working fine.

one more suggestion, can we add material aslo with the density ?

Re: journal to check density applied in assembly components

Siemens Phenom Siemens Phenom
Siemens Phenom

@FORZINDI , See updated journal above. Please note that there is no error checking or all conditions considered, so check it at your end.

Regards,
Samadhan

GTAC | NX Help: NX1102 | NX1202 | NX1847
Please mark post as an "Accepted Solution", if it answers your question/is more helpful!

Re: journal to check density applied in assembly components

Pioneer
Pioneer

@SamadhanGaikwad yeah i have checked its working good for density, but for material its behaviour is random.

for e.g. in assembly of 20 components i have applied material to only one component, but when i run this journal, its showing same material for all components.

Re: journal to check density applied in assembly components

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

I see that you have a dictionary object, PartDictionary, to help you process only the unique parts. However, I don't see anywhere in the code where items are added to the dictionary. With an empty dictionary, the .ContainsKey method will always return false and the part will be processed.

Re: journal to check density applied in assembly components

Siemens Phenom Siemens Phenom
Siemens Phenom

I see. But why I'm getting the unique parts then?

Regards,
Samadhan

GTAC | NX Help: NX1102 | NX1202 | NX1847
Please mark post as an "Accepted Solution", if it answers your question/is more helpful!
Highlighted

Re: journal to check density applied in assembly components

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

@SamadhanGaikwad wrote:

I see. But why I'm getting the unique parts then?


The code isn't processing the assembly component by component; it is processing all the parts open in the current session. No matter how many times a particular part is used in an assembly, it is only open once in the session. Also note that the current code will process all parts open in session, not only those in the currently open assembly. This behavior may or may not be desired, depending on your needs.

 

I'd suggest removing the "on error resume next" statement as that will just hide any errors from you. If you get errors while you are developing the code, check for the cause and add code to handle the error, when possible. If the cause of the error is out of your control, wrap the error prone statement(s) in a Try/Catch block.

Re: journal to check density applied in assembly components

Siemens Phenom Siemens Phenom
Siemens Phenom

@FORZINDI , this is the link I was talking about: http://nxjournaling.com/content/return-physical-material-usage

Regards,
Samadhan

GTAC | NX Help: NX1102 | NX1202 | NX1847
Please mark post as an "Accepted Solution", if it answers your question/is more helpful!