Monday 1 December 2014

Making of PacEdge Part 09

...continued from part 8

Finally, the function ProcessSolidEdgeFiles()adds the files to the TreeView which are finally sent for PacEdging.

Add Other Files: This button allows non-Solid Edge files to the PacEdge and also respects the file selection modes viz. Multiple Files and All Files from Folder.

The filter for this button is *.* which allows picking any type of file like PDF, Excel or even other ZIP files to the PacEdge.

In the Folder mode, this button also looks at the Sub-Folder check box and accordingly picks ALL files from under the selected folder or all its sub-folders, hence the Pick Folder option should be used with caution when adding Other Files.

The Other Files button stores all files cumulatively in list called the ListofOtherFiles and calls the ProcessOtherFiles() function.

The ProcessOtherFiles function iterates through each file in the list and adds them under the Other Files node also mentioning the Location Path, File Size and Count:

For Each s As String In ListOfOtherFiles

  sExt = Path.GetExtension(s).ToUpper()

  sFileName = Path.GetFileName(s)

  sPath = Path.GetDirectoryName(s)

  n = nodeOther.Items.Add(sFileName, 5) 'File name

  n.SubItems.Add("") ' Count, leave blank

  n.SubItems.Add(sPath) ' Path

  n.SubItems.Add(GetFileSize(s)) ' File size

Next

The FileSize for all Solid Edge and non-SE files is obtained from the GetFileSize function which returns the ‘Size on Disk’ of the file in bytes. This is formatted as KB, MB or simply bytes as below:

Private Function GetFileSize(ByVal sFile As String) As String

  Dim lFileSize As Long = My.Computer.FileSystem.GetFileInfo(sFile).Length

  Dim sFileSize As String = String.Empty

  If lFileSize > 1000000 Then

    sFileSize = String.Format("{0:0}", (lFileSize / 1000000.0)) + " MB"

  ElseIf lFileSize < 1000000 And lFileSize > 999 Then

    sFileSize = String.Format("{0:0}", (lFileSize / 1000.0)) + " KB"

  ElseIf lFileSize < 1000 Then

    sFileSize = String.Format("{0:0}", lFileSize) + " bytes"

  End If

  Return sFileSize

End Function

19 Tip: My.Computer is unique to VB.Net and is not available in CSharp unless you add a reference to the Microsoft.VisualBasic.Compatibility.dll which typically sits in the \Program Files\Reference Assemblies\Microsoft\Framework\.NetFramework\vx.x\ folder or just search for the DLL with that name.

Inside the function, the Length property returns the file size in bytes which is categorized as over 1 MB between and MB and a KB and below 1 KB. The bytes are accordingly divided by 1000 or 1000000 and formatted as strings with a MB, KB or bytes suffix.

The String.Format function takes a string argument which specifies the format of the second argument which is a number.

In “{0:0}” the first 0 specifies that the whole number should be displayed and the second 0 indicates the number of decimal places in the number to be displayed.

So using “{0:0}” with 152.12323423 will format it as 152 while “{0:2}” will display the same number as 152.12

Finally the function ProcessOtherFiles is called which again dissects the files into filename with extension and the path and displays all data in the relevant columns along with the file size and count, all under the separate OtherFiles node in the TreeView.

Remove Files: Files can be removed only for the PAR+PSM and Draft PacEdges. Hence the first line in the Remove Files button’s Click event checks this and notifies the user followed by exiting the Sub:

If ActivePacEdgeMode = PacEdgeMode.AssemblyPacEdge Then

MessageBox.Show("Cannot remove file from an Assembly PacEdge.")

Exit Sub

End If

It then iterates through the children nodes under the Part node first.

For i As Integer = nodePAR.Items.Count - 1 To 0 Step -1

  If nodePAR.Items(i).Selected = True Then

  nodePAR.Items(i).Remove()

  nodePAR.SubItems(1).Text = (Convert.ToInt16(nodePAR.SubItems(1).Text) - 1).ToString

  nodePAR.Parent.SubItems(1).Text = (Convert.ToInt16(nodePAR.Parent.SubItems(1).Text) - 1).ToString

  End If

Next

And if a node is found selected, removes it.

The Multiselect property for the TreeList is also set to True so that users can pick multiple files using Ctrl or the Shift key combinations before hitting the Remove Files button.

The For loop is run in a reverse order from Items.Count - 1 to 0 with Step-1 since running the loop in an ascending order throws an error which can be understood through a situation described below:

Consider a list of 5 items to be removed from a list. Each item's index within the list is shown in brackets.

Anchor.par (0)

Axle.par (1)

Bar.par (2)

Frame.par (3)

Pulley.par (4)

When i = 0, Item(0) can find Anchor.par and removes it.

The list now appears with new indices as below:

Axle.par (0)

Bar.par (1)

Frame.par (2)

Pulley.par (3)

Next i = 1, so the item with index 1 is sought:

Item(1) seeks Bar.par in the list above since Bar.par has indes of 1 and is removed.

So the remaining list with the new indices is:

Axle.par (0)

Frame.par (1)

Pulley.par (2)

Next, i = 2 and Item(2) seeks the third item Pulley.par which is at index=2 and hence it get removed too without problems.

The list is now as below and i = 3 seeks the item at an index which does not exist and the loop as also the function fails.

Axle.par (0)

Frame.par (1)

Running the For loop in the reverse manner solves this problem as below.

Consider the original list:

Anchor.par (0)

Axle.par (1)

Bar.par (2)

Frame.par (3)

Pulley.par (4)

The reverse For loop starts at index 4 down to 1:

At i = 4, Pulley.par goes leaving the list as below:

Anchor.par (0)

Axle.par (1)

Bar.par (2)

Frame.par (3)

Next, at i = 3, Frame.par is removed from the list which looks like:

Anchor.par (0)

Axle.par (1)

Bar.par (2)

Further at, i = 2 Bar.par still exits at index = 2 and so on till i = 0 when Anchor.par is finally removed from the list. Thus the loop does not fail.

Continued to part 10... 


Index of all Solid Edge Tips, Tutorials and Videos...


No comments:

Post a Comment