Vous êtes sur la page 1sur 30

VBA

VBA at a glance
g

6
Lecture 1

Activating VBA within


SOLIDWORKS

Lecture 6

VBA
Sub main()

F
Function
ti
D
Declaration
l
ti

Di A
Dim
A, B
B, C,
C D
D, E A
As Double
D bl
Dim Message, Title, Default
Message = "A
" : " ' Set
S prompt.
Title = "InputBox" ' Set title.
Default = "0.0" ' Set default.

End Sub

VBA statement

End of the function


Lecture 6

Data Declaration
1 Numeric:
1.
N
i Integer,
I t
L
Long, Si
Single,
l Double,
D bl
Currency

Di A
Dim
A, B,
B C As
A Integer
I t

2. String
Dim M
Di
MyStr
St As
A String
St i
MyStr = SolidWorks

Lecture 6

InputBox
InputBox(prompt[,
p
(p
p [, title]
] [, default]
] [, xpos]
p ] [, yp
ypos]
])

Dim Message, Title, Default


Message = "A : "
Title = "InputBox"
Default = "0.0"
A = InputBox(Message,
p
g
Title, Default)

Title
Message
Default
Lecture 6

MsgBox
MsgBox(prompt[, buttons] [, title] )
Dim Msg, Style, Help, Ctxt, Response, MyString
Msg
g = "Do y
you want to continue ?"
Style = vbYesNo + vbCritical + vbDefaultButton2
Title = "MsgBox Demonstration"
Response = MsgBox(Msg
MsgBox(Msg, Style
Style, Title)
Title

Msg

vbCritical
vbDefaultButton2

vbYesNo

(highlighted)
Lecture 6

MSgBox: Style
vbOKOnly
vbOKCancel
vbAbortRetryIgnore
vbYesNoCancel
vbYesNo
vbRetryCancel
vbCritical
vbDefaultButton1
vbDefaultButton2
vbDefaultButton3
vbDefaultButton4

Display OK button only


Display
p y OK and Cancel buttons
Display Abort, Retry, and Ignore buttons
Display Yes, No, and Cancel buttons
Display Yes and No buttons
Display Retry and Cancel buttons
Display Critical Message icon
First button is default.
Second button is default.
default
Third button is default.
Fourth button is default.

Lecture 6

MsgBox
Display numeric value
Dim A As Double
A = 10
10.5
5
Dim Msg, Style, Title, Response
Msg = A
Style
Sty
e = vbOKOnly
bO O y
Title = Output "
Response = MsgBox(Msg, Style, Title)

Lecture 6

Arithmetic
Numeric
+
/
\
*
^
Mod

Add
Subtract
Divide
Integer Division
Multiply
Exponent (power of)
Remainder of division

5+5
10-5
25/5
20\3
5*4
3^3
20 Mod 6

String
&
+

String concatenation "G"&" "&"B"


String concatenation A + B

Lecture 6

10
5
5
6
20
27
2

G B
AB

Mathematical Examples
Converting radian to degree

180

deg = rad
pi

pi = 4 * atn(1.0)
deg = rad
ad * 180.0
80 0 / pi
p

Sub main()
DIM deg, rad, pi AS DOUBLE
* input the rad
pi = 4 * atn(1.0)
deg = rad * 180.0 / pi
* display the result
End Sub

Lecture 6

10

Mathematical Examples
Converting degree to rad

pi
rad = deg

180

pi = 4 * atn(1.0)
ad = deg * p
pi / 180
80
rad

Length
g calculation

l=

x2 + y2

l = sqr ( x^2 + y^2)

Lecture 6

11

Mathematical Examples
Quadratic Equation

b b 4ac
x1, x 2 =
2a
2

par1 = sqr ( b^2 4 * a * c )


x1 = (-b
( b + par1)/(2 * a)
x2 = (-b - par1)/(2 * a)

Note:
Equations can written using one
equation or using multiple
l l equations
(depends on individual)

Lecture 6

12

Mathematical Operators : +
+ : can be applied to string and number.
number
Dim A, B As DOUBLE
A=2
B=3
C = A + B C =23
23 (C = 2 + 3)
Therefore,
o , Val
a is used
u d to
o convert
o
to
o numerical
u
a
value
C = Val(A) + Val(B) C = 5
Lecture 6

13

Function: with returned value


Sub main()
Dim length As Double
length = Hypotenuse(3, 4) Interfacing the function
MsgBox "Length
Length is " & length
End Sub
Function
Function Declaration As Double: return Double value
Function Hypotenuse(A As Double, B As Double) As Double
Hypotenuse = Sqr(A ^ 2 + B ^ 2)
End Function

Lecture 6

14

Function: with no returned value


Sub main()
Dim first
first = subr1()
End Sub
No As Double or Integer : will not return any value

Function subr1()
Dim Msg, Style, Title, Response
Msg = A
Style = vbOKOnly
Title = "First subr"
Response = MsgBox(Msg, Style, Title)
End Function
Lecture 6

15

Tips to write good program


Write short source code
Short source code is easy to comprehend.
Main function will the manage the sub function
Sub Main()
func1()
func2()

End Sub
Function func1()

End Function
Lecture 6

16

Tips to write good program


Write note
: use to write the remark.
Function Declaration
Function Hypotenuse(A As Double, B As Double) As
Double

Hypotenuse = Sqr(A ^ 2 + B ^ 2)
End Function

Lecture 6

17

Tips to write good program

Use tab to differentiate between segments


Sub Main()
Dim A As Double

If rad < 0 Then


rad =

Else If a < 0 Then


dim

End If
End Sub
Lecture 6

18

Tips to write good program


Use global declaration for variables and
constant
Dim pi As Double
Sub Main()
pi = 4 * atn(1.0)
End Sub

Lecture 6

19

Task
C
Calculate
l l t th
the coordinates
di t off a
inscribed hexagon based on the
radius of the circle.
circle

Lecture 6

20

Procedure
1.

Input the radius of the circle

2.

Calculate all the vertices


Point.x
Point
x = Rad* cos(ang)
Point.y = Rad* sin(ang)
where
h
ang is
i 0o, 60o, 120o, 180o, 240o,300
300o

Lecture 6

21

Programming: Step 1
Dim
i Message, Title
i l
Dim Default, Rad As Double
Message = Radius of the circle "
Titl = "I
Title
"InputBox"
tB "
Default = 10.0"
Rad = InputBox(Message
InputBox(Message, Title
Title, Default)

Lecture 6

22

Programming: Step 2
Note: ang is radian, therefore the ang in cos function is radian.
Dim pi, p1x, p1y, p2x, p2y As Double
pi
i = 4 * At
Atn(1)
(1)
p1x = Rad
p1y = 0
p2x = Rad * Cos(pi / 3)
p2y = Rad * Sin(pi / 3)

p6x = Rad * Cos(5*pi / 3)


p6y = Rad * Sin(5*pi / 3)

Lecture 6

23

Programming: Step 3
Verifying the program,
program by displaying and checking
the answer with known radis

Possible amendment: round-off error when rad =


10.0
p2x = 5
p
p2x = Round(Rad * Cos(pi / 3), 2)

Lecture 6

24

Integration with SolidWorks


Easiest method to write VBA for Solidworks is to
record the process of modeling.
Tool Macro Record or Icon

Lecture 6

25

Macro for line drawing


1.
1
2.
3.
4.
5.
6.

Sett the
S
th sketch
k t h plane
l
Set the macro to record
Draw the line (single line)
Stop the recording
Save the macro
View the macro

Lecture 6

26

' *****************************************************
' C:\DOCUME~1\use\LOCALS~1\Temp\swx2956\Macro1.swb - macro
recorded on 02/22/09 by use
' *****************************************************
Dim swApp As Object
Dim Part As Object
Dim SelMgr As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Feature As Object
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Part.CreateLine2 -0.0445163235549, 0.08583865080093, 0,
0.07046754356449, -0.05908893171413,
0.05908893171413, 0
End Sub

Lecture 6

27

Createline2 Function
retval = ModelDoc2
ModelDoc2.CreateLine2
CreateLine2 ( p1x
p1x, p1y,
p1y p1z,
p1z p2x,
p2x p2y,
p2y
p2z)
Input:
(double )p1x
X value of the line start point
(double) p1y
Y value of the line start point
((double)) p
p1z
Z value of the line start p
point
(double) p2x
X value of the line end point
(double) p2y
Y value of the line end point
(double) p2z
Z value of the line end point
Return:
(LPDISPATCH) retval
Pointer to a Dispatch object, if the
operation fails, then NULL is returned
Lecture 6

28

Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Conversion from the macro created
p1x =
p1y =
Part.CreateLine2 p1x, p1y, 0, p2x, p2y, 0
Or
Dim line1 As Object
Set line1 = Part.CreateLine2 (p1x, p1y, 0, p2x, p2y, 0)
End Sub
Lecture 6

29

Task 2
Write
W
it a program to
t create
t the
th hexagon
h
based on user input radius?

Lecture 6

30

Vous aimerez peut-être aussi