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:

Importing SLDPRT into MCX-5


Recommended Posts

Hello Forum,

 

My name is Matt and this is my first post. I work in kitchener and have a small vbs program that I wanted to create since we do this string of instructions over and over.

 

Pretty much we want to be able to have a hotkey that will import a SLDPRT file into mastercam and resave it in another directory with the same name so we can edit it. We dont want to edit the original file, just import and resave. I have no real VB experience since college and am unsure how to do this. Any pointers are appreciated.

 

Thank-you Forum.

Link to comment
Share on other sites

Hello Forum,

 

My name is Matt and this is my first post. I work in kitchener and have a small vbs program that I wanted to create since we do this string of instructions over and over.

 

Pretty much we want to be able to have a hotkey that will import a SLDPRT file into mastercam and resave it in another directory with the same name so we can edit it. We dont want to edit the original file, just import and resave. I have no real VB experience since college and am unsure how to do this. Any pointers are appreciated.

 

Thank-you Forum.

 

 

Also I figure this is really close to what I wanted to do.

 

' *****************************************************************************

' * Script Name: Batch Import DXF Files.vbs

' * Written By: Mick George [email protected]

' * Date: August 15 2002

' * Dependency: Microsoft Scripting Runtime (scrrun.dll)

' * Microsoft Windows Scripting Host (WScript.exe)

' *

' * Function: Batch Import dxf files and save as native mastercam drawings

' * Select folder containing dxf files and select a folder to save drawings to

' *

' * Updated: Mick George added LCase() call to correctly identify dxf file extension.

' * January 21 2003

' *

' * Updated: Mick George added call to PROGRESS class

' *****************************************************************************

'

 

Include "CProgress.vbs"

 

 

Public Const DEF_CENTERED = " "

 

 

' -- Kick off script --

'

Call Main

'

' --------------------

 

 

 

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

' Sub Declaration

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

Sub main()

 

 

Dim FSO ' -- File System Object

Dim fsoDXFFolder ' -- Folder containing DXF Files

Dim fsoSaveToFolder ' -- Folder to save *.MC9 files

Dim fsoFiles ' -- Files located in selected folder

Dim fsoDXF ' -- A dxf File

Dim strDXFPath ' -- Return value of selected folder containing dxf files

Dim strSavePath ' -- Return value of selected folder to save files to

Dim blnOkToGo ' -- Flag

Dim strPrompt

Dim strMC ' -- Stores a Mastercam filename from a dxf filename

'

' ======================================

Dim CProgressBar ' -- Our progressbox

' ======================================

 

 

 

' -- Prompt user for a folder containing dxf files

strDXFPath = BrowseForFolder("Choose DXF Files Folder")

 

' -- Make sure a valid folder was selected

If IsNull(strDXFPath) Then

ShowString "Invalid Folder Selection"

Exit Sub

Else

' -- Prompt user for a save folder

strSavePath = BrowseForFolder("Save Drawings")

 

' -- Make sure a valid folder was selected

If IsNull(strSavePath) Then

ShowString "Invalid Folder Selection"

Exit Sub

End If

 

' -- Create an instance of a File System Object

Set FSO = CreateObject("Scripting.FileSystemObject")

 

' -- Get the selected folder

Set fsoDXFFolder = FSO.GetFolder(strDXFPath)

 

' -- Get all files in this folder

Set fsoFiles = fsoDXFFolder.Files

 

' -- Make sure we have some files

If fsoFiles.Count = 0 Then

ShowString "There are no files in the selected folder"

Exit Sub

Else

' -- Initialize to be sure

blnOkToGo = False

 

 

' -- Make sure there is at least one dxf file

For Each fsoDXF In fsoFiles

' -- dxf file?

If Right(LCase(fsoDXF.Name), 3) = "dxf" Then

' -- ok to go

blnOkToGo = True

Exit For

End If

Next

 

 

If blnOkToGo Then

 

Dim blnRet

 

strPrompt = fsoFiles.Count & " dxf files found" & DEF_CENTERED

strPrompt = strPrompt & vbCrLf

strPrompt = strPrompt & "Do you wish to continue?" & DEF_CENTERED

 

blnRet = askYesNo(strPrompt)

 

If blnRet = mcMSG_NO Then Exit Sub

 

' -- Check for Root

Call AddBackSlash(strSavePath)

 

' -- Erase the Mastercam prompt area at the bottom of the Mastercam window and Menus

Call ClearMenuAndPrompts

 

' -- Clear screen

Call ResetAll

 

' ======================================

Set CProgressBar = New PROGRESS

'

CProgressBar.Show 500, 100, 550, 550

' ======================================

 

' -- Convert each dxf file

For Each fsoDXF In fsoFiles

' -- dxf file?

If Right(LCase(fsoDXF.Name), 3) = "dxf" Then

' ======================================================

CProgressBar.Caption "Converting " & fsoDXF.Name & "..."

' ======================================================

 

' -- Display current file in prompt area

Call WriteString("Converting " & fsoDXF.Name & "...")

 

' -- Import this DXF file into the current Mastercam session

Call ReadDXF(fsoDXF.Path)

 

' -- Fit screen

Call RepaintScreen(True)

 

' -- Build Mastercam filename

strMC = Mid(fsoDXF.Name, 1, InStrRev(fsoDXF.Name, "."))

 

' -- Build fullpath to folder

strMC = strSavePath & strMC & "MC9"

 

' -- Save the current Mastercam file

If Not SaveMCAs(strMC, True) Then

If Not askYesNo("Unable to save file '" & strMC & "' Do you wish to continue?" & DEF_CENTERED) Then

Exit For

End If

End If

 

' -- Clear screen

Call ResetAll

 

End If

Next

 

 

Call ResetAll

 

' =====================================

CProgressBar.Caption "Process Complete"

'

CProgressBar.Shutdown

' =====================================

 

' -- If blnOkToGo

Else

ShowString "There are no dxf files in the selected folder"

End If

 

End If

 

 

 

End If

 

Set CProgressBar = Nothing

 

End Sub

 

 

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

' Function Declaration

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

Function AddBackSlash(sPath)

 

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

 

AddBackSlash = sPath

 

End Function

 

 

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

' Function Declaration

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

Function BrowseForFolder(strPrompt)

 

On Error Resume Next

 

Dim objShell, objFolder, intColonPos, objWshShell

 

Set objWshShell = CreateObject("WScript.Shell")

 

Set objShell = CreateObject("Shell.Application")

 

 

Set objFolder = objShell.BrowseForFolder(&H0&, strPrompt, &H1&)

 

BrowseForFolder = objFolder.ParentFolder.ParseName(objFolder.Title).Path

 

If Err.Number <> 0 Then

 

BrowseForFolder = Null 'will be null of no special case applies

 

If objFolder.Title = "Desktop" Then

BrowseForFolder = objWshShell.SpecialFolders("Desktop")

End If

 

intColonPos = InStr(objFolder.Title, ":")

 

If intColonPos > 0 Then

BrowseForFolder = Mid(objFolder.Title, intColonPos - 1, 2) & "\"

End If

 

End If

End Function

 

 

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

' Function Declaration

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

Sub ResetAll()

 

 

' -- Clear screen

Call NewMC(False)

 

' -- Fit screen

Call RepaintScreen(True)

 

' -- Clear prompt area

Call ClearPromptLines

 

 

 

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

Link to comment
Share on other sites

Going through this reminds me why I dislike coding in VB Script - YUCK! :(

 

The attached VB Script [ Import SLDPRT Files.vbs ]

is a revised version oif the Batch DXF inport.

 

It prompts the user for a folder containing .SDLPRT files

and then for an output folder to place the resulting .MCX-5 files into.

And process ALL of the .SLDPRT files in the source folder into .MCX-5 files that are placed in the destination folder.

This Browse for Folder (twice!) is OK for dong batch conversion, but for a quick "do this file".

*NOTE* This sctript does not prompt the user to save any currently loaded data when it starts up!

 

Of course VB Scripts cannot be "directly" launched via a toolbar icon and/or keystroke.

To accomplish that, you need to the the ScripLinker AddOn

Import SLDPRT Files (VBS).zip

Link to comment
Share on other sites

Going through this reminds me why I dislike coding in VB Script - YUCK! :(

 

The attached VB Script [ Import SLDPRT Files.vbs ]

is a revised version oif the Batch DXF inport.

 

It prompts the user for a folder containing .SDLPRT files

and then for an output folder to place the resulting .MCX-5 files into.

And process ALL of the .SLDPRT files in the source folder into .MCX-5 files that are placed in the destination folder.

This Browse for Folder (twice!) is OK for dong batch conversion, but for a quick "do this file".

*NOTE* This sctript does not prompt the user to save any currently loaded data when it starts up!

 

Of course VB Scripts cannot be "directly" launched via a toolbar icon and/or keystroke.

To accomplish that, you need to the the ScripLinker AddOn

 

 

Thank you so much Roger. That is a great start. There are a few more things I want to add so I will start on that.

 

I did notice that this opens the files, and not imports it. Is there a way to import instead?

Link to comment
Share on other sites

Right now we use File Merge/Pattern to import a SLDPRT file into a "template" file that has some standard tools and operations defined. When we do that we need to "Save As" and pick up the filename of the imported file because the imported file maintains the name of the file it was imported to, in this case "template.mcx-5". The machine group name and template name are then changed from "template-1" and "template-op10" respectively to match the name of the part file.

 

We are just looking to automate this process to save repeating this process for each part we program and to standardize to make sure the the mcx-5 files are going into a certain subdirectory of the directory where the SLDPRT file is.

Link to comment
Share on other sites

AH!

 

Your "import" is really File-Pattern/Merge

This is a very important distinction!

 

The ImportFile command you're seeing the the VB Scripting is...

 

// Import ANY file type (IGES, STEP, DWG, DXF, Pro/E, Inventor, etc)

A File-Open command that allows for "opening" any type of supported file.

 

The File-Merge support in VB Script is the MergeMC command.

Note the 'MC' in the name. It will not merge "non-native" Mastercam file types.

 

' -- Kick off script --
'
Call Main
'
' --------------------

Sub main()

Dim strSLDPRTPath 	' -- The path portion of the input file
Dim strSLDPRTName 	' -- The filename portion of the input file
Dim strFullName		' -- The full path\filename of the input file

strSLDPRTPath = "C:\temp\SolidWorks Test Files\inch\"
strSLDPRTName = "Island Facing_INCH.SLDPRT"
strFullName = strSLDPRTPath & strSLDPRTName

' -- Import (load) this SLDPRT file into the current Mastercam session
Call ImportFile(strFullName) ' <- This will work with a .SLDPRT file type

' --  Merge a Mastercam file
Call MergeMC(strFullName) ' <- This will NOT work with a .SLDPRT file type

Call RepaintScreen(True)

End Sub

Link to comment
Share on other sites

Subject: Merge SLDPRT file into the active Mastercam database.

 

MergeSLDPRT2MCX AddOn

 

*The AddOn and the full C# source code is included in the attached MergeSLDPRT2MCX.zip

You can build (or alter and rebuild) the project using Microsoft Visual Studio.

You can even use the free Express version.

 

You may think that VB Script is easy (or easier) than C# or VB.NET

Well... maybe is you do a lot of VB Scripts.

But compare this NETHook code to the VB Scripting in the earlier posts.

*It's true that this AddOn does not do exactly the same thing, but the comparison is still valid.

 

This code in the MergeFile.cs does all of the real Merge/Save work ->

It is also saving the folder path so that it can recall it the next time it is run. (You can see that code in the Config.cs file)

       /// <summary>Merge a .SLDPRT file and save out the result as an .MCX-5 file.</summary>
       /// <remarks>Note that "failure" can just mean the user aborted.</remarks>
       /// <returns>On success, returns the full path/name of the new file, or string.Empty if failed.</returns>
       public string ProcessFile()
       {
           // The "new" filename that we return to the caller.
           // Will be string.Empty if the process did not complete.
           string newFile = string.Empty;

           // Get the previously used folder path
           Config cfg = new Config();
           string initialInputFolder = cfg.InputFolder;

           // Check to be sure the this folder actually exists
           if (!Directory.Exists(initialInputFolder)) // If not..
               { initialInputFolder = string.Empty; } // Clear it.

           // The user selected input folder
           string folder = string.Empty;

           // Ask the user for the input file
           string filename = SelectFile(initialInputFolder);
           if (!string.IsNullOrEmpty(filename))
           {
               bool promptToSaveCurrent = false;
               bool mergeFile = true;
               bool result = Mastercam.IO.FileManager.Open(promptToSaveCurrent, filename, mergeFile);
               if (result)
               {                      
                   filename = Path.ChangeExtension(filename, ".MCX-5");                                      
                   // Tell Mastercam to save it as...
                   if (Mastercam.IO.FileManager.SaveAs(filename))
                       { newFile = filename; } // success? - then get the new filename                    

                   // If the input folder has changed from what we had before...
                   // Save this path for later (to store off in the config)
                   folder = Path.GetDirectoryName(filename);               
                   if (initialInputFolder != folder) 
                       { cfg.InputFolder = folder; }
               }                                
           }
           return newFile;
       }

 

What it does...

This AddOn prompts the user to select a SLDPRT file.

It then "merges" the data from that file into the current Mastercam database

and immediately saves the result as a Mastercam .MCX-5 file.

 

Example:

 

In -> C:\MyCadData\SolidWorks\MyDesign.SLDPRT

Out -> C:\MyCadData\SolidWorks\MyDesign.MCX-5

 

To install this Mastercam X5 NETHook AddOn.

Copy the MergeSLDPRT2MCX.DLL and MergeSLDPRT2MCX.FR files from the "Release DLL" folder to your Mastercam CHooks folder.

 

This is usually ->

C:\Program Files\mcamx5\CHooks

-or-

C:\Program Files (x86)\mcamx5\CHooks

 

After you restart Mastercam, there will be a new command icon available in the (main menu)

Settings, Customize, Category = File

You can add this command icon to a toolbar and/or map the command to a keystroke.

Or you can always run the AddOn "manually" via the main menu -

Setting - Run User Application and selecting the MergeSLDPRT2MCX.DLL

MergeSLDPRT2MCX.zip

Link to comment
Share on other sites

Subject: Merge SLDPRT file into the active Mastercam database.

 

MergeSLDPRT2MCX AddOn

 

*The AddOn and the full C# source code is included in the attached MergeSLDPRT2MCX.zip

You can build (or alter and rebuild) the project using Microsoft Visual Studio.

You can even use the free Express version.

 

You may think that VB Script is easy (or easier) than C# or VB.NET

Well... maybe is you do a lot of VB Scripts.

But compare this NETHook code to the VB Scripting in the earlier posts.

*It's true that this AddOn does not do exactly the same thing, but the comparison is still valid.

 

This code in the MergeFile.cs does all of the real Merge/Save work ->

It is also saving the folder path so that it can recall it the next time it is run. (You can see that code in the Config.cs file)

       /// <summary>Merge a .SLDPRT file and save out the result as an .MCX-5 file.</summary>
       /// <remarks>Note that "failure" can just mean the user aborted.</remarks>
       /// <returns>On success, returns the full path/name of the new file, or string.Empty if failed.</returns>
       public string ProcessFile()
       {
           // The "new" filename that we return to the caller.
           // Will be string.Empty if the process did not complete.
           string newFile = string.Empty;

           // Get the previously used folder path
           Config cfg = new Config();
           string initialInputFolder = cfg.InputFolder;

           // The user selected input folder
           string folder = string.Empty;

           // Ask the user for the input file
           string filename = SelectFile(initialInputFolder);
           if (!string.IsNullOrEmpty(filename))
           {
               bool promptToSaveCurrent = false;
               bool mergeFile = true;
               bool result = Mastercam.IO.FileManager.Open(promptToSaveCurrent, filename, mergeFile);
               if (result)
               {                      
                   filename = Path.ChangeExtension(filename, ".MCX-5");                                      
                   // Tell Mastercam to save it as...
                   if (Mastercam.IO.FileManager.SaveAs(filename))
                       { newFile = filename; } // success? - then get the new filename                    

                   // If the input folder has changed from what we had before...
                   // Save this path for later (to store off in the config)
                   folder = Path.GetDirectoryName(filename);               
                   if (initialInputFolder != folder) 
                       { cfg.InputFolder = folder; }
               }                                
           }
           return newFile;
       }

 

What it does...

This AddOn prompts the user to select a SLDPRT file.

It then "merges" the data from that file into the current Mastercam database

and immediately saves the result as a Mastercam .MCX-5 file.

 

Example:

 

In -> C:\MyCadData\SolidWorks\MyDesign.SLDPRT

Out -> C:\MyCadData\SolidWorks\MyDesign.MCX-5

 

To install this Mastercam X5 NETHook AddOn.

Copy the MergeSLDPRT2MCX.DLL and MergeSLDPRT2MCX.FR files from the "Release DLL" folder to your Mastercam CHooks folder.

 

This is usually ->

C:\Program Files\mcamx5\CHooks

-or-

C:\Program Files (x86)\mcamx5\CHooks

 

After you restart Mastercam, there will be a new command icon available in the (main menu)

Settings, Customize, Category = File

You can add this command icon to a toolbar and/or map the command to a keystroke.

Or you can always run the AddOn "manually" via the main menu -

Setting - Run User Application and selecting the MergeSLDPRT2MCX.DLL

 

 

Thank-you. We will play around with that and see where we are at.

 

Again I appreciate all you help.

Link to comment
Share on other sites

Hi Matt.

 

I do about the same as you describe as far as organizing the files in sub-directories, and same kind of procedure also as far as merging into template to keep using standard tool list & toolpaths. One problem I had is one of the guys overwrote on the template once so we had to clean it up. This comes from as you described, he merged the solidworks file into MCX template.

 

What if you go the other way around and ask you script to open the SLDPRT file and then Import/merge your template in? I say Import/Merge because a regular file Merge will call geometry and named levels table but it won't call toolpaths nor WCS's you have created, you have to actually import those.

 

I hope it helps, but I'm feeling you might be done already with your project :)

Link to comment
Share on other sites

Hi Matt.

 

I do about the same as you describe as far as organizing the files in sub-directories, and same kind of procedure also as far as merging into template to keep using standard tool list & toolpaths. One problem I had is one of the guys overwrote on the template once so we had to clean it up. This comes from as you described, he merged the solidworks file into MCX template.

 

What if you go the other way around and ask you script to open the SLDPRT file and then Import/merge your template in? I say Import/Merge because a regular file Merge will call geometry and named levels table but it won't call toolpaths nor WCS's you have created, you have to actually import those.

 

I hope it helps, but I'm feeling you might be done already with your project :)

 

 

I am still looking at different ways that this goal can be achieved. I appreciate your input. I didnt think of doing it the other way around. Does it give you the same end result?

Link to comment
Share on other sites

Actually, I was just testing Roger's addon and from what i can see, it does fine as far as getting rid of the overwriting on the template issue, but it doesn't save to a separate sub-directory as I understood you wanted.

 

So at this point is the users call whether you want to open your SLDPRT file 1st and then apply a template or open your template and then call a SLDPRT file to apply it to as this is the way Roger's Add on works.

Link to comment
Share on other sites

Actually, I was just testing Roger's addon and from what i can see, it does fine as far as getting rid of the overwriting on the template issue, but it doesn't save to a separate sub-directory as I understood you wanted.

 

So at this point is the users call whether you want to open your SLDPRT file 1st and then apply a template or open your template and then call a SLDPRT file to apply it to as this is the way Roger's Add on works.

 

 

We would normally save to a sub-directory.

 

Eg.

D:\RP\2011\RPC-11-00768

to

D:\RP\2011\RPC-11-00768\RPC Reference Files

 

However dragging the file afterwards is easy. It would be neat if it could do that as well tho.

Link to comment
Share on other sites

How you you want to specify this "incremental" sub-directory path?

 

Is this something that changes often?

Meaning you would want/need to be able to specify it each time.

-or-

Is it something that only changes "every once in a while"?

It would be easy enough to add a Config option allowing the user to set this

and the AddOn would use that until you changed in its config.

 

You're finding out how important the details are when designing software! :D

Link to comment
Share on other sites

How you you want to specify this "incremental" sub-directory path?

 

Is this something that changes often?

Meaning you would want/need to be able to specify it each time.

-or-

Is it something that only changes "every once in a while"?

It would be easy enough to add a Config option allowing the user to set this

and the AddOn would use that until you changed in its config.

 

You're finding out how important the details are when designing software! :D

 

I am indeed. I think a problem is solved and another arises. :)

 

We will always be adding the files to the sub-directory folder within the current directory named '\RPC Reference Files'

 

eg

 

C:\001 -To-

C:\001\RPC Reference Files

C:\002 -To-

C:\002\RPC Reference Files

 

or whatever our directory is.

Link to comment
Share on other sites

The MergeSLDPRT2MCX AddOn has been updated.

 

It now outputs the "new" (MCX-5) file to a sub-folder under the .SLPRT input folder.

*This is optional and the sub-folder name is configurable.

 

See the ReadMe-First.txt file in the ZIP.

 

The updated ZIP is attached to the original posting above.

 

 

I cannot thank you enough Roger. It is a big help to what were trying to do.

 

Again, thanks.

 

Also thank you Manuel for helping to convey my ideas to Roger. :)

Link to comment
Share on other sites

You have the source for the Project, so you can alter it, and re-build it using one of the free "Express" version of Visual Studio 2010.

 

Programming .NET (for NETHook) is not that difficult, as much of what you're doing is not directly Mastercam related. *Like the MergeSLDPRT2MCX project (done in C#) for example!*

I left the full "Mastercam.<namespace>. on each command that is a Mastercam NETHook method

and you can see that much of what MergeSLDPRT2MCX is doing is really just "plain old" Microsoft .NET

Which there is lots of help available on the Web.

Link to comment
Share on other sites

Hi Roger. Thanks for doing this script. Matt L is our co-op student here this term and I had asked him if he could do something up like this as he said he had some VB programming experience. I'm glad you were able to help out with this (ie. do it for him :D:-) )

 

I've had script linker installed and now this chook is working as I had hope, saving the file in the proper directory. There are a couple of additions that would make it better if possible:

 

1. To save the chance of overwriting an existing file, could it check the directory that it is writing to see if a file by the same name already exists, and if so give us the option of over writing or not, or ask for a new filename? I'm sure this won't happen a lot but I could see it biting someone down the road.

 

2. My "template" that I use already has a machine group and a toolpath group with the name "template-1" and "template-op10" respectively. Could these be change to replace the word "template" with the filename of the MCX-5 file?

 

Thanks again for all your help on this. It should save me a good chunk of time and help to standardize where we all save our files.

 

Matt.

Link to comment
Share on other sites

Thank-you. I do appreciate it.

 

However, to be fair, I wasn't looking for a freebie.

 

We were willing to pay our co-op student to do this, and he was asking for help on how to accomplish this on Emastercam which I recommended to him as a great resource, however he quickly determined it was beyond his capabilities and informed us of this.

 

I did also make this request to my re-seller and have yet to receive a response, possibly because they saw it being taken care of here.

 

Thanks again.

 

Enjoy your weekend.

 

Matt.

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