Jump to content

Recommended Posts

Posted (edited)

HI all!!

I have written a LISP routine to change certain text. However.... if one of the items that I want to change does not exist in the drawings the lisp routine doesn't work. Can some one tell me where I have gone wrong?

 

My work computer wont allow me to add the file so this is a portion of the file....

 

(defun C:GHJ ; = Text Replace for Complete text/mtext strings, pre-defined content
 (/ tss tdata)
 (setq tss (ssget "X" (list (cons 1 "*0*VERTICAL BEND"))))
 (repeat (sslength tss)
   (setq
     tdata (entget (ssname tss 0))
     tdata (subst (cons 1 "JOINT DEFLECTION") (assoc 1 tdata) tdata)
   ); end setq
   (entmod tdata)
   (ssdel (ssname tss 0) tss)
 ); end repeat
 (setq tss (ssget "X" (list (cons 1 "*4*VERTICAL BEND"))))
 (repeat (sslength tss)
   (setq
     tdata (entget (ssname tss 0))
     tdata (subst (cons 1 "2 JOINT DEFLECTIONs") (assoc 1 tdata) tdata)
   ); end setq
   (entmod tdata)
   (ssdel (ssname tss 0) tss)
 ); end repeat
); end defun

 

If the first instance isn't met, the second instance wont work.

Edited by SLW210
Posted

If the first instance isn't met, the second instance wont work.

 

That's because you're not using any code logic to evaluate if a dependent criteria has been met prior to processing... Consider this example:

(defun c:FOO (/ ss i eData asso)
 (if (setq ss (ssget "_x" '((1 . "*0*VERTICAL BEND,*4*VERTICAL BEND"))))
   (repeat (setq i (sslength ss))
     (entmod
       (subst (cons 1
                    (if (wcmatch (strcase (cdr (setq asso (assoc 1
                                                                 (setq eData
                                                                        (entget (ssname ss (setq i (1- i)))
                                                                        )
                                                                 )
                                                          )
                                               )
                                          )
                                 )
                                 "*0*VERTICAL BEND"
                        )
                      "JOINT DEFLECTION"
                      "2 JOINT DEFLECTIONs"
                    )
              )
              asso
              eData
       )
     )
   )
 )
 (princ)
)

 

Also worthy of note, is that DXF 1 is case sensitive, so be sure to account for that as well.

Posted

In addition to BlackBox's suggestions, I would recommend the following:

([color=BLUE]defun[/color] c:ghj ( [color=BLUE]/[/color] enx idx sel str )
   ([color=BLUE]if[/color] ([color=BLUE]setq[/color] sel ([color=BLUE]ssget[/color] [color=MAROON]"_X"[/color] '((0 . [color=MAROON]"TEXT,MTEXT"[/color]) (1 . [color=MAROON]"*[04]*VERTICAL BEND"[/color]))))
       ([color=BLUE]repeat[/color] ([color=BLUE]setq[/color] idx ([color=BLUE]sslength[/color] sel))
           ([color=BLUE]setq[/color] enx ([color=BLUE]entget[/color] ([color=BLUE]ssname[/color] sel ([color=BLUE]setq[/color] idx ([color=BLUE]1-[/color] idx))))
                 str ([color=BLUE]assoc[/color] 1 enx)
           )
           ([color=BLUE]entmod[/color]
               ([color=BLUE]subst[/color]
                   ([color=BLUE]if[/color] ([color=BLUE]wcmatch[/color] ([color=BLUE]strcase[/color] ([color=BLUE]cdr[/color] str)) [color=MAROON]"*0*"[/color])
                      '(1 . [color=MAROON]"JOINT DEFLECTION"[/color])
                      '(1 . [color=MAROON]"2 JOINT DEFLECTIONs"[/color])
                   )
                   str enx
               )
           )
       )
   )
   ([color=BLUE]princ[/color])
)

Posted

Thanks guys that works great.

Would I need to add additional if commands for more instances?

e.g.

"0*VERTICAL BEND" => "JOINT DEFLECTION"

"-0*VERTICAL BEND" => "JOINT DEFLECTION"

"1*VERTICAL BEND" => "JOINT DEFLECTION"

"-1*VERTICAL BEND" => "JOINT DEFLECTION"

"2*VERTICAL BEND" => "JOINT DEFLECTION"

"-2*VERTICAL BEND" => "JOINT DEFLECTION"

"3*VERTICAL BEND" => "JOINT DEFLECTION"

"-3*VERTICAL BEND" => "JOINT DEFLECTION"

"4*VERTICAL BEND" => "2 JOINT DEFLECTIONS"

"-4*VERTICAL BEND" => "2 JOINT DEFLECTIONS"

"5*VERTICAL BEND" => "2 JOINT DEFLECTIONS"

"-5*VERTICAL BEND" => "2 JOINT DEFLECTIONS"

"6*VERTICAL BEND" => "2 JOINT DEFLECTIONS"

"-6*VERTICAL BEND" => "2 JOINT DEFLECTIONS"

"7*VERTICAL BEND" => "11.25%%D VERTICAL BEND"

"-7*VERTICAL BEND" => "11.25%%D VERTICAL BEND"

"8*VERTICAL BEND" => "11.25%%D VERTICAL BEND"

"-8*VERTICAL BEND" => "11.25%%D VERTICAL BEND"

"9*VERTICAL BEND" => "11.25%%D VERTICAL BEND"

"-9*VERTICAL BEND" => "11.25%%D VERTICAL BEND"

"10*VERTICAL BEND" => "11.25%%D VERTICAL BEND"

"-10*VERTICAL BEND" => "11.25%%D VERTICAL BEND"

"11*VERTICAL BEND" => "11.25%%D VERTICAL BEND"

"-11*VERTICAL BEND" => "11.25%%D VERTICAL BEND"

"12*VERTICAL BEND" => "11.25%%D VERTICAL BEND"

"-12*VERTICAL BEND" => "11.25%%D VERTICAL BEND"

"13*VERTICAL BEND" => "11.25%%D VERTICAL BEND"

"-13*VERTICAL BEND" => "11.25%%D VERTICAL BEND"

"14*VERTICAL BEND" => "11.25%%D VERTICAL BEND"

"-14*VERTICAL BEND" => "11.25%%D VERTICAL BEND"

"15*VERTICAL BEND" => "11.25%%D VERTICAL BEND"

"-15*VERTICAL BEND" => "11.25%%D VERTICAL BEND"

"16*VERTICAL BEND" => "11.25%%D VERTICAL BEND"

"-16*VERTICAL BEND" => "11.25%%D VERTICAL BEND"

 

I can change the colour of the "11.25%%D VERTICAL BEND" by the following at the end of the lisp routine.

 

(setq tss (ssget "X" (list (cons 1 "11.25%%D VERTICAL BEND"))))

(repeat (sslength tss)

(command "chprop" tss "" "c" "6" "")

(entmod tdata)

(ssdel (ssname tss 0) tss)

); end repeat

Posted

([color=BLUE]if[/color] ([color=BLUE]setq[/color] sel ([color=BLUE]ssget[/color] [color=MAROON]"_X"[/color] '((0 . [color=MAROON]"TEXT,MTEXT"[/color]) (1 . [color=MAROON]"*[04]*VERTICAL BEND"[/color]))))

 

Doh!... I forgot about [].

 

Also, why not:

           (entmod
               (subst
                 (cons 1
                       (if (wcmatch (cdr str) "*0*")
                         "JOINT DEFLECTION"
                         "2 JOINT DEFLECTIONs"
                       )
                 )
                 str
                 enx
               )
           )

Posted (edited)

Whats the character after "1"

"1*VERTICAL BEND"

 

You realize there will be complications with wcmatch when differentiating between this two.

 

"1*VERTICAL BEND"

"15*VERTICAL BEND"

 

Unless of course we start from the highest number [in this case 16]

 

(defun c:ghj  (/ enx idx sel str [color="blue"]f n _Str[/color])
     (if (setq sel (ssget "_X"
                          '((0 . "TEXT,MTEXT")
                            (1 . "*VERTICAL BEND"))))
           (repeat (setq idx (sslength sel))
                 (setq enx  (entget (ssname sel (setq idx (1- idx))))
                       str  (assoc 1 enx)
                      [color="blue"] _str (Cdr str)[/color])
                [color="blue"] (setq f nil
                       n 16)
                 (while (and (not f) (> n -1))
                       (setq f    (vl-some
                                        '(lambda (k)
                                               (if   (wcmatch
                                                           _str
                                                           (strcat
                                                                 k
                                                                 "*VERTICAL BEND"))
                                                          (atoi k)))
                                        (list (itoa n)
                                              (if (zerop n)
                                                    "-0"
                                                    (itoa (- n))))))
                       (setq n (1- n))
                       )
                 (if (setq nstr
                                (cond
                                      ((or (<= 0 f 6)
                                           (<= -6 f -1))
                                       "2 JOINT DEFLECTIONS")
                                      ((or (<= 7 f 16)
                                           (<= -16 f -7))
                                       "11.25%%D VERTICAL BEND")))[/color]
                       (entmod
                             (subst
                                   (cons 1 nstr)
                                   str enx
                                    
                                   )
                             )
                       )
                 )
           )
     (princ)
     )

Edited by pBe
Posted
Also, why not:

           (entmod
               (subst
                 (cons 1
                       (if (wcmatch (cdr str) "*0*")
                         "JOINT DEFLECTION"
                         "2 JOINT DEFLECTIONs"
                       )
                 )
                 str
                 enx
               )
           )

 

Because the cons expression is unnecessary.

Posted
Because the cons expression is unnecessary.

 

... So too is the STRCASE to match *0* (in your original code), no?

 

The CONS may be less keystrokes, but perhaps there's a minor performance hit vs. the duplicate QUOTEd pairs. *shrug*

Posted
... So too is the STRCASE to match *0* (in your original code), no?

 

True - the strcase is indeed unnecessary for this case.

 

The CONS may be less keystrokes, but perhaps there's a minor performance hit vs. the duplicate QUOTEd pairs. *shrug*

 

Only one of the quoted pairs will be evaluated depending on result of the test expression.

Posted

Only one of the quoted pairs will be evaluated depending on result of the test expression.

 

No... I meant my performance (fat-finger typing). :rofl:

Posted

Thanks for the help. It worked perfectly until I added a few more parameters.

Now I get an error when I try to load it.

(defun c:ghj  (/ enx idx sel str f n _Str)
     (if (setq sel (ssget "_X"
                          '((0 . "TEXT,MTEXT")
                            (1 . "*VERTICAL BEND"))))
           (repeat (setq idx (sslength sel))
                 (setq enx  (entget (ssname sel (setq idx (1- idx))))
                       str  (assoc 1 enx)
                       _str (Cdr str))
                 (setq f nil
                       n 16)
                 (while (and (not f) (> n -1))
                       (setq f    (vl-some
                                        '(lambda (k)
                                               (if   (wcmatch
                                                           _str
                                                           (strcat
                                                                 k
                                                                 "*VERTICAL BEND"))
                                                          (atoi k)))
                                        (list (itoa n)
                                              (if (zerop n)
                                                    "-0"
                                                    (itoa (- n))))))
                       (setq n (1- n))
                       )
                 (if (setq nstr
                                (cond
                                      ((or (<= 0 f 3)
                                           (<= -3 f -1))
                                       "JOINT DEFLECTION")
                                      ((or (<= 4 f 6)
                                           (<= -6 f -4))
                                       "2 JOINT DEFLECTIONS")
                                      ((or (<= 7 f 16)
                                           (<= -16 f -7))
                                       "11.25%%D VERTICAL BEND")))
                                      ((or (<= 17 f 28)
                                           (<= -28 f -17))
                                       "22.5%%D VERTICAL BEND")))
                                      ((or (<=39 f 51)
                                           (<= -51 f -39))
                                       "45%%D VERTICAL BEND")))
                       (entmod
                             (subst
                                   (cons 1 nstr)
                                   str enx
                                    
                                   )
                             )
                       )
                 )
           )
     (princ)
 (setq tss (ssget "X" (list (cons 1 "*VERTICAL BEND"))))
 (repeat (sslength tss)
   (command "chprop" tss "" "c" "6" "")
   (entmod tdata)
   (ssdel (ssname tss 0) tss)
 ); end repeat
     )

Posted (edited)

Denverado.

 

I have to say, you only replied when you hit a snag otherwise we would've never known how helpful we are here.:lol:

 

Anyhoo. initial observation is too many closing parenthesis on the cond line.

(setq nstr (cond  ((or (<= 0 f 3) (<= -3 f -1))  "JOINT DEFLECTION")
                 ((or (<= 4 f 6) (<= -6 f -4))  "2 JOINT DEFLECTIONS")
                 ((or (<= 7 f 16)  (<= -16 f -7))  "11.25%%D VERTICAL BEND")
                 ((or (<= 17 f 28) (<= -28 f -17))  "22.5%%D VERTICAL BEND")
                 ((or (<=39 f 51)  (<= -51 f -39)) "45%%D VERTICAL BEND"))
                           )

Also

(setq f nil n [b][color="blue"]51[/color][/b])

 

Not sure but try it and tell us how it goes. or better yet post a sample drawing.

Edited by pBe
Posted

Anyhoo. initial observation is too many closing parenthesis on the cond line.

                (<[b]=39[/b] f 51)  
                           

 

One more issue , the space between the equal operator and the number 39 ;)

Posted
One more issue , the space between the equal operator and the number 39 ;)

 

:lol: guess you are right tharwat.. still not sure if it will work. Am too lazy to debug and try the code.

 

Cheers Tharwat

Posted

Thanks everyone for your help.

I was on holidays.... that's why I never responded until I did.

You have all been very helpful.

Posted
Thanks everyone for your help.

I was on holidays.... that's why I never responded until I did.

You have all been very helpful.

 

Wow, that explains it, Good for you. Now did you made it work with the changes on the code?

Posted

Sorry pBe..... Stuck on site for the last week.

I couldn't get it to work.:(

the "22.5 vertical bend" and "45vvertical bend" did not work.

(defun c:ghj  (/ enx idx sel str f n _Str)
     (if (setq sel (ssget "_X"
                          '((0 . "TEXT,MTEXT")
                            (1 . "*VERTICAL BEND"))))
           (repeat (setq idx (sslength sel))
                 (setq enx  (entget (ssname sel (setq idx (1- idx))))
                       str  (assoc 1 enx)
                       _str (Cdr str))
                 (setq f nil
                       n 16)
                 (while (and (not f) (> n -1))
                       (setq f    (vl-some
                                        '(lambda (k)
                                               (if   (wcmatch
                                                           _str
                                                           (strcat
                                                                 k
                                                                 "*VERTICAL BEND"))
                                                          (atoi k)))
                                        (list (itoa n)
                                              (if (zerop n)
                                                    "-0"
                                                    (itoa (- n))))))
                       (setq n (1- n))
                       )
                 (if (setq nstr (cond  ((or (<= 0 f 3) (<= -3 f -1))  "JOINT DEFLECTION")
                                       ((or (<= 4 f 6) (<= -6 f -4))  "2 JOINT DEFLECTIONS")
                                       ((or (<= 7 f 16)  (<= -16 f -7))  "11.25%%D VERTICAL BEND")
                                       ((or (<= 17 f 28) (<= -28 f -17))  "22.5%%D VERTICAL BEND")
                                       ((or (<= 39 f 51)  (<= -51 f -39)) "45%%D VERTICAL BEND"))
                           )
                       (entmod
                             (subst
                                   (cons 1 nstr)
                                   str enx
                                    
                                   )
                             )
                       )
                 )
           )
     (princ)
;
; horizontal bend change
;
 (setq tss (ssget "X" (list (cons 1 "*HORIZONTAL BEND"))))
 (repeat (sslength tss)
   (command "-layer" "n" "DEFPOINTS" "")
   (command "chprop" tss "" "LA" "DEFPOINTS" "")
   (entmod tdata)
   (ssdel (ssname tss 0) tss)
 ); end repeat
;
; vertical bend change
;
 (setq tss (ssget "X" (list (cons 1 "*VERTICAL BEND"))))
 (repeat (sslength tss)
   (command "chprop" tss "" "c" "6" "")
   (entmod tdata)
   (ssdel (ssname tss 0) tss)
 ); end repeat
     )

I would upload an acad file as well but work IT wont let me!:x

Posted (edited)

Again here:

(setq f nil n [b]51[/b])

 

I noticed you skipped 29 to 38? why is that? and if it should be included what is the new string then?

 

EDIT:

(defun c:ghj  (/ enx idx sel str f n _Str)
(if (setq sel (ssget "_X"  '((0 . "TEXT,MTEXT")
                            (1 . "*VERTICAL BEND[color="blue"],*HORIZONTAL BEND"[/color]))))
           (repeat (setq idx (sslength sel))
                 (setq enx  (entget (ssname sel (setq idx (1- idx))))
                       str  (assoc 1 enx)  
                       _str (Cdr str))
                 (setq f nil  n[b][color="blue"] 51[/color][/b])
          [color="blue"] (if (wcmatch _str "*HORIZONTAL BEND")
               (entmod (append enx (list (cons 8 "Defpoints"))))[/color]
               (progn  
		(while (and (not f) (> n -1))
                       (setq f (vl-some
                                        '(lambda (k)
                                               (if   (wcmatch
                                                           _str
                                                           (strcat
                                                                 k
                                                                 "*VERTICAL BEND"))
                                                          (atoi k)))
                                        (list (itoa n)
                                              (if (zerop n)
                                                    "-0"
                                                    (itoa (- n))))))
                       (setq n (1- n))
                       )
                 (if  (and f
                      (setq nstr (cond  ((or (<= 0 f 3) (<= -3 f -1))  "JOINT DEFLECTION")
                                       ((or (<= 4 f 6) (<= -6 f -4))  "2 JOINT DEFLECTIONS")
                                       ((or (<= 7 f 16)  (<= -16 f -7))  "11.25%%D VERTICAL BEND")
                                       ((or (<= 17 f 28) (<= -28 f -17))  "22.5%%D VERTICAL BEND")
                                       ((or (<= 39 f 51)  (<= -51 f -39)) "45%%D VERTICAL BEND"))
                           ))
                       (setq enx (entmod (subst (cons 1 nstr)  str enx))
                          [color="blue"] [b]  enx (entmod (append enx (list (cons 62 6))))[/b][/color])
			)
                       )
                 )
           )
         )
     (princ)
)

 

Still doesnt account for numbers 29 to 38.

Edited by pBe
Posted

Thanks pBe,

I ended up doing the following to make it work right.

(defun c:ghj  (/ enx idx sel str f n _Str)
     (if (setq sel (ssget "_X"
                          '((0 . "TEXT,MTEXT")
                            (1 . "*VERTICAL BEND"))))
           (repeat (setq idx (sslength sel))
                 (setq enx  (entget (ssname sel (setq idx (1- idx))))
                       str  (assoc 1 enx)
                       _str (Cdr str))
                 (setq f nil
                       n 92)
                 (while (and (not f) (> n -1))
                       (setq f    (vl-some
                                        '(lambda (k)
                                               (if   (wcmatch
                                                           _str
                                                           (strcat
                                                                 k
                                                                 "*VERTICAL BEND"))
                                                          (atoi k)))
                                        (list (itoa n)
                                              (if (zerop n)
                                                    "-0"
                                                    (itoa (- n))))))
                       (setq n (1- n))
                       )
                 (if (setq nstr (cond  ((or (<= 0 f 3) (<= -3 f -1))  "JOINT DEFLECTION")
                                       ((or (<= 4 f 6) (<= -6 f -4))  "2 JOINT DEFLECTIONS")
                                       ((or (<= 7 f 16)  (<= -16 f -7))  "11.25%%D VERTICAL BEND")
                                       ((or (<= 17 f 28) (<= -28 f -17))  "22.5%%D VERTICAL BEND")
                                       ((or (<= 29 f 38)  (<= -38 f -29))  "CHECK VERTICAL BEND")
                                       ((or (<= 39 f 51)  (<= -51 f -39)) "45%%D VERTICAL BEND")
                                       ((or (<= 52 f 88)  (<= -88 f -52)) "CHECK VERTICAL BEND")
                                       ((or (<= 89 f 91)  (<= -91 f -89)) "90%%D VERTICAL BEND"))
                           )
                       (entmod
                             (subst
                                   (cons 1 nstr)
                                   str enx
                                    
                                   )
                             )
                       )
                 )
           )
     (princ)
;
; horizontal bend change
;
 (setq tss (ssget "X" (list (cons 1 "*HORIZONTAL BEND"))))
 (repeat (sslength tss)
   (command "-layer" "n" "DEFPOINTS" "")
   (command "chprop" tss "" "LA" "DEFPOINTS" "")
   (entmod tdata)
   (ssdel (ssname tss 0) tss)
 ); end repeat
;
; vertical bend change
;
 (setq tss (ssget "X" (list (cons 1 "*VERTICAL BEND"))))
 (repeat (sslength tss)
   (command "chprop" tss "" "c" "6" "")
   (entmod tdata)
   (ssdel (ssname tss 0) tss)
 ); end repeat
     )

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...