Community
PowerMill Forum
Welcome to Autodeskā€™s PowerMill Forums. Share your knowledge, ask questions, and explore popular PowerMill topics.
cancel
Showing results forĀ 
ShowĀ Ā onlyĀ  | Search instead forĀ 
Did you mean:Ā 

Macro to Export Selected Tools as Template Objects

12 REPLIES 12
SOLVED
Reply
Message 1 of 13
evo80
600 Views, 12 Replies

Macro to Export Selected Tools as Template Objects

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

 

12 REPLIES 12
Message 2 of 13
iamcdn79
in reply to: evo80

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

Message 3 of 13
evo80
in reply to: iamcdn79

@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
Message 4 of 13
iamcdn79
in reply to: evo80

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

Message 5 of 13
evo80
in reply to: iamcdn79

Hmmm,

Same Error

Message 6 of 13
icse
in reply to: evo80

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

Message 7 of 13
evo80
in reply to: icse

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
Message 8 of 13
iamcdn79
in reply to: evo80

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

Message 9 of 13
evo80
in reply to: iamcdn79

@iamcdn79 Same error.

Message 10 of 13
icse
in reply to: evo80

I think you need to write the else if together. 

 

Elseif

Message 11 of 13
evo80
in reply to: icse

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

 

Message 12 of 13
icse
in reply to: evo80

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
in reply to: evo80

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 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report