Add & Remove Columns from existing parts list on a drawing.

Add & Remove Columns from existing parts list on a drawing.

jameshockney
Participant Participant
594 Views
4 Replies
Message 1 of 5

Add & Remove Columns from existing parts list on a drawing.

jameshockney
Participant
Participant

I am trying to work out if it is possible to add an ilogic script that will take a drawing which already contains a parts list, check what columns exist (this is Style specific, there are only 2 alternatives) add a specified column set to the existing parts list (including one which may or may not already exist depending on the style in use). Sort the columns into a specific order and then export the parts list as a .CSV file to a network folder.

After doing this it would be ideal if it could put the parts list back to as it was originally, although this is certainly the least important step.

I can see how to have it mess about with the style libraries and delete/place parts lists, but this looses any text overrides which are on the original list which is not acceptable.

Does anyone know how to achieve any of this?

Many thanks for your help.

0 Likes
595 Views
4 Replies
Replies (4)
Message 2 of 5

JelteDeJong
Mentor
Mentor

I am not sure what you are exactly looking for but i guess this is a step in the correct direction.

Dim csvFileName = "d:\temp\partslist.csv"

Dim doc As DrawingDocument = ThisDoc.Document
Dim sheet As Sheet = doc.ActiveSheet
Dim pl As PartsList = sheet.PartsLists.Item(1)

' Set the default column that you want to sort by
Dim sortColumn = 1
' Set the column you want to sort by based on the style
If (pl.Style.Name = "Parts List (ANSI)") Then
    sortColumn = 4
End If

' Convert the parts list to a list of lists of cell values
Dim list As New List(Of List(Of String))
For Each row As PartsListRow In pl.PartsListRows
    Dim cells = Row.Cast(Of PartsListCell).Select(Of String)(Function(c) c.Value).ToList()
    list.Add(cells)
Next

' Order the list
Dim orderdList = list.OrderBy(Function(c) c.Item(sortColumn - 1))

' Create the csvLine
Dim csvLines As New List(Of String)
For Each row As List(Of String) In orderdList
    csvLines.Add(String.Join(";", Row))
Next

' Write the csv line to a file
If (System.IO.File.Exists(csvFileName)) Then
    System.IO.File.Delete(csvFileName)
End If
System.IO.File.AppendAllLines(csvFileName, csvLines)

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 3 of 5

jameshockney
Participant
Participant

Thanks for your help.  The start  and end of what you gave me looks good, but I am not looking to sort the table values I need to set the columns into a specific order.

 

The default for our main parts list is:

ITEM   |   DESCRIPTION   |   PART No.   |   REV.   |   MATERIAL   |   QUANTITY.

 

I need to ensure that all of those 6 columns exist, in that specific order, then add an additional SAP_DES  & SH_DES Columns at the end. this additional column then needs to be removed after the csv file is created.

 

We also have a secondary parts list on some drawings which is ordered:

ITEM   |   SH_DES   |   PART No.   |    MATERIAL   |   QUANTITY.

 

again I need this one to go to:

ITEM   |   DESCRIPTION   |   PART No.   |   REV.   |   MATERIAL   |   QUANTITY.   |   SAP_DES   |   SH_DES

for the export and then back to how it started afterwards.

 

Life becomes more complex if any user has reordered (or omitted/added) columns for any reason.

 

0 Likes
Message 4 of 5

JelteDeJong
Mentor
Mentor

You could leave the parts list untouched and use the bom. It would save you from finding out which partslist type you have to deal with and adding/deleting columns. Something like this:

Sub Main()

    Dim csvFileName = "d:\temp\partslist.csv"

    Dim doc As DrawingDocument = ThisDoc.Document
    Dim sheet As Sheet = doc.ActiveSheet
    Dim refDoc As AssemblyDocument = sheet.DrawingViews.Item(1).ReferencedDocumentDescriptor.ReferencedDocument

    Dim bom = refDoc.ComponentDefinition.BOM.BOMViews.Item("Structured")

    Dim csvInfo As New List(Of List(Of String))
    For Each row As BOMRow In bom.BOMRows

        Dim rowDef As ComponentDefinition = Row.ComponentDefinitions.Item(1)

        If TypeOf rowDef Is VirtualComponentDefinition Then
            MsgBox("I cant handle virtual components (jet).")
            Continue For
        End If

        Dim rowDoc = rowDef.Document
        Dim item = Row.ItemNumber
        Dim description = GetIProperty(rowDoc, "Design Tracking Properties", "Description")
        Dim partNo = GetIProperty(rowDoc, "Design Tracking Properties", "Part Number")
        Dim rev = GetIProperty(rowDoc, "Inventor Summary Information", "Revision Number")
        Dim material = GetIProperty(rowDoc, "Design Tracking Properties", "Material")
        Dim quantity = Row.ItemQuantity
        Dim sap_des = GetIProperty(rowDoc, "Design Tracking Properties", "SAP_DES")
        Dim sh_des = GetIProperty(rowDoc, "Design Tracking Properties", "SH_DES")

        Dim csvRow As New List(Of String)
        csvRow.Add(item)
        csvRow.Add(description)
        csvRow.Add(partNo)
        csvRow.Add(rev)
        csvRow.Add(material)
        csvRow.Add(quantity)
        csvRow.Add(sap_des)
        csvRow.Add(sh_des)
        csvInfo.Add(csvRow)
    Next

    ' Create the csvLine
    Dim csvLines As New List(Of String)
    For Each row As List(Of String) In csvInfo
        csvLines.Add(String.Join(";", Row))
    Next

    ' Write the csv line to a file
    If (System.IO.File.Exists(csvFileName)) Then
        System.IO.File.Delete(csvFileName)
    End If
    System.IO.File.AppendAllLines(csvFileName, csvLines)
End Sub

Public Function GetIProperty(doc As Document, setName As String, propertyName As String) As String
    Dim propSets As PropertySets = doc.PropertySets
    Try
        Return propSets.Item(setName).Item(propertyName).Value
    Catch ex As Exception
        Return String.Format("No property {0} in document: {1}", propertyName, doc.DisplayName)
    End Try
End Function

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 5 of 5

jameshockney
Participant
Participant

That was my first thought, but unfortunately using the BOM is not practical as any text alterations do not pass from the drawing table back into the BOM. Nor do any Custom lines get included. 

 

0 Likes