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:

VBS: Reusing code


Recommended Posts

In languages like C++, Pascal, Java, etc., it's simple to reuse code. C/C++ has the #include directive, Pascal has the uses keyword and Java has the import keyword. If you have code that you find yourself copying and pasting into your scripts over and over, include 'em smile.gif I know of three ways to do it from VBS, but only 2 of them seem to work with Mastercam, and it's still a little bit tricky.

 

If you have code that doesn't use any Mastercam functions such as file I/O stuff or whatever, you can use WSC (Windows Script Component). WSCs are like C/C++ header files but require a specific syntax that'll look weird at first. You can use the WSC Wizard to create the skeleton of WSC files for you. Here's a little script, let's call it VBMsg.wsc that simply prints out a message using VB's MsgBox() function:

 

code:

<?xml version="1.0"?>

<component>

 

<?component error="true" debug="true"?>

<public>

<property name="message">

<get/>

<put/>

</property>

<method name="display">

</method>

</public>

 

<script language="VBScript">

<![CDATA[

Dim strMessage ' the global message

 

' Purpose: retrieve what's currently in the global message variable

' I: (nothing)

' O: the value of the global message variable

Function get_message()

get_message = message

End Function

 

' Purpose: sets the value of the global message variable

' I: new value for the global message

' O: (nothing)

Function put_message(strNewMessage)

strMessage = strNewMessage

End Function

 

' Purpose: displays the global message

' I: (nothing)

' O: (nothing)

Function display()

MsgBox strMessage

End Function

 

]]>

</script>

 

</component>

Now from our Mastercam script, say Test1.vbs, we can do the following:

 

code:

' -- Start Script

Call Main()

 

' Purpose: the main sub routine

Sub Main()

Dim objTestInclude ' object from our WSC file

 

 

' get a reference to the WSC

Set objTestInclude = GetObject("script:C:FullPathToVBMsg.wsc")

 

' set the message var

objTestInclude.message = "Yo, from me!"

 

' run the function that displays the message var

objTestInclude.display

 

' clean up

Set objTestInclude = Nothing

End Sub

I haven't found a way to run Mastercam VBS functions from WSC yet. In that case, you can create your own include function that uses the ExecuteGlobal statement. Let's say I have a file called BullinesArc.txt that contains code to simply create an arc at origin with a diameter of 4:

 

code:

' Purpose: creates an arc at origin with a diamter of 4

' I: (nothing)

Sub BullinesCreateArc()

Dim nRet ' return value of CreateArc()

Dim arcTheArc ' the new arc

 

' get a reference to a Mastercam arc object

Set arcTheArc = New McAr

 

' set the parameters of th earc

arcTheArc.X = 0

arcTheArc.Y = 0

arcTheArc.Z = 0

arcTheArc.R = 2

arcTheArc.SA = 0

arcTheArc.SW = 360

 

' create the new arc

nRet = CreateArc(arcTheArc, mcCONSTRUCTION_COLOR_CURRENT, mcCONSTRUCTION_LEVEL_CURRENT)

 

' clean up

Set arcTheArc = Nothing

End Sub

Then call our Include() sub routine as soon as we enter our Mastercam script, Test2.vbs so that we can use the function in BullinesArc.txt:

 

code:

' -- Start Script

Call Main()

 

 

' Purpose: Include code from an external file

' I: full path to the file to be included

' O: (nothing)

Sub Include(FilePath)

Const ForReading = 1, TristateUseDefault = -2, DoNotCreateFile = False ' just like in VB

Dim objFSO ' FileSystem object

Dim objCurrFile ' File object

 

' get a reference to the objFSO

Set objFSO = CreateObject("Scripting.FileSystemObject")

 

If (objFSO.FileExists(FilePath)) Then ' the include file is there

If (objFSO.GetFile(FilePath).Size > 0) Then ' the include file actually has something in it

' get a reference to the file object using the include file

Set objCurrFile = objFSO.OpenTextFile(FilePath, ForReading, False, TristateUseDefault)

 

' allow our imported code to run

ExecuteGlobal(objCurrFile.ReadAll): objCurrFile.Close

End If

End If

 

' clean up

Set objFSO = Nothing

Set objCurrFile = Nothing

End Sub

 

 

' Purpose: the main function

Sub Main()

' include the file

Include("C:FullPathToBullinesArc.txt")

 

' call BullinesCreateArc() from the included file

Call BullinesCreateArc()

End Sub

Hopefully, this'll come in handy biggrin.gif

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...