vba1 introduction to vba programming
TRANSCRIPT
-
7/26/2019 VBA1 Introduction to VBA Programming
1/30
VBA at a lance
Lecture 6 1Lecture 1
-
7/26/2019 VBA1 Introduction to VBA Programming
2/30
Activating VBA withinSOLIDWORKS
Lecture 6 2
-
7/26/2019 VBA1 Introduction to VBA Programming
3/30
VBA
unc on ec ara on
m , , , , s ou e
Dim Message, Title, Default VBA statementMessage = A : Set prompt.
Title = "InputBox" ' Set title.
Default = "0.0" ' Set default.
End Sub End of the function
Lecture 6 3
-
7/26/2019 VBA1 Introduction to VBA Programming
4/30
Data Declaration
. umer c: n eger, ong, ng e, ou e,Currency
m , , s n eger
2. Stringm y r s r ng
MyStr = SolidWorks
Lecture 6 4
-
7/26/2019 VBA1 Introduction to VBA Programming
5/30
InputBoxIn utBox r o m t t i t l e d e f a u l t x o s o s
Dim Message, Title, Default
Message = "A : "
Title = "InputBox"
Default = "0.0"A = InputBox(Message, Title, Default)
Title
Message
Default
Lecture 6 5
-
7/26/2019 VBA1 Introduction to VBA Programming
6/30
MsgBox
MsgBox(p r o m p t[, b u t t o n s] [, t i t l e ] )
Dim Msg, Style, Help, Ctxt, Response, MyString
Ms = "Do ou want to continue ?"
Style = vbYesNo + vbCritical + vbDefaultButton2
Title = "MsgBox Demonstration" , ,
MsgTitle
vbCritical
vbDefaultButton2
Lecture 6 6
(highlighted)
-
7/26/2019 VBA1 Introduction to VBA Programming
7/30
MSgBox: Style
vbOKOnly Display OK button only
vbOKCancel Display OK and Cancel buttons
vbAbortRetryIgnore Display Abort, Retry, and Ignore buttons
vbYesNoCancel Display Yes, No, and Cancel buttons
vbYesNo Display Yes and No buttons
vbRetryCancel Display Retry and Cancel buttons
vbCritical DisplayCritical Message icon
vbDefaultButton1 First button is default.
.vbDefaultButton3 Third button is default.
vbDefaultButton4 Fourth button is default.
Lecture 6 7
-
7/26/2019 VBA1 Introduction to VBA Programming
8/30
MsgBox
Dim A As Double
.
Dim Msg, Style, Title, Response
Msg = ASt le = vbOKOnl
Title = Output "
Response = MsgBox(Msg, Style, Title)
Lecture 6 8
-
7/26/2019 VBA1 Introduction to VBA Programming
9/30
Arithmetic
Numeric+ Add 5+5 10- -
/ Divide 25/5 5\ Integer Division 20\3 6* Multiply 5*4 20^ Exponent (power of) 3^3 27Mod Remainder of division 20 Mod 6 2
& String concatenation "G"&" "&"B" G B+ String concatenation A + B AB
Lecture 6 9
-
7/26/2019 VBA1 Introduction to VBA Programming
10/30
Mathematical Examples
pi = 4 * atn(1.0)
de = rad * 180.0 i
=rad
180deg
Sub main()
* input the rad
pi = 4 * atn(1.0)
= *
* display the result
End Sub
Lecture 6 10
-
7/26/2019 VBA1 Introduction to VBA Programming
11/30
Mathematical Examples
pi = 4 * atn(1.0)
rad = de * i 180
=deg pi
rad
Len th calculation
22 yxl += l = sqr ( x^2 + y^2)
Lecture 6 11
-
7/26/2019 VBA1 Introduction to VBA Programming
12/30
Mathematical Examples
par1 = sqr ( b^2 4 * a * c )
x1 = -b ar1 2 * a
ac4bb 2
x2 = (-b - par1)/(2 * a)a2, 21
Note:
Equations can written using one
equation or using mu tip e equations(depends on individual)
Lecture 6 12
-
7/26/2019 VBA1 Introduction to VBA Programming
13/30
Mathematical Operators : +
.
Dim A, B As DOUBLE
A = 2
B = 3= = =
Therefore Val is used to convert to numerical
value
C = Val(A) + Val(B) C = 5
Lecture 6 13
-
7/26/2019 VBA1 Introduction to VBA Programming
14/30
Function: with returned value
Dim length As Doublelength = Hypotenuse(3, 4) Interfacing the function
" "
End Sub
Function Hypotenuse(A As Double, B As Double) As Double
Hypotenuse = Sqr(A ^ 2 + B ^ 2)
Lecture 6 14
-
7/26/2019 VBA1 Introduction to VBA Programming
15/30
Function: with no returned value
Dim firstfirst = subr1()
End Sub
No As Double or Integer : will not return any value
Function subr1()Dim Msg, Style, Title, Response
Msg = A
Style = vbOKOnly
Response = MsgBox(Msg, Style, Title)
Lecture 6 15
-
7/26/2019 VBA1 Introduction to VBA Programming
16/30
Tips to write good program
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
-
7/26/2019 VBA1 Introduction to VBA Programming
17/30
Tips to write good program
: use to write the remark.
Function Declaration
Function Hypotenuse(A As Double, B As Double) As
Double
Hypotenuse = Sqr(A ^ 2 + B ^ 2)
Lecture 6 17
-
7/26/2019 VBA1 Introduction to VBA Programming
18/30
Tips to write good program
Sub Main()
Dim A As Double
If rad < 0 Then
rad =
Else If a < 0 Then
dim
End If
End Sub
Lecture 6 18
-
7/26/2019 VBA1 Introduction to VBA Programming
19/30
Tips to write good program
r r v r
constant
Dim pi As Double
Sub Main()
pi = 4 * atn(1.0)
End Sub
Lecture 6 19
-
7/26/2019 VBA1 Introduction to VBA Programming
20/30
Task
a cu a e e coor na es o ainscribed hexagon based on the
.
Lecture 6 20
-
7/26/2019 VBA1 Introduction to VBA Programming
21/30
Procedure
1. In ut the radius of the circle
2. Calculate all the vertices
o n .x = a cos ang
Point.y = Rad* sin(ang)
w ere ang s , , , , ,
Lecture 6 21
-
7/26/2019 VBA1 Introduction to VBA Programming
22/30
Programming: Step 1
Dim Message, Title
Dim Default, Rad As Double
Message = Radius of the circle "
e = npu ox
Default = 10.0"
, ,
Lecture 6 22
-
7/26/2019 VBA1 Introduction to VBA Programming
23/30
Programming: Step 2
Dim pi, p1x, p1y, p2x, p2y As Double
p = n
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
-
7/26/2019 VBA1 Introduction to VBA Programming
24/30
Programming: Step 3
,
the answer with known radis
Possible amendment: round-off error when rad =10.0
p2x = 5
p2x = Round(Rad * Cos(pi / 3), 2)
Lecture 6 24
-
7/26/2019 VBA1 Introduction to VBA Programming
25/30
Integration with SolidWorks
record the process of modeling.
Tool Macro Record or Icon
Lecture 6 25
-
7/26/2019 VBA1 Introduction to VBA Programming
26/30
Macro for line drawing
. e e s e c p ane
2. Set the macro to record
3. Draw the line (single line)
4. Stop the recording5. Save the macro
Lecture 6 26
-
7/26/2019 VBA1 Introduction to VBA Programming
27/30
' *****************************************************
' C:\DOCUME~1\use\LOCALS~1\Temp\swx2956\Macro1.swb - macro
recorded on 02/22/09 by use' *****************************************************
Dim Part As Object
Dim SelMgr As Object
Dim boolstatus As BooleanDim lon status As Lon lon warnin s As Lon
Dim Feature As Object
Sub main()
Set swA = A lication.SldWorks
Set Part = swApp.ActiveDoc
Part.CreateLine2 -0.0445163235549, 0.08583865080093, 0,
0.07046754356449 -0.05908893171413 0
Lecture 6 27
End Sub
-
7/26/2019 VBA1 Introduction to VBA Programming
28/30
Createline2 Function
= . , , , , ,p2z)
(double )p1x X value of the line start point(double) p1y Y value of the line start point
double 1z Z value of the line start oint(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
-
7/26/2019 VBA1 Introduction to VBA Programming
29/30
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 ObjectSet line1 = Part.CreateLine2 (p1x, p1y, 0, p2x, p2y, 0)
End Sub
Lecture 6 29
-
7/26/2019 VBA1 Introduction to VBA Programming
30/30
Task 2
r e a program o crea e e exagon
based on user input radius?
Lecture 6 30