• No results found

Linking the list with SolidWorks

The simplest task to perform on any component or feature is to select it. What we will do is that every time the selection in our tree view list is changed, we will select the corresponding object in SolidWorks.

Start by going to the Form Designer, selecting the TreeView object and going to its Events in the Property Window. Scroll down to the AfterSelect entry and double-click it to add an event function for every time the selection is changed.

This function will get called every time the user selected an item from our list. So we want to get the currently selected item from the tree view, and use that as the key in our hash table to lookup whatever component or feature that item links back to. From there we will have access to the Component2 or Feature object to do with as we please.

Inside the AfterSelect function place the following code:

C#

if (treeView1.SelectedNode == null) return;

if (hashStore.Contains(treeView1.SelectedNode)) {

object link = hashStore[treeView1.SelectedNode];

if (treeView1.SelectedNode.Index != 0) {

We first check that we have an item in our list selected, and if so that our hash table actually contains a link from the selected node (although from our current code it always will). Then we acquire the associated SolidWorks object be retrieving it from the hash table passing in the selected node as the key. We then check if the

selected node is the first item, in which case it is the model we add in the beginning. Because we cannot select the model itself we skip the code if it is.

If the selected node is not the first node, then it will be either a Component2 object or a Feature object, but because the object is a COM object, we cannot simply test what type it is, so we use a Try/Catch block. If casting the object to a Component2 object fails, we know it is a Feature.

Once we have the desired object, we call its respective Select function to select it. Run our program and click the button to fill the list, then select some items from the list and watch in SolidWorks as the object gets selected.

Pretty good hu? That’s not all. We are now going to add the ability to toggle the suppression state of the components and features using a right-click menu to our list. This can then be easily expanded to call any function or do any task you would like on any component or feature within an assembly or part.

Go back to the Form Designer and this time we are going to drag a new ContextMenuStrip from the toolbar to the form.

You will then notice at the bottom of the Form Designer that you have your newly created menu called ContextMenuStrip1. If you single-click on this the menu designer will appear at the top of your form. To add a new menu item you single-click the box where it states “Type Here”, type the name of the menu, and press enter.

Add a new menu item called “Toggle Suppression”, and in order to add an event function for OnClick, just double-click the menu item.

Before we continue, go back to the Form Designer and select the TreeView control.

We must associate our newly create menu with this control so that when the user right-clicks within it the menu displays. In the Property Window with the TreeView control selected, for the ContextMenuStrip property, select your menu.

Now it’s time to implement our suppression function. Within the event function for the menu click that we created a minute ago place the following code. This is mostly identical with the previous code but where the Select coding is, we replace it with the suppression toggle code:

C#

if (treeView1.SelectedNode == null) return;

if (hashStore.Contains(treeView1.SelectedNode)) {

object link = hashStore[treeView1.SelectedNode];

if (treeView1.SelectedNode.Index != 0) {

catch

If the associated object is a component we call the GetSuppression function to retrieve the current state of the component. Next, we check if it is already suppressed; if it is, we set the new state to unsuppressed, if it isn’t we set it to suppressed.

With the state defined, we set it using the function SetSuppression2 function.

If the object is a feature, we call the Feature function IsSuppressed2, which returns an array of suppression states for the feature in all

configurations. Since we are only interested in the current

configuration, we just check the first item in the returns array. Again, if it is already suppressed we unsuppress it, and if it is unsuppressed we suppress it. With the state defined, we set it using the function SetSuppression2 just like the component.

Test the program, and this time you can select an object first by left-clicking, and then right-click to show the context menu, and click Toggle Suppression to toggle the suppression state of the item.

Here endith the lesson. You should now have a good understanding of the traversing process, as well as some of the functions and methods you can use upon components and features. You should be able to easily expand this program to do anything you please.

Related documents