Make your own free website on Tripod.com

API Tip #2 5/16/2001


Model Parameters Using Excel VBA Tutorial

Tutorial Files for this tip (click here) 

One of the first things you use as a SolidWorks user are parameters.  That’s why the software is called “parametric”.  Design tables are a great way to use Microsoft Excel’s spreadsheet functionality to drive these parameters.  This section will cover another method of controlling SolidWorks parameters through Excel’s VBA utilities.  The great thing about VBA is that it works the same in any program that supports it!  We will end up with an assembly containing an embedded Excel spreadsheet that controls part level dimensions.

Dimension Parameters

The easiest things to control through the API are dimension values.  Let’s record another macro in SolidWorks to see how this is done.  For this example I will be using the assembly BEVELGEARBOX.sldasm from the gearboxfiles.zip file.  Changing dimensions of single parts is just as simple, but this will illustrate the general idea.

Open the assembly BEVELGEARBOX.sldasm.  Show all your feature dimensions by right-clicking on the Annotations folder in the FeatureManager Design Tree.  You might also want to turn on the option to Show dimension names from the general category for easy access to specific dimensions.  Now start recording a macro.  Increase the value of ShaftDia1, BevDia1, ShaftDia2 and BevDia2 by 0.5 inches by double-clicking on each dimension.  Rebuid the assembly and stop the macro.  Name the macro dimensions.swp. 

Let’s examine what was recorded.  Edit the macro dimensions.swp.  Your main procedure should be similar to the following code.  I have modified the code below with … to fit the width of the page.

Sub main()  

Set swApp = CreateObject("SldWorks.Application")
Set Part = swApp.ActiveDoc
Part.SelectByID "ShaftDia1@Sketch1@bevelgear-1@BevelGearbox",…
Part.Parameter("ShaftDia1@Sketch1@bevelgear.Part").SystemValue = 0.0254
Part.SelectByID "ShaftDia2@Sketch1@bevelgear2-1@BevelGearbox",…
Part.Parameter("ShaftDia2@Sketch1@bevelgear2.Part").SystemValue = 0.0254
Part.SelectByID "BevDia1@Sketch1@bevelgear-1@BevelGearbox",…
Part.Parameter("BevDia1@Sketch1@bevelgear.Part").SystemValue = 0.1016
Part.SelectByID "BevDia2@Sketch1@bevelgear2-1@BevelGearbox", … Part.Parameter("BevDia2@Sketch1@bevelgear2.Part").SystemValue = 0.0762
Part.EditRebuild
Part.ClearSelection
End Sub

 

SolidWorks will frequently record much more code than a macro would require.  Each time you modify a dimension in SolidWorks, the macro records the action of selecting the dimension as well as changing its value.  The selection of dimensions will not prevent the macro from working, but it will cause unnecessary processing to occur.  Let’s optimize our macro by deleting or commenting out each line of code that uses the SelectByID method.  If you deleted the extra code, your main procedure should look something like this.

Sub main() 

Set swApp = CreateObject("SldWorks.Application")
Set Part = swApp.ActiveDoc
Part.Parameter("ShaftDia1@Sketch1@bevelgear.Part").SystemValue = 0.0254
Part.Parameter("ShaftDia2@Sketch1@bevelgear2.Part").SystemValue = 0.0254
Part.Parameter("BevDia1@Sketch1@bevelgear.Part").SystemValue = 0.1016
Part.Parameter("BevDia2@Sketch1@bevelgear2.Part").SystemValue = 0.0762
Part.EditRebuild
Part.ClearSelection

End Sub

Using Excel’s VBA

In the distance mate example, we learned that we can replace values in our code with a variable.  We can also populate those variables with values by using the InputBox method.  However, isn’t typing values into individual boxes the same as changing dimensions by double-clicking on them?  All you are automating is the double-click.  Maybe that is fun, but it’s not saving you any time.  If you don’t save any time, how can you go home early on Friday?  You also lose functionality if you use the InputBox method rather than the dimension modify dialog because you can’t type in arithmetic or units in an InputBox!  So let’s use the power of Excel to help us.  Excel was built to hold multiple values and to quickly define functional relations between them.  There’s no need to reinvent the wheel.

Open the Excel spreadsheet named BevelGearbox.xls.  It already has some cells filled out for the values we wish to control.  These cells will be used in place of the InputBox method for a “one stop shop” for values.  Let’s add a button to our spreadsheet that will activate a macro inside Excel.  If the Excel Control Toolbox is not displayed, point to Toolbars on the View menu, and then click Control Toolbox.  Click the button for the Command Button  then click in the spreadsheet window to insert one.  You should see something similar to Figure 1.

Figure 1

 

Now lets copy our macro we recorded in SolidWorks to the code behind this button.  Toggle back to the SolidWorks VBA interface and copy all the code between, but not including, Sub main() and End Sub.  Toggle back to Excel.  To view the code behind the new command button, double-click on it or right-click on it and select View Code.  You should see a VBA interface show up with a code module for this command button.  Paste your copied code above the End Sub line.  This procedure already has a name defined by the name of the button we created.  When the button is clicked, the code in the procedure will be executed.

 

Private Sub CommandButton1_Click()
Set swApp = CreateObject("SldWorks.Application")
Set Part = swApp.ActiveDoc
Part.Parameter("ShaftDia1@Sketch1@bevelgear.Part").SystemValue = 0.0254
Part.Parameter("ShaftDia2@Sketch1@bevelgear2.Part").SystemValue = 0.0254
Part.Parameter("BevDia1@Sketch1@bevelgear.Part").SystemValue = 0.1016
Part.Parameter("BevDia2@Sketch1@bevelgear2.Part").SystemValue = 0.0762
Part.EditRebuild
Part.ClearSelection
End Sub

 

How do we access the values of our cells from the Excel spreadsheet?  VBA in Excel is the same idea as VBA in SolidWorks.  We’ll use an Excel API call to get the cell values from the current worksheet.  Modify your code as show below to access the Excel values.  The underscores are added to make the code more readable on the page.

 

Private Sub CommandButton1_Click()
Set swApp = CreateObject("SldWorks.Application")
Set Part = swApp.ActiveDoc
Part.Parameter("ShaftDia1@Sketch1@bevelgear.Part").SystemValue = _
   
Excel.Range("B1") * 0.0254
Part.Parameter("BevDia1@Sketch1@bevelgear.Part").SystemValue = _
   
Excel.Range("B2") * 0.0254
Part.Parameter("ShaftDia2@Sketch1@bevelgear2.Part").SystemValue = _
   
Excel.Range("B3") * 0.0254
Part.Parameter("BevDia2@Sketch1@bevelgear2.Part").SystemValue = _
   
Excel.Range("B4") * 0.0254
Part.EditRebuild
Part.ClearSelection
End Sub

 

We have accessed the Excel values by using the Range method.  This returns the value of that cell range to input into our dimension parameters.  We’ve also added a multiplier of 0.0254 to convert the inch value shown on the spreadsheet meters.  Remember that most SolidWorks API calls require an input value in meters. This is all the code we need to try our example.  From the File menu in the VBA editor, select Close and Return to Microsoft Excel.  To quit design mode and enable the command button control, click Exit Design Mode  from the Control Toolbox.  The Excel spreadsheet will now control the SolidWorks assembly.  Try changing the values in the cells and then click the command button.  Because the macro extracts the cell values, you can use Excel formulas and functions or additional worksheets to drive them.  The options are practically limitless! 

 

Rather than having an external file control our model, lets use OLE capability to copy and paste this Excel spreadsheet into our assembly.  Simply select all the required cells (A1 to C7  if your spreadsheet looks like Figure 1) and copy them.  Then toggle to your SolidWorks assembly and paste.  Now your spreadsheet is embedded in your assembly.  You no longer need that extra file.  Go ahead, drop it in the recycle bin.  To activate the embedded spreadsheet, double-click on it.  Change the cell values and click your command button to watch the assembly update.  Click outside the Excel object window to go back to the assembly and deactivate the spreadsheet.

 

Happy programming! 

Mike Spens