Macro to create NC code

Macro to create NC code

newbie123abc
Enthusiast Enthusiast
4,141 Views
28 Replies
Message 1 of 29

Macro to create NC code

newbie123abc
Enthusiast
Enthusiast

Hi everybody,

 

I usually create NC code for active toolpath by right click on it and then choose command "Create Individual NC Program". Now, I would like to make a macro to do this. This is my macro (active toolpath is 6000):

 

EXPLORER SELECT Toolpath "Toolpath\6000" NEW
CREATE NCPROGRAM "6000" EDIT NCPROGRAM "6000" APPEND TOOLPATH "6000" DEACTIVATE NCPROGRAM

 

I would like to active another toolpath like 6001 and run this macro for creating NC code file with name 6001, could you please fix it for me ?

 

Thank you very much ! 

 

 

0 Likes
4,142 Views
28 Replies
Replies (28)
Message 2 of 29

NanchenO
Collaborator
Collaborator

@newbie123abc,

 

It is not exactly what you need, but you could alter this one:

 

EXPLORER SELECT FOLDER "NCProgram" NEW
DEACTIVATE NCProgram
ENTITY LIST $ents = INPUT ENTITY MULTIPLE ncprogram "Select NC programs"
FOREACH $nc IN $ents {
	ACTIVATE NCProgram $nc.Name
	EDIT NCPROGRAM SELECTED FOLDER "P:\bla\bla\"\r
	NCSELECTED APPLY\r
// Put your "rename" / append functionality here
	EDIT NCPROGRAM SELECTED FILEMASK  P:\bla\bla\{ncprogram}.MPF
	NCSELECTED APPLY\r
	EDIT NCPROGRAM SELECTED TAPEOPTIONS "P:\Powermill\Post-pro\YOUR_PP.pmoptz"\r
	NCSELECTED ACCEPT\r
	KEEP NCPROGRAM SELECTED\r
	YES\n
}
DEACTIVATE NCProgram

It should be a good starting base...

 

Olivier

Message 3 of 29

GlennM.MCAM
Collaborator
Collaborator

 

You can use the build in function for this without macro.

 

Select your toolpaths you want to make a nc program off, right click and select "create individual nc programs".

 

Glenn


Intel i7 4790 @ 3.60GHz -16GB RAM - Nvidia Quadro K620 - 250GB SSD - Win 7 Pro x64 SP1
PowerMILL Premium 2019.1 - PowerINSPECT Ultimate OMV 2017 SP6
Message 4 of 29

GlennM.MCAM
Collaborator
Collaborator

But offcourse, @NanchenO gives a great solution with much more possibilities Smiley Wink


Intel i7 4790 @ 3.60GHz -16GB RAM - Nvidia Quadro K620 - 250GB SSD - Win 7 Pro x64 SP1
PowerMILL Premium 2019.1 - PowerINSPECT Ultimate OMV 2017 SP6
0 Likes
Message 5 of 29

newbie123abc
Enthusiast
Enthusiast

Hi @GlennM.MCAM@NanchenO

 

I'm beginner and I'm trying to learn macro by recording. Could you please help me to make a simple macro just create NC code for any active toolpath instead of right click on active toolpath and choose "Create Individual NC Program".

 

Thank you very much !

0 Likes
Message 6 of 29

kukelyk
Advisor
Advisor

Try this:

CREATE NCPROGRAM ; ACTIVATE NCPROGRAM # EDIT NCPROGRAM ; APPEND TOOLPATH ; DEACTIVATE NCPROGRAM
Message 7 of 29

newbie123abc
Enthusiast
Enthusiast

Hi @kukelyk,

 

Thank you very much, it work good. But the name of NC code file is not match with toolpath. Could you please fix for me ( for detail, please see attached photo)

0 Likes
Message 8 of 29

kukelyk
Advisor
Advisor
Accepted solution

IF NOT ENTITY_EXISTS('NCPROGRAM', $toolpath.name) {
CREATE NCPROGRAM $toolpath.name ACTIVATE NCPROGRAM # EDIT NCPROGRAM ; APPEND TOOLPATH ; DEACTIVATE NCPROGRAM
} ELSE {
STRING msg = "Ncprogram '" +$toolpath.name + "' already exists"
MESSAGE INFO $msg
}

Message 9 of 29

newbie123abc
Enthusiast
Enthusiast

Hi @kukelyk

 

Great ! This is a macro I need. Thank you very much !

0 Likes
Message 10 of 29

newbie123abc
Enthusiast
Enthusiast

Hi @kukelyk,

 

Can I have one more question ? With your macro, I inserted some codes to make code file like picture which was attached. It is right with active toolpath 6000 and is not right with another active toolpath. Could you please fix it so that can run with any active toolpath. Thank you very much !

 

Here is my macro:

 

IF NOT ENTITY_EXISTS('NCPROGRAM', $toolpath.name) {
CREATE NCPROGRAM $toolpath.name ACTIVATE NCPROGRAM # EDIT NCPROGRAM ; APPEND TOOLPATH ; DEACTIVATE NCPROGRAM
} ELSE {
STRING msg = "Ncprogram '" +$toolpath.name + "' already exists"
MESSAGE INFO $msg
}

EXPLORER SELECT NCProgram "NCProgram\6000" NEW
EDIT NCPROGRAM "6000" QUIT FORM NCTOOLPATH
EDIT NCPROGRAM "6000" FILENAME "C:\Users\AESI-CAM3\Desktop\New project\test\sample\ncprograms\6000.H"
EDIT NCPROGRAM "6000" NUMBER "6000"
EDIT NCPROGRAM '6000' ITEM 0 COMPONENT 0 FIXTUREOFFSET "1"
NCTOOLPATH ACCEPT FORM ACCEPT NCTOOLPATHLIST FORM ACCEPT NCTOOLLIST

0 Likes
Message 11 of 29

kukelyk
Advisor
Advisor
Accepted solution
IF NOT ENTITY_EXISTS('NCPROGRAM', $toolpath.name) {
	CREATE NCPROGRAM $toolpath.name ACTIVATE NCPROGRAM # EDIT NCPROGRAM ; APPEND TOOLPATH ; 
	STRING ncp_path =  "C:\Users\AESI-CAM3\Desktop\New project\test\sample\ncprograms\"+ $toolpath.name + ".H"
	EDIT NCPROGRAM ; FILENAME $ncp_path
	INT k = INT($toolpath.name) //You should add some comparison, if the toolpath name can be converted into int, or not..
	EDIT NCPROGRAM ; NUMBER $k
	EDIT NCPROGRAM ; ITEM 0 COMPONENT 0 FIXTUREOFFSET "1"
	DEACTIVATE NCPROGRAM
} ELSE {
	STRING msg = "Ncprogram '" +$toolpath.name + "' already exists"
	MESSAGE INFO $msg
}
Message 12 of 29

newbie123abc
Enthusiast
Enthusiast

Hi @kukelyk,

 

Thank you so much. It works very well.

0 Likes
Message 13 of 29

newbie123abc
Enthusiast
Enthusiast

Hi @kukelyk,

 

Sorry to bother you again, but could you please help me this issue ?

 

I had a macro that will export toolpath name, tool name and toolpath length to excel file. This is the macro:

 

//
// Macro to export NC data to Excel
//

// Open File
FILE OPEN "c:\temp\NC.csv" FOR WRITE AS "output"
// Cycle through the NC programs
FOREACH ncp IN folder('NCProgram') {
	// Create an empty list
	STRING List names = {}
	// Output NC Program name
	STRING nome = "NC Program:," + ncp.Name
	FILE WRITE $nome TO "output"
	// Output Header for Toolpath data
	STRING header = "Toolpath Name, Tool Name, Toolpath Length"
	FILE WRITE $header TO "output"
	// loop over the components in the nc program
	FOREACH item IN components(ncp) {
		// Check that it is a toolpath
		IF item.RootType == 'nctoolpath' {
			// Use MEMBER to check that we have not seen this name before
			IF NOT member(names, item.Name) {
				// Add name to list
				bool ok = add_last(names, item.Name)
				// Output data for current toolpath
				STRING line = item.Name + "," + item.Toolname.value + "," + string(item.Length.value)
				FILE WRITE $line TO "output"
			} else {

			}
		}
	}	
	// Write an empty line
	STRING empty_line = " "
	FILE WRITE $empty_line TO "output"
}
// Close File	
FILE CLOSE "output"

 

Is there any way to get time of toolpath instead of toolpath length ? Here is the value which I want to export to excel file:

 

 

a.jpg

 

Thank you very much !

0 Likes
Message 14 of 29

kukelyk
Advisor
Advisor

Try this:

REAL Time = $entity('toolpath',$item).statistics.LeadsandLinks.Times.plunge + $entity('toolpath',$item).statistics.LeadsandLinks.Times.ramp+ $entity('toolpath',$item).statistics.LeadsandLinks.Times.rapid
$Time = $Time + $entity('toolpath',$item).statistics.LeadsandLinks.Times.others + $entity('toolpath',$item).statistics.cuttingmoves.Times.arcs + $entity('toolpath',$item).statistics.cuttingmoves.Times.linear
STRING Timestring =  ''

STRING hours = STRING(INT($Time/60))
IF INT($hours) < 10 {
	$hours = '0' + $hours
}
STRING mins = STRING(INT($Time%60))
IF INT($mins) < 10 {
	$mins = '0' + $mins
}				
STRING secs = STRING(INT((($Time-INT($Time%60))*60)))
IF INT($secs) < 10 {
	$secs = '0' + $secs
}	

$Timestring =  $hours + ":" + $mins + ":" + $secs
Message 15 of 29

newbie123abc
Enthusiast
Enthusiast

Hi @kukelyk

 

Thank you for you help, but it doesn't match with my expectation. I had a NC program like this:

 

a.jpg

And when I run my macro above, I will have a file in C drive like

 

 

b.jpg

 

Can I have the macro so that when I run it, I can get toolpath time instead of toolpath length like this: ( the time will be copied at the row where i highlighted at the photo above)

 

c.jpg

 

Please help me if you have any solution. Thank you very much !

0 Likes
Message 16 of 29

kukelyk
Advisor
Advisor

 Try to continue this:

REAL sumtime = 0
FOREACH ncp IN FOLDER('ncprogram') {
	REAL ncptime = 0
	FOREACH $tp IN COMPONENTS($entity('ncprogram',$ncp)) {
		IF $tp.RootType == 'nctoolpath' {
			REAL Time = 0
			$Time = $entity('toolpath',$tp.name).statistics.LeadsandLinks.Times.plunge + ...
			
			$ncptime = $ncptime + $Time
			$sumtime = $sumtime + $Time
		}			
	}
}
Message 17 of 29

newbie123abc
Enthusiast
Enthusiast

Hi @kukelyk,

 

I'm a beginner so I don't know how to use your macro. Could you please write full macro for me ? I tried to edit my macro like this but it didn't work:

 

//
// Macro to export NC data to Excel
//

// Open File
FILE OPEN "c:\temp\NC.csv" FOR WRITE AS "output"
// Cycle through the NC programs
FOREACH ncp IN folder('NCProgram') {
// Create an empty list
STRING List names = {}
// Output NC Program name
STRING nome = "NC Program:," + ncp.Name
FILE WRITE $nome TO "output"
// Output Header for Toolpath data
STRING header = "Toolpath Name, Tool Name, Toolpath Time"
FILE WRITE $header TO "output"
// loop over the components in the nc program
FOREACH item IN components(ncp) {
// Check that it is a toolpath
IF item.RootType == 'nctoolpath' {
// Use MEMBER to check that we have not seen this name before
IF NOT member(names, item.Name) {
// Add name to list
bool ok = add_last(names, item.Name)
// Output data for current toolpath
STRING line = item.Name + "," + item.Toolname.value + "," + string(item.Statistics.Time.value)
FILE WRITE $line TO "output"
} else {

}
}
}
// Write an empty line
STRING empty_line = " "
FILE WRITE $empty_line TO "output"
}
// Close File
FILE CLOSE "output"

 

I changed my macro at the red cirlce.

 

d.jpg

0 Likes
Message 18 of 29

kukelyk
Advisor
Advisor

Try this:

//
// Macro to export NC data to Excel
//

// Open File
FILE OPEN "c:\temp\NC.csv" FOR WRITE AS "output"
// Cycle through the NC programs
REAL sumtime = 0
FOREACH ncp IN folder('NCProgram') {
	REAL ncptime = 0
	// Create an empty list
	STRING List names = {}
	// Output NC Program name
	STRING nome = "NC Program:," + ncp.Name
	FILE WRITE $nome TO "output"
	// Output Header for Toolpath data
	STRING header = "Toolpath Name, Tool Name, Toolpath Length"
	FILE WRITE $header TO "output"
	// loop over the components in the nc program
	FOREACH item IN components(ncp) {
		// Check that it is a toolpath
		IF item.RootType == 'nctoolpath' {

			REAL Time = $entity('toolpath',$item).statistics.LeadsandLinks.Times.plunge + $entity('toolpath',$item).statistics.LeadsandLinks.Times.ramp+ $entity('toolpath',$item).statistics.LeadsandLinks.Times.rapid
			$Time = $Time + $entity('toolpath',$item).statistics.LeadsandLinks.Times.others + $entity('toolpath',$item).statistics.cuttingmoves.Times.arcs + $entity('toolpath',$item).statistics.cuttingmoves.Times.linear
			STRING tpTime =  ''

			STRING hours = STRING(INT($Time/60))
			IF INT($hours) < 10 {
				$hours = '0' + $hours
			}
			STRING mins = STRING(INT($Time%60))
			IF INT($mins) < 10 {
				$mins = '0' + $mins
			}				
			STRING secs = STRING(INT((($Time-INT($Time%60))*60)))
			IF INT($secs) < 10 {
				$secs = '0' + $secs
			}	
			$tpTime =  $hours + ":" + $mins + ":" + $secs
			$ncptime = $ncptime + $Time
			$sumtime = $sumtime + $Time
			// Use MEMBER to check that we have not seen this name before
			IF NOT member(names, item.Name) {
				// Add name to list
				bool ok = add_last(names, item.Name)
				// Output data for current toolpath
				STRING line = item.Name + "," + item.Toolname.value + "," + $tpTime
				FILE WRITE $line TO "output"
			}
		}
	}	
	// Write an empty line
	STRING empty_line = " "
	FILE WRITE $empty_line TO "output"
}
// Close File	
FILE CLOSE "output"
Message 19 of 29

newbie123abc
Enthusiast
Enthusiast

Hi @kukelyk,

 

Thank you for your full macro. I tried to run it but there is a mistake like this. Could you have any solution to solve ?

 

d.jpg

0 Likes
Message 20 of 29

kukelyk
Advisor
Advisor
Accepted solution

Hope this will work :

//
// Macro to export NC data to Excel
//

// Open File
FILE OPEN "c:\temp\NC.csv" FOR WRITE AS "output"
// Cycle through the NC programs
REAL sumtime = 0
FOREACH ncp IN folder('NCProgram') {
	REAL ncptime = 0
	// Create an empty list
	STRING List names = {}
	// Output NC Program name
	STRING nome = "NC Program:," + ncp.Name
	FILE WRITE $nome TO "output"
	// Output Header for Toolpath data
	STRING header = "Toolpath Name, Tool Name, Toolpath Length"
	FILE WRITE $header TO "output"
	// loop over the components in the nc program
	FOREACH item IN components(ncp) {
		// Check that it is a toolpath
		IF item.RootType == 'nctoolpath' {

			REAL Time = $entity('toolpath',$item.name).statistics.LeadsandLinks.Times.plunge + $entity('toolpath',$item.name).statistics.LeadsandLinks.Times.ramp+ $entity('toolpath',$item.name).statistics.LeadsandLinks.Times.rapid
			$Time = $Time + $entity('toolpath',$item.name).statistics.LeadsandLinks.Times.others + $entity('toolpath',$item.name).statistics.cuttingmoves.Times.arcs + $entity('toolpath',$item.name).statistics.cuttingmoves.Times.linear
			STRING tpTime =  ''

			STRING hours = STRING(INT($Time/60))
			IF INT($hours) < 10 {
				$hours = '0' + $hours
			}
			STRING mins = STRING(INT($Time%60))
			IF INT($mins) < 10 {
				$mins = '0' + $mins
			}				
			STRING secs = STRING(INT((($Time-INT($Time%60))*60)))
			IF INT($secs) < 10 {
				$secs = '0' + $secs
			}	
			$tpTime =  $hours + ":" + $mins + ":" + $secs
			$ncptime = $ncptime + $Time
			$sumtime = $sumtime + $Time
			// Use MEMBER to check that we have not seen this name before
			IF NOT member(names, item.Name) {
				// Add name to list
				bool ok = add_last(names, item.Name)
				// Output data for current toolpath
				STRING line = item.Name + "," + item.Toolname.value + "," + $tpTime
				FILE WRITE $line TO "output"
			}
		}
	}	
	// Write an empty line
	STRING empty_line = " "
	FILE WRITE $empty_line TO "output"
}
// Close File	
FILE CLOSE "output"