VBA. VBA at a glance. Lecture 61

Similar documents
An InputBox( ) function will display an input Box window where the user can enter a value or a text. The format is

Programming Concepts and Skills. Arrays continued and Functions

Understanding the MsgBox command in Visual Basic

variables programming statements

Excel & Visual Basic for Applications (VBA)

MICROSOFT EXCEL 2000 LEVEL 5 VBA PROGRAMMING INTRODUCTION

IFA/QFN VBA Tutorial Notes prepared by Keith Wong

VBScript: Math Functions

Programming with visual Basic:

3 IN THIS CHAPTER. Understanding Program Variables

6/14/2010. VBA program units: Subroutines and Functions. Functions: Examples: Examples:

Excel Macro Record and VBA Editor. Presented by Wayne Wilmeth

SolidWorks 2006 API 1

Copyrighted Material. Copyrighted. Material. Copyrighted

Visual Programming 1. What is Visual Basic? 2. What are different Editions available in VB? 3. List the various features of VB

Creating Macros David Giusto, Technical Services Specialist, Synergy Resources

INFORMATICS: Computer Hardware and Programming in Visual Basic 81

MICROSOFT EXCEL VISUAL BASIC FOR APPLICATIONS INTERMEDIATE

SolidWorks A Visual Basic for Applications tutorial for SolidWorks users SDC PUBLICATIONS

Program Workspace. Why numerical methods? Problem examples Why programming? Why numerical methods and programming? Why VBA?

Outline. Midterm Review. Using Excel. Midterm Review: Excel Basics. Using VBA. Sample Exam Question. Midterm Review April 4, 2014

Visual Basic for Applications

Agenda. First Example 24/09/2009 INTRODUCTION TO VBA PROGRAMMING. First Example. The world s simplest calculator...

MathsGeeks

Final Exam: Precalculus

Math Interim Mini-Tests. 3rd Grade Mini-Tests

Contents. Some Basics Simple VBA Procedure (Macro) To Execute The Procedure Recording A Macro About Macro Recorder VBA Objects Reference

Chapter 2.4: Common facilities of procedural languages

Start Visual Basic. Session 1. The User Interface Form (I/II) The Visual Basic Programming Environment. The Tool Box (I/II)

Alternatives To Custom Dialog Box

CS313D: ADVANCED PROGRAMMING LANGUAGE

Macro Programming Reference Guide. Copyright 2005 Scott Martinez

2 nd Week Lecture Notes

Full file at

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

This tutorial will teach you about operators. Operators are symbols that are used to represent an actions used in programming.

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

These are reserved words of the C language. For example int, float, if, else, for, while etc.

BASIC ELEMENTS OF A COMPUTER PROGRAM

ME009 Engineering Graphics and Design CAD 1. 1 Create a new part. Click. New Bar. 2 Click the Tutorial tab. 3 Select the Part icon. 4 Click OK.

VB FUNCTIONS AND OPERATORS

2-4 Graphing Rational Functions

Mathematics Background

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

You will have mastered the material in this chapter when you can:

Review for Programming Exam and Final May 4-9, Ribbon with icons for commands Quick access toolbar (more at lecture end)

Computer System and programming in C

Long (LONGMATH) variables may be used the same as short variables. The syntax is the same. A few limitations apply (see below).

Math Content

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

TABLE 2: Mathematics College Readiness Standards for Score Range 13 15

Working with Algebraic Expressions

VBA with SOLIDWORKS (Base Solid Model) Lecture 61

Lecture 5. If, as shown in figure, we form a right triangle With P1 and P2 as vertices, then length of the horizontal

Long (or LONGMATH ) floating-point (or integer) variables (length up to 1 million, limited by machine memory, range: approx. ±10 1,000,000.

Chapter 2: Using Data

Prentice Hall Mathematics Course Correlated to: Archdiocese of Chicago (Illinois) Mathematics Curriculum Frameworks (2004) Grades 6-12

2.Simplification & Approximation

Chapter 1 An Introduction to Computer Science. INVITATION TO Computer Science 1

Outline. Writing Functions and Subs. Review Immediate (1-line) Errors. Quiz Two on Thursday (2/23) Same Code Without Option Explicit

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

CS321 Introduction To Numerical Methods

Section 14: Trigonometry Part 1

Excel VBA Programming

r the COR d e s 3 A lg e b r a Alabama Pathways

r the COR d e s 3 A lg e b r a New York Common Core Pathways

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

INSIDE the circle. The angle is MADE BY. The angle EQUALS

Algebra II Trigonometric Functions

VISUAL BASIC 6.0 OVERVIEW

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

S206E Lecture 13, 5/22/2016, Grasshopper Math and Logic Rules

MATLAB Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras

Higher Computing Science Software Design and Development - Programming Summary Notes

Built-in Types of Data

Number- Algebra. Problem solving Statistics Investigations

Visual Basic distinguishes between a number of fundamental data types. Of these, the ones we will use most commonly are:

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 1 IDL Operators

Algebra II. Slide 1 / 162. Slide 2 / 162. Slide 3 / 162. Trigonometric Functions. Trig Functions

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

Content Map For Mathematics

Function Exit Function End Function bold End Function Exit Function

ENGG1811 Computing for Engineers Week 1 Introduction to Programming and Python

Statements and Operators

ABE Math TABE Modules 1-10

CISC 110 Day 1. Hardware, Algorithms, and Programming

Unit 12. Electronic Spreadsheets - Microsoft Excel. Desired Outcomes

Computer Programming in MATLAB

Indirect measure the measurement of an object through the known measure of another object.

The Juice Seller s Problem

Topic A- F th Grade Math Unit 1 Dates: Aug 1st- Aug 25 th

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

11.4 CIRCUMFERENCE AND ARC LENGTH 11.5 AREA OF A CIRCLE & SECTORS

Amphitheater School District End Of Year Algebra II Performance Assessment Review

Year 6 Mathematics Overview

Respond to Data Entry Events

CIS 110: Introduction to Computer Programming

SL1Trig.notebook April 06, 2013

Algorithms 4. Odd or even Algorithm 5. Greatest among three numbers Algorithm 6. Simple Calculator Algorithm

Transcription:

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