Sunteți pe pagina 1din 23

SolidWorks API Tutorials

Pgina 1 de 23

SolidWorks API Tutorials


The SolidWorks Application Programming Interface (API) is the basis for the SolidWorks three macro recorders: C#, VB.NET, and VBA. Each of the three SolidWorks API tutorials shows you how to: record a macro to create a cut-extrude on a specific pre-selected face on a part, NREVIOUS P EXT TOPIC TOPIC modify the macro to create the cut-extrude on any pre-selected face on the part, and the SolidWorks Recording Sketching Examining Modifying Starting Running Creating Changing Finishing Writing Adding Saving a Controls Variables Code to a a Macro Debugging the Names a a Macro Generic Form a Debug Code API Macro Circle Macro Macro Form for C# VB.NET VBA the for for of a Cutthe a Overview Program Extrude Controls Form Program create a form where users can specify the cut-extrude's radius and depth values and create the cutNEXT TOPIC extrude on any pre-selected face on the part N EXT TOPIC Saving Examining Modifying Starting Running Creating Changing Finishing Adding a Variables to a a Macro Debugging the Names a Macro Generic a Debug Macro Macro Form for of a Cuta using the SolidWorks C#, VB.NET, or VBA macro recorder. Click a link to open the Testing SolidWorks API tutorial Sketching Program Finishing Extrude Creating Adding Writing Controls Saving the Controls Code the Program a Debugging a Generic Form Code Program Circle for the for Cutthe a and using that macro recorder. Extrude Program Program Form

C#

VB.NET

VBA

SolidWorks API C# Overview


This tutorial introduces you to the SolidWorks Application Programming Interface (API), the SolidWorks Visual Studio Tools for Applications, and its Integrated Development Environment (IDE). The SolidWorks API:

is the basis for the SolidWorks macro recorders. lets you automate and customize the SolidWorks software and integrate your software with the SolidWorks software.

The quickest and easiest way to start programming with the SolidWorks API is to record a macro. Then you can modify the macro to fit your work site's needs. This tutorial provides instructions on recording, editing, and running a macro written in C# in the SolidWorks software. This tutorial assumes that you are familiar with using the SolidWorks software interactively. If you plan on automating, customizing, or integrating your software with the SolidWorks software using the SolidWorks API, you should take a Visual Basic or Visual C# programming course and then attend a SolidWorks API training course offered at a SolidWorks Authorized Training and Support Center.

Recording a Macro
Create a SolidWorks API C# program by recording the operations performed to create a cut-extrude on a face. The SolidWorks macro recorder records every operation you perform while recording. 1. Click here to open the candlestick part (or browse to install_dir\samples\tutorial\api\cstick.sldprt.) 2. Click Options (Standard toolbar) and make sure Stop VSTA debugger on macro exit is selected and Automatically edit macro after recording is not selected. 3. Click OK. 4. Click Record\Pause Macro 5. Select the face shown. (Macro toolbar) to begin recording the macro.

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm

26/08/2013

SolidWorks API Tutorials

Pgina 2 de 23

NREVIOUS P EXT TOPIC TOPIC 6. Click Sketch 7. Click Normal To (Sketch toolbar) to insert a sketch. (Standard Views toolbar). NEXT TOPIC N EXT TOPIC Saving Examining Modifying Starting Running Creating Changing Finishing Adding a Variables to a a Macro Debugging the Names a Macro Generic a Debug Macro Macro Form for of a Cuta
SolidWorks Recording Sketching Saving Examining Modifying Starting Running Creating Changing Finishing Writing Adding a the Controls Variables Code to a a Macro Debugging the Names a a Macro Generic Form a Debug Code API Macro Circle Macro Macro Form for C# VB.NET VBA the for for of a Cutthe a Overview Program Extrude Controls Form Program

Testing the Sketching Program Finishing Extrude Creating Adding Writing Controls Saving Controls Code the Program a Debugging a Generic Form Code Program Circle for the for Cutthe a and Extrude Program Program Form

Sketching a Circle
1. Click Circle (Sketch toolbar).

2. Sketch a circle centered at the sketch origin. 3. In the PropertyManager, set Radius 4. Click to 15.

Saving a Macro
1. Click Extruded Cut (Features toolbar).

2. In the PropertyManager, under Direction 1:


Set End Condition to Blind. Set Depth to 25. , and set Draft Angle to 15.

Click Draft On/Off .

3. Click

A cut-extrude with a radius of 15mm, depth of 25mm, and draft of 15 centered on the top face of the candlestick holder is created. 4. Click Stop Macro (Macro toolbar) to stop recording the macro.

5. In the Save As dialog box, browse to the folder where you want to save the macro. 6. Make sure Save as type is SW VSTA C# Macro (*.csproj). 7. Type CreateCutExtrudeCSharp in File name. 8. Click Save.

Examining a Macro
1. Click Edit Macro (Macro toolbar).

2. Open CreateCutExtrudeCSharp.csproj. 3. Click Open. The SolidWorks VSTA IDE opens. The Code Editor window contains the SolidWorks API C# code generated for the macro.

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm

26/08/2013

SolidWorks API Tutorials

Pgina 3 de 23

4. Click here to display the macro code in a browser. Your macro may contain more lines of code than the displayed code if you performed any operations not specified in the previous exercise; for example, rotating the part. 5. Delete any lines of code in your macro that do not appear in the displayed code. NREVIOUS P EXT TOPIC TOPIC If swDoc.SketchManager.InsertSketch(true) does not appear in your code, then you created the SolidWorks Recording Sketching Examining Modifying Starting Running Creating Changing Finishing Writing Adding Saving a the Controls Variables Code to a a Macro Debugging the Names a a Macro Generic Form a Debug Code API Macro Circle Macro Macro Form for C# VB.NET VBA the for for of a Cutthe a circle without first inserting a sketch. NEXT TOPIC 6. Type swDoc.SketchManager.InsertSketch(true) where it appears in the displayed code, if N EXT TOPIC Saving Examining Modifying Starting Running Creating Changing Finishing Adding a Variables to a a Macro Debugging the Names a Macro Generic a Debug Macro Macro Form for of a Cuta necessary.
Overview Program Extrude Controls Form Program

Some arguments' numeric values in your macro may be different than those in the displayed Extrude Program Form Program code. These values reflect coordinates of the model. Do not modify the values in your macro to match the values in the displayed code. 7. Close the browser displaying the code. Leave the Code Editor window open.

Sketching Program Finishing Extrude Creating Adding Writing Controls Saving Testing the Controls Code the Program a Debugging a Generic Form Code Program Circle for the for Cutthe a and

Modifying a Macro
Certain variables are declared automatically in a SolidWorks macro. You can delete any variables not used in the program. 1. Delete these variables declarations. PartDoc swPart = null; DrawingDoc swDrawing = null; AssemblyDoc swAssembly = null; int longstatus = 0; int longwarnings = 0; 2. Test the modified macro by recreating the cut-extrude in the candlestick holder. a. In the SolidWorks software, drag the rollback bar up the FeatureManager design tree until it is above the Cut-Extrude1 feature. b. In the SolidWorks VSTA IDE, click Start Debugging A new cut-extrude is created in the candlestick holder. 3. Delete the cut-extrude and the sketch just created. 4. Drag the rollback bar back to the bottom of the FeatureManager design tree. 5. Delete the original cut-extrude and sketch. .

Starting to Debug a Program


To learn how to debug code in C#, change the code in the Code Editor window to cause errors to occur while executing it. 1. Delete the letters el from the variable ModelDoc2 and change the name of the property ActiveDoc to ActiveDoc2.

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm

26/08/2013

SolidWorks API Tutorials

Pgina 4 de 23

You cannot edit a running macro in SolidWorks x64. Instead, you must stop the running macro, edit the macro, and run the macro again. 2. In the SolidWorks graphics area, select the top face.

Finishing Debugging a Program


1. Click Start Debugging in the SolidWorks VSTA IDE.

NREVIOUS P EXT TOPIC TOPIC NEXT TOPIC N EXT TOPIC Saving Examining Modifying Starting Running Creating Changing Finishing Adding a Variables to a a Macro Debugging the Names a Macro Generic a Debug Macro Macro Form for of a Cuta
SolidWorks Recording Sketching Saving Examining Modifying Starting Running Creating Changing Finishing Writing Adding a the Controls Variables Code to a a Macro Debugging the Names a a Macro Generic Form a Debug Code API Macro Circle Macro Macro Form for C# VB.NET VBA the for for of a Cutthe a Overview Program Extrude Controls Form Program

A message box is displayed informing you that there were build errors. 2. To debug the errors, click No.

Testing the Sketching Program Finishing Extrude Creating Adding Writing Controls Saving Controls Code the Program a Debugging a Generic Form Code Program Circle for the for Cutthe a and Extrude Program Program Form

3. Double-click the first error to move the cursor to the line of code that contains the error. 4. Insert the letters el so ModDoc2 becomes ModelDoc2, which is a declared variable in the code. 5. Click Start Debugging .

6. Click No and change ActiveDoc2 to ActiveDoc. 7. Click Start Debugging .

The code compiles, and a cut-extrude is created on the selected face. 8. Delete the cut-extrude and sketch.

Creating a Generic Cut-Extrude Program


Modify the macro in the Code Editor window so that you can create a cut-extrude on any pre-selected face in any part. To make the program more generic, eliminate the draft. 1. Click here for instructions on how to modify your macro. 2. Click Save SolidWorksMacro.cs in the SolidWorks VSTA IDE to save the modified code.

3. In the SolidWorks graphics area, select the face on the bottom of the part. 4. Click Start Debugging in the SolidWorks VSTA IDE.

A cut-extrude with a radius of 15mm and a depth of 25mm is created on the selected face and is centered on the sketch point. 5. Delete the cut-extrude and sketch.

Creating a Form
Currently you must modify the code to change the radius and depth values programmatically. To increase the flexibility of the program, you can create a C# form where users can specify these values. 1. In the SolidWorks VSTA IDE, click View > Project Explorer . 2. In the Project Explorer, select SolidWorksMacro.cs. 3. Click Project > Add Windows Form. 4. Select Windows Form, leave Form1.cs in Name, and click Add. 5. To change the name of the form, click the form in the Design View window. 6. Type View > Properties Window to display the properties associated with Form1. 7. Type frmCutExtrude in the (Name) row in the Properties window.

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm

26/08/2013

SolidWorks API Tutorials

Pgina 5 de 23

Use the scroll bar in the Properties window to locate the (Name) row, which is near the beginning of the Properties window. 8. Click the form for the change to take effect. The name at the top of the Properties window changes to frmCutExtrude. However, the name shown N REVIOUS EXT TOPIC TOPIC on the form is the Text property of the form, which you did not change, so itP remains Form1.

Adding Controls
1. Display the toolbox by clicking View > Toolbox. 2. Expand Common Controls. 3. Drag and place it near the upper-right corner of the form.

NEXT TOPIC N EXT TOPIC Saving Examining Modifying Starting Running Creating Changing Finishing Adding a Variables to a a Macro Debugging the Names a Macro Generic a Debug Macro Macro Form for of a Cuta

SolidWorks Recording Sketching Saving Examining Modifying Starting Running Creating Changing Finishing Writing Adding a the Controls Variables Code to a a Macro Debugging the Names a a Macro Generic Form a Debug Code API Macro Circle Macro Macro Form for C# VB.NET VBA the for for of a Cutthe a Overview Program Extrude Controls Form Program

Add two text boxes and a command button, called controls, to the form using the Toolbox. Extrude Program Program Form

Sketching Program Finishing Extrude Creating Adding Writing Controls Saving Testing the Controls Code the Program a Debugging a Generic Form Code Program Circle for the for Cutthe a and

4. Drag another text box and place it below the text box that you just created. 5. Drag boxes. , place it below the text boxes, and stretch it so that it is the same length as the text

Changing Names of Controls


Change the names of the text boxes and the command button to more meaningful names. 1. Click the top text box on the form. 2. Type txtRadius in the (Name) row in the Properties window. 3. Click outside the row for the change to take effect. 4. Click the other text box and name it txtDepth. 5. Click the command button and name it bOK.

Finishing the Form


Add labels for both text boxes so that users know what information to type in the text boxes. Also, change the caption on the command button.

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm

26/08/2013

SolidWorks API Tutorials

Pgina 6 de 23

1. Drag

and place it beside the top text box.

2. Type Radius: in the Text row in the Properties window. 3. Click anywhere on the form. 4. Place a label named Depth: beside the other text box. 5. Click button1 on the form and change its caption to OK. 6. Click anywhere on the form. NREVIOUS P EXT TOPIC TOPIC NEXT TOPIC N EXT TOPIC Saving Examining Modifying Starting Running Creating Changing Finishing Adding a Variables to a a Macro Debugging the Names a Macro Generic a Debug Macro Macro Form for of a Cuta
SolidWorks Recording Sketching Saving Examining Modifying Starting Running Creating Changing Finishing Writing Adding a the Controls Variables Code to a a Macro Debugging the Names a a Macro Generic Form a Debug Code API Macro Circle Macro Macro Form for C# VB.NET VBA the for for of a Cutthe a Overview Program Extrude Controls Form Program

Testing the Sketching Program Finishing Extrude Creating Adding Writing Controls Saving Controls Code the Program a Debugging a Generic Form Code Program Circle for the for Cutthe a and Extrude Program Program Form

Writing Code for the Form


C# can insert code templates for event procedures for controls. You modify the code template to specify what you want to happen when an event occurs, such as when users click the OK button. 1. Double-click the OK button on the form to open the Code Editor window containing an event procedure for this control. The name of the event procedure, bOK_Click, is a combination of the control's name as specified in the control's Name property, an underscore (_), and the name of the event. The Click procedure is the default procedure for a command button. 2. Type, or copy and paste, these declarations above Private void bOK_Click(object sender, EventArgs e). public double radius; public double depth; 3. Type, or copy and paste, this code between Private void bOK_Click(object sender, EventArgs e) event's curly brackets.

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm

26/08/2013

SolidWorks API Tutorials

Pgina 7 de 23

double radiusResult; double depthResult;

//Convert strings to doubles and //make sure that the text in the two //text boxes on the form are positive, NREVIOUS P EXT TOPIC TOPIC //non-zero, numeric values SolidWorks Recording Sketching Examining Modifying Starting Running Creating Changing Finishing Writing Adding Saving a the Controls Variables Code to a a Macro Debugging the Names a a Macro Generic Form a Debug Code API Macro Circle Macro Macro Form for C# VB.NET VBA the for for of a Cutthe a double.TryParse(txtRadius.Text, out radiusResult); Overview Program Extrude Controls Form Program double.TryParse(txtDepth.Text, out depthResult); NEXT TOPIC if (radiusResult > 0 && depthResult > 0) N EXT TOPIC Saving Examining Modifying Starting Running Creating Changing Finishing Adding a Variables to a a Macro Debugging the Names a Macro Generic a Debug Macro Macro Form for of a Cuta { Sketching Program Finishing Extrude Creating Adding Writing Controls Saving Testing the Controls Code the Program a Debugging a Generic Form Code Program Circle for the for Cutthe a and radius = radiusResult; Extrude Program Program Form depth = depthResult; //Close the form and continue Close(); } else { //Display a message box telling the user to type //positive, non-zero, numeric values in both text boxes MessageBox.Show("You must type positive, non-zero, numeric values in both Radius and Depth."); }

Adding Variables for Controls


Modify SolidWorksMacro.cs to display the form to users and to retrieve the depth and radius values from the form. 1. Double-click SolidWorksMacro.cs in the Project Explorer to display its code in the Code Editor window. 2. Scroll to the top of the macro and locate swDoc = ((ModelDoc2)(swApp.ActiveDoc));. 3. Insert the following code between swDoc = ((ModelDoc2)(swApp.ActiveDoc)); and //COMMENT OUT THIS LINE OF CODE to display the form and retrieve the depth and radius values typed by the user. //ADD THESE LINES OF CODE double holeRadius; double holeDepth; //Create an instance of the user form frmCutExtrude myForm = new frmCutExtrude(); //Set the title for the form myForm.Text = "Size of Cut-Extrude in Millimeters"; //Display a model user form and retrieve radius //depth values typed by the user; divide those values //by 1000 to change mmillimeters to meters myForm.ShowDialog(); holeRadius = myForm.radius / 1000; holeDepth = myForm.depth / 1000; //Dispose of the user form and remove it from //memory because it's no longer needed myForm.Dispose();

Saving the Code for the Form


Replace the radius and depth values, which were automatically inserted by the SolidWorks macro recorder when you created the cut-extrude in the SolidWorks user interface, with variables. 1. Scroll to near the bottom of the macro and locate dz = darray[2];. 2. Replace all of the code appear after dz = darray[2]; with this code.

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm

26/08/2013

SolidWorks API Tutorials

Pgina 8 de 23

//Create the sketch for cut-extrude SketchSegment swSketchSegment = null; swSketchSegment = (SketchSegment)swDoc.SketchManager.CreateCircleByRadius(dx, dy, dz, holeRadius) //Create the cut-extrude feature Feature swFeature = null; swFeature = (Feature)swDoc.FeatureManager.FeatureCut3(true, false, false, 0, 0, holeDepth, 0.01, NREVIOUS P EXT TOPIC TOPIC SolidWorks Recording Sketching Examining Modifying Starting Running Creating Changing Finishing Writing Adding Saving a the Controls Variables Code to a a Macro Debugging the Names a a Macro Generic Form a Debug Code API Macro Circle Macro Macro Form for C# VB.NET VBA the for for of a Cutthe a } Overview Program Extrude Controls Form Program } NEXT TOPIC //KEEP THESE LINES OF CODE N EXT TOPIC Saving Examining Modifying Starting Running Creating Changing Finishing Adding a Variables to a a Macro Debugging the Names a Macro Generic a Debug Macro Macro Form for of a Cuta ///<summary> Sketching Program Finishing Extrude Creating Adding Writing Controls Saving Testing the Controls Code the Program a Debugging a Generic Form Code Program Circle for the for Cutthe a and ///The Sldworks swApp variable is pre-assigned for you. Extrude Program Program Form ///</summary> public SldWorks swApp; }

3. Click Save SolidWorksMacro.cs

to save the modified code.

Testing the Program and Form


Now test the modified program and the form. 1. In the SolidWorks software, select the bottom face of the candlestick holder. 2. Click Start Debugging in the SolidWorks VSTA IDE.

The Size of Cut-Extrude in Millimeters dialog is displayed. 3. Type 15 in Radius and 10 in Depth. 4. Click OK to create a cut-extrude of the specified depth and radius on the bottom face. 5. Click File > Exit in the SolidWorks VSTA IDE to exit it and return to SolidWorks. 6. Select the bottom face of the candlestick holder again. 7. Click Run Macro (Macro toolbar).

8. Browse to the folder where you saved the project and then browse to \SwMacro\bin\Debug. 9. Select CreateCutExtrudeCSharp.dll and click Open. 10. Type 10 in Radius and 5 in Depth. 11. Click OK to create another cut-extrude of the specified depth and radius on the bottom face. 12. Close the candlestick part document without saving it. Congratulations! You have completed this tutorial. Return to the tutorials overview page.

SolidWorks API VB.NET Overview


This tutorial introduces you to the SolidWorks Application Programming Interface (API), the SolidWorks Visual Studio Tools for Applications, and its Integrated Development Environment (IDE). The SolidWorks API:

Is the basis for the SolidWorks macro recorders. Lets you automate and customize the SolidWorks software and integrate your software with the SolidWorks software.

The quickest and easiest way to start programming with the SolidWorks API is to record a macro. Then you can modify the macro to fit your work site's needs. This tutorial provides instructions on recording, editing, and running a macro written in Visual Basic (VB.NET) in the SolidWorks software.

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm

26/08/2013

SolidWorks API Tutorials

Pgina 9 de 23

This tutorial assumes that you are familiar with using the SolidWorks software interactively. If you plan on automating, customizing, or integrating your software with the SolidWorks software using the SolidWorks API, you should take a Visual Basic or Visual C# programming course and then attend a SolidWorks API training course offered at a SolidWorks Authorized Training and Support Center.

Recording a Macro

NREVIOUS P EXT TOPIC TOPIC


SolidWorks Recording Sketching Examining Modifying Starting Running Creating Changing Finishing Writing Adding Saving a the Controls Variables Code to a a Macro Debugging the Names a a Macro Generic Form a Debug Code API Macro Circle Macro Macro Form for C# VB.NET VBA the for for of a Cutthe a

Overview Program Extrude Controls Form Create a SolidWorks API VB.NET program by recording the operations performed to createProgram a cut-extrude on EXT TOPIC a face. The SolidWorks macro recorder records every operation you perform whileN recording. N EXT TOPIC Saving Examining Modifying Starting Running Creating Changing Finishing Adding a Variables to a a Macro Debugging the Names a Macro Generic a Debug Macro Macro Form for of a Cuta

1. Click here to open the candlestick part (or browse to install_dir\samples\tutorial\api\cstick.sldprt.)

Testing the Sketching Program Finishing Extrude Creating Adding Writing Controls Saving Controls Code the Program a Debugging a Generic Form Code Program Circle for the for Cutthe a and Extrude Program Program Form

2. Click Options (Standard toolbar) and make sure Stop VSTA debugger on macro exit is selected and Automatically edit macro after recording is not selected. 3. Click OK. 4. Click Record\Pause Macro 5. Select the face shown. (Macro toolbar) to begin recording the macro.

6. Click Sketch 7. Click Normal To

(Sketch toolbar) to insert a sketch. (Standard Views toolbar).

Sketching a Circle
1. Click Circle (Sketch toolbar).

2. Sketch a circle centered at the sketch origin. 3. In the PropertyManager, set Radius 4. Click to 15.

Saving a Macro
1. Click Extruded Cut (Features toolbar).

2. In the PropertyManager, under Direction 1:


Set End Condition to Blind. Set Depth to 25. , and set Draft Angle to 15.

Click Draft On/Off .

3. Click

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm

26/08/2013

SolidWorks API Tutorials

Pgina 10 de 23

A cut-extrude with a radius of 15mm, depth of 25mm, and draft of 15 centered on the top face of the candlestick holder is created. 4. Click Stop Macro (Macro toolbar) to stop recording the macro.

5. In the Save As dialog box, browse to the folder where you want to save the macro. NREVIOUS P EXT TOPIC TOPIC 6. Make sure Save as type is SW VSTA VB Macro (*.vbproj). 7. Type CreateCutExtrudeVBNET in File name. 8. Click Save. NEXT TOPIC N EXT TOPIC Saving Examining Modifying Starting Running Creating Changing Finishing Adding a Variables to a a Macro Debugging the Names a Macro Generic a Debug Macro Macro Form for of a Cuta
SolidWorks Recording Sketching Saving Examining Modifying Starting Running Creating Changing Finishing Writing Adding a the Controls Variables Code to a a Macro Debugging the Names a a Macro Generic Form a Debug Code API Macro Circle Macro Macro Form for C# VB.NET VBA the for for of a Cutthe a Overview Program Extrude Controls Form Program

Testing the Sketching Program Finishing Extrude Creating Adding Writing Controls Saving Controls Code the Program a Debugging a Generic Form Code Program Circle for the for Cutthe a and Extrude Program Program Form

Examining a Macro
1. Click Edit Macro (Macro toolbar).

2. Open CreateCutExtrudeVBNET.vbproj. 3. Click Open. The SolidWorks VSTA IDE opens. The Code Editor window contains the SolidWorks API VB.NET code generated for the macro. 4. Click here to display the macro code in a browser. Your macro may contain more lines of code than the displayed code if you performed any operations not specified in the previous exercise; for example, rotating the part. 5. Delete any lines of code in your macro that do not appear in the displayed code. If swDoc.SketchManager.InsertSketch(true) does not appear in your code, then you created the circle without first inserting a sketch. 6. Type swDoc.SketchManager.InsertSketch(true) where it appears in the displayed code, if necessary. Some arguments' numeric values in your macro may be different than those in the displayed code. These values reflect coordinates of the model. Do not modify the values in your macro to match the values in the displayed code. 7. Close the browser displaying the code. Leave the Code Editor window open.

Modifying a Macro
Creating associations between two entities is called binding. For example, in the VB.NET line of code Dim boolstatus as Boolean = false, the variable named boolstatus is associated, or bound, to the data type Boolean. When binding occurs at compile time, it is called early binding. To avoid problems like misnamed variables and to reduce debugging time and improve the performance of your program, you should explicitly declare and bind variables early. 1. Type Option Explicit On at the top of the program, above Imports SolidWorks.Interop.sldworks to ensure that all varialbes are explicity declared. Certain variables are declared automatically in a SolidWorks macro. You can delete any variables not used in the program. 2. Delete these variables declarations. Dim swPart As PartDoc = Nothing Dim swDrawing As DrawingDoc = Nothing Dim swAssembly As AssemblyDoc = Nothing Dim longstatus As Integer = 0 Dim longwarnings As Integer = 0 3. Test the modified macro by recreating the cut-extrude in the candlestick holder.

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm

26/08/2013

SolidWorks API Tutorials

Pgina 11 de 23

a. In the SolidWorks software, drag the rollback bar up the FeatureManager design tree until it is above the Cut-Extrude1 feature. b. In the Solidworks Visual Studio for Applications IDE, click Start Debugging A new cut-extrude is created in the candlestick holder. 4. Delete the cut-extrude and the sketch just created. .

NREVIOUS P EXT TOPIC TOPIC


SolidWorks Recording Sketching Saving Examining Modifying Starting Running Creating Changing Finishing Writing Adding a the Controls Variables Code to a a Macro Debugging the Names a a Macro Generic Form a Debug Code API Macro Circle Macro Macro Form for C# VB.NET VBA the for for of a Cutthe a Overview Program Extrude Controls Form Program

5. Drag the rollback bar back to the bottom of the FeatureManager design tree.NEXT TOPIC N EXT TOPIC Saving Examining Modifying Starting Running Creating Changing Finishing Adding a Variables to a a Macro Debugging the Names a Macro Generic a Debug Macro Macro Form for of a Cuta 6. Delete the original cut-extrude and sketch.

Testing the Sketching Program Finishing Extrude Creating Adding Writing Controls Saving Controls Code the Program a Debugging a Generic Form Code Program Circle for the for Cutthe a and Extrude Program Program Form

Starting to Debug a Program


To learn how to debug code in VB.NET, change the code in the Code Editor window to cause errors to occur while executing it. 1. Delete the letters el from the variable ModelDoc2 and change the name of the property ActiveDoc to ActiveDoc2.

You cannot edit a running macro in SolidWorks x64. Instead, you must stop the running macro, edit the macro, and run the macro again. 2. In the SolidWorks graphics area, select the top face. 3. Click Start Debugging in the SolidWorks VSTA IDE.

A message box is displayed informing you that there was a build error. 4. To debug the error, click No. The error is displayed in the Error List window in the SolidWorks VSTA IDE.

Finishing Debugging a Program


1. Double-click the error to move the cursor to the line of code that contains the error. 2. Insert the letters el so ModDoc2 becomes ModelDoc2, which is a declared variable in the code. 3. Click Start Debugging selected face. to finish executing the program and to create a cut-extrude on the

4. Click Enable editing in the message box. 5. Change ActiveDoc2 back to ActiveDoc and press the Tab key to accept ActiveDoc, which is highlighted in IntelliSense. As you type or modify code in the Code Editor window, a list containing similarly spelled objects,

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm

26/08/2013

SolidWorks API Tutorials

Pgina 12 de 23

methods, and properties is displayed. This list is IntelliSense, Microsoft's autocompletion system. When the correct object, method, or property is selected in the list, press the Tab key to insert it in your program. 6. Click Start Debugging to compile the code and to create a cut-extrude on the selected face. NREVIOUS P EXT TOPIC TOPIC NEXT TOPIC N EXT TOPIC Saving Examining Modifying Starting Running Creating Changing Finishing Adding a Variables to a a Macro Debugging the Names a Macro Generic a Debug Macro Macro Form for of a Cuta
SolidWorks Recording Sketching Saving Examining Modifying Starting Running Creating Changing Finishing Writing Adding a the Controls Variables Code to a a Macro Debugging the Names a a Macro Generic Form a Debug Code API Macro Circle Macro Macro Form for C# VB.NET VBA the for for of a Cutthe a Overview Program Extrude Controls Form Program

7. Delete the cut-extrude and sketch.

Creating a Generic Cut-Extrude Program

Modify the macro in the Code Editor window so that you can create a cut-extrude on any pre-selected face in Extrude Program Form Program any part. To make the program more generic, eliminate the draft. 1. Click here for instructions on how to modify your macro. 2. Click Save SolidWorksMacro.vb in the SolidWorks VSTA IDE to save the modified code.

Sketching Program Finishing Extrude Creating Adding Writing Controls Saving Testing the Controls Code the Program a Debugging a Generic Form Code Program Circle for the for Cutthe a and

3. In the SolidWorks graphics area, select the face on the bottom of the part. 4. Click Start Debugging in the SolidWorks VSTA IDE.

A cut-extrude with a radius of 15mm and a depth of 25mm is created on the selected face and is centered on the sketch point. 5. Delete the cut-extrude and sketch.

Creating a Form
Currently you must modify the code to change the radius and depth values programmatically. To increase the flexibility of the program, you can create a VB.NET form where users can specify these values. 1. In the SolidWorks VSTA IDE, click View > Project Explorer. 2. In the Project Explorer, select SolidWorksMacro.vb. 3. Click Project > Add Windows Form. 4. Select Windows Form, leave Form1.vb in Name, and click Add. 5. To change the name of the form, click the form in the Design View window. 6. Type View > Properties Window to display the properties associated with Form1. 7. Type frmCutExtrude in the (Name) row in the Properties window. Use the scroll bar in the Properties window to locate the (Name) row, which is near the beginning of the Properties window. 8. Click the form for the change to take effect. The name shown on the form is the Caption property of the form, which you did not change, so it remains Form1.

Adding Controls
Add two text boxes and a command button, called controls, to the form using the Toolbox. 1. Display the toolbox by clicking View > Toolbox. 2. Expand Common Controls. 3. Drag and place it near the upper-right corner of the form.

4. Drag another text box and place it below the text box that you just created.

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm

26/08/2013

SolidWorks API Tutorials

Pgina 13 de 23

5. Drag boxes.

, place it below the text boxes, and stretch it so that it is the same length as the text

NREVIOUS P EXT TOPIC TOPIC NEXT TOPIC N EXT TOPIC Saving Examining Modifying Starting Running Creating Changing Finishing Adding a Variables to a a Macro Debugging the Names a Macro Generic a Debug Macro Macro Form for of a Cuta
SolidWorks Recording Sketching Saving Examining Modifying Starting Running Creating Changing Finishing Writing Adding a the Controls Variables Code to a a Macro Debugging the Names a a Macro Generic Form a Debug Code API Macro Circle Macro Macro Form for C# VB.NET VBA the for for of a Cutthe a Overview Program Extrude Controls Form Program

Testing the Sketching Program Finishing Extrude Creating Adding Writing Controls Saving Controls Code the Program a Debugging a Generic Form Code Program Circle for the for Cutthe a and Extrude Program Program Form

Changing Names of Controls


Change the names of the text boxes and the command button to more meaningful names. 1. Click the top text box on the form. 2. Type txtRadius in the (Name) row in the Properties window. 3. Click outside the row for the change to take effect. 4. Click the other text box and name it txtDepth. 5. Click the command button and name it bOK.

Finishing the Form


Add labels for both text boxes so that users know what information to type in the text boxes. Also, change the caption on the command button. 1. Drag and place it beside the top text box.

2. Type Radius: in the Text row in the Properties window. 3. Click anywhere on the form. 4. Place a label named Depth: beside the other text box. 5. Click Button1 on the form and change its caption to OK. 6. Click anywhere on the form.

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm

26/08/2013

SolidWorks API Tutorials

Pgina 14 de 23

NREVIOUS P EXT TOPIC TOPIC NEXT TOPIC N EXT TOPIC Saving Examining Modifying Starting Running Creating Changing Finishing Adding a Variables to a a Macro Debugging the Names a Macro Generic a Debug Macro Macro Form for of a Cuta
SolidWorks Recording Sketching Saving Examining Modifying Starting Running Creating Changing Finishing Writing Adding a the Controls Variables Code to a a Macro Debugging the Names a a Macro Generic Form a Debug Code API Macro Circle Macro Macro Form for C# VB.NET VBA the for for of a Cutthe a Overview Program Extrude Controls Form Program

Testing the Sketching Program Finishing Extrude Creating Adding Writing Controls Saving Controls Code the Program a Debugging a Generic Form Code Program Circle for the for Cutthe a and Extrude Program Program Form

Writing Code for the Form


VB.NET can insert code templates for event procedures for controls. You modify the code template to specify what you want to happen when an event occurs, such as when users click the OK button. 1. Double-click the OK button on the form to open the Code Editor window containing an event procedure for this control. The name of the event procedure, bOK_Click, is a combination of the control's name as specified in the control's Name property, an underscore (_), and the name of the event. The Click procedure is the default procedure for a command button. 2. Type, or copy and paste, these declarations above Private Sub bOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bOK.Click). Public depth As Double = 0 Public radius As Double = 0 3. Type, or copy and paste, this code between Private Sub bOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bOK.Click) and End Sub.

' Convert strings to doubles and ' make sure that the text in the two text boxes on ' the form are positive, non-zero, numeric values Dim radiusResult As Double = 0 Dim depthResult As Double = 0 Double.TryParse(txtRadius.Text, radiusResult) Double.TryParse(txtDepth.Text, depthResult) If (radiusResult > 0 and depthResult > 0) Then radius = radiusResult depth = depthResult ' Close the form and continue Close() Else ' Display a message box telling the user to type ' positive, non-zero, numeric values in both text ' boxes MsgBox("You must type positive, non-zero, numeric values for both Radius and Depth.")

Adding Variables for Controls


Modify the SolidWorksMacro.vb to display the form to users and to retrieve the depth and radius values

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm

26/08/2013

SolidWorks API Tutorials

Pgina 15 de 23

from the form. 1. Double-click SolidWorksMacro.vb in the Project Explorer to display its code in the Code Editor window. 2. Scroll to the top of the macro and locate swDoc = CType(swApp.ActiveDoc, ModelDoc2). NREVIOUS P EXT TOPIC TOPIC 3. Insert the following code between swDoc = CType(swApp.ActiveDoc, ModelDoc2) and 'COMMENT OUT SolidWorks Recording Sketching Examining Modifying Starting Running Creating Changing Finishing Writing Adding Saving a the Controls Variables Code to a a Macro Debugging the Names a a Macro Generic Form a Debug Code API Macro Circle Macro Macro Form for C# VB.NET VBA the for for of a Cutthe a user . THESE LINES OF CODE to display the form and retrieve the depth and radius values typed by the Dim HoleRadius As Double = 0 Dim HoleDepth As Double = 0 ' Create an instance of the user form Dim myForm as New frmCutExtrude ' Set the caption for the form myForm.Text = "Size of Cut-Extrude in Millimeters" ' Display the user form and retrieve radius ' and depth values entered by users ' Divide the values by 1000 to change ' millimeters to meters myForm.ShowDialog() HoleRadius = myForm.radius / 1000 HoleDepth = myForm.depth / 1000 ' Destroy the user form and remove it from memory ' because it is no longer needed Set myForm = Nothing NEXT TOPIC N EXT TOPIC Saving Examining Modifying Starting Running Creating Changing Finishing Adding a Variables to a a Macro Debugging the Names a Macro Generic a Debug Macro Macro Form for of a Cuta
Overview Program Extrude Controls Form Program

Testing the Sketching Program Finishing Extrude Creating Adding Writing Controls Saving Controls Code the Program a Debugging a Generic Form Code Program Circle for the for Cutthe a and Extrude Program Program Form

Saving the Code for the Form


Replace the radius and depth values, which were automatically inserted by the SolidWorks macro recorder when you created the cut-extrude in the SolidWorks user interface, with variables. 1. Scroll to near the bottom of the macro and locate dz = swMathPoint.ArrayData(2). 2. Replace all of the code appear after dz = swMathPoint.ArrayData(2) with this code. 'Sketch circle centered on the sketch point swSketchManager.CreateCircleByRadius(dx, dy, dz, HoleRadius) 'Create cut-extrude without draft Dim swFeatureManager As FeatureManager = Nothing swFeaturemanager = swDoc.FeatureManager swFeatureManager.FeatureCut3(True, False, False, _ 0, 0, HoleDepth, 0.01, True, False, False, False, _ 0, 0, False, False, False, False, False, True, True, _ False, False, False, swStartConditions_e.swStartSketchPlane, 0, False) End If End Sub '''<summary> ''' The Sldworks swApp variable is pre-assigned for you. ''' </summary> Public swApp as SldWorks

End Class 3. Click Save SolidWorksMacro.vb to save the modified code.

Testing the Program and Form

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm

26/08/2013

SolidWorks API Tutorials

Pgina 16 de 23

Now test the modified program and the form. 1. In the SolidWorks software, select the bottom face of the candlestick holder. 2. Click Start Debugging in the SolidWorks VSTA IDE. NREVIOUS P EXT TOPIC TOPIC
SolidWorks Recording Sketching Saving Examining Modifying Starting Running Creating Changing Finishing Writing Adding a the Controls Variables Code to a a Macro Debugging the Names a a Macro Generic Form a Debug Code API Macro Circle Macro Macro Form for C# VB.NET VBA the for for of a Cutthe a Overview Program Extrude Controls Form Program

The Size of Cut-Extrude in Millimeters dialog is displayed. 3. Type 15 in Radius and 10 in Depth.

NEXT TOPIC 4. Click OK to create a cut-extrude of the specified depth and radius on the bottom face. N EXT TOPIC Saving Examining Modifying Starting Running Creating Changing Finishing Adding a Variables to a a Macro Debugging the Names a Macro Generic a Debug Macro Macro Form for of a Cuta 5. Select Build > Build CreateCutExtrudeVBNET in the SolidWorks VSTA IDE. Extrude Program Program Form 6. Click File > Exit in the SolidWorks VSTA IDE to exit it and return to SolidWorks. 7. Select the bottom face of the candlestick holder again. 8. Click Run Macro (Macro toolbar).

Sketching Program Finishing Extrude Creating Adding Writing Controls Saving Testing the Controls Code the Program a Debugging a Generic Form Code Program Circle for the for Cutthe a and

9. Browse to the folder where you saved the project and then browse to \SwMacro\bin. 10. Select CreateCutExtrudeVBNET.dll and click Open. 11. Type 10 in Radius and 5 in Depth. 12. Click OK to create another cut-extrude of the specified depth and radius on the bottom face. 13. Close the candlestick part document without saving it. Congratulations! You have completed this tutorial. Return to the tutorials overview page.

SolidWorks API VBA Overview


This tutorial introduces you to the SolidWorks Application Programming Interface (API), the SolidWorks VBA macro recorder, and the Microsoft Visual Basic for Applications (VBA) Integrated Development Environment (IDE). The SolidWorks API:

Is the basis for the SolidWorks macro recorders. Lets you automate and customize the SolidWorks software and integrate your software with the SolidWorks software.

The quickest and easiest way to start programming with the SolidWorks API is to record a macro. Then you can modify the macro to fit your work site's needs. This tutorial provides instructions on recording, editing, and running a macro written in VBA in the SolidWorks software. This tutorial assumes that you are familiar with using the SolidWorks software interactively. If you plan on automating, customizing, or integrating your software with the SolidWorks software using the SolidWorks API, you should take a Visual Basic or Visual C# programming course and then attend a SolidWorks API training course offered at a SolidWorks Authorized Training and and Support Center.

Recording a Macro
Create a SolidWorks API VBA program by recording the operations performed to create a cut-extrude on a face. The SolidWorks macro recorder records every operation you perform while recording. 1. Click here to open the candlestick part (or browse to install_dir\samples\tutorial\api\cstick.sldprt.) 2. Click Record\Pause Macro 3. Select the face shown. (Macro toolbar) to begin recording the macro.

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm

26/08/2013

SolidWorks API Tutorials

Pgina 17 de 23

NREVIOUS P EXT TOPIC TOPIC 4. Click Sketch 5. Click Normal To (Sketch toolbar) to insert a sketch. (Standard Views toolbar). NEXT TOPIC N EXT TOPIC Saving Examining Modifying Starting Running Creating Changing Finishing Adding a Variables to a a Macro Debugging the Names a Macro Generic a Debug Macro Macro Form for of a Cuta
SolidWorks Recording Sketching Saving Examining Modifying Starting Running Creating Changing Finishing Writing Adding a the Controls Variables Code to a a Macro Debugging the Names a a Macro Generic Form a Debug Code API Macro Circle Macro Macro Form for C# VB.NET VBA the for for of a Cutthe a Overview Program Extrude Controls Form Program

Testing the Sketching Program Finishing Extrude Creating Adding Writing Controls Saving Controls Code the Program a Debugging a Generic Form Code Program Circle for the for Cutthe a and Extrude Program Program Form

Sketching a Circle
1. Click Circle (Sketch toolbar).

2. Sketch a circle centered at the sketch origin. 3. In the PropertyManager, set Radius 4. Click to 15.

Saving a Macro
1. Click Extruded Cut (Features toolbar).

2. In the PropertyManager, under Direction 1:


Set End Condition to Blind. Set Depth to 25. , and set Draft Angle to 15.

Click Draft On/Off .

3. Click

A cut-extrude with a radius of 15mm, depth of 25mm, and draft of 15 centered on the top face of the candlestick holder is created. 4. Click Stop Macro (Macro toolbar) to stop recording the macro.

5. In the Save As dialog box, browse to the folder where you want to save the macro. 6. Make sure Save as type is SW VBA Macros (*.swp). 7. Type CreateCutExtrudeVBA in File name. 8. Click Save.

Examining a Macro
1. Click Edit Macro (Macro toolbar).

2. Open CreateCutExtrudeVBA.swp. The Visual Basic Editor and the Code window open. The Code window contains the SolidWorks API code generated for the macro. 3. Click Open.

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm

26/08/2013

SolidWorks API Tutorials

Pgina 18 de 23

4. Click here to display the macro code in a browser. Your macro may contain more lines of code than the displayed code if you performed any operations not specified in the previous exercise; for example, rotating the part. 5. Delete any lines of code in your macro that do not appear in the displayed code. N REVIOUS EXT you TOPIC TOPIC the circle If Part.SketchManager.InsertSketch True does not appear in your code, P then created SolidWorks Recording Sketching Examining Modifying Starting Running Creating Changing Finishing Writing Adding Saving a the Controls Variables Code to a a Macro Debugging the Names a a Macro Generic Form a Debug Code API Macro Circle Macro Macro Form for C# VB.NET VBA the for for of a Cutthe a without first inserting a sketch. NEXT TOPIC 6. Type Part.SketchManager.InsertSketch True where it appears in the displayed code, if necessary. N EXT TOPIC Saving Examining Modifying Starting Running Creating Changing Finishing Adding a Variables to a a Macro Debugging the Names a Macro Generic a Debug Macro Macro Form for of a Cuta Some arguments' numeric values in your macro may be different than those in the displayed code. Sketching Program Finishing Extrude Creating Adding Writing Controls Saving Testing the Controls Code the Program a Debugging a Generic Form Code Program Circle for the for Cutthe a and These values reflect coordinates of the model. Do not modify the values in your macro to match the Extrude Program Form Program values in the displayed code. 7. Close the browser displaying the code. Leave the Code window open.
Overview Program Extrude Controls Form Program

Modifying a Macro
To avoid problems like misnamed variables and to improve the performance of your program, you should explicitly declare and bind variables early. 1. To explicitly declare all variables, type Option Explicit at the top of the program, before Dim swApp As Object. Option Explicit Dim swApp As Object About Binding Certain variables are declared automatically in a SolidWorks macro. You can delete any variables not used in the program. 2. Delete these variables declarations. Dim longstatus As Long, longwarnings As Long

3. Modify the swApp and Part variables to make them early bound. Dim swApp As SldWorks.SldWorks Dim Part As SldWorks.ModelDoc2 As you type code in the Code window, a list containing similarly spelled objects, methods, and properties is displayed. When the correct object, method, or property is selected in the list, press the Tab key to insert it in your program. 4. In the Solidworks software, drag the rollback bar up the FeatureManager design tree until it is above the Cut-Extrude1 feature. 5. In the Visual Basic editor, click Run Sub/UserForm 6. In the Macros dialog box, click Run. A new cut-extrude is created in the candlestick holder. 7. Delete the cut-extrude and sketch just created. 8. Drag the rollback bar back to the bottom of the FeatureManager design tree. 9. Delete the original cut-extrude and sketch. .

Starting to Debug a Program


To learn how to debug code in Visual Basic, change the code in the Code window to cause errors to occur while executing it. 1. Delete the letter r from the variable Part and change the name of the method InsertSketch to

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm

26/08/2013

SolidWorks API Tutorials

Pgina 19 de 23

InsertSketch2.

NREVIOUS P EXT TOPIC TOPIC NEXT TOPIC N EXT TOPIC Saving Examining Modifying Starting Running Creating Changing Finishing Adding a Variables to a a Macro Debugging the Names a Macro Generic a Debug Macro Macro Form for of a Cuta 2. In the SolidWorks graphics area, select the top face.
SolidWorks Recording Sketching Saving Examining Modifying Starting Running Creating Changing Finishing Writing Adding a the Controls Variables Code to a a Macro Debugging the Names a a Macro Generic Form a Debug Code API Macro Circle Macro Macro Form for C# VB.NET VBA the for for of a Cutthe a Overview Program Extrude Controls Form Program

Testing the Sketching Program Finishing Extrude Creating Adding Writing Controls Saving Controls Code the Program a Debugging a Generic Form Code Program Circle for the for Cutthe a and Extrude Program Program Form

You cannot edit a running macro in SolidWorks x64. Instead, you must stop the running macro, edit the macro, and run the macro again.

Running a Macro
1. Click Run Macro (Macro toolbar).

2. Open CreateCutExtrudeVBA.swp. A message box is displayed informing you that the code contains an undefined variable. Because the program contains the Option Explicit statement, all variables in the program must be defined. 3. Click OK. The offending code is selected in the Code window. The yellow arrow in the left margin indicates that execution will resume at this line of code. 4. Click the selected code, and insert the letter r so Pat becomes Part, which is a declared variable in the code.

Finishing Debugging a Program


1. Click Continue in the Visual Basic Editor.

A message box is displayed informing you that the code contains a runtime error. An object does not support the specified method or property. 2. Click Debug. The offending code is highlighted in yellow. This object does not have an InsertSketch2 method. 3. Change InsertSketch2 back to InsertSketch. 4. Click Continue to finish executing the program and to create a cut-extrude on the selected face.

5. Delete the cut-extrude and sketch.

Creating a Generic Cut-Extrude Program


Modify the macro in the Code window so that you can create a cut-extrude on any pre-selected face in any part. To make the program more generic, eliminate the draft. 1. Click here for instructions on how to modify your macro. 2. Click Save CreateCutExtrudeVBA in the Visual Basic Editor to save the modified code. 3. In the SolidWorks graphics area, select the face on the bottom of the part. 4. Click Run Sub/UserForm in the Visual Basic Editor.

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm

26/08/2013

SolidWorks API Tutorials

Pgina 20 de 23

A cut-extrude with a radius of 15mm and a depth of 25mm is created on the selected face and is centered on the sketch point. 5. Delete the cut-extrude and sketch.

Creating a Form

NREVIOUS P EXT TOPIC TOPIC

SolidWorks Recording Sketching Saving Examining Modifying Starting Running Creating Changing Finishing Writing Adding a the Controls Variables Code to a a Macro Debugging the Names a a Macro Generic Form a Debug Code API Macro Circle Macro Macro Form for C# VB.NET VBA the for for of a Cutthe a Overview Program Extrude Controls Form Program Currently you must modify the code to change the radius and depth values programmatically. To increase

NEXT TOPIC the flexibility of the program, you can create a Visual Basic form where users can specify these values. N EXT TOPIC Saving Examining Modifying Starting Running Creating Changing Finishing Adding a Variables to a a Macro Debugging the Names a Macro Generic a Debug Macro Macro Form for of a Cuta 1. In the Visual Basic Editor, click View > Project Explorer . 2. In the Project Explorer, right-click the CreateCutExtrudeVBA1 module.

Testing the Sketching Program Finishing Extrude Creating Adding Writing Controls Saving Controls Code the Program a Debugging a Generic Form Code Program Circle for the for Cutthe a and Extrude Program Program Form

3. Click Insert > UserForm to add a blank form called UserForm1 to the project and to display the Toolbox. 4. Click View > Properties Window to display the properties associated with UserForm1. 5. To change the name of the form, click UserForm1 in the Project Explorer. If you do not see UserForm1 in the Project Explorer, drag the window downward to lengthen it. 6. Type frmCutExtrude in the (Name) row in the Properties window. 7. Click the form for the change to take effect. The name at the top of the Properties window and the name of the form module in the Project Explorer change to frmCutExtrude. However, the name shown on the form is the Caption property of the form, which you did not change, so it remains UserForm1.

Adding Controls
Add two text boxes and a command button, called controls, to the form using the Toolbox. 1. Drag TextBox and place it near the upper-right corner of the form.

2. Drag another text box and place it below the text box that you just created. 3. Drag CommandButton and place it below the text boxes.

Changing Names of Controls

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm

26/08/2013

SolidWorks API Tutorials

Pgina 21 de 23

Change the names of the text boxes and the command button to more meaningful names. 1. Click the top text box on the form. 2. Type txtRadius in the (Name) row in the Properties window. 3. Click outside the row for the change to take effect. 4. Click the other text box and name it txtDepth. 5. Click the command button and name it bOK. NREVIOUS P EXT TOPIC TOPIC NEXT TOPIC N EXT TOPIC Saving Examining Modifying Starting Running Creating Changing Finishing Adding a Variables to a a Macro Debugging the Names a Macro Generic a Debug Macro Macro Form for of a Cuta
SolidWorks Recording Sketching Saving Examining Modifying Starting Running Creating Changing Finishing Writing Adding a the Controls Variables Code to a a Macro Debugging the Names a a Macro Generic Form a Debug Code API Macro Circle Macro Macro Form for C# VB.NET VBA the for for of a Cutthe a Overview Program Extrude Controls Form Program

Finishing the Form

Testing the Sketching Program Finishing Extrude Creating Adding Writing Controls Saving Controls Code the Program a Debugging a Generic Form Code Program Circle for the for Cutthe a and Extrude Program Program Form

Add labels for both text boxes so that users know what information to type in the text boxes. Also, change the caption on the command button. 1. Drag Label and place it beside the top text box.

2. Type Radius: in the Caption row in the Properties window. 3. Click anywhere on the form. 4. Place a label named Depth: beside the other text box. 5. Click CommandButton1 on the form and change its caption to OK. 6. Click anywhere on the form.

Writing Code for the Form


Visual Basic can insert code templates for event procedures for controls. You modify the code template to specify what you want to happen when an event occurs, such as when users click the OK button. 1. Double-click the OK button on the form to open the Code window containing an event procedure for this control. The name of the event procedure, bOK_Click, is a combination of the control's name as specified in the control's Name property, an underscore (_), and the name of the event. The Click procedure is the default procedure for a command button. 2. Type, or copy and paste, these declarations above Private Sub bOK_Click(). Public depth As Double Public radius As Double 3. Type, or copy and paste, this code between Private Sub bOK_Click() and End Sub.

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm

26/08/2013

SolidWorks API Tutorials

Pgina 22 de 23

' Make sure that the text in the two text boxes on the ' form, txtDepth and txtRadius, is numerical If IsNumeric(txtDepth.text) And IsNumeric(txtRadius.text) Then depth = txtDepth.text radius = txtRadius.text ' Close the dialog box and continue NREVIOUS P EXT TOPIC TOPIC Hide SolidWorks Recording Sketching Examining Modifying Starting Running Creating Changing Finishing Writing Adding Saving a the Controls Variables Code to a a Macro Debugging the Names a a Macro Generic Form a Debug Code API Macro Circle Macro Macro Form for C# VB.NET VBA the for for of a Cutthe a Else Overview Program Extrude Controls Form Program ' Display a message box telling users to enter numerical NEXT TOPIC ' values for both depth and radius EXT TOPIC Saving Examining Modifying Starting Running Creating Changing Finishing Adding a Variables to a a Macro Debugging the Names a Macro Generic a Debug Macro Macro Form for of a Cuta MsgBox "You must type numeric values for both depth and radius." N Sketching Program Finishing Extrude Creating Adding Writing Controls Saving Testing the Controls Code the Program a Debugging a Generic Form Code Program Circle for the for Cutthe a and End If
Extrude Program Program Form

Adding Variables for Controls


Modify the CreateCutExtrude1 module to display the form to users and to retrieve the depth and radius values from the form. 1. Double-click CreateCutExtrudeVBA1 in the Project Explorer to display its code in the Code window. 2. Add these two variables after Dim dz As Double. Dim HoleRadius As Double Dim HoleDepth As Double 3. Add this code after Set Part = swApp.ActiveDoc and before ' CODE TO COMMENT OUT to display the form and retrieve the depth and radius values entered by users. ' Create an instance of the user form Dim myForm as New frmCutExtrude ' Set the caption for the form myForm.Caption = "Size of Cut-Extrude in Millimeters" ' Display the user form and retrieve radius and depth ' values entered by users ' Divide the values by 1000 to change millimeters to meters myForm.Show HoleRadius = myForm.radius / 1000 HoleDepth = myForm.depth / 1000 ' Destroy the user form and remove it from memory ' because it is no longer needed Set myForm = Nothing

Saving the Code for the Form


Replace the radius and depth values, which were automatically inserted by the SolidWorks macro recorder when you created the cut-extrude in the SolidWorks user interface, with variables. 1. Replace all of the code appearing after dz = SketchPoint.ArrayData(2) with this code. 'Sketch circle centered on the sketch point Part.SketchManager.CreateCircleByRadius dx, dy, dz, HoleRadius 'Create cut-extrude without draft Part.FeatureManager.FeatureCut3 True, False, False, _ 0, 0, HoleDepth, 0.01, True, False, False, False, _ 0, 0, False, False, False, False, False, True, True, _ False, False, False, swStartSketchPlane, 0, False End If End Sub 2. Click Save CreateCutExtrudeVBA to save the modified code. 3. Click File > Close and Return to SolidWorks.

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm

26/08/2013

SolidWorks API Tutorials

Pgina 23 de 23

Testing the Program and Form


Now test the modified program and the form. 1. In the SolidWorks software, select the bottom face of the candlestick holder 2. Click Run Macro (Macro toolbar). NREVIOUS P EXT TOPIC TOPIC NEXT TOPIC N EXT TOPIC Saving Examining Modifying Starting Running Creating Changing Finishing Adding a Variables to a a Macro Debugging the Names a Macro Generic a Debug Macro Macro Form for of a Cuta
SolidWorks Recording Sketching Saving Examining Modifying Starting Running Creating Changing Finishing Writing Adding a the Controls Variables Code to a a Macro Debugging the Names a a Macro Generic Form a Debug Code API Macro Circle Macro Macro Form for C# VB.NET VBA the for for of a Cutthe a Overview Program Extrude Controls Form Program

3. Open CreateCutExtrudeVBA.swp. 4. Type 20 in Radius. 5. Type 10 in Depth.

Testing the Sketching Program Finishing Extrude Creating Adding Writing Controls Saving Controls Code the Program a Debugging a Generic Form Code Program Circle for the for Cutthe a and Extrude Program Program Form

6. Click OK to create a cut-extrude of the specified depth and radius on the bottom face. 7. Repeat steps 1 through 6 to create another cut-extrude on this face. Specify 15 in Radius and 10 in Depth. 8. Close the part without saving it. Congratulations! You have completed this tutorial. Return to the tutorials overview page.

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm

26/08/2013

S-ar putea să vă placă și