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:

Another VB question for VB guys.


Recommended Posts

How much of the NCI files can we access from the VB editor and implement in scripts to output data from there to our applications. Example might be take Data from 1016 1 in a 1050 NCI section to help seperate operations for operation processing for a set-up sheet. Another place might be the 20001 to output tool description. Tihs Idea takes me in a different direction that what I was thinking for my set-up sheet but might give me greater access to the parts and pieces I need t oget an effective set-up sheet. I relaize I will have to make an NCI files before using the script but since I would have 5 programs for operation then having 5 nci files for my script and then this would go along with seperating the operations in my set-up sheet as well as the tools so much easier that it might be then a none NCI method. This just seems to make sense to me use the NCI for a database call of information for the set-up sheet script that trying to un mangle the information via a Vb scipt that has to array it self then sub divide itself even futher to get the correct sorting. I hope that makes sense and there are thinigs avaiable to put me on the right path.

 

In advance Thanks for any asstaince or help in this.

Link to comment
Share on other sites

I don't see much in the way of NCI access. For a script that I wrote for a client of ours a few months back, I just used a run o' the mill (I should actually say 'router', ha biggrin.gif ) FileSystem object and parsed the NCI myself to make the changes that needed to be done. If there's a way using Mastercam VBS, that would be the safest, but you could always do it yourself if worse came to worse.

Link to comment
Share on other sites

Ok Maybe someone can give me an example of how to open up a spefic Excel file. Here is the path and what I did but can not get it to work. headscratch.gifheadscratch.gif

code:

OpenExcelFile(K:CNC-MILLSetupsheetsSETUP SHEET 2004.xls)  

 

I tired this and predefined the variable this way still did not work.

 

OpenExcelFile(xlfilelocation)

What am I doing wrong or Missing here.

Link to comment
Share on other sites

You need your path in quotes or stored in a variable. Either this:

 

code:

' Ron, note the quotes

If (OpenExcelFile("K:CNC-MILLSetupsheetsSETUP SHEET 2004.xls") = False) Then ShowString "Couldn't open the file!"

Or

 

code:

Dim strXLSPath   ' path to your Excel file

 

 

' store the path to the file in the variable

strXLSPath = "K:CNC-MILLSetupsheetsSETUP SHEET 2004.xls"

 

If (OpenExcelFile(strXLSPath) = False) Then ShowString "Damnit! Why can't I open this stupid file?!?"

HTH

Link to comment
Share on other sites

Well I can get that one to work but to get a made Excel file ot open via Vb is provingto be a challege. I used this along with the use XYZ.XLS exmaple in the VB folder as ways to map my efforts no good. I can make a new workbook with no problem but did not want to have to reconstruct the whole workbook everytime I do the script. I wanted ot take a made script and use it as a template then plug in the needed informtion that way. I do not even know if this is possible like from word to excel but everything I keep reading the 6 books I have tell me I am on the right track. So this present another problem is the ability of Vb in Mastercma still so much in infancy that I am hitting this brick wall becuase of that problem. I did get soem ghosting of Excel today and was hoping that was my problem did not help. I even took and renamed the file to SETUP.XLS and put in the VB folder on my computer verses the mapping method that was the K: drive way still can not get this file to open which should not matter. I used the make workbook active funtion and others commands I can find noone of them are working. I can make a script that takes excel stuff and use it Mastercam with very litle problem thanks to your exmaple in that thread and a couple exmaple I got off of here and one Mick did but just not this one. When I get into to work tommorow I will post up my script attmepts and maybe you can point me in the right direction.

Link to comment
Share on other sites

Here it is:

code:

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

'//

'// Author: Ronald D. Branch

'// Date: 09/02/2004 01:15 PM

'// File Name: New Mastercam Script

'//

'// Description: Mastercam VB Script

'//

'// Comments: Make a Template Excel Sheet Open From Mastercam

'//

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

 

 

'///////////////// My Constants /////////////////

Dim XlApp ' Name To Open Excel Application

'Dim excelworkbook

Dim strFileName

strFileName = "K:CNC-MILLSetupsheetsSETUP SHEET 2004.xls"

'Call Main()

'Sub Main()

'If (OpenExcelFile("K:CNC-MILLSetupsheetsSETUP SHEET 2004.xls") = False) Then ShowString "Couldn't find your File"

If (OpenExcelFile(strFileName) = False) Then ShowString "What are you thinking?"

'Set XlApp = CreateObject("Excel.Application")

 

' XlApp.Visible = True

' XlApp.Workbooks.Add

' XlApp.ActiveWorkbook.Cells(1,1).Formula = 500

' If (

'End Sub

The funny thing is that it will open up excel but not show it to the screen. I have to go to the task manger and shut it down. I had 12 open yesterday and then it dawned on me about ghosting so rebooted the computer did not help me to open and see the file. I feel if I can get over this I have a good grasp of questions and arrays thanks to you, John, Matt, and Mick. What am I doing wrong here.

 

Do I have to create the whole excel workbook or sheet as a complete script to get this to work?

Link to comment
Share on other sites

Ok, I see what you're doing. When you're calling this:

 

code:

Set XlApp = CreateObject("Excel.Application")

You're accessing Excel via its COM interfaces.

 

Using the OpenExcelFile() function, you're using Mastercam VBS to access Excel. For simplicity's sake, I wouldn't try to use both. Before you go further, you'd have to choose which path you want to take to access Excel; COM or Mastercam VBS. The COM method would be the most flexible at the price of a a higher learning curve. The Mastercam VBS method is easier to use but more restrictive (simple cell data entry and retrieval).

Link to comment
Share on other sites

The com Method was how John apporached his Word set-up sheet but since we have a 45 Field set-up sheet already made was hoping I could use the functions Via Mastercam but you confim my fear and my restrictions of it's use for this type of application. I can eaisly open the Excel program using the

code:

Dim XlApp

Set XlApp = CreateObject("Excel.Application")

XlApp.Visible = True

XlApp.Workbooks.Add


To open Excel and Open a workbook. Now I have to make the Com method do what I need would the FSO method work here as well ofr is the Create doing pretty much the same thing for the file creation of the Excel file.

 

One other thing is you noice the ' in the script that is where I was trying one metod over the other at different time I crashed Mastercma and my computer trying to combine the 2 methods.

 

If you do not mind me picking your brain. The Next and With call in VB I think I get Next in that it is like a structure call for one thing to happen then when that is complete then do the Next thing in the script verse jumping up and down the script to do certian functions of it.

 

The What is where I am a little puzzled. Is What a type of resolver for the script. Will it make the script assing the whole section doing the work to the variable defined by the what statement then when you use another command ot auto complete the documnet be it a Excel or Wrod or Access then it helps the definned varailbe act like a date buffer of some sorts?

Link to comment
Share on other sites

quote:

The Next and With call in VB I think I get Next in that it is like a structure call for one thing to happen then when that is complete then do the Next thing in the script verse jumping up and down the script to do certian functions of it.


The NEXT statement is primarily used as the "bookend" to a FOR loop:

 

code:

Dim i

 

' print twenty message boxes running from 0 to 20

For i = 0 To 20

ShowString i

Next ' <-- tells the loop to keep executing code within the FOR loop (until i = 20)

quote:

The What is where I am a little puzzled. Is What a type of resolver for the script. Will it make the script assing the whole section doing the work to the variable defined by the what statement then when you use another command ot auto complete the documnet be it a Excel or Wrod or Access then it helps the definned varailbe act like a date buffer of some sorts?


You mean WITH instead of WHAT, right? With the WITH statement (that's fun to say), you can eliminate some extra keystrokes. You only use WITH with object variables. Those are variables that contain an instance of a class, instead of a primitive variable like a string or integer. Here's a snippet of using WITH with a Mastercam Point object:

 

code:

' create a Mastercam Point object

Set objCurrPt = New McPt

 

' make sure the user picked a ppoint

If askPoint(objCurrPt) Then ' picked a point

' let's see the coords of our point

With objCurrPt

ShowString "X: " & .X & vbLf &_

"Y: " & .Y & vbLf &_

"Z: " & .Z

End With

Else ' didn't pick a point

' no point selected

Call ShowString("You didn't select a point.")

End If

 

' cleanup

Set objCurrPt = Nothing

Here's the same snippet not using the WITH statement:

 

code:

' create a Mastercam Point object

Set objCurrPt = New McPt

 

' make sure the user picked a ppoint

If askPoint(objCurrPt) Then ' picked a point

' let's see the coords of our point

ShowString "X: " & objCurrPt.X & vbLf &_

"Y: " & objCurrPt.Y & vbLf &_

"Z: " & objCurrPt.Z

Else ' didn't pick a point

' no point selected

Call ShowString("You didn't select a point.")

End If

 

' cleanup

Set objCurrPt = Nothing

So using WITH just eliminates the need to fully qualify the current object specified with the WITH statement (still fun to say). It's more of a personal preference thing.

Link to comment
Share on other sites

Well this is from John's Script for the Setup sheet is where I got the What from:

code:

'open word

Set wordDoc=CreateObject("Word.Application")

 

wordDoc.Documents.Add

 

With wordDoc

 

.Visible=True

.WindowState=wdWindowStateMaximize

.DisplayAlerts=wdAlertsNone

 

'Sets Margins

.Selection.PageSetup.LeftMargin = .InchesToPoints(0.5) 'start of margin setting macro

.Selection.PageSetup.RightMargin = .InchesToPoints(0.5)

.Selection.PageSetup.TopMargin = .InchesToPoints(0.5)

.ActiveWindow.ActivePane.VerticalPercentScrolled = 20

.Selection.PageSetup.BottomMargin = .InchesToPoints(0.5) 'end of margin setting macro

 

 

And Again here in his script:End With 'end output to word

 

 

If Not wordDoc Is Nothing Then Set wordDoc = Nothing

 

ShowString "Setup Sheet Finished"&vbNewLine&"file saved as "&saveworddoc

 

 

 

 

End Sub

 

' Writes tool list to word

Public Sub WriteToWord(strOutText)

Dim wordCounts

 

With wordDoc

.Selection.MoveDown wdLine, .ActiveDocument.Words.Count

.Selection.TypeParagraph

.Selection.TypeText strOutText

End With

 

End Sub

I am lookign at it like Sub and End Sud but not really sure if I am thinking right. His script is on the FTP in the VB folder as setupsheet.vbs .

 

Much thanks for your help and explaintions I am getting a good understanding here thanks to you.

Link to comment
Share on other sites

Right, total preference. His WriteToWord() sub could also be written as:

 

code:

' Writes tool list to word

Public Sub WriteToWord(strOutText)

Dim wordCounts

 

wordDoc.Selection.MoveDown wdLine, wordDoc.ActiveDocument.Words.Count

wordDoc.Selection.TypeParagraph

wordDoc.Selection.TypeText strOutText

End Sub

It would do the exact same thing.

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