Jump to content

Remove a section of text in a text file from between 2 keywords with autolisp or visual lisp


Mr Bojangles

Recommended Posts

1. I will go through some files tomorrow and see what else appears on these lines, I don't think it's a lot.

 

2. Yes the    Qualify(1.000,0.0,0.0)    will always be the same and will always change to    Qualify(1.000,0.0,0.035)

 

3. Could you do it similar to the code you made for MrBojangles, but i'd only need to pass the 1st part for filename as this will change from job to job, the other 2 will be set as shown in 2.

 

4. They're not coordinates (although they do look similar, they're tool setup data for the cnc machine, so they will always be as shown. In this case Qualify is the name for the operation type, the first part of numbers"1.000,0" is the safe position above the part for the tool, the next part "0,0" is for any offset  above the part to leave any excess material if that's required (not in my case), and the last part "0,0" is an offset to the side of the part, again to leave some excess material (this i what i do want and the amount is always the same.

 

I understand your questions, and i'll answer as many as you have or need. I never thought autolisp could do this until i saw this Topic today

 

Link to comment
Share on other sites

2 minutes ago, Sharper said:

1. I will go through some files tomorrow and see what else appears on these lines, I don't think it's a lot.

 

2. Yes the    Qualify(1.000,0.0,0.0)    will always be the same and will always change to    Qualify(1.000,0.0,0.035)

 

3. Could you do it similar to the code you made for MrBojangles, but i'd only need to pass the 1st part for filename as this will change from job to job, the other 2 will be set as shown in 2.

 

4. They're not coordinates (although they do look similar, they're tool setup data for the cnc machine, so they will always be as shown. In this case Qualify is the name for the operation type, the first part of numbers"1.000,0" is the safe position above the part for the tool, the next part "0,0" is for any offset  above the part to leave any excess material if that's required (not in my case), and the last part "0,0" is an offset to the side of the part, again to leave some excess material (this i what i do want and the amount is always the same.

 

I understand your questions, and i'll answer as many as you have or need. I never thought autolisp could do this until i saw this Topic today

 

 

@Sharper Ok thanks for the answers. It should not be too much effort to write this for you. Just to let you know however, I'll be out of communication for the next 5 or 6 days since I'll be on Holiday. I will get on this afterwards. If I get any time tonight I might get to it sooner, but I'll be traveling tomorrow morning.

  • Thanks 1
Link to comment
Share on other sites

My friend there is no rush.
i've been doing this manually for years lol.

 

I break up from work tomorrow evening for a week also. Have safe travels.

  • Thanks 1
Link to comment
Share on other sites

2 minutes ago, Sharper said:

I break up from work tomorrow evening for a week also. Have safe travels.

 

You as Well! 🍻

  • Like 1
Link to comment
Share on other sites

I've looked through a few hundred files
There seems to be only 2 variants, and they're as follows 

 

  Qualify(1.000,0.0,0.0) ; Qualify Tool

 

   LP Qualify(1.000,0.0,0.0) ; Qualify Offsets

Link to comment
Share on other sites

Posted (edited)

@Sharper Hi - I've been on Holiday but didn't forget you :), Please give this code a try. I minimally tested it with the example code you posted and it worked. Alter the path and file extension to suit you needs in the UPDATEGC command. Just in case - I recommend you backup your Gcode file before using.

;; Escape Wildcards  -  Lee Mac
;; Escapes wildcard special characters in a supplied string
(defun LM:escapewildcards ( str )
    (if (wcmatch str "*[-#@.*?~`[`,]*,*`]*")
        (if (wcmatch str "[-#@.*?~`[`,]*,`]*")
            (strcat "`" (substr str 1 1) (LM:escapewildcards (substr str 2)))
            (strcat     (substr str 1 1) (LM:escapewildcards (substr str 2)))
        )
        str
    )
)

(defun ReplaceCodes (file kw1 kw2 / flg fp ln ls n)
   (if (and file (setq file (findfile file)))
      (progn
         (setq fp (open file "r") flg nil)
         (while (setq ln (read-line fp))
            (if (wcmatch ln (strcat "*" (LM:escapewildcards kw1) "*"))
               (setq ln (vl-string-subst kw2 kw1 ln)
                     ls (cons ln ls) flg T
               )
               (setq ls (cons ln ls))
            )
         )
         (close fp)
         (if (and ls flg)
            (progn
               (setq fp (open file "w")
                     ls (reverse ls)
                     n 0
               )
               (repeat (length ls)
                  (write-line (nth n ls) fp)
                  (setq n (1+ n))
               )
               (close fp)
               (princ "\nFile Updated.")
            )
            (princ "\nNo lines found in file that match the pattern. ")
         )
      )
      (princ "\nFile Not found.")
   )
   (princ)
)

(defun c:UpdateGC ()
   (if (setq fil (getfiled "Select Gcode File to Update" "C:\\Myfolder\\" "gco" 4))
      (ReplaceCodes fil "Qualify(1.000,0.0,0.0)" "Qualify(1.000,0.0,0.035)")
   )
)

Thanks to Lee Mac for the "Escape Wildcards" code.

Edited by pkenewell
  • Thanks 1
Link to comment
Share on other sites

Hey pkenewell, I'm still on holiday myself, I had some spare time due to weather so looked through some files.
I wasn't expecting you to do anything whilst your away , but seeing as you have lol i will indeed give it a go when I get back tomorrow

 

  • Like 1
Link to comment
Share on other sites

1 hour ago, Sharper said:

Hey pkenewell, I'm still on holiday myself, I had some spare time due to weather so looked through some files.
I wasn't expecting you to do anything whilst your away , but seeing as you have lol i will indeed give it a go when I get back tomorrow

 

@Sharper Actually I'm back from Holiday now so no problem :). Hope your Holiday is going well. Just let me know how it works when you have time to look at it.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

@pkenewellThis works perfectly.
I can usually see how the programs work, but this work of genius, i can't get my head around... 
I have a couple of projects coming up where this would be used around 10 times each.

 

Thank you my friend

  • Like 2
Link to comment
Share on other sites

16 hours ago, pkenewell said:

@Sharper Hi - I've been on Holiday but didn't forget you :), Please give this code a try. I minimally tested it with the example code you posted and it worked. Alter the path and file extension to suit you needs in the UPDATEGC command. Just in case - I recommend you backup your Gcode file before using.

;; Escape Wildcards  -  Lee Mac
;; Escapes wildcard special characters in a supplied string
(defun LM:escapewildcards ( str )
    (if (wcmatch str "*[-#@.*?~`[`,]*,*`]*")
        (if (wcmatch str "[-#@.*?~`[`,]*,`]*")
            (strcat "`" (substr str 1 1) (LM:escapewildcards (substr str 2)))
            (strcat     (substr str 1 1) (LM:escapewildcards (substr str 2)))
        )
        str
    )
)

(defun ReplaceCodes (file kw1 kw2 / flg fp ln ls n)
   (if (and file (setq file (findfile file)))
      (progn
         (setq fp (open file "r") flg nil)
         (while (setq ln (read-line fp))
            (if (wcmatch ln (strcat "*" (LM:escapewildcards kw1) "*"))
               (setq ln (vl-string-subst kw2 kw1 ln)
                     ls (cons ln ls) flg T
               )
               (setq ls (cons ln ls))
            )
         )
         (close fp)
         (if (and ls flg)
            (progn
               (setq fp (open file "w")
                     ls (reverse ls)
                     n 0
               )
               (repeat (length ls)
                  (write-line (nth n ls) fp)
                  (setq n (1+ n))
               )
               (close fp)
               (princ "\nFile Updated.")
            )
            (princ "\nNo lines found in file that match the pattern. ")
         )
      )
      (princ "\nFile Not found.")
   )
   (princ)
)

(defun c:UpdateGC ()
   (if (setq fil (getfiled "Select Gcode File to Update" "C:\\Myfolder\\" "gco" 4))
      (ReplaceCodes fil "Qualify(1.000,0.0,0.0)" "Qualify(1.000,0.0,0.035)")
   )
)

Thanks to Lee Mac for the "Escape Wildcards" code.

 

I'll be honest, I didn't understand what you were asking for originally 

Now I see this lisp I understand, and it has the gears in my mind turning with possibilities haha

 

Knew you were in good hands.. 

 

Have a great weekend both

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...