Вы находитесь на странице: 1из 23

SolidWorks API Tutorials Página 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,P
NREVIOUS
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
aaMacro
Debugging
the
Names
aaMacro
Generic
Form
aDebug
Code
API
Macro
Circle
Macro
Macro
Form
forVBA
C#
VB.NET
the
for
for
ofaCut-
the
a
 create a form where users can specify the cut-extrude's radius and depth valuesForm
Overview
Program
Extrude
Controls Program
and create the cut-
extrude on any pre-selected face on the part NEXT TOPIC
N EXT TOPIC
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Adding a
Variables
to
aaMacro
Debugging
the
Names
aMacro
Generic
aDebug
Macro
Macro
Form for
ofaCut-
a
using the SolidWorks C#, VB.NET, or VBA macro recorder. Click a link to open the Testing
SolidWorks
Sketching
Program
Finishing
Extrude
Creating
Adding
Writing
Controls
Saving the
Controls
Code
the
Program
API
aDebugging
aForm
Generic
Code
Program
Circle
tutorial
for the
forCut-
the
aand
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 (Macro toolbar) to begin recording the macro.

5. Select the face shown.

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm 26/08/2013
SolidWorks API Tutorials Página 2 de 23

NREVIOUS
P EXT TOPIC
TOPIC
SolidWorks
Recording
Sketching
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Writing
Adding a
the
Controls
Variables
Code
to
aaMacro
Debugging
the
Names
aaMacro
Generic
Form
aDebug
Code
API
Macro
Circle
Macro
Macro
Form
forVBA
C#
VB.NET
the
for
for
ofaCut-
the
a
6. Click Sketch (Sketch toolbar) to insert a sketch. OverviewProgram
Program
Extrude
Controls
Form
NEXT TOPIC
NEXT TOPIC
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Adding a
Variables
to
aaMacro
Debugging
the
Names
aMacro
Generic
aDebug
Macro
Macro
Formfor
ofaCut-
a
7. Click Normal To (Standard Views toolbar). Testing the
Sketching
Program
Finishing
Extrude
Creating
Adding
Writing
Controls
Saving Controls
Code
the
Program
aDebugging
aForm
Generic
Code
Program
Circle
for the
forCut-
the
aand
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 to 15.

4. Click

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.

 Click Draft On/Off , and set Draft Angle to 15.

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 Página 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.
If swDoc.SketchManager.InsertSketch(true) does not appear in your code, NREVIOUS
P EXT TOPIC
then youTOPICcreated the
circle without first inserting a sketch. SolidWorks
Recording
Sketching
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Writing
Adding
Saving a
the
Controls
Variables
Code
to
aaMacro
Debugging
the
Names
aaMacro
Generic
Form
aDebug
Code
API
Macro
Circle
Macro
Macro
Form
forVBA
C#
VB.NET
the
for
for
ofaCut-
the
a
OverviewProgram
Program
Extrude
Controls
Form
NEXT TOPIC
6. Type swDoc.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
aaMacro
Debugging
the
Names
aMacro
Generic
aDebug
Macro
Macro
Formfor
ofaCut-
a
Sketching
Program
Finishing
Extrude
Creating
Adding
Writing
Controls
Saving
Testing the
Controls
Code
the
Program
aDebugging
aForm
Generic
Code
Program
Circle
for the
forCut-
the
aand
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.

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 Página 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.

NREVIOUS
P EXT TOPIC
TOPIC
Finishing Debugging a Program SolidWorks
Recording
Sketching
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Writing
Adding a
the
Controls
Variables
Code
to
aaMacro
Debugging
the
Names
aaMacro
Generic
Form
aDebug
Code
API
Macro
Circle
Macro
Macro
Form
forVBA
C#
VB.NET
the
for
for
ofaCut-
the
a
OverviewProgram
Program
Extrude
Controls
Form
NEXT TOPIC
NEXT TOPIC
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Adding a
Variables
to
aaMacro
Debugging
the
Names
aMacro
Generic
aDebug
Macro
Macro
Formfor
ofaCut-
a
1. Click Start Debugging in the SolidWorks VSTA IDE.
Testing the
Sketching
Program
Finishing
Extrude
Creating
Adding
Writing
Controls
Saving Controls
Code
the
Program
aDebugging
aForm
Generic
Code
Program
Circle
for the
forCut-
the
aand
A message box is displayed informing you that there were build errors. Extrude Program
Program
Form

2. To debug the errors, click No.

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 Página 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
on the form is the Text property of the form, which you did not change, so itP
NREVIOUS
EXT TOPIC
remains TOPIC
Form1.
SolidWorks
Recording
Sketching
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Writing
Adding a
the
Controls
Variables
Code
to
aaMacro
Debugging
the
Names
aaMacro
Generic
Form
aDebug
Code
API
Macro
Circle
Macro
Macro
Form
forVBA
C#
VB.NET
the
for
for
ofaCut-
the
a
OverviewProgram
Program
Extrude
Controls
Form
NEXT TOPIC
Adding Controls NEXT TOPIC
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Adding a
Variables
to
aaMacro
Debugging
the
Names
aMacro
Generic
aDebug
Macro
Macro
Formfor
ofaCut-
a
Sketching
Program
Finishing
Extrude
Creating
Adding
Writing
Controls
Saving
Testing the
Controls
Code
the
Program
aDebugging
aForm
Generic
Code
Program
Circle
for the
forCut-
the
aand
Add two text boxes and a command button, called controls, to the form using the Toolbox.
Extrude Program
Program
Form

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.

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

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 Página 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. NREVIOUS
P EXT TOPIC
TOPIC
SolidWorks
Recording
Sketching
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Writing
Adding a
the
Controls
Variables
Code
to
aaMacro
Debugging
the
Names
aaMacro
Generic
Form
aDebug
Code
API
Macro
Circle
Macro
Macro
Form
forVBA
C#
VB.NET
the
for
for
ofaCut-
the
a
5. Click button1 on the form and change its caption to OK. OverviewProgram
Program
Extrude
Controls
Form
NEXT TOPIC
6. Click anywhere on the form. NEXT TOPIC
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Adding a
Variables
to
aaMacro
Debugging
the
Names
aMacro
Generic
aDebug
Macro
Macro
Formfor
ofaCut-
a
Testing the
Sketching
Program
Finishing
Extrude
Creating
Adding
Writing
Controls
Saving Controls
Code
the
Program
aDebugging
aForm
Generic
Code
Program
Circle
for the
forCut-
the
aand
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 Página 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,
//non-zero, numeric values NREVIOUS
P EXT TOPIC TOPIC
double.TryParse(txtRadius.Text, out radiusResult); SolidWorks
Recording
Sketching
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Writing
Adding
Saving a
the
Controls
Variables
Code
to
aaMacro
Debugging
the
Names
aaMacro
Generic
Form
aDebug
Code
API
Macro
Circle
Macro
Macro
Form
forVBA
C#
VB.NET
the
for
for
ofaCut-
the
a
double.TryParse(txtDepth.Text, out depthResult); OverviewProgram
Program
Extrude
Controls
Form
if (radiusResult > 0 && depthResult > 0) NEXT TOPIC
{ N EXT TOPIC
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Adding a
Variables
to
aaMacro
Debugging
the
Names
aMacro
Generic
aDebug
Macro
Macro
Form for
ofaCut-
a
radius = radiusResult; Sketching
Program
Finishing
Extrude
Creating
Adding
Writing
Controls
Saving
Testing the
Controls
Code
the
Program
aDebugging
aForm
Generic
Code
Program
Circle
for the
forCut-
the
aand
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 Página 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
aaMacro
Debugging
the
Names
aaMacro
Generic
Form
aDebug
Code
API
Macro
Circle
Macro
Macro
Form
forVBA
C#
VB.NET
the
for
for
ofaCut-
the
a
} OverviewProgram
Program
Extrude
Controls
Form
//KEEP THESE LINES OF CODE NEXT TOPIC
///<summary> N EXT TOPIC
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Adding a
Variables
to
aaMacro
Debugging
the
Names
aMacro
Generic
aDebug
Macro
Macro
Form for
ofaCut-
a
///The Sldworks swApp variable is pre-assigned for you. Sketching
Program
Finishing
Extrude
Creating
Adding
Writing
Controls
Saving
Testing the
Controls
Code
the
Program
aDebugging
aForm
Generic
Code
Program
Circle
for the
forCut-
the
aand


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 Página 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
aaMacro
Debugging
the
Names
aaMacro
Generic
Form
aDebug
Code
API
Macro
Circle
Macro
Macro
Form
forVBA
C#
VB.NET
the
for
for
ofaCut-
the
a
Create a SolidWorks API VB.NET program by recording the operations performed toForm
Overview
Program
Extrude
Controls
createProgram
a cut-extrude on
a face. The SolidWorks macro recorder records every operation you perform whileNrecording.
EXT TOPIC
N EXT TOPIC
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Adding a
Variables
to
aaMacro
Debugging
the
Names
aMacro
Generic
aDebug
Macro
Macro
Formfor
ofaCut-
a
1. Click here to open the candlestick part (or browse to Testing the
Sketching
Program
Finishing
Extrude
Creating
Adding
Writing
Controls
Saving Controls
Code
the
Program
aDebugging
aForm
Generic
Code
Program
Circle
for the
forCut-
the
aand
install_dir\samples\tutorial\api\cstick.sldprt.) 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 (Macro toolbar) to begin recording the macro.

5. Select the face shown.

6. Click Sketch (Sketch toolbar) to insert a sketch.

7. Click Normal To (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 to 15.

4. Click

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.

 Click Draft On/Off , and set Draft Angle to 15.

3. Click .

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm 26/08/2013
SolidWorks API Tutorials Página 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). SolidWorks


Recording
Sketching
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Writing
Adding a
the
Controls
Variables
Code
to
aaMacro
Debugging
the
Names
aaMacro
Generic
Form
aDebug
Code
API
Macro
Circle
Macro
Macro
Form
forVBA
C#
VB.NET
the
for
for
ofaCut-
the
a
OverviewProgram
Program
Extrude
Controls
Form
7. Type CreateCutExtrudeVBNET in File name. NEXT TOPIC
NEXT TOPIC
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Adding a
Variables
to
aaMacro
Debugging
the
Names
aMacro
Generic
aDebug
Macro
Macro
Formfor
ofaCut-
a
8. Click Save. Testing the
Sketching
Program
Finishing
Extrude
Creating
Adding
Writing
Controls
Saving Controls
Code
the
Program
aDebugging
aForm
Generic
Code
Program
Circle
for the
forCut-
the
aand
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 Página 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. NREVIOUS
P EXT TOPIC
TOPIC

4. Delete the cut-extrude and the sketch just created. SolidWorks


Recording
Sketching
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Writing
Adding a
the
Controls
Variables
Code
to
aaMacro
Debugging
the
Names
aaMacro
Generic
Form
aDebug
Code
API
Macro
Circle
Macro
Macro
Form
forVBA
C#
VB.NET
the
for
for
ofaCut-
the
a
OverviewProgram
Program
Extrude
Controls
Form
5. Drag the rollback bar back to the bottom of the FeatureManager design tree.NEXT TOPIC
NEXT TOPIC
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Adding a
Variables
to
aaMacro
Debugging
the
Names
aMacro
Generic
aDebug
Macro
Macro
Formfor
ofaCut-
a
6. Delete the original cut-extrude and sketch. Testing the
Sketching
Program
Finishing
Extrude
Creating
Adding
Writing
Controls
Saving Controls
Code
the
Program
aDebugging
aForm
Generic
Code
Program
Circle
for the
forCut-
the
aand
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 to finish executing the program and to create a cut-extrude on the
selected face.

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 Página 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.

7. Delete the cut-extrude and sketch. NREVIOUS


P EXT TOPIC
TOPIC
SolidWorks
Recording
Sketching
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Writing
Adding a
the
Controls
Variables
Code
to
aaMacro
Debugging
the
Names
aaMacro
Generic
Form
aDebug
Code
API
Macro
Circle
Macro
Macro
Form
forVBA
C#
VB.NET
the
for
for
ofaCut-
the
a
OverviewProgram
Program
Extrude
Controls
Form
NEXT TOPIC
Creating a Generic Cut-Extrude Program NEXT TOPIC
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Adding a
Variables
to
aaMacro
Debugging
the
Names
aMacro
Generic
aDebug
Macro
Macro
Formfor
ofaCut-
a
Sketching
Program
Finishing
Extrude
Creating
Adding
Writing
Controls
Saving
Testing the
Controls
Code
the
Program
aDebugging
aForm
Generic
Code
Program
Circle
for the
forCut-
the
aand
Modify the macro in the Code Editor window so that you can create a cut-extrude on any pre-selected
Extrude
Program
Form Program 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.vb 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 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 Página 13 de 23

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

NREVIOUS
P EXT TOPIC
TOPIC
SolidWorks
Recording
Sketching
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Writing
Adding a
the
Controls
Variables
Code
to
aaMacro
Debugging
the
Names
aaMacro
Generic
Form
aDebug
Code
API
Macro
Circle
Macro
Macro
Form
forVBA
C#
VB.NET
the
for
for
ofaCut-
the
a
OverviewProgram
Program
Extrude
Controls
Form
NEXT TOPIC
NEXT TOPIC
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Adding a
Variables
to
aaMacro
Debugging
the
Names
aMacro
Generic
aDebug
Macro
Macro
Formfor
ofaCut-
a
Testing the
Sketching
Program
Finishing
Extrude
Creating
Adding
Writing
Controls
Saving Controls
Code
the
Program
aDebugging
aForm
Generic
Code
Program
Circle
for the
forCut-
the
aand
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 Página 14 de 23

NREVIOUS
P EXT TOPIC
TOPIC
SolidWorks
Recording
Sketching
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Writing
Adding a
the
Controls
Variables
Code
to
aaMacro
Debugging
the
Names
aaMacro
Generic
Form
aDebug
Code
API
Macro
Circle
Macro
Macro
Form
forVBA
C#
VB.NET
the
for
for
ofaCut-
the
a
OverviewProgram
Program
Extrude
Controls
Form
NEXT TOPIC
NEXT TOPIC
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Adding a
Variables
to
aaMacro
Debugging
the
Names
aMacro
Generic
aDebug
Macro
Macro
Formfor
ofaCut-
a
Testing the
Sketching
Program
Finishing
Extrude
Creating
Adding
Writing
Controls
Saving Controls
Code
the
Program
aDebugging
aForm
Generic
Code
Program
Circle
for the
forCut-
the
aand
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 Página 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
3. Insert the following code between swDoc = CType(swApp.ActiveDoc, ModelDoc2)EXT TOPIC
andTOPIC 'COMMENT OUT
THESE LINES OF CODE to display the form and retrieve the depth and radius values
Creating
Changingtyped
SolidWorks
Recording
Sketching
Examining
Modifying
Starting
Running
Finishing
Writing
Adding
Saving a
the
Controls
Variables
Code
to
aaMacro by
Debugging
the
Names
aaMacro
Generic
Form
aDebug
Code
API
Macro
Circle
Macro
Macro
Form
forC# the
VB.NET
VBA
the
for
for
of a user .
aCut-
the
OverviewProgram
Program
Extrude
Controls
Form
Dim HoleRadius As Double = 0 NEXT TOPIC
Dim HoleDepth As Double = 0 NEXT TOPIC
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Adding a
Variables
to
aaMacro
Debugging
the
Names
aMacro
Generic
aDebug
Macro
Macro
Formfor
ofaCut-
a
Testing the
Sketching
Program
Finishing
Extrude
Creating
Adding
Writing
Controls
Saving Controls
Code
the
Program
aDebugging
aForm
Generic
Code
Program
Circle
for the
forCut-
the
aand
' Create an instance of the user form Extrude Program
Program
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

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 Página 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.


The Size of Cut-Extrude in Millimeters dialog is displayed. NREVIOUS
P EXT TOPIC
TOPIC
SolidWorks
Recording
Sketching
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Writing
Adding a
the
Controls
Variables
Code
to
aaMacro
Debugging
the
Names
aaMacro
Generic
Form
aDebug
Code
API
Macro
Circle
Macro
Macro
Form
forVBA
C#
VB.NET
the
for
for
ofaCut-
the
a
3. Type 15 in Radius and 10 in Depth. OverviewProgram
Program
Extrude
Controls
Form
NEXT TOPIC
4. Click OK to create a cut-extrude of the specified depth and radius on the bottom
N EXTface.
SavingTOPIC
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Adding a
Variables
to
aaMacro
Debugging
the
Names
aMacro
Generic
aDebug
Macro
Macro
Formfor
ofaCut-
a
Sketching
Program
Finishing
Extrude
Creating
Adding
Writing
Controls
Saving
Testing the
Controls
Code
the
Program
aDebugging
aForm
Generic
Code
Program
Circle
for the
forCut-
the
aand
5. Select Build > Build CreateCutExtrudeVBNET in the SolidWorks VSTA IDE.Form
Extrude Program
Program

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).

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 (Macro toolbar) to begin recording the macro.

3. Select the face shown.

file:///C:/Users/paco25/AppData/Local/Temp/~hh95D6.htm 26/08/2013
SolidWorks API Tutorials Página 17 de 23

NREVIOUS
P EXT TOPIC
TOPIC
SolidWorks
Recording
Sketching
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Writing
Adding a
the
Controls
Variables
Code
to
aaMacro
Debugging
the
Names
aaMacro
Generic
Form
aDebug
Code
API
Macro
Circle
Macro
Macro
Form
forVBA
C#
VB.NET
the
for
for
ofaCut-
the
a
4. Click Sketch (Sketch toolbar) to insert a sketch. OverviewProgram
Program
Extrude
Controls
Form
NEXT TOPIC
NEXT TOPIC
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Adding a
Variables
to
aaMacro
Debugging
the
Names
aMacro
Generic
aDebug
Macro
Macro
Formfor
ofaCut-
a
5. Click Normal To (Standard Views toolbar). Testing the
Sketching
Program
Finishing
Extrude
Creating
Adding
Writing
Controls
Saving Controls
Code
the
Program
aDebugging
aForm
Generic
Code
Program
Circle
for the
forCut-
the
aand
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 to 15.

4. Click

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.

 Click Draft On/Off , and set Draft Angle to 15.

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 Página 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.
If Part.SketchManager.InsertSketch True does not appear in your code, P N REVIOUS
EXT you
then TOPIC TOPIC the circle
created
without first inserting a sketch. SolidWorks
Recording
Sketching
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Writing
Adding
Saving a
the
Controls
Variables
Code
to
aa
Macro
Debugging
the
Names
aa
Macro
Generic
Form
aDebug
Code
API
Macro
Circle
Macro
Macro
Form
forVBA
C#
VB.NET
the
for
for
ofaCut-
the
a
OverviewProgram
Program
Extrude
Controls
Form
6. Type Part.SketchManager.InsertSketch True where it appears in the displayed NEXT TOPIC
code, if necessary.
N EXT TOPIC
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Adding a
Variables
to
aaMacro
Debugging
the
Names
aMacro
Generic
aDebug
Macro
Macro
Form for
ofaCut-
a
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
aDebugging
aForm
Generic
Code
Program
Circle
for the
forCut-
the
aand
These values reflect coordinates of the model. Do not modify the values in your macro
Extrude
Program
Form to match the
Program
values in the displayed code.

7. Close the browser displaying the code. Leave the Code window open.

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 Página 19 de 23

InsertSketch2.

NREVIOUS
P EXT TOPIC
TOPIC
SolidWorks
Recording
Sketching
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Writing
Adding a
the
Controls
Variables
Code
to
aaMacro
Debugging
the
Names
aaMacro
Generic
Form
aDebug
Code
API
Macro
Circle
Macro
Macro
Form
forVBA
C#
VB.NET
the
for
for
ofaCut-
the
a
OverviewProgram
Program
Extrude
Controls
Form
NEXT TOPIC
NEXT TOPIC
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Adding a
Variables
to
aaMacro
Debugging
the
Names
aMacro
Generic
aDebug
Macro
Macro
Formfor
ofaCut-
a
Testing the
Sketching
Program
Finishing
Extrude
Creating
Adding
Writing
Controls
Saving Controls
Code
the
Program
aDebugging
aForm
Generic
Code
Program
Circle
for the
forCut-
the
aand
Extrude Program
Program
Form
2. In the SolidWorks graphics area, select the top face.

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 Página 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
aaMacro
Debugging
the
Names
aaMacro
Generic
Form
aDebug
Code
API
Macro
Circle
Macro
Macro
Form
forVBA
C#
VB.NET
the
for
for
ofaCut-
the
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
aaMacro
Debugging
the
Names
aMacro
Generic
aDebug
Macro
Macro
Formfor
ofaCut-
a
1. In the Visual Basic Editor, click View > Project Explorer . Testing the
Sketching
Program
Finishing
Extrude
Creating
Adding
Writing
Controls
Saving Controls
Code
the
Program
aDebugging
aForm
Generic
Code
Program
Circle
for the
forCut-
the
aand
Extrude Program
Program
Form
2. In the Project Explorer, right-click the CreateCutExtrudeVBA1 module.

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 Página 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. NREVIOUS
P EXT TOPIC
TOPIC

4. Click the other text box and name it txtDepth. SolidWorks


Recording
Sketching
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Writing
Adding a
the
Controls
Variables
Code
to
aaMacro
Debugging
the
Names
aaMacro
Generic
Form
aDebug
Code
API
Macro
Circle
Macro
Macro
Form
forVBA
C#
VB.NET
the
for
for
ofaCut-
the
a
OverviewProgram
Program
Extrude
Controls
Form
NEXT TOPIC
5. Click the command button and name it bOK.
NEXT TOPIC
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Adding a
Variables
to
aaMacro
Debugging
the
Names
aMacro
Generic
aDebug
Macro
Macro
Formfor
ofaCut-
a
Testing the
Sketching
Program
Finishing
Extrude
Creating
Adding
Writing
Controls
Saving Controls
Code
the
Program
aDebugging
aForm
Generic
Code
Program
Circle
for the
forCut-
the
aand
Extrude Program
Program
Form
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 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 Página 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
Hide NREVIOUS
P EXT TOPIC TOPIC
Else SolidWorks
Recording
Sketching
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Writing
Adding
Saving a
the
Controls
Variables
Code
to
aaMacro
Debugging
the
Names
aaMacro
Generic
Form
aDebug
Code
API
Macro
Circle
Macro
Macro
Form
forVBA
C#
VB.NET
the
for
for
ofaCut-
the
a
' Display a message box telling users to enter numerical OverviewProgram
Program
Extrude
Controls
Form
' values for both depth and radius NEXT TOPIC
MsgBox "You must type numeric values for both depth and radius." N EXT TOPIC
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Adding a
Variables
to
aaMacro
Debugging
the
Names
aMacro
Generic
aDebug
Macro
Macro
Form for
ofaCut-
a
End If Sketching
Program
Finishing
Extrude
Creating
Adding
Writing
Controls
Saving
Testing the
Controls
Code
the
Program
aDebugging
aForm
Generic
Code
Program
Circle
for the
forCut-
the
aand
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 Página 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
NREVIOUS
P EXT TOPIC
TOPIC
SolidWorks
Recording
Sketching
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Writing
Adding a
the
Controls
Variables
Code
to
aaMacro
Debugging
the
Names
aaMacro
Generic
Form
aDebug
Code
API
Macro
Circle
Macro
Macro
Form
forVBA
C#
VB.NET
the
for
for
ofaCut-
the
a
2. Click Run Macro (Macro toolbar).
OverviewProgram
Program
Extrude
Controls
Form
NEXT TOPIC
3. Open CreateCutExtrudeVBA.swp. NEXT TOPIC
Saving
Examining
Modifying
Starting
Running
Creating
Changing
Finishing
Adding a
Variables
to
aaMacro
Debugging
the
Names
aMacro
Generic
aDebug
Macro
Macro
Formfor
ofaCut-
a
Testing the
Sketching
Program
Finishing
Extrude
Creating
Adding
Writing
Controls
Saving Controls
Code
the
Program
aDebugging
aForm
Generic
Code
Program
Circle
for the
forCut-
the
aand
4. Type 20 in Radius.
Extrude Program
Program
Form
5. Type 10 in Depth.

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

Вам также может понравиться