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 to Post to Multiple Machines


Recommended Posts

Good Morning All,

 

When I make a change to a program I need to re-post it to up to 8 different machine types depending on the product line.

 

The typical routine in this shop is to got to the Machine Definition Manager, Pick the required Post-Processor from the drop-down list, click the green check, say yes to the "Replace group machine" prompt, click Post Selected Operations, Name the files (Naming convention is the same depending on machine), and posting.

 

Is there a good VB Script somewhere that I can pick apart and Frankenstein to make this process a little less tedious? If not, can someone point me in the direction of a good example line or two of code for picking the post processor and posting?

 

Thanks!

James

Link to comment
Share on other sites

Below is a very old script from V9 that would post selected operations to two post processors.  There used to be a "runvbs.dll" chook to launch vb scripts but I don't see it in there so I'm not sure how to launch a vb script in 2017.

 

 

 

'////////////////////////////////////////////////////////////////////////////////
'//
'//        Author:   Roger Peterson
'//          Date:   10/15/04
'//     File Name:   post_selected.vbs
'//
'//   Description:   Post out all selected operation.  Scan file to determine which operations are selected,
'//                  ghost any operation not selected, post to selected post processors, un-ghost operations that were
'//                  previously ghosted.  Prompts the user to select the directory for the nc file and again for the
'//                  directory for the doc file.  Constant spost2 holds the path and name of the second post to be used.
'//                  If you desire to use a .set file you must rename it so it has a .pst extension.
'//
'//      Comments:   Requires March Maintenance release
'//
'////////////////////////////////////////////////////////////////////////////////

' -- Start Script
Dim sprompt

Call Main()


' ////////////////////
' Sub Declaration
' ////////////////////
Sub Main()

Dim posting(999)
Dim selected(999)
Dim intOpCount, idx, intCount
Const spost2    = "c:\mcam9\mill\posts\mill2.pst"
Const sdoc      = ".doc"
Const snc       = ".nc"
Const snci      = ".nci"
Const spst      = ".pst"
Dim spathnci
Dim snamenci
Dim spathnamenci
Dim namepos
Dim ncipos
Dim sname
Dim spathnc
Dim strPath
Dim spathdoc
Dim contposting
Dim spostoriginal
Dim spost1
Dim spathpst

'--resume the script on errors
On Error Resume Next

' -- Determine how many operations are in the active file?              
intOpCount = GetOperationCount(vbNullString)

' -- If the file does not contain any operations, exit the script
If intOpCount = 0 Then ShowString "No operations in the current mc9 file": Exit Sub  

' -- Initialize
intCount = 0

'------------------------------------------------------------------------------------
' which and how many operations are selected
'------------------------------------------------------------------------------------
For idx = 1 To intOpCount

    If GetOperationSelectedFromID(vbNullString, idx) Then
       selected(idx) = 1
       intCount = intCount + 1
     Else
      selected(idx) = 0
    End If     

Next     

'------------------------------------------------------------------------------------
' which and how many operations are NOT ghosted
'------------------------------------------------------------------------------------
For idx = 1 To intOpCount

    If GetOperationPostingFromID(vbNullString, idx) Then
       posting(idx) = 1
     Else
       posting(idx) = 0
    End If     

Next        

'------------------------------------------------------------------------------------
' -- If no operations are selected exit post
'------------------------------------------------------------------------------------
If intCount  = 0 Then
   ShowString "No operations are selected, You will be returned to Mastercam  "
    Exit Sub
'Else
'   ShowString intCount & " of " & intopcount & " operations are currently selected"
End If


'------------------------------------------------------------------------------------
' -- Turn posting off on all ops not selected
'------------------------------------------------------------------------------------
For idx = 1 To intOpCount

 If Not GetOperationSelectedFromID(vbNullString, idx) Then
       'ShowString  " operation # " & idx & " not selected"
    Call SetOperationPostingFromID(vbNullString, idx, True)
 End If

Next  




'---------------------------------------------------------------------------------------------
'Get NCI file name information
'---------------------------------------------------------------------------------------------
spathnci = GetPathFromExtension("nci")
spathnamenci = GetNciNameFromOperationID(vbNullString, 2)
namepos = Len(spathnci)' Determine the length of the nci path - not included is the name of the nci file
namepos = namepos + 1 ' Add one so \ is not included in the name
snamenci = Mid(spathnamenci, namepos) ' snamenci includes just the name of the nci file and not the directory path
ncipos = InStr(1, snamenci, snci, 1)
ncipos = ncipos - 1 ' Subtract one so when removing the extension the . is also removed
If ncipos Then
 sname = Left(snamenci, ncipos) ' This removes .nci from the name
End If


'---------------------------------------------------------------------------------------------
'get post to be used so it can be reset at the end of script
'---------------------------------------------------------------------------------------------
' -- Get the currently set post processor
spostoriginal = GetPostName
spathpst = GetPathFromExtension("pst")
spathpst = spathpst & spostoriginal & spst


'---------------------------------------------------------------------------------------------
'-- Tell the user the post name, the name of the nci file, and how many operations have been selected
'-- and ask if they would like to continue or go back and change the settings
'---------------------------------------------------------------------------------------------
contposting = askYesNo("Postname:  " & spostoriginal & "   " & vbCrLf & "NC and DOC filenames:  " & sname & vbCrLf & intCount & " of " & intopcount & " operations are currently selected" & vbCrLf & vbCrLf & "Select Yes To continue posting, No To return To Mastercam     ")


If contposting = 0 Then
 For idx = 1 To intOpCount        
    Call SetOperationPostingFromID (vbNullString, idx, posting(idx))
 Next        
 Exit Sub
End If


'---------------------------------------------------------------------------------------------
'get directories for nc and doc files
'---------------------------------------------------------------------------------------------

' -- Prompt user for a folder to post nc file-------------------------------------------------
strpath = vbNullString
sprompt = "Select folder for nc file:"

While strPath = vbNullString
   ' -- Invalid path....
   strPath = BrowseForFolder
Wend

strpath = AddBackSlash(strpath)
spathnc = strpath



' -- Prompt user for a folder to post doc file------------------------------------------------
strpath = vbNullString
sprompt = "Select folder for doc file:"

While strPath = vbNullString
   ' -- Invalid path....
   strPath = BrowseForFolder
Wend

strpath = AddBackSlash(strpath)
spathdoc = strpath

'---------------------------------------------------------------------------------------------
'post .pst and .set files to their specified directories
'---------------------------------------------------------------------------------------------
'---post to the current nci file name, using post #1------------------------------------------

Call RunPostAll(spathnc, True)


'---post to the current nci file name, using post #2------------------------------------------
Call SetPostName(vbNullString)
Call SetPostName(spost2)
Call RunPostAll(spathdoc, True)

'---------------------------------------------------------------------------------------------
' -- Set posting (Operations ghosted) back To original settings
'---------------------------------------------------------------------------------------------
For idx = 1 To intOpCount        
    Call SetOperationPostingFromID (vbNullString, idx, posting(idx))
Next        

Call SetPostName(vbNullString)
Call SetPostName(spathpst)
Call SetPostName(vbNullString)
Call SetPostName(spostoriginal)
    
End Sub








' ////////////////////
' Function Declaration
' ////////////////////

Function BrowseForFolder()

    On Error Resume Next

Const WINDOW_HANDLE = 0
Const NO_OPTIONS = 0
    Dim objShell, objFolder, objFolderItem

    Set objShell = CreateObject("Shell.Application")

    Set objFolder = objShell.BrowseForFolder(WINDOW_HANDLE, sprompt, NO_OPTIONS, "Desktop")

    Set objFolderItem = objFolder.Self

    If Err.Number <> 0 Then

       BrowseForFolder = vbNullString
       Err.Clear
       Exit Function
    End If

    BrowseForFolder = objFolderItem.Path
End Function


' ////////////////////
' Function Declaration
' ////////////////////

Function AddBackSlash(sPath)

If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
AddBackSlash = sPath

End Function
 

Link to comment
Share on other sites
  • 2 weeks later...

This is a very old vb script.  I was browsing through the code and most of the logic is to ghost non-selected operations and then un-ghost them after posting has completed.  This was due to the fact there was no "post selected operations" functionality, but only "post all operations" functionality.  Because this was done many years ago you may want to check to see if there is a "post selected", it would make the vb script much simpler.

 

good luck!

Link to comment
Share on other sites

Good Afternoon!

 

So, I've been playing around with this for a few hours now and have gotten it to pretty much do what I want. I have run into a hangup though where it is overwriting the files.

 

When I use the example above they do post two different files but overwrite eachother.

 

Is there a way to change the File Name Before posting of the to-be created TAP file?

Something like...

 

Call SetTapFileName("Machine1.TAP")

Call SetPostName("o:\post location\post 1's file name.pst")
Call RunPostAll("\\DNC Path\\Folder Full of TAP Files\", False)
 
Call SetTapFileName("Machine2.TAP")
Call SetPostName("o:\post location\post 2's file name.pst")
Call RunPostAll("\\DNC Path\\Folder Full of TAP Files\", False)
 
And for bonus points, is there a way to auto select "No" when the Operations Manager prompts saying "Multiple tools using the same tool number were found! Generate a tool change in the NCI?" in this process as well?
 
Thanks again guys!
James
Link to comment
Share on other sites

you would have to rename the nci via code before posting the second machine.  or you could post the first one, rename the file, post the second one, rename the file, etc...  When I originally set this up each machine posted to a separate folder so the filename wasn't an issue.  I do not know of a way to ignore the warning message.

 

 

 

from the help file:

 

// Set the nci file name of  the supplied id of the operation

Bool SetNciNameFromOperationID (

            Integer (Input: the operation ID number)

            String  ( path and name to the nci file )

)

 

 

 

so you would need to set the nci for each op individually but the script is already checking each op to see if it selected so you could change it then.  But, if you wanted to set them back to what they originally were you would need to keep track of the name in a string array.

Link to comment
Share on other sites

Ok, last time I bother you guys today I promise!  :)

 

I've re-written so I fully understand the example above and it works like a charm until I rename the NCI.

 

When I do the highlighted step below "Change File Names File Names To Match Machine Post" it seems to work fine when I end it after. It renames all of the File Names appropriately when I right click on an op, Edit Selected Ops, Change NC File Name it appears to be changed as it should be. However, it will not post as if it has unselected all of the operations.

 

When I comment this re-naming bit out it posts just fine but over writes my posts like I was having troubles with before.

Is there something I need to do to apply these changes after the "Call SetNciNameFromOperationID" line? Or do I need to re-select the operations again somehow after?

 
'============================================================================
'{--- Loop and Post for Each Machine Using Data Gathered ---}
'============================================================================
 
 Dim PostLoopCounter
 For PostLoopCounter = 0 to PostCount - 1
 
  '============================================================================
  '{--- Change File Names File Names To Match Machine Post ---}
  '============================================================================
  ' For LoopCounter = 0 To NoOfOps
  '   If GetOperationSelectedFromID(vbNullString, LoopCounter) Then
  '    Call SetNciNameFromOperationID(LoopCounter, (MachName(PostLoopCounter) & DefinedNCFile))
  '   End If
  ' Next
  '============================================================================
  '{--- Post Program ---}
  '============================================================================
   Call SetPostName(vbNullString)
   Call SetPostName(Posts(PostLoopCounter))
   Call RunPostAll(TapeLoc, False)
 
 
    ShowString ("Number of Ops : " & NoOfOps & VBCRLF & _
  "Number of Posts : " & PostCount & VBCRLF & _
  "Selected : " & NoOfSelectedOps & "/" & NoOfOps & VBCRLF & _
   "Defined File : " & DefinedNCFile & VBCRLF & _
   "Posted File : " & (MachName(PostLoopCounter) & DefinedNCFile) & VBCRLF & _
   "Using Post : " & Posts(PostLoopCounter) & VBCRLF & _
   "Post To : " & TapeLoc)
 Next
Link to comment
Share on other sites

 

Good Afternoon!

 

And for bonus points, is there a way to auto select "No" when the Operations Manager prompts saying "Multiple tools using the same tool number were found! Generate a tool change in the NCI?" in this process as well?
 
Thanks again guys!
James

 

 

That is a programming issue and you want that to come up. Someone may have done it on purpose, but most times you would want that type of behavior done and would want the messgae to come up some the programmer is aware of their possible mistake.

Link to comment
Share on other sites

Hi there,

 

That is a great idea! I didn't even think about renaming them!

I did a search and in VBS it is "Fso.MoveFile oldfile, newfile" but I get an error that says...

"

Microsoft VBScript runtime error

Variable is undefined: 'Fso'

IActiveScriptSite::OnScriptError()

Line:

"

 

Do I need to declare or reference something before I use FSO lines in mastercam scripts?

Here is the modified code if you'd like to see it...

 

 

 
'============================================================================
'{--- Loop and Post for Each Machine Using Data Gathered ---}
'============================================================================
 
 Dim PostLoopCounter
 For PostLoopCounter = 0 to PostCount - 1
 
  '============================================================================
  '{--- Change File Names File Names To Match Machine Post ---}
  '============================================================================
  ' For LoopCounter = 0 To NoOfOps
  '   If GetOperationSelectedFromID(vbNullString, LoopCounter) Then
  '    Call SetNciNameFromOperationID(LoopCounter, (MachName(PostLoopCounter) & DefinedNCFile))
  '   End If
  ' Next
  '============================================================================
  '{--- Post Program ---}
  '============================================================================
   Call SetPostName(vbNullString)
   Call SetPostName(Posts(PostLoopCounter))
   Call RunPostAll(TapeLoc, False)
   Fso.MoveFile (TapeLoc & DefinedNCFile), (TapeLoc & (MachName(PostLoopCounter) & DefinedNCFile))
 
 
    ShowString ("Number of Ops : " & NoOfOps & VBCRLF & _
  "Number of Posts : " & PostCount & VBCRLF & _
  "Selected : " & NoOfSelectedOps & "/" & NoOfOps & VBCRLF & _
  "Defined File : " & DefinedNCFile & VBCRLF & _
  "Posted File : " & (MachName(PostLoopCounter) & DefinedNCFile) & VBCRLF & _
"Using Post : " & Posts(PostLoopCounter) & VBCRLF & _
"Post To : " & TapeLoc)
 Next
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...