vba1 introduction to vba programming.pptjamalt/sme4513/vba1 introduction to... · programming: step...

30
VBA VBA VBA at a glance Lecture 6 1 Lecture 1

Upload: hahuong

Post on 15-Apr-2018

276 views

Category:

Documents


16 download

TRANSCRIPT

VBAVBA

VBA at a glanceg

Lecture 6 1Lecture 1

Activating VBA within SOLIDWORKS

Lecture 6 2

VBA

Sub main() F ti D l tiSub main()

Di A B C D E A D bl

Function Declaration

Dim A, B, C, D, E As Double

Dim Message, Title, Default" " ' S

VBA statementMessage = "A : " ' Set prompt.Title = "InputBox" ' Set title.Default = "0.0" ' Set default.…

End Sub End of the function

Lecture 6 3

Data Declaration

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

Di A B C A I tDim A, B, C As Integer

2. StringDi M St A St iDim MyStr As StringMyStr = “SolidWorks”

Lecture 6 4

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

Dim Message, Title, Default

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

Title

Message

Default

Lecture 6 5

MsgBox

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

MsgTitle

vbCritical

vbYesNovbDefaultButton2

Lecture 6 6

vbYesNo(highlighted)

MSgBox: Style

vbOKOnly Display OK button onlyvbOKCancel Display OK and Cancel buttonsp yvbAbortRetryIgnore Display Abort, Retry, and Ignore buttonsvbYesNoCancel Display Yes, No, and Cancel buttonsvbYesNo Display Yes and No buttonsvbRetryCancel Display Retry and Cancel buttonsvbCritical Display Critical Message iconvbDefaultButton1 First button is default. vbDefaultButton2 Second button is default vbDefaultButton2 Second button is default. vbDefaultButton3 Third button is default. vbDefaultButton4 Fourth button is default.

Lecture 6 7

MsgBoxDisplay numeric value Display numeric value

Dim A As DoubleA = 10 5A = 10.5Dim Msg, Style, Title, ResponseMsg = AStyle = vbOKOnlySty e bO O yTitle = “Output "Response = MsgBox(Msg, Style, Title)

Lecture 6 8

Arithmetic

Numeric+ Add 5+5 10- Subtract 10-5 5- Subtract 10-5 5/ 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

StringString& String concatenation "G"&" "&"B" “ “G B“+ String concatenation “A” + “B” “AB”

Lecture 6 9

Mathematical ExamplesConverting radian to degree

pi = 4 * atn(1.0)deg = rad * 180.0 / pi⎟⎟

⎞⎜⎜⎝

⎛=

pirad 180deg

Converting radian to degree

deg ad 80 0 / p⎠⎝ pi

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

End Sub

Lecture 6 10

Mathematical ExamplesConverting degree to rad

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

⎟⎠⎞

⎜⎝⎛=180

deg pirad

Converting degree to rad

ad deg p / 80⎠⎝180

Length calculation

22 yxl +=

g

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

Lecture 6 11

Mathematical ExamplesQuadratic Equation

par1 = sqr ( b^2 – 4 * a * c )x1 = (-b + par1)/(2 * a)

Quadratic Equation

ac4bbxx2 −±−

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

x,x 21 =

Note:Equations can written using one

l lequation or using multiple equations (depends on individual)

Lecture 6 12

Mathematical Operators : ++ : can be applied to string and number+ : can be applied to string and number.

Dim A, B As DOUBLEA = 2B = 3C A + B C 23 (C “2” + “3”)C = A + B C =23 (C = “2” + “3”)

Therefore, Val is used to convert to numerical o , a u d o o o u avalue

C = Val(A) + Val(B) C = 5

Lecture 6 13

Function: with returned value Sub main()Sub main()

Dim length As Doublelength = Hypotenuse(3, 4) ‘Interfacing the functionMsgBox "Length is " & lengthMsgBox Length is & length

End Sub

‘Function Declaration As Double: return Double valueFunction Declaration As Double: return Double valueFunction Hypotenuse(A As Double, B As Double) As DoubleHypotenuse = Sqr(A ^ 2 + B ^ 2)End FunctionEnd Function

Lecture 6 14

Function: with no returned value Sub main()Sub main()

Dim firstfirst = subr1()

End Sub

‘No As Double or Integer : will not return any valueFunction subr1()

Dim Msg, Style, Title, ResponseMsg = AStyle = vbOKOnlyTitle = "First subr"Title = "First subr"Response = MsgBox(Msg, Style, Title)

End Function

Lecture 6 15

End Function

Tips to write good programWrite short source codeWrite short source codeShort 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 programWrite noteWrite note‘ : use to write the remark.

‘Function DeclarationFunction Hypotenuse(A As Double, B As Double) As

Double…Hypotenuse = Sqr(A ^ 2 + B ^ 2)

End FunctionEnd Function

Lecture 6 17

Tips to write good programUse tab to differentiate between segmentsUse tab to differentiate between segments

Sub Main()Dim A As Double…If rad < 0 Then

rad = ……Else If a < 0 Then

dim ……

End IfEnd Sub

Lecture 6 18

Tips to write good program

Use global declaration for variables and 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 l l t th di t f Calculate the coordinates of a inscribed hexagon based on the radius of the circle radius of the circle.

Lecture 6 20

Procedure1. Input the radius of the circle1. Input the radius of the circle

2. Calculate all the vertices

Point x Rad* cos(ang)Point.x = Rad* cos(ang)Point.y = Rad* sin(ang)

h i 0o 60o 120o 180o 240o 300owhere ang is 0o, 60o, 120o, 180o, 240o,300o

Lecture 6 21

Programming: Step 1

i i lDim Message, TitleDim Default, Rad As Double

Message = “Radius of the circle " Titl "I tB "Title = "InputBox" Default = “10.0" Rad = InputBox(Message Title Default)Rad = InputBox(Message, Title, Default)

Lecture 6 22

Programming: Step 2Note: ang is radian, therefore the ang in cos function is radian.Note: ang is radian, therefore the ang in cos function is radian.

Dim pi, p1x, p1y, p2x, p2y As Double

i 4 * At (1)pi = 4 * Atn(1)

p1x = Radp1y = 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 3Verifying the program by displaying and checking Verifying the program, by displaying and checking the answer with known radis

Possible amendment: round-off error when rad = 10.0p2x = 5 p

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

Lecture 6 24

Integration with SolidWorksEasiest method to write VBA for Solidworks is to 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 S t th k t h l1. Set the sketch plane2. Set the macro to record3. Draw the line (single line)4. Stop the recording5. Save the macro6. View the macro6. 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 ObjectDim swApp As ObjectDim Part As ObjectDim SelMgr As ObjectDim boolstatus As BooleanDim longstatus As Long, longwarnings As LongDim longstatus As Long, longwarnings As LongDim Feature As ObjectSub main()

Set swApp = Application.SldWorksSet swApp Application.SldWorksSet Part = swApp.ActiveDoc

Part.CreateLine2 -0.0445163235549, 0.08583865080093, 0, 0.07046754356449, -0.05908893171413, 0

Lecture 6 27

0.07046754356449, 0.05908893171413, 0

End Sub

Createline2 Functionretval = ModelDoc2 CreateLine2 ( p1x p1y p1z p2x p2y retval = ModelDoc2.CreateLine2 ( p1x, p1y, p1z, p2x, p2y,

p2z)

Input:Input:(double )p1x X value of the line start point(double) p1y Y value of the line start point(double) p1z Z value of the line start point( ) p p(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 pointReturn:(LPDISPATCH) retval Pointer to a Dispatch object, if the

operation fails, then NULL is returned

Lecture 6 28

Sub main()Set swApp = Application.SldWorksSet 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

Task 2

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

Lecture 6 30