Journal to set properties to parts
Journal to set properties to parts
(OP)
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:
Can anyone can help me?
With best regards
Michael
What I have done for now, I create code which works, but:
- only on parts in main assembly gets value. I would like improve it to sets the same value to all parts
- when I set part as work part, journal doesn't work. I would like improve code to automatically set main assembly as work part
- I set properties to "apply to part", but it doesn't automatic set to "apply to component" and apply to "instance"
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





RE: Journal to set properties to parts
http://www.nxjournaling.com/content/creating-subro...
www.nxjournaling.com
RE: Journal to set properties to parts
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 ModuleIs 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
RE: Journal to set properties to parts
CODE
www.nxjournaling.com
RE: Journal to set properties to parts
I solved by using Your code from previous post to display names of every part in assembly. Below is what I have done.
CODE
I tried use splitstring, which I found on similar thread, but its only works for expressions.
With best regards
Michael
RE: Journal to set properties to parts
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
RE: Journal to set properties to parts
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
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
RE: Journal to set properties to parts
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 Ifwww.nxjournaling.com
RE: Journal to set properties to parts
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 ModuleIn the meantime, can someone help me with my problem, which I described in previous post?
With best regards
Michael