Jump to content

Welcome to eMastercam

Register now to participate in the forums, access the download area, buy Mastercam training materials, post processors and more. This message will be removed once you have signed in.

Use your display name or email address to sign in:

VB script advice and examples request


Recommended Posts

I am trying streamline the repetetive task of programming common holemaking operations for moldbases using VB.

 

I would greatly appreciate any advice or examples put forth as the programming language has evolved quite a bit.

 

I took some of the stock examples and modified them to serve as modules but have no idea how to inteligently link and call them from seperate files.

 

The data comes as 2D detail files. Features are mostly drilling cycles, tapped holes, cap screw clearance and counterbore, stepped reamed ejector pin holes, and leader pins the larger of wich are helicaly milled. Thanks Guys

 

[email protected] web page

Link to comment
Share on other sites

Hi,

 

Here is an example of how to include multiple scripts in a main script and have the main script access functionality in the include scripts.

 

First, create two scripts, one called ShowMessage1.vbs and another called ShowMessage2.vbs. In each script create a new function as shown below and save the files to your mcamvb folder.

 

ShowMessage1.vbs

code:

Function ShowMessage1

 

ShowString "Called from ShowMessage1.vbs"

 

End Function

ShowMessage2.vbs

code:

Function ShowMessage2

 

ShowString "Called from ShowMessage2.vbs"

 

End Function

Create the main script to use the two above scripts, make sure you save this script to the same folder as the other two scripts.

 

code:

Include "ShowMessage1.vbs"

Include "ShowMessage2.vbs"

 

 

' -- Start Script

Call Main()

 

 

' ////////////////////

' Sub Declaration

' ////////////////////

Sub Main()

 

Call ShowMessage1

 

Call ShowMessage2

 

 

End Sub

 

' '//////////////////////////

' '// Sub Declaration

' '//////////////////////////

Sub Include(sNameScript)

 

Dim FSO

Dim fsoInclude

 

Set FSO = CreateObject("Scripting.FileSystemObject")

 

sNameScript = GetPathOfThisScript & sNameScript

 

Set fsoInclude = FSO.OpenTextFile(sNameScript)

 

With fsoInclude

 

ExecuteGlobal .ReadAll()

 

.Close

 

End With

 

' -- Clean up

Set fsoInclude = Nothing

Set FSO = Nothing

 

End Sub

When you run the main script it will load the two "helper" scripts, all functionality in those scripts are now available to the main script.

Link to comment
Share on other sites

Thanks for the response guys.

 

Despite pretty carefull prep work on program flow, variables, coding, etc I have been challenged trying code and debug my scripts.

 

After trying to code and add several subs in one program it became clear that understanding flow and debugging quickly becomes more difficult. Not being the sharpest tool in the shed, I tend to get confused trying to connect the dots between VB scripting and MC VBsSripting.

 

My thought is that a master program, accessing seperate modules using global/main and local variables makes better sense than a big confusing heap of code.

 

Pretty much I have been using Micks examples from the VB directory, stripping them down by commenting unnessary code line at a time before deleting it, then modifying and testing each section of code one variable at a time, and making much better progress.

 

This Include "xxx.vbs" looks like the ticket for calling subs in files. Also the advice "You can put the Script name in the shortcut and follow it with /exit"

 

Clean code produces clean results at least in theory. Any advice or examples on passing variables?

 

I have been trying to study your ATP scripts but they are way more advanced than the examples and out of my league.

 

Thank you very much for the help

 

"A man has to know his limitations" Clint Eastwood

Link to comment
Share on other sites

quote:

Any advice or examples on passing variables?


Passing variables into subs and functions can be done in two ways; by value and by reference. The by value approach copies the variable that you're passing into a local variable in the sub or function. The by reference approach works directly with the variable kinda sorta like pointers in C/C++ (but not really wink.gif ). Here's an example:

 

code:

Call Main()

 

Sub Main()

Dim iXVal

Dim iYVal

 

 

iXVal = 5

iYVal = 7

 

Call ByRefExample(iXVal)

 

ShowString iXVal ' should show the value 6

 

Call ByValExample(iYVal)

 

ShowString iYVal ' should show the value 7, not 14

End Sub

 

' This Sub will directly modify x

Sub ByRefExample(ByRef x)

If (IsNumeric(x)) Then x = x + 1

End Sub

 

 

' This Sub will not modify y. Instead, it will

' make a local copy of it within the sub.

Sub ByValExample(ByVal y)

If (IsNumeric(y)) Then y = y * 2

End Sub

The ByValExample() sub could also be written as:

 

code:

' This Sub will not modify y.  Instead, it will

' make a local copy of it within the sub.

Sub ByValExample(y)

If (IsNumeric(y)) Then y = y * 2

End Sub

This is because ByVal is the default method for passing variables into subs and functions.

 

You can pass more than one variable into a sub/function by separating them with commas:

 

code:

Call Main()

 

Sub Main()

Dim strName

Dim iAge

 

 

strName = "Chris"

iAge = 28

 

Call ShowDetails(strName, iAge)

End Sub

 

Sub ShowDetails(name, age)

ShowString "My name is " & name & " and I'm " & age & " years old."

End Sub

And the main difference between a Sub and Function is that Functions return a value, where subs do not.

 

code:

Call Main()

 

Sub Main()

Dim iFirst

Dim iSecond

Dim iSum

 

 

iFirst = 5

iSecond = 3

 

iSum = AddEmUp(iFirst, iSecond)

 

ShowString iSum ' should show 8

End Sub

 

Function AddEmUp(a,
B)

AddEmUp = a + b

End Function

HTH

Link to comment
Share on other sites

"After trying to code and add several subs in one program it became clear that understanding flow and debugging quickly becomes more difficult. Not being the sharpest tool in the shed, I tend to get confused trying to connect the dots between VB scripting and MC VBsSripting.

 

My thought is that a master program, accessing seperate modules using global/main and local variables makes better sense than a big confusing heap of code. "

 

Big confusing heaps of code are a good thing to avoid, not least because you'll have no idea what they're doing. The next guy to look at it will have no idea what they're doing either.

You're going to have a lot more luck by isolating functionality in subroutines (functions), and then calling them repeatedly. A good rule of thumb is that each one should do only one thing, and try to think of it as a verb. Names like "GetNciFile" or "DrillPoint" will get you out of a lot of trouble early on.

 

I'd avoid the global variables. +1 Bullines on his explanation of passing variables. Anything that can be passed instead of being global should be.

 

Best of luck with your task. I'm sure the forum will be more than happy to help you along.

 

Jon from CNC Software

Link to comment
Share on other sites

quote:

I picked it up early on in my CS courses, and have been an avid supporter ever since.


Me too; I had profs that couldn't stress good naming enough. But there's some other good nuggets in there, too, like PDL (Program Design Language) which is basically using comments to say what a given block of code will do before actually coding it and then leaving it once coding is done.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.

Join us!

eMastercam - your online source for all things Mastercam.

Together, we are the strongest Mastercam community on the web with over 56,000 members, and our online store offers a wide selection of training materials for all applications and skill levels.

Follow us

×
×
  • Create New...