VBA VBA at a glance Lecture 61 1
Activating VBA within SOLIDWORKS Lecture 6 2
VBA Sub main() Function Declaration Dim A, B, C, D, E As Double Dim Message, Title, Default Message = "A : " ' Set prompt. Title = "InputBox" ' Set title. Default = "0.0" ' Set default. VBA statement End Sub End of the function Lecture 6 3
Data Declaration 1. Numeric: Integer, Long, Single, Double, Currency Dim A, B, C As Integer 2. String Dim MyStr As String MyStr = SolidWorks Lecture 6 4
InputBox InputBox(prompt[, title] [, default] [, xpos] [, ypos] ) Title Dim Message, Title, Default Message = "A : " Title = "InputBox" Default = "0.0" A = InputBox(Message, Title, Default) Message Default Lecture 6 5
MsgBox MsgBox(prompt[, buttons] [, title] ) Dim Msg, Style, Help, Ctxt, Response, MyString Msg = "Do you want to continue?" Style = vbyesno + vbcritical + vbdefaultbutton2 Title = "MsgBox Demonstration" Response = MsgBox(Msg, Style, Title) Title Msg vbcritical vbyesno vbdefaultbutton2 (highlighted) Lecture 6 6
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 Display Critical Message icon vbdefaultbutton1 First button is default. vbdefaultbutton2 Second button is default. vbdefaultbutton3 Third button is default. vbdefaultbutton4 Fourth button is default. Lecture 6 7
MsgBox Display numeric value Dim A As Double A = 10.5 Dim Msg, Style, Title, Response Msg = A Style = vbokonly Title = Output " Response = MsgBox(Msg, Style, Title) Lecture 6 8
Arithmetic Numeric + Add 5+5 10 - Subtract 10-5 5 / Divide 25/5 5 \ Integer Division 20\3 6 * Multiply 5*4 20 ^ Exponent (power of) 3^3 27 Mod Remainder of division 20 Mod 6 2 String & String concatenation "G"&" "&"B" G B + String concatenation A + B AB Lecture 6 9
Mathematical Examples Converting radian to degree deg rad 180 pi Sub main() pi = 4 * atn(1.0) deg = rad * 180.0 / pi 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 rad deg pi 180 pi = 4 * atn(1.0) rad = deg * pi / 180 Length calculation l x 2 y 2 l = sqr ( x^2 + y^2) Lecture 6 11
Mathematical Examples Quadratic Equation x 1,x 2 b 2 b 2a 4ac par1 = sqr ( b^2 4 * a * c ) x1 = (-b + par1)/(2 * a) x2 = (-b - par1)/(2 * a) Note: Equations can written using one equation or using multiple equations (depends on individual) Lecture 6 12
Mathematical Operators : + + : can be applied to string and number. Dim A, B As DOUBLE A = 2 B = 3 C = A + B C =23 (C = 2 + 3 ) Therefore, Val is used to convert to numerical 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 is " & length End Sub 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 Calculate the coordinates of a inscribed hexagon based on the radius of the circle. Lecture 6 20
Procedure 1. Input the radius of the circle 2. Calculate all the vertices Point.x = Rad* cos(ang) Point.y = Rad* sin(ang) where ang is 0 o, 60 o, 120 o, 180 o, 240 o,300 o Lecture 6 21
Programming: Step 1 Dim Message, Title Dim Default, Rad As Double Message = Radius of the circle " Title = "InputBox" Default = 10.0" Rad = InputBox(Message, 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 = 4 * Atn(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, by displaying and checking 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
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. Set the sketch plane 2. Set the macro to record 3. Draw the line (single line) 4. Stop the recording 5. Save the macro 6. 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 End Sub Lecture 6 27
Createline2 Function retval = ModelDoc2.CreateLine2 ( p1x, p1y, p1z, p2x, p2y, p2z) 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 (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 a program to create the hexagon based on user input radius? Lecture 6 30