Post Processor Lessons - Lesson 6

 

 

Parent    Previous    Next

POST MODIFICATION LESSON 6: VB SCRIPTING 1

The BobCAD-CAM posting engine allows the post writer to have access to VB Scripting (Visual Basic Scripting) functionality to allow total flexibility to accomplish the most demanding post processor output needs.

Wikipedia on VB Scripting

VBScript ("Microsoft Visual Basic Scripting Edition") is an Active Scripting language developed by Microsoft that is modeled on Visual Basic. It allows Microsoft Windows system administrators to generate powerful tools for managing computers with error handling, subroutines, and other advanced programming constructs. It can give the user complete control over many aspects of their computing environment.
VBScript uses the Component Object Model to access elements of the environment within which it is running; for example, the FileSystemObject (FSO) is used to create, read, update and delete files. VBScript has been installed by default in every desktop release of Microsoft Windows since Windows 98;[1] in Windows Server since Windows NT 4.0 Option Pack;[2] and optionally with Windows CE (depending on the device it is installed on).
A VBScript script must be executed within a host environment, of which there are several provided with Microsoft Windows, including: Windows Script Host (WSH), Internet Explorer (IE), and Internet Information Services (IIS).[3] Additionally, the VBScript hosting environment is embeddable in other programs, through technologies such as the Microsoft Script Control (msscript.ocx).


The BobCAD-CAM posting engine allows the post writer to create scripting blocks in Post Blocks: 2001 thru 2099. The syntax used inside these Post Blocks must be VB Script. You cannot use normal BobCAD-CAM posting variables except for in special cases that will be discussed later.

This guide will not go into full detail of how to use Visual Basic Scripting however we will go over some fundamentals to show accomplishing common tasks and basic alterations using the scripting language. To learn more about VB Scripting you can access many free tutorials and documents online about the subject.



Introduction to Scripting

In this section I we will cover some of the basic understandings to be able to use VB Scripting for the reasons of BobCAD-CAM Post Processor modification.

Calling a Scripting Block

In the BobCAD-CAM post processor, the scripting blocks are available using Post Blocks 2001. thru 2099. Each scripting block has an assigned command variable, that when placed in another Post Block will make the posting engine call and execute the VB Script found in the scripting block.

Example:

Scripting Block 2001 is called using the command variable “program_block_1”
Scripting Block 2002 is called using the command variable “program_block_2”
Scripting Block 2015 is called using the command variable “program_block_15”


Variables – What they are and how they are used

A variable is a word that represents a storage location. Think of a variable as a box. You can put things in the box or the box can be empty. Before we can use a variable we must declare the variable. Declaring a variable basically creates the variable in the computer so it exists and can be used. The following code example illustrates declaring a variable and assigning it a value.

Dim examplevar
examplevar = 1000


Line 1 - In the example code is instructing the computer to reserve some memory space and assigning our name of “examplevar” to that space.

Line 2 - In the example code is assigning the value of 1000 into the memory space we have reserved with the name “examplevar”.


Variables can contain string values (also known as text or alphanumeric) characters, or numeric values. There is a big difference to a computer between numbers and letters. You cannot perform mathematical calculations using string/text  where you can perform mathematical calculations using numeric values. This is important when using variables because the computer will define a variable Type to the variable when it is created. A variable that is holding alphanumeric values or text is referred to as a String variable type. Some programming languages have very distinct definitions when creating variables where VB Scripting makes this very easy on the user.


Variable Types

When you create/declare a variable in the scripting blocks the system does not define a type to the variable until a value has been assigned to it. If we create the variable “testvar” and assign it a value of 1, the system will create a numeric variable.

Example creating a numeric variable:

dim testvar
testvar = 1


Example creating a string variable:

dim testvar
testvar = “this is my text”


Understanding the difference between variable types is important because when you get into creating formulas you cannot use the incorrect variable types or the VB Scripting engine will return errors.

Example: Bad Variable Formula

dim testvar1
dim testvar2
dim testresult

testvar1 = 14
testvar2 = “1”
testresult = testvar1 + testvar2


The previous code example will result in an error because you cannot add the number 14 to the text “1”. Understanding that there is a difference between the number 1 and the text value “1” is important.

 NOTE: There are special VB Scripting functions for converting variable type conversions. Search google for topics on “Variable Type Conversions” for more information on this subject.



Variables and Mathematical calculations

Performing mathematical calculations is one of the most common uses for variables when writing scripts. The previous example on Variable Types showed an example of completing a mathematical calculation. Using the value of one variable and working with a fixed value or another value to achieve the desired result is common practice. The following is a short list of the available operators in the VB Scripting language. Search Google for “VB Scripting Operators” for a complete listing.
Some Common Arithmetic Operators

Operator

Description

Example

+

Adds two operands

A + B will give 15

-

Subtracts second operand from the first

A - B will give -5

*

Multiply both operands

A * B will give 50

/

Divide numerator by denumerator

B / A will give 2


Example: Variable Formula

dim testvar1
dim testvar2
dim test result


testvar1 = 14
testvar2 = 5
test result = testvar1 + testvar2


In the previous example the end result is:  the variable test result equals the value of: 19

Before we continue on to additional functionality of the scripting inside the BobCAD-CAM posting engine we need to apply what we have learned so far.
We are going to use the following files for this lesson:

Machine File: BC_3x_Mill   

Post Processor: BC_3x_Mill.BCPst

Part File: Post Guide – Lesson 6.bbcd

 In this lesson we are going to go over some of the scripting basics. Using variables, performing basic mathematics, and using a message box so we can see the result.


Lesson 6 – Step 1:

Open the post processor in your editor and navigate to Post Block: 0.

In this post block we are going to add the call to our scripting block so the posting engine will call and execute the script. Add the command variable “program_block_1” to the beginning of the post block as shown below.

0. File header
    program_block_1


Lesson 6 – Step 2:

Now we are going to start creating the script itself. In the post processor, navigate to Post Block: 2001. In this step we are going to declare our variables. Enter the following into the post block.

2001. Program Block 1.
    Dim testvar1
    Dim testvar2
    Dim testresult

 

Lesson 6 – Step 3:

Now we are going to assign values to the variables we created. Add the following to the postblock after the previous lines.

    testvar1 = 5
    testvar2 = 8


Your Post Block 2001. should now look like the following:

2001. Program Block 1.
    Dim testvar1
    Dim testvar2
    Dim testresult

    testvar1 = 5
    testvar2 = 8


NOTE: By assigning values to a variable you are also defining the type of the variable. Once the type of the variable has been defined errors will result if you try to assign data of the incorrect type to the variable.


Lesson 6 – Step 4:

Now your script has 3 variables defined, and the variables testvar1 and testvar2 have values assigned. The testresult variable still does not have any value assigned to it because we are going to use it to receive the value of our calculation.

Our calculation is to simply add the value from the two variables together. The below code performs this addition and puts the result into the testresult variable.

2001. Program Block 1.
    Dim testvar1
    Dim testvar2
    Dim testresult

    testvar1 = 5
    testvar2 = 8
    
testresult = testvar1 + testvar2


Lesson 6 – Step 5:

We have created the code to perform the addition of the two variables however we have no way to actually see this result. For us to see this result we either need to output the value into our NC code output or we can display a Message Box. For script writing and debugging purposes it is very useful to know how to use a Message Box. The following code display a message box in VB Scripting and is supported in the BobCAD-CAM posting engine.

MsgBox(“This is my message”)

Add the code for the Message Box shown above into the scripting block of the post processor and save the post processor.

Your Post Block should now look like the following:

Dim testvar1
Dim testvar2
Dim testresult

testvar1 = 5
testvar2 = 8
    
testresult = testvar1 + testvar2
    
MsgBox("This is my message")


Lesson 6 – Step 6:

Save the post processor and generate the NC program. When the posting engine is executed you should receive a Message Dialog Box displayed with the text message “This is my message” as shown in the following image:

 

Clicking the OK button of the Message Box will close the dialog and continue the posting process.

 

Lesson 6 – Step 7:

Now that we have the Message Box being displayed we need to replace the message itself with the result of the calculation. The following code will replace the string of text for the message for the value of the variable testresult.

2001. Program Block 1.
    Dim testvar1
    Dim testvar2
    Dim testresult

    testvar1 = 5
    testvar2 = 8
    
     testresult = testvar1 + testvar2
    
    MsgBox(testresult)

Lesson 6 – Step 8:

Save and run the post processor. The system will display the Message Box again, this time the message that is displayed will be the value that is stored in the variable testresult.

 

 

If the message you receive is the value 13 as shown in the image above then the math has successfully been calculated, stored in the variable, and displayed in a Message Box using your VB Script in the BobCAD-CAM posting engine.