Creating a Script

More
20 Mar 2013 16:01 #31633 by ArcEye
Replied by ArcEye on topic Creating a Script
Hi

I cut and pasted the code JT posted and it works fine.

One thing I did though, is have the top line read #! /usr/bin/python, because I dont have a python environment setting, I just have the python binary in /usr/bin
This could be your problem, or that you have not chmod 755 todd.py to make the script executable

Attached is a copy in a zip already with mode set etc

Put into a directory, copy over the .ngc file you want to convert to the same directory and run
./todd.py filetoconvert.ngc

regards


File Attachment:

File Name: todd.zip
File Size:0 KB
Attachments:

Please Log in or Create an account to join the conversation.

More
20 Mar 2013 17:29 - 20 Mar 2013 17:31 #31635 by BigJohnT
Replied by BigJohnT on topic Creating a Script

I've not had any luck getting it to work, and I was hoping that there were some revisions in the last attachment that might clue me in to what I'm doing wrong.


Luck doesn't have much to do with getting the program to work :) Can you be more specific about what the error is?

I uploaded a zipped version and it gives me the internal error too... I wonder what is hosed with the forum database?

Finally this one attached ok... ArcEye I noticed your attachment has a file size of 0.

John
Attachments:
Last edit: 20 Mar 2013 17:31 by BigJohnT.

Please Log in or Create an account to join the conversation.

More
20 Mar 2013 19:42 - 20 Mar 2013 21:47 #31638 by Todd Zuercher
Replied by Todd Zuercher on topic Creating a Script
It runs now, but it is putting the W codes on a new line instead of at the end of the line. It was also adding a line feed between every line but I was able to stop it from doing that.

Also what changes would I have to make to use this as a filter script when opening a file with linuxCNC.

Not knowing any thing about programming this sure seems harder than it should be.
Last edit: 20 Mar 2013 21:47 by Todd Zuercher.

Please Log in or Create an account to join the conversation.

More
21 Mar 2013 00:21 #31644 by ArcEye
Replied by ArcEye on topic Creating a Script

....ArcEye I noticed your attachment has a file size of 0.


I know, but if you download it, it is 492 bytes and as it should be internally :angry: How does that work?

Please Log in or Create an account to join the conversation.

More
21 Mar 2013 02:10 #31646 by Todd Zuercher
Replied by Todd Zuercher on topic Creating a Script
The output looks like this:
G1 X2.4367 Y1.4479 Z-0.0827
 W-0.0827
G1 X2.4409 Y1.4376 Z-0.0739
 W-0.0739
G1 X2.4505 Y1.4109 Z-0.051
 W-0.051
G1 X2.4557 Y1.3982 Z-0.0395
 W-0.0395

Instead of this:
G1 X2.4367 Y1.4479 Z-0.0827 W-0.0827
G1 X2.4409 Y1.4376 Z-0.0739 W-0.0739
G1 X2.4505 Y1.4109 Z-0.051 W-0.051
G1 X2.4557 Y1.3982 Z-0.0395 W-0.0395

Please Log in or Create an account to join the conversation.

More
21 Mar 2013 07:15 - 21 Mar 2013 07:15 #31653 by BigJohnT
Replied by BigJohnT on topic Creating a Script
Todd,

It was not adding a line feed when I tested it... I'll look at it in the AM to see what's up.

It's not word wrapping is it?

John
Last edit: 21 Mar 2013 07:15 by BigJohnT.

Please Log in or Create an account to join the conversation.

More
21 Mar 2013 20:04 #31673 by Todd Zuercher
Replied by Todd Zuercher on topic Creating a Script
This is what I had to do to make it stop inserting line feeds
#! /usr/bin/env python
# remove the .txt extension
# Usage: ./todd.py filenametoconvert.ngc
# Output: converted_filenametoconvert.ngc

import sys

def main(argv):

  openfile = open(argv[0], 'r')
  file_in = openfile.readlines()
  openfile.close()

  file_out = []
  for line in file_in:
    # print line
    if line.find('Z') != -1:
      words = line.rstrip()
      words = words.split(' ')
      newword = ''
      for i in words:
        if i[0] == 'Z':
          newword = 'W'+ i[1:]
      if len(newword) > 0:
        words.append(newword)
        newline = ' '.join(words)
        file_out.append(newline+'\n')
    else:
      file_out.append(line)
  newfile = open('converted_'+argv[0], 'w')
  for item in file_out:
    newfile.write("%s" % item)
  newfile.close()

if __name__ == "__main__":
   main(sys.argv[1:])

Please Log in or Create an account to join the conversation.

More
21 Mar 2013 23:09 #31684 by Todd Zuercher
Replied by Todd Zuercher on topic Creating a Script
Now all I need to do is figure out how to make this work a filter for loading files in axis.
When I try it now It just says no such file or directory.

Please Log in or Create an account to join the conversation.

More
22 Mar 2013 06:50 - 22 Mar 2013 07:10 #31702 by arvidb
Replied by arvidb on topic Creating a Script
Here's another way, using a sed one-liner:
sed 's|Z\([-.0-9]*\)|Z\1 W\1|g' $1

I attached a script using this method as a file to this post. You will have to
$ cd <folder-where-you-downloaded-the-file>
$ gunzip w_add.sh.gz
$ chmod +x w_add.sh
before you can use it. Then use it like this:
$ ./w_add.sh in-file > out-file

File Attachment:

File Name: w_add.sh.gz
File Size:0 KB



Edit: For info on how to set up filters, look here: "www.linuxcnc.org/docs/2.4/html/config_ini_config.html#sub:[FILTER]-Section" (the forum can't handle links with '[' in them, it seems). I believe the script you use will have to generate the modified code to stdout, so BigJohnT's script might not work.

Edit2: Oh, and to avoid having to write an absolute path to the program in your INI-file, do
$ sudo cp w_add.sh /usr/bin/
This makes the script "globally" available without having to say exactly where it is.
Attachments:
Last edit: 22 Mar 2013 07:10 by arvidb.
The following user(s) said Thank You: Todd Zuercher

Please Log in or Create an account to join the conversation.

More
26 Mar 2013 08:19 #31887 by Todd Zuercher
Replied by Todd Zuercher on topic Creating a Script
Ok, this sed script works but...

It will load and convert files saved on the hard drive, but when I try to load a file that was saved on our network I get file not found errors. But files opened from the network that don't go through the script still open fine. Why?

I think I would still like to learn how to modify John's script to work with as an axis filter.

Please Log in or Create an account to join the conversation.

Time to create page: 0.110 seconds
Powered by Kunena Forum