Macro to Export Selected Tools as Template Objects

Macro to Export Selected Tools as Template Objects

evo80
Collaborator Collaborator
1,293 Views
12 Replies
Message 1 of 13

Macro to Export Selected Tools as Template Objects

evo80
Collaborator
Collaborator

Good Day,

I'm trying to create this macro to export selected tools as *.ptf files to a location and drop them into folders based on their tool type, but It's not working for me, any ideas where I'm going wrong.

Thanks Jon K 

DIALOGS MESSAGE OFF
DIALOGS ERROR OFF
GRAPHICS LOCK

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

STRING $pmlprj_content = ""

FOREACH $tl IN explorer_selected_entities() {
	IF $tl.type == 'tip_radiused'  {
STRING $template_path = "C:\Powermill\Template Objects\Tools\Tip Radiused"
}ELSE IF $tl.type == 'routing' {
STRING $template_path = "C:\Powermill\Template Objects\Tools\Routing"


    STRING $tool_name = "x" + $tl.ID + ".pmlent"
	STRING $tool_name_new = $tl.Name + ".ptf"
	STRING $tool_full = project_pathname(0) + "\" + $tool_name
	STRING $dest_path_full = $template_path + "\" + $tool_name_new
	COPY FILE $tool_full $dest_path_full
}
	}

DIALOGS MESSAGE ON
DIALOGS ERROR ON
GRAPHICS UNLOCK

 

0 Likes
Accepted solutions (1)
1,294 Views
12 Replies
Replies (12)
Message 2 of 13

iamcdn79
Mentor
Mentor

Does this work?

 

DIALOGS MESSAGE OFF
DIALOGS ERROR OFF
GRAPHICS LOCK

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

STRING $pmlprj_content = ""

FOREACH $tl IN explorer_selected_entities() {
    IF $tl.type == 'tip_radiused' {
        STRING $template_path = "C:\\Powermill\\Template Objects\\Tools\\Tip Radiused"
    } ELSE IF $tl.type == 'routing' {
        STRING $template_path = "C:\\Powermill\\Template Objects\\Tools\\Routing"
    }

    STRING $tool_name = "x" + $tl.ID + ".pmlent"
    STRING $tool_name_new = $tl.Name + ".ptf"
    STRING $tool_full = project_pathname(0) + "\\" + $tool_name
    STRING $dest_path_full = $template_path + "\\" + $tool_name_new
    COPY FILE $tool_full $dest_path_full
}

DIALOGS MESSAGE ON
DIALOGS ERROR ON
GRAPHICS UNLOCK

 

 


Intel Core i9 13900KF CPU
128 GB Kingston Beast DDR4 SDRAM
PNY RTX A2000 6GB Video Card
WD 1 TB SSD Hard Drive
Windows 11 Pro

0 Likes
Message 3 of 13

evo80
Collaborator
Collaborator

@iamcdn79 ,

Didn't work.

Here's the macro error I'm getting...

Macro Error:

File: C:\dcam\config\pmill2/Export-Selected-Tools.mac
Line: 20

}ELSE IF $tl.type == 'routing' {



Unexpected tokens found
0 Likes
Message 4 of 13

iamcdn79
Mentor
Mentor

Change to

 } ELSEIF $tl.type == 'routing' {

Intel Core i9 13900KF CPU
128 GB Kingston Beast DDR4 SDRAM
PNY RTX A2000 6GB Video Card
WD 1 TB SSD Hard Drive
Windows 11 Pro

0 Likes
Message 5 of 13

evo80
Collaborator
Collaborator

Hmmm,

Same Error

0 Likes
Message 6 of 13

icse
Advisor
Advisor

I can't test it atm but somethimes checking enums in powermill in a loop/if statement does not work correctly try to create a string variable first and check those.


try this:

DIALOGS MESSAGE OFF
DIALOGS ERROR OFF
GRAPHICS LOCK

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

STRING $pmlprj_content = ""

FOREACH $tl IN filter(explorer_selected_entities(),'this.RootType == "tool"') {
	string $e = $tl.type
	
    IF $e == 'tip_radiused' {
        STRING $template_path = "C:\\Powermill\\Template Objects\\Tools\\Tip Radiused"
    } ELSE IF $e == 'routing' {
        STRING $template_path = "C:\\Powermill\\Template Objects\\Tools\\Routing"
    }

    STRING $tool_name = "x" + $tl.ID + ".pmlent"
    STRING $tool_name_new = $tl.Name + ".ptf"
    STRING $tool_full = project_pathname(0) + "\\" + $tool_name
    STRING $dest_path_full = $template_path + "\\" + $tool_name_new
    COPY FILE $tool_full $dest_path_full
}

DIALOGS MESSAGE ON
DIALOGS ERROR ON
GRAPHICS UNLOCK

 also added a filter to the explorer selected enties so it does only work with tools

0 Likes
Message 7 of 13

evo80
Collaborator
Collaborator

Thanks for your help guys!

@icse , still getting that same macro error at

Macro Error:

File: C:\dcam\config\pmill2/Export-Selected-Tools.mac
Line: 22

    } ELSE IF $e == 'routing' {



Unexpected tokens found
0 Likes
Message 8 of 13

iamcdn79
Mentor
Mentor

Close, reopen the project and rerun the macro


Intel Core i9 13900KF CPU
128 GB Kingston Beast DDR4 SDRAM
PNY RTX A2000 6GB Video Card
WD 1 TB SSD Hard Drive
Windows 11 Pro

0 Likes
Message 9 of 13

evo80
Collaborator
Collaborator

@iamcdn79 Same error.

0 Likes
Message 10 of 13

icse
Advisor
Advisor

I think you need to write the else if together. 

 

Elseif

0 Likes
Message 11 of 13

evo80
Collaborator
Collaborator

Ok, I fiddled around with the code a bit, so when I select routing tools and run the macro it puts them into the correct folder, however when I select tip radiused tools it fails to populate the correct folder.

DIALOGS MESSAGE OFF
DIALOGS ERROR OFF
GRAPHICS LOCK

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

STRING $pmlprj_content = ""

FOREACH $tl IN filter(explorer_selected_entities(),'this.RootType == "tool"') {
	string $e = $tl.type
	
    IF $e == 'tip_radiused' {
        STRING $template_path = "C:\Powermill\Template Objects\Tools\Tip Radiused"
    } ELSEIF $e == 'routing' {
        STRING $template_path = "C:\Powermill\Template Objects\Tools\Routing"
    

    STRING $tool_name = "x" + $tl.ID + ".pmlent"
    STRING $tool_name_new = $tl.Name + ".ptf"
    STRING $tool_full = project_pathname(0) + "\" + $tool_name
    STRING $dest_path_full = $template_path + "\" + $tool_name_new
    COPY FILE $tool_full $dest_path_full
}
}

DIALOGS MESSAGE ON
DIALOGS ERROR ON
GRAPHICS UNLOCK

 

0 Likes
Message 12 of 13

icse
Advisor
Advisor
Accepted solution

I looked it up in the debugger found some bugs, the $template_path variable is declared in the if statement and gets lost if the if block is closed also some {} where wrong.

 

I updated the macro so it works with every tool type out of the box:

 

if length(project_pathname(0)) == 0 OR project_modified() == 1 {
	bool $save = 0
	$save = query 'Save?'
	if $save {
		project save
	} else {
		return
	}
}


foreach $t in filter(explorer_selected_entities(),'this.RootType == "tool"') {

	string $template_path = 'C:/Powermill/Template Objects/Tools/' + $t.type
	
	STRING $tool_name = "x" + $t.ID + ".pmlent"
	STRING $tool_name_new = $t.Name + ".ptf"
	STRING $tool_full = project_pathname(0) + "/" + $tool_name
	STRING $dest_path_full = $template_path + "/" + $tool_name_new
	
	if not dir_exists($template_path) {
		mkdir ${template_path}
	}
	COPY FILE ${tool_full} ${dest_path_full}
}

 

You may have to change the / to \ in the path variable.

Message 13 of 13

evo80
Collaborator
Collaborator

Thanks so much, I've been wanting to migrate away from using the tool database and this has tidied up that decision.

Best Regards Jon K 

0 Likes