Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Count parts in assembly 1

Status
Not open for further replies.

niedzviedz

Mechanical
Apr 1, 2012
307
Hello everyone,

I would like create journal to count all parts in assembly. When all parts are counted, I wanna place this info in array for example part_name and quantity. I will use this array to Add each parts in assembly quantity number.

Anyone can help me?

With best regards
Michael
 
Replies continue below

Recommended for you

You know of course that you can get that info from the Assembly Navigator which can be exported to a spreadsheet for further manipulation and formatting.

John R. Baker, P.E.
Product 'Evangelist'
Product Engineering Software
Siemens PLM Software Inc.
Digital Factory
Cypress, CA
Siemens PLM:
UG/NX Museum:

To an Engineer, the glass is twice as big as it needs to be.
 
Yes John I known that, I also can create BOM and export it to txt, and then import it to spreadsheet.
I want this journal in only one purpose - I wanna use this value (quantity) on drawings. We have created own title blocks with quantity. Now, when mould is almost ready I create drawing for whole assembly, I add part list, next I use BOM and manually fill our quantity cell based on NX quantity [$~Q]. When someone manually fill cell, there is always chance to made some mistakes and whole process cost some time. I would like to use NX [$~Q] but I cannot find where NX store it.

With best regards
Michael
 
Hello everyone, I solved it by myself.
Below I present my code, maybe someone use it in the future.
Code:
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 qty as string	

    Sub Main()
    Dim workPart As Part = theSession.Parts.Work
    Dim dispPart As Part = theSession.Parts.Display

	reportComponents1
	reportComponents2
 
    End Sub

'**********************************************************
    Sub reportComponents1

    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

            ReportComponentChildren1(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 reportComponents2

    Dim workPart As Part = theSession.Parts.Work
    Dim dispPart As Part = theSession.Parts.Display

    lw.Open
    Try
        Dim d As ComponentAssembly = dispPart.ComponentAssembly

	if not IsNothing(d.RootComponent) then

            ReportComponentChildren2(d.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 reportComponentChildren1( ByVal comp As Component, _
        ByVal indent As Integer)

	Dim dispPart As Part = theSession.Parts.Display
	
        For Each child As Component In comp.GetChildren()	
            
	Dim MyPart As Part = child.Prototype.OwningPart
	Dim quantities As integer
	
	quantities = 0	         
	
	myPart.SetAttribute("qty", quantities)

            reportComponentChildren1(child, indent + 1)
        Next

    End Sub

'**********************************************************
    Sub reportComponentChildren2( ByVal comp As Component, _
        ByVal indent As Integer)

	Dim dispPart As Part = theSession.Parts.Display
	Dim workPart As Part = theSession.Parts.Work
	
        For Each child As Component In comp.GetChildren()

	Dim MyPart As Part = child.Prototype.OwningPart
	Dim quantities As integer
	
      	quantities = myPart.GetStringAttribute("qty")

	quantities = quantities + 1

	myPart.SetAttribute("qty", quantities)

            reportComponentChildren2(child, indent + 1)
        Next
    End Sub

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


With best regards
Michael
 
Hello, I think, when I copied code I haven't selected all lines. First 3 lines are missing. The whole code should looks like this:

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 qty as string	

    Sub Main()
    Dim workPart As Part = theSession.Parts.Work
    Dim dispPart As Part = theSession.Parts.Display

	reportComponents1
	reportComponents2
 
    End Sub

'**********************************************************
    Sub reportComponents1

    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

            ReportComponentChildren1(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 reportComponents2

    Dim workPart As Part = theSession.Parts.Work
    Dim dispPart As Part = theSession.Parts.Display

    lw.Open
    Try
        Dim d As ComponentAssembly = dispPart.ComponentAssembly

	if not IsNothing(d.RootComponent) then

            ReportComponentChildren2(d.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 reportComponentChildren1( ByVal comp As Component, _
        ByVal indent As Integer)

	Dim dispPart As Part = theSession.Parts.Display
	
        For Each child As Component In comp.GetChildren()	
            
	Dim MyPart As Part = child.Prototype.OwningPart
	Dim quantities As integer
	
	quantities = 0	         
	
	myPart.SetAttribute("qty", quantities)

            reportComponentChildren1(child, indent + 1)
        Next

    End Sub

'**********************************************************
    Sub reportComponentChildren2( ByVal comp As Component, _
        ByVal indent As Integer)

	Dim dispPart As Part = theSession.Parts.Display
	Dim workPart As Part = theSession.Parts.Work
	
        For Each child As Component In comp.GetChildren()

	Dim MyPart As Part = child.Prototype.OwningPart
	Dim quantities As integer
	
      	quantities = myPart.GetStringAttribute("qty")

	quantities = quantities + 1

	myPart.SetAttribute("qty", quantities)

            reportComponentChildren2(child, indent + 1)
        Next
    End Sub

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

Now it should work.


With best regards
Michael
 
Hi Michael,
Now It's Running without error but It hasn't created any attribute in name with "QTY"
 
Do You run it in assembly?

With best regards
Michael
 
Yes, It was in assembly mode and it has components also
 
Hi Michael,
It's my bad, till now I am checking in assembly attributes not in component level attributes. in component attributes its was there. Thanks for your support and I am also looking for same solution.

Also I am looking for journal need to run get their overall size in attributes. Do you have any journal for that, could you share with me please. My requirement is I need to mention stock sizes in drawing with .25" stock all sides.

 
I don't understand, could You explain it more?


With best regards
Michael
 
Hi,
I used below journal provided by Cowski in previous threads to create bounding box sizes in attributes. It's giving results with accuracy of 14 decimals. I want those values with 3 decimals and also need to add user defined stock to those values for example: my bounding box length is 2" and I need to add stock .25" so total will be 2.25" should be appear in my attributes
Thanks in advance

[code='NXJournaling.com][/code]
'NXJournaling.com
'June 9, 2014
'journal to report bounding box dimensions based on selected solid and selected csys
'dimensions, vector directions, and timestamp will be assigned to part attributes

'for NX 8 and above only

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpenUI

Module Module2

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work

Sub Main()

Dim ufs As UFSession = UFSession.GetUFSession()
Dim displayPart As Part = theSession.Parts.Display
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Dim bbox(5) As Double
Dim dblAcc_Value(11) As Double
Dim dblMass_Props(46) As Double
Dim dblStats(12) As Double
Dim strOutput As String
Dim boundX As Double
Dim boundY As Double
Dim boundZ As Double
Dim minCorner(2) As Double
Dim boxDirections(2, 2) As Double
Dim boxDistances(2) As Double
Dim useACS As Boolean = False
Dim dirX As New Vector3d(1, 0, 0)
Dim dirY As New Vector3d(0, 1, 0)
Dim dirZ As New Vector3d(0, 0, 1)

Dim solid1 As Body
If SelectSolid("Select solid", solid1) = Selection.Response.Cancel Then
Return
End If

Dim tagList(0) As NXOpen.Tag
tagList(0) = solid1.Tag

Dim myCsys As CoordinateSystem = Nothing
If SelectCSYS("Select a saved CSYS, 'OK' to use ACS", myCsys) = Selection.Response.Cancel Then
Exit Sub
End If

If IsNothing(myCsys) Then
useACS = True

dirX.X = 1
dirX.Y = 0
dirX.Z = 0

dirY.X = 0
dirY.Y = 1
dirY.Z = 0

dirZ.X = 0
dirZ.Y = 0
dirZ.Z = 1

Else
With myCsys.Orientation.Element

dirX.X = .Xx
dirX.Y = .Xy
dirX.Z = .Xz

dirY.X = .Yx
dirY.Y = .Yy
dirY.Z = .Yz

dirZ.X = .Zx
dirZ.Y = .Zy
dirZ.Z = .Zz

End With

End If


'get volume
dblAcc_Value(0) = 0.999
'AskMassProps3d(in_Tags(),in_num_objs,in_type,in_units,in_density,in_accuracy,in_accuracy_values(),out_mass_props(),out_stats())
ufs.Modl.AskMassProps3d(tagList, 1, 1, 1, 0.0375, 1, dblAcc_Value, dblMass_Props, dblStats)
strOutput = "Surface Area: " & dblMass_Props(0) & vbCrLf
strOutput = strOutput & "Volume: " & dblMass_Props(1) & vbCrLf
strOutput = strOutput & "Mass: " & dblMass_Props(2) & vbCrLf
strOutput = strOutput & "COG: " & dblMass_Props(3) & ", " & dblMass_Props(4) & ", " & dblMass_Props(5) & vbCrLf
strOutput = strOutput & "Density: " & dblMass_Props(46)

If useACS Then
'get solid body bounding box extents aligned to absolute csys
ufs.Modl.AskBoundingBox(solid1.Tag, bbox)
boundX = bbox(3) - bbox(0)
boundY = bbox(4) - bbox(1)
boundZ = bbox(5) - bbox(2)

Else
'get solid body bounding box extents aligned to work csys (pass null tag to use work csys)
ufs.Modl.AskBoundingBoxAligned(solid1.Tag, myCsys.Tag, expand:=False, min_corner:=minCorner, directions:=boxDirections, distances:=boxDistances)
boundX = boxDistances(0)
boundY = boxDistances(1)
boundZ = boxDistances(2)

End If

AttributeLength("Bounds X", boundX)
AttributeLength("Bounds Y", boundY)
AttributeLength("Bounds Z", boundZ)

AttributeDirection("Direction X.X", dirX.X)
AttributeDirection("Direction X.Y", dirX.Y)
AttributeDirection("Direction X.Z", dirX.Z)

AttributeDirection("Direction Y.X", dirY.X)
AttributeDirection("Direction Y.Y", dirY.Y)
AttributeDirection("Direction Y.Z", dirY.Z)

AttributeDirection("Direction Z.X", dirZ.X)
AttributeDirection("Direction Z.Y", dirZ.Y)
AttributeDirection("Direction Z.Z", dirZ.Z)

AttributeTimeStamp()

End Sub

'**********************************************************
Function SelectSolid(ByVal prompt As String, ByRef selObj As TaggedObject) As Selection.Response

Dim theUI As UI = UI.GetUI
Dim title As String = "Select a solid body"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim cursor As Point3d
Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
Dim selectionMask_array(0) As Selection.MaskTriple

With selectionMask_array(0)
.Type = UFConstants.UF_solid_type
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_BODY
End With

Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
title, scope, selAction, _
includeFeatures, keepHighlighted, selectionMask_array, _
selObj, cursor)
If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If

End Function

'*******************

Function SelectCSYS(ByVal prompt As String, ByRef csysObj As CoordinateSystem) As Selection.Response

Dim theUI As UI = UI.GetUI
Dim title As String = prompt
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim cursor As Point3d
Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
Dim selectionMask_array(0) As Selection.MaskTriple

With selectionMask_array(0)
.Type = UFConstants.UF_coordinate_system_type
.Subtype = UFConstants.UF_all_subtype
End With

Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
title, scope, selAction, _
includeFeatures, keepHighlighted, selectionMask_array, _
csysObj, cursor)

If resp = Selection.Response.ObjectSelected OrElse _
resp = Selection.Response.ObjectSelectedByName OrElse _
resp = Selection.Response.Ok Then
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If

End Function

Sub AttributeLength(ByVal theName As String, ByVal theLength As Double)

Dim objects1(0) As NXObject
objects1(0) = workPart
Dim attributePropertiesBuilder1 As AttributePropertiesBuilder
attributePropertiesBuilder1 = theSession.AttributeManager.CreateAttributePropertiesBuilder(workPart, objects1, AttributePropertiesBuilder.OperationType.None)

With attributePropertiesBuilder1

.IsArray = False
.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.Number
.SetAttributeObjects(objects1)

If workPart.PartUnits = BasePart.Units.Inches Then
.Units = "Inch"
Else
.Units = "Millimeter"
End If

.Category = "BoundingBox"
.Title = theName
.NumberValue = theLength

Dim nXObject1 As NXObject
nXObject1 = .Commit()

End With

attributePropertiesBuilder1.Destroy()

End Sub

Sub AttributeDirection(ByVal theName As String, ByVal theDirection As Double)

Dim objects6(0) As NXObject
objects6(0) = workPart
Dim attributePropertiesBuilder2 As AttributePropertiesBuilder
attributePropertiesBuilder2 = theSession.AttributeManager.CreateAttributePropertiesBuilder(workPart, objects6, AttributePropertiesBuilder.OperationType.None)

With attributePropertiesBuilder2
.IsArray = False
.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.Number
.SetAttributeObjects(objects6)
.Category = "BoundingBox"
.Title = theName
.Units = ""
.NumberValue = theDirection

Dim nXObject4 As NXObject
nXObject4 = attributePropertiesBuilder2.Commit()

End With

attributePropertiesBuilder2.Destroy()

End Sub

Sub AttributeTimeStamp()

Dim myDateTime As DateTime = Now

Dim objects11(0) As NXObject
objects11(0) = workPart
Dim attributePropertiesBuilder3 As AttributePropertiesBuilder
attributePropertiesBuilder3 = theSession.AttributeManager.CreateAttributePropertiesBuilder(workPart, objects11, AttributePropertiesBuilder.OperationType.None)
With attributePropertiesBuilder3
.IsArray = False
.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.Date
.Category = "BoundingBox"
.Title = "TimeStamp"
.DateValue.DateItem.Day = myDateTime.Day - 1
.DateValue.DateItem.Month = myDateTime.Month - 1
.DateValue.DateItem.Year = myDateTime.Year.ToString
.DateValue.DateItem.Time = myDateTime.ToString("HH:mm:ss")

.SetAttributeObjects(objects11)

Dim nXObject7 As NXObject
nXObject7 = .Commit()

End With

attributePropertiesBuilder3.Destroy()

End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer

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

'----Other unload options-------
'Unloads the image immediately after execution within NX
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

'Unloads the image explicitly, via an unload dialog
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Explicitly
'-------------------------------

End Function

End Module
 
 http://files.engineering.com/getfile.aspx?folder=74a96e5b-e65f-460d-9c93-0d3aade361a1&file=Capture.JPG
Try this:

Code:
boundX = FormatNumber(boundX, 3) + 0.25
boundy = FormatNumber(boundy, 3) + 0.25
boundz = FormatNumber(boundz, 3) + 0.25

before:

Code:
AttributeLength("Bounds X", boundX)





With best regards
Michael
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor