replace a character in file with a macro

replace a character in file with a macro

Anonymous
Not applicable
2,792 Views
21 Replies
Message 1 of 22

replace a character in file with a macro

Anonymous
Not applicable

hello,

how can i replace a character in file with a macro on powermill.

for example :

 

O6992
T30 M6
G8 P1
G0 G90 X74.057 Y-430.296 S12000 M3
G0 G43 Z12.021 H30 M8
Z5.0 F3000
G1 Z-1.791 F30000
Y-430.466
X74.173 Y-431.051
X74.505 Y-431.548
X75.003 Y-431.88

 

I want to replace M3 by M3 T17 

 

O6992
T30 M6
G8 P1
G0 G90 X74.057 Y-430.296 S12000 M3 T17 
G0 G43 Z12.021 H30 M8
Z5.0 F3000
G1 Z-1.791 F30000
Y-430.466
X74.173 Y-431.051
X74.505 Y-431.548
X75.003 Y-431.88

 

if someone can help me Smiley Happy

0 Likes
Accepted solutions (1)
2,793 Views
21 Replies
Replies (21)
Message 2 of 22

NanchenO
Collaborator
Collaborator

HI @Anonymous,

 

Since it is in the NC program, I suggest that you do it in the postprocessor. You could do it adding a user variable in the toolpath, but it will be more reliable if you add the command in the postprocessor itself.

 

Olivier

0 Likes
Message 3 of 22

Anonymous
Not applicable

Thank you for your answer

but if i want to do with macro, It is possible with string replace for example by using file open and replace?

0 Likes
Message 4 of 22

NanchenO
Collaborator
Collaborator

Yes, it should. I never wrote such a macro. I think, that it could be easier if you call a VB program with your macro and let the VB program do the job.

 

Olivier

Message 5 of 22

Anonymous
Not applicable
FUNCTION Main(){
	//Path to file
	STRING projectPath = "D:\test.txt"
	STRING LIST lineList = {}
	INT a = 0
	
	//Read file
	IF length(projectPath) > 0 {
		FILE OPEN $projectPath FOR READ AS inFile
		FILE READ $lineList FROM inFile
		FILE CLOSE inFile
	}
	
	// Define replace character list
	STRING LIST replaceLineList = {}
	
	FOREACH line IN $lineList {
		STRING replaceLine = replace(line, "M3", "M3 T17") //Replace text
		$a = add_last(replaceLineList, $replaceLine)
	}
	
	$projectPath = substring($projectPath, 0, length($projectPath)-4)
	$projectPath = $projectPath + "_replace.txt"
	
	FILE OPEN $projectPath FOR WRITE AS out
	FOREACH line IN replaceLineList {
		FILE WRITE $line TO out
	}
	FILE CLOSE out
	
	MESSAGE INFO "Done"
}

 

 

Mateusz

0 Likes
Message 6 of 22

Anonymous
Not applicable

thank you, i'm going to try because for me 'M3 T17' and 'text.txt' are variables.

0 Likes
Message 7 of 22

Anonymous
Not applicable

hello

The calculation is too long to read all the file and write it. The file has many lines.

Can just one look for characters and replace him without all to rewrite?

 

0 Likes
Message 8 of 22

Anonymous
Not applicable

hello

The calculation is too long to read all the file and write it. The file has many lines.

Can just one look for characters and replace him without all to rewrite?

I would want to write directly in the file. I would want a macro in this style,

 

CD "F:\PWM\BLOCK"

STRING P = 6992
STRING m3 = "M3"
STRING TOOL = $m3 + " T17"

STRING projectPath = "O" + $P
STRING Valuem3= M3

FILE OPEN $projectPath FOR READ AS inFile
FILE READ $Valuem3 FROM inFile
FILE CLOSE inFile

FILE OPEN $projectPath FOR WRITE AS output
$Valuem3 = replace($Valuem3, $m3, $TOOL)
FILE CLOSE output

MESSAGE INFO "Done"

 

but it does not work, the file is empty after the calculation.
I am not strong for that Smiley Sad

 

thank

 

 

 

0 Likes
Message 9 of 22

Anonymous
Not applicable

It is really difficult to make it faster. I'm affraid that my solution is fastest, if you will do this by macro. I suppose there is no faster way, like load the file to RAM memory, and all the operations do inside it. And my solution works like that.

In your code there is one big problem, you are trying to load the content of this file to String variable. This type of data has a limited length. I suppose you can save inside only 255 signs. This is much much less than you need.

By the way your file is empty, because you open file for write, but you didn't write anything.

I tested few possibilities, but at this time I didn't find better solution.

I have one more idea, but you have to wait to modify the code and test it.


My question is, why you have to do this by PowerMill macro? Like Olivier wrote, there is many better solutions to do this what you need.


Mateusz

0 Likes
Message 10 of 22

Anonymous
Not applicable

thanks for your answer piro_man.

I want do by PowerMill macro because at present I use a Python macro and I wish to make him directly on Powermill. On Python, works great

0 Likes
Message 11 of 22

NanchenO
Collaborator
Collaborator

Since you can call DOS or other functions with a macro, why don't you try to call Python from PowerMill with you macro as an argument ?

 

Olivier

0 Likes
Message 12 of 22

Anonymous
Not applicable

Yes, I could call Python from Powermill, but it obliges me to have to inform variables under Powermill and under Python. It is not what I want just to change 2 characters

0 Likes
Message 13 of 22

NanchenO
Collaborator
Collaborator

PMPost does that clean and easily... was my first answer, I guess Smiley Happy

 

Olivier

0 Likes
Message 14 of 22

Anonymous
Not applicable

OK OK Olivier. I go and look at that. I never have to work on pmpost. I am going to begin in it and seeing how it works.
Thanks to all for your answers

Smiley Happy

0 Likes
Message 15 of 22

NanchenO
Collaborator
Collaborator

If you never used it, here are the first steps:

 

  1. Take a sample project and post a NC program using File type = "Cutter location" instead of "NCProgram".
  2. Copy it to the same location as your post pro.
  3. MAKE A COPY OF THE ORIGINAL POSTPRO !!! (I still do it systematically... in case I make a big mistake).
  4. Start a new PMPost session where you import your .pmpoptz post pro and the cutter location file.
  5. Save the session to be able to retrieve it easily.
  6. Right click on the program and "Process as debug".
  7. In the result, locate the section that you want to modify and double-clic on it.
  8. Then you can try to make your modifications, adding a postfix for instance.
  9. Reprocess as debug and see if it matches your needs...
  10. If yes, save your pmoptz and your session and make a live test with PowerMill, using some other NCprograms, if not, go back to 7. and perhaps have a look at the docs.

Olivier

0 Likes
Message 16 of 22

Anonymous
Not applicable

ok, thanks, 

I followed the steps, I understood the principle. Now, how make take a parameter in pmpost by knowing that following tools is in the following ncprogram. We make a program by tools

0 Likes
Message 17 of 22

NanchenO
Collaborator
Collaborator

I don't know how is your postpro, but you should have a "Load tool" and a "Load tool first" sections. I have a Fanuc PP, where I write at least "Txx M06" in these sections.

 

Then another one is "Spindle on" where I write "Syyy M03".

 

If you want to have a correct behaviour, you should basically have these sections correctly defined. PowerMill gives these infos to PMPost in these sections, i.e. at the correct moment. You just have to translate them correctly.

 

There is also a little trick: When you right click on the parameter, you can force the output to tape to "always", if yo want to always have this parameter written. If not, then you can choose "If updated".

 

Hope it helps !

 

Olivier

 

 

0 Likes
Message 18 of 22

Anonymous
Not applicable

I also have pp Fanuc. " Load tool " and " Load tool first " OK
But for the next tool, I think that I has to create a parameter to look for it in the following ncprogram and integrate it into pmpost ?

0 Likes
Message 19 of 22

NanchenO
Collaborator
Collaborator

Thera are "Tool Name next" and "Tool number next", which work inside a ncprogram with different tools.

 

If you post one program per tool, this info will not be available, I fear. In that case, you could define a user parameter in PowerMill and pass it to PmPost when posting.

 

Have a look at http://forum.delcam.com/viewtopic.php?f=14&t=11293&hilit=UDP&start=15

 

I use a macro to write this parameter into my toolpathes and it is passed to PMPost, where I defined the corresponding parameter. Then, you can place this parameter where you need it.

 

Olivier

0 Likes
Message 20 of 22

Anonymous
Not applicable

Hello

after several tries, i don't understand where I can declare my user parameter of powermill to pmpost Smiley Frustrated

 

 

Sans titre 1.jpg

 

Sans titre 2.jpg

 

can you help me please 

0 Likes