Thursday 5 February 2015

Making of Solid Edge Close Multiple Part2

...continued from part 1.

In the Form's Load event, a running instance of Solid Edge is connected to and all open documents are accessed into oDocs and are counted. If no document is open then a message is displayed, else the ListAllFiles function is called:

07

The Marshal class comes from System.Runtime.Interopservices namespace and its GetActiveObject method helps the macro to connect to the running instance of Solid Edge specified as a string "SolidEdge.Application".

This string is the ProgID or Program ID of Solid Edge which is stored in the Registry and can be searched, upon which you will also find lying nearby the installed path of (Edge.exe) i.e. Solid Edge from where it is accessed or invoked.

Inside the ListAllFiles function, an item is declared as a ListViewItem which is a single item in the ListView control meant for listing all open files in Solid Edge:

08

A For loop is used to loop through all open files in Solid Edge via the oDocs collection and the name of each document is accessed using the FullName property. The GetFileName method of the Path class from the System.IO namespace is used for this:

09

The GetFileName extracts just the filename with its extension:

Before GetFileName:

C:\Program File\Solid Edge\Training\anchor.par

After GetFileName:

anchor.par

10 This filename is added to the lvItems ListView control in the first column and is returned as a ListViewItem stored in the variable 'item'.

Similarly, the GetDirectoryName function gets just the Folder part of the FullName.

In pre-Windows era, i.e. in DOS, a Folder was also known a Directory, hence the function name.

The Folder name is added as a sub-item in the second column.

Following this, the Columns are autofit to the contents which expands the column to display the largest file name and path.

You can also double-click on the border of two column heads to autofit when the cursor changes to a vertical line with a two sided arrow.

12

Finally the Click event of the button Check All is called.

13

This event puts the check marks in all the file name items in the ListView.

In the Close Checked Files button's event, first check if any item is checked in the ListView. Then the items are iterated in a reverse fashion and files are closed in reverse order.

14

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 files to be closed. 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 closes 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 index of 1 and is closed.

So the remaining list with the new indices is:

Axle.par (0)
Frame.par (2)
Pulley.par (3)

Next, i = 2 and Item(2) seeks the third item Pulley.par which is at index=2 and hence it can be closed 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 closes 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 closed and removed from the list. Thus the loop does not fail.

Continued to part 3...


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


No comments:

Post a Comment