Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Journal to set properties to parts

Status
Not open for further replies.

niedzviedz

Mechanical
Apr 1, 2012
307
Hello. I need journal which automatically adds Order number to each parts in assembly and sub assemblies.
What I have done for now, I create code which works, but:
[ul]
[li]only on parts in main assembly gets value. I would like improve it to sets the same value to all parts[/li]
[Li]when I set part as work part, journal doesn't work. I would like improve code to automatically set main assembly as work part[/li]
[Li]I set properties to "apply to part", but it doesn't automatic set to "apply to component" and apply to "instance"[/li]
[/ul]

Below Is my code:
Code:
Option Strict Off
Imports System
Imports NXOpen

Module Module1

    Public theSession As Session = Session.GetSession()
    Dim blnCancel As Boolean = False

      Sub Main()
        Dim workpart As Part = theSession.Parts.Work
        Dim dispPart As Part = theSession.Parts.Display
        Dim assm As Assemblies.Component = workpart.ComponentAssembly.RootComponent
        Dim comp as component
        Dim children() As Assemblies.Component = assm.GetChildren
        Dim Order As String
		    Order = InputBox("Enter Order number:", "Order Number", "14/xxxx")
		    If Order = "14/xxxx" Then
                	'user pressed cancel
                	blnCancel = True
               		Exit Sub
            	    End If

        If UBound(children) > -1 Then		
            	For Each obj As Assemblies.Component In children
                Try
                    Dim objects(0) As NXObject
                    objects(0) = obj
                    Dim attributePropertiesBuilder1 As AttributePropertiesBuilder
                    Dim assembliesParameterPropertiesBuilder1 As Assemblies.AssembliesParameterPropertiesBuilder

                    attributePropertiesBuilder1 = workpart.PropertiesManager.CreateAttributePropertiesBuilder(objects)
                    attributePropertiesBuilder1.ObjectPicker = 

AttributePropertiesBaseBuilder.ObjectOptions.ComponentAsPartAttribute
                    attributePropertiesBuilder1.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.String
                    attributePropertiesBuilder1.Title = "Zlecenie"
                    attributePropertiesBuilder1.IsArray = False
                    attributePropertiesBuilder1.StringValue = order.ToString.PadLeft(3, "0"c)

                    Dim nXObject1 As NXObject
                    nXObject1 = attributePropertiesBuilder1.Commit()
                    attributePropertiesBuilder1.Destroy()

                Catch ex As NXException
                    If ex.ErrorCode = 512008 Then
                        'add code to handle error
                    Else
                        MsgBox(ex.ErrorCode & ": " & ex.Message)
                        'code to handle other error
                    End If
                End Try
            Next
       End If
    End Sub
    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

Can anyone can help me?

With best regards
Michael
 
Replies continue below

Recommended for you

Thanks Cowski. I merged Your code with mine. Below is what I've got, maybe someone will use it :)

Code:
Option Strict Off
 
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies
 
Module NXJournal
 
    Public theSession As Session = Session.GetSession()
    Public ufs As UFSession = UFSession.GetUFSession()
    Public lw As ListingWindow = theSession.ListingWindow
    Dim blnCancel As Boolean = False	
    Dim Order As String
 
    Sub Main()
    Dim workPart As Part = theSession.Parts.Work
    Dim dispPart As Part = theSession.Parts.Display

 		Order = InputBox("Enter Order number:", "Order Number", "14/xxxx")
		    If Order = "14/xxxx" Then
                	'user pressed cancel
                	blnCancel = True
               		Exit Sub
            	    End If
     lw.Open
    Try
        Dim c As ComponentAssembly = dispPart.ComponentAssembly
        if not IsNothing(c.RootComponent) then
                 ReportComponentChildren(c.RootComponent, 0)		   
        end if
    Catch e As Exception
        theSession.ListingWindow.WriteLine("Failed: " & e.ToString)
    End Try
    lw.Close
 
    End Sub
 
'**********************************************************
    Sub reportComponentChildren( ByVal comp As Component, ByVal indent As Integer)
    		Dim workPart As Part = theSession.Parts.Work
    		Dim dispPart As Part = theSession.Parts.Display

        For Each child As Component In comp.GetChildren()
           	    Dim objects(0) As NXObject
                    objects(0) = child
                    Dim attributePropertiesBuilder1 As AttributePropertiesBuilder
                    Dim assembliesParameterPropertiesBuilder1 As Assemblies.AssembliesParameterPropertiesBuilder
                    attributePropertiesBuilder1 = disppart.PropertiesManager.CreateAttributePropertiesBuilder(objects)
                    attributePropertiesBuilder1.ObjectPicker = AttributePropertiesBaseBuilder.ObjectOptions.ComponentAsPartAttribute
                    attributePropertiesBuilder1.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.String
                    attributePropertiesBuilder1.Title = "Order"
                    attributePropertiesBuilder1.IsArray = False
		    attributePropertiesBuilder1.StringValue = order

                    Dim nXObject2 As NXObject
                    nXObject2 = attributePropertiesBuilder1.Commit()
                    attributePropertiesBuilder1.Destroy()

            reportComponentChildren(child, indent+1)
       
        Next
    End Sub
'**********************************************************
    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        Return Session.LibraryUnloadOption.Immediately
    End Function
'**********************************************************
End Module

Is there some option in vb nx to read 3 first char. from file name, and write it to properties as part_position? For example 001_Part_1 -> pos = 001, 002_Part_2 -> Pos = 002.

With best regards
Michael
 
If you are using native NX, you can get the first three characters of the work part name like this:

Code:
dim first3Char as string = workPart.Leaf.Substring(0,3)

www.nxjournaling.com
 
Thanks Cowski,

I solved by using Your code from previous post to display names of every part in assembly. Below is what I have done.
Code:
name = child.DisplayName()
poz = name.Substring(0, 3)

I tried use splitstring, which I found on similar thread, but its only works for expressions.

With best regards
Michael

 
Hello everybody,

I have one more question. Where NX store QTY value, after creating BOM in drawing, and how can I read it? What I wanna do is copy this value to additional cell in properties, and then I will use it to automatic fill table in drawings. Now I have to manually copy each value to new cell under BOM.

With best regards
Michael
 
Hi,

I am trying to use the examples above to fill out an attribute. All of this information is very helpfull thank you.

However I am running into one snag. Below is an example structure of our assembly trees.

AB-XXXX_123-456_REV0
AB-XXXX_123-457_REV0
AB-XXXX_M12X25_SHCS​
AB-XXXX_TEST
TEST

Now the goal is to retrieve the "123-456" section of the part name and input that into an attribute. I have been using attributePropertiesBuilder1.StringValue = child.Displayname.Substring(9,7) in my journal, but if the assembly has files with names shorter than the caracter numbers in the sub string the journal wont run.(example being to two "test" components).

I belive that the direction i need to go is an If Then statement but I am fairly new to journaling. I would like to write something like if the substring returns nothing then fill the attribute with a space.

Any help is really appreciated.

Thank you

 
JSDesign said:
I belive that the direction i need to go is an If Then statement but I am fairly new to journaling. I would like to write something like if the substring returns nothing then fill the attribute with a space.

If your actual data is similar to your posted example, then splitting the string on the underscore character is probably the easiest way to get what you need. Below is some sample code, replace testString with the name of the part.

Code:
Dim testString As String = "AB-XXXX_123-456_REV0"
Dim strParts() As String = testString.Split("_")
If UBound(strParts) > 0 Then
    MsgBox(strParts(1))
Else
    MsgBox("default value")
End If


www.nxjournaling.com
 
Hello JSDesign,
try this code, it should solve most of Your problems.
Code:
Option Strict Off
 
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies
 
Module NXJournal
 
    Public theSession As Session = Session.GetSession()
    Public ufs As UFSession = UFSession.GetUFSession()
    Public lw As ListingWindow = theSession.ListingWindow
    Dim name as string
    Dim poz as string
    Dim poz1 as string	
  
    Sub Main()
    Dim workPart As Part = theSession.Parts.Work
    Dim dispPart As Part = theSession.Parts.Display
 
    lw.Open
    Try
        Dim c As ComponentAssembly = dispPart.ComponentAssembly

	if not IsNothing(c.RootComponent) then

            ReportComponentChildren(c.RootComponent, 0)
        else

            lw.WriteLine("Part has no components")

        end if
    Catch e As Exception
        theSession.ListingWindow.WriteLine("Failed: " & e.ToString)
    End Try
    lw.Close
 
    End Sub
 
'**********************************************************
    Sub reportComponentChildren( ByVal comp As Component, _
        ByVal indent As Integer)

	Dim dispPart As Part = theSession.Parts.Display
        Dim Length as Integer
	
        For Each child As Component In comp.GetChildren()	
	           
            name = child.DisplayName()
	    length = name.length

	    if length > 12 then         		
		
	    poz1 = name.Substring(8, 3)
	    poz = name.Substring(8, 7)
	 	   
		If isNumeric(poz1) Then  

		    Dim objects(0) As NXObject
                    objects(0) = child

                    Dim attributePropertiesBuilder1 As AttributePropertiesBuilder
                    Dim assembliesParameterPropertiesBuilder1 As Assemblies.AssembliesParameterPropertiesBuilder
                    attributePropertiesBuilder1 = disppart.PropertiesManager.CreateAttributePropertiesBuilder(objects)
                    attributePropertiesBuilder1.ObjectPicker = AttributePropertiesBaseBuilder.ObjectOptions.ComponentAsPartAttribute
                    attributePropertiesBuilder1.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.String
                    attributePropertiesBuilder1.Title = "poz"
                    attributePropertiesBuilder1.IsArray = False
		    attributePropertiesBuilder1.StringValue = poz

                    Dim nXObject2 As NXObject
                    nXObject2 = attributePropertiesBuilder1.Commit()
                    attributePropertiesBuilder1.Destroy()

		else

                    Dim objects(0) As NXObject
                    objects(0) = child

                    Dim attributePropertiesBuilder1 As AttributePropertiesBuilder
                    Dim assembliesParameterPropertiesBuilder1 As Assemblies.AssembliesParameterPropertiesBuilder
                    attributePropertiesBuilder1 = disppart.PropertiesManager.CreateAttributePropertiesBuilder(objects)
                    attributePropertiesBuilder1.ObjectPicker = AttributePropertiesBaseBuilder.ObjectOptions.ComponentAsPartAttribute
                    attributePropertiesBuilder1.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.String
                    attributePropertiesBuilder1.Title = "poz"
                    attributePropertiesBuilder1.IsArray = False
		    attributePropertiesBuilder1.StringValue = ""
                    Dim nXObject2 As NXObject
                    nXObject2 = attributePropertiesBuilder1.Commit()
                    attributePropertiesBuilder1.Destroy()
	
  	      end if
           End if

            reportComponentChildren(child, indent + 1)
        Next
    End Sub
'**********************************************************
    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        Return Session.LibraryUnloadOption.Immediately
    End Function
'**********************************************************
 
End Module

In the meantime, can someone help me with my problem, which I described in previous post?

With best regards
Michael
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor