Macro - Tool Template

Talk about Autdesk PowerMill
User avatar
tk421
Posts: 15
Joined: Thu Aug 24, 2023 5:33 pm
Answers: 1
Location: Milwaukee, WI
x 4
x 10

Macro - Tool Template

Unread post by tk421 »

In an effort to get more traffic on this board, I'm going to post tips & tricks that I've found over the years as well as macros that I've written.

Here are two macros that I use to save out tools from a project and then load them into another project. A couple notes: The project has to be saved first, and the path that it writes/reads from is hard coded.

The saving macro:

Code: Select all

//----------------------------------------------------------------//
// This macro saves template cutters from current project.        //
// written by David 09/21/2017                                    //
// Jesus is Lord!                                                 //
//----------------------------------------------------------------//
//                          UPDATES                               //
//																  //
// 03/01/2019 - DRA - Updated logic to make more robust. Special  //
//                    thanks to Rafael Sanso for the assistance.  //
//----------------------------------------------------------------//
// Some functions used in this macro may be located another file. //
// If there is an error or the macro does not function properly,  //
// please report it to the developer: adamcadcam@pm.me            //
//----------------------------------------------------------------//
// This macro and all included functions and code is/are the      //
// intellectual property of David R.W. Adam. Any unauthorized     //
// copy or use of these without expressed permission is strictly  //
// prohibited. 												      //
//----------------------------------------------------------------//


reset localvars

FUNCTION MAIN() {
	
	STRING $template_name = INPUT "Save Tool Template As..."
	STRING $template_path = "S:/Software/PowerMill/Templates/Tool Templates/" + $template_name
	IF $length(project_pathname(0)) == 0 {
		MESSAGE WARN 'Save project first.'
		MACRO ABORT ALL
	}

	IF $SIZE(folder('tool')) == 0 {
		MESSAGE WARN 'No tools in project.'
		MACRO ABORT ALL
	}

	PROJECT SAVE

	IF dir_exists($template_path) {
		DELETE DIRECTORY $template_path
	}

	MKDIR $template_path
	STRING $pmlprj_content = ""

	FOREACH t IN folder('Tool') {
		STRING $tool_name = "x" + $t.ID + ".pmlent"
		STRING $tool_full = project_pathname(0) + "/" + $tool_name
		STRING $dest_path_full = $template_path + "/" + $tool_name
		COPY FILE $tool_full $dest_path_full
		$pmlprj_content = $pmlprj_content + $tool_name + " pmlEntTool '" + $t.Name + "'" + CRLF
	}

	STRING $proj_name = "/" + project_pathname(1) + ".pmlprj"
	STRING $path_full = project_pathname(0) + $proj_name
	FILE OPEN $path_full FOR READ AS input
	STRING L1 = ""
	STRING L2 = ""
	STRING L3 = ""
	FILE READ $L1 FROM input
	FILE READ $L2 FROM input
	FILE READ $L3 FROM input
	FILE CLOSE input

	$L1 = $SIZE(folder('tool')) + substring($L1, position($L1, " ", 0), length($L1))
	STRING $dest_path_full = $template_path + $proj_name
	FILE OPEN $dest_path_full FOR WRITE AS output
	FILE WRITE $L1 TO "output"
	FILE WRITE $L2 TO "output"
	FILE WRITE $L3 TO "output"
	FILE WRITE $pmlprj_content TO "output"
	FILE CLOSE output

	MESSAGE INFO "Tool objects saved successfully."
}
The importing macro:

Code: Select all

//----------------------------------------------------------------//
// This macro imports template cutters from selected file.        //
// written by David 09/21/2017                                    //
// Jesus is Lord!                                                 //
//----------------------------------------------------------------//
//                          UPDATES                               //
//																  //
// 03/01/2019 - DRA - Loads project file (containing tools only)  //
//                    from server                                 //
// 04/21/2022 - DRA - Code changed to select & import multiple    //
//                    files                                       //
//----------------------------------------------------------------//
// Some functions used in this macro may be located another file. //
// If there is an error or the macro does not function properly,  //
// please report it to the developer: adamcadcam@pm.me            //
//----------------------------------------------------------------//
// This macro and all included functions and code is/are the      //
// intellectual property of David R.W. Adam. Any unauthorized     //
// copy or use of these without expressed permission is strictly  //
// prohibited. 												      //
//----------------------------------------------------------------//

reset localvars

FUNCTION MAIN() {

    // Create a list of ptf files including the full folder path.
    STRING LIST $ptf_files = list_files('dirs', 'S:\Software\PowerMill\Templates\Tool Templates')

    // Create a new list "$Template_Names" without the pathname included. Only the filename.
    STRING LIST $Template_Names = {}
    FOREACH $File IN $ptf_files {
        INT $Ok = add_last($Template_Names, TOKENS($File, "/")[SIZE(TOKENS($File, "/"))-1])
    }

    // Display only the "$Template_Names" in your pulldown list
    INT LIST $C = INPUT CHOICE MULTIPLE $Template_Names "Select File"

    // Import file(s) from the original list which includes the full pathname
    foreach file IN C {
        PROJECT IMPORT ${$ptf_files[$file]}	
    }
}
David
<><
-----------------------------------
the numbers never lie

www.accuratepattern.com
arsalan
Posts: 1
Joined: Sun Nov 24, 2024 6:23 am
Answers: 0
x 1
x 1

Re: Macro - Tool Template

Unread post by arsalan »

Thank you very much for your work.
User avatar
tk421
Posts: 15
Joined: Thu Aug 24, 2023 5:33 pm
Answers: 1
Location: Milwaukee, WI
x 4
x 10

Re: Macro - Tool Template

Unread post by tk421 »

my pleasure. I'll be posting more on this board starting next week. we're up against due dates this week at work so i'm a little busy with that.

if there's something in particular you're looking for, i can see if i have something, otherwise we can write something together
David
<><
-----------------------------------
the numbers never lie

www.accuratepattern.com
standardpattern
Posts: 5
Joined: Wed Nov 20, 2024 10:32 am
Answers: 0
x 1

Re: Macro - Tool Template

Unread post by standardpattern »

Thanks, I'll check them out. I appreciate you taking the time.
Post Reply