Author Topic: issues changing 1 item in a list of multiple items with identical items in list  (Read 5454 times)

0 Members and 1 Guest are viewing this topic.

andrew_nao

  • Guest
in my test list, i have the word "hello" in 3 positions
(62 292 384)

when i load this code, it changes the last position and not the first one.

i tried reversing the list but it returns the position of another item and not the item im looking for.

anyone have any clue as to what im doing wrong or why its changing the last position and not the first?

Code: [Select]
(setq en (ssget "X" (list (cons 1 (NTH (VL-POSITION "HELLO" MAINLST) MAINLST)))))
(setq ent (ssname en 0))
(SETQ EN (ENTGET ENT))
(SETQ TXTO (CDR (ASSOC 1 EN)))
(SETQ TXTN "-")
(setq EN (subst (cons 1 TXTN) (cons 1 TXTO) EN))
(entmod EN)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
I you have the position use this:
Code: [Select]
; Function: ALE_List_SubstNth
;
; Version 2.02 - 15/02/2008 > old name: ALE_SubstNth
; Version 2.01 - 16/06/2007
; Version 1.01 - 20/12/2004 > old name: ALE_Subst_Nth
;
; Description:
;   returns a copy of the list with a new item substituted in place
;   of the item in the nth position (NthPos)
;
; Arguments:
;   NthPos = Integer - nth like
;   NewItm = An atom or list
;   In_Lst = A list
;   InRLst = Original list reversed
;
; Return Values:
;   A list with a new item substituted in place of the item in the
;   nth position
;
; Examples:
;   (setq alist '((0 . "A") (1 . "B") nil (3 . "D") (4 . "E") nil))
;
;   (ALE_List_SubstNth 0 "NEW" alist (reverse alist))
;   => ("NEW" (1 . "B") nil (3 . "D") (4 . "E") nil)
;
;   (ALE_List_SubstNth 2 "NEW" alist (reverse alist))
;   => ((0 . "A") (1 . "B") "NEW" (3 . "D") (4 . "E") nil)
;
;   (ALE_List_SubstNth 4 "NEW" alist (reverse alist))
;   => ((0 . "A") (1 . "B") nil (3 . "D") "NEW" nil)
;
;   (ALE_List_SubstNth 5 '(9 . Z) alist (reverse alist))
;   => ((0 . "A") (1 . "B") nil (3 . "D") (4 . "E") (9 . Z))
;
;   (ALE_List_SubstNth 6 '(9 . Z) alist (reverse alist))
;   => ((0 . "A") (1 . "B") nil (3 . "D") (4 . "E") nil)
;
;   (ALE_List_SubstNth 1 nil alist (reverse alist))
;   => ((0 . "A") nil nil (3 . "D") (4 . "E") nil)
;
(defun ALE_List_SubstNth (NthPos NewItm In_Lst InRLst / LstLng OldItm)
  (cond
    ( (null In_Lst)                                nil )
    ( (zerop NthPos)        (cons NewItm (cdr In_Lst)) )
    ( (<= (setq LstLng (length In_Lst)) NthPos) In_Lst )
    ( (zerop (setq LstLng (- LstLng (1+ NthPos))))
      (append (reverse (cdr InRLst)) (list NewItm))
    )
    ( T
      (setq OldItm (nth NthPos In_Lst))
      (while
        (/=
          NthPos
          (length (setq InRLst (cdr (member OldItm InRLst))))
        )
      )
      (while
        (/=
          LstLng
          (length (setq In_Lst (cdr (member OldItm In_Lst))))
        )
      )
      (append (reverse InRLst) (cons NewItm In_Lst))
    )
  )
)

ronjonp

  • Needs a day job
  • Posts: 7527
What is in 'MAINLST' ?

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

andrew_nao

  • Guest
What is in 'MAINLST' ?

that is my list that im trying to change out specific items in that list that corresponds to the placement of text on screen
« Last Edit: July 29, 2014, 12:20:53 PM by andrew_nao »

andrew_nao

  • Guest
I you have the position use this:
Code: [Select]
; Function: ALE_List_SubstNth
;
; Version 2.02 - 15/02/2008 > old name: ALE_SubstNth
; Version 2.01 - 16/06/2007
; Version 1.01 - 20/12/2004 > old name: ALE_Subst_Nth
;
; Description:
;   returns a copy of the list with a new item substituted in place
;   of the item in the nth position (NthPos)
;
; Arguments:
;   NthPos = Integer - nth like
;   NewItm = An atom or list
;   In_Lst = A list
;   InRLst = Original list reversed
;
; Return Values:
;   A list with a new item substituted in place of the item in the
;   nth position
;
; Examples:
;   (setq alist '((0 . "A") (1 . "B") nil (3 . "D") (4 . "E") nil))
;
;   (ALE_List_SubstNth 0 "NEW" alist (reverse alist))
;   => ("NEW" (1 . "B") nil (3 . "D") (4 . "E") nil)
;
;   (ALE_List_SubstNth 2 "NEW" alist (reverse alist))
;   => ((0 . "A") (1 . "B") "NEW" (3 . "D") (4 . "E") nil)
;
;   (ALE_List_SubstNth 4 "NEW" alist (reverse alist))
;   => ((0 . "A") (1 . "B") nil (3 . "D") "NEW" nil)
;
;   (ALE_List_SubstNth 5 '(9 . Z) alist (reverse alist))
;   => ((0 . "A") (1 . "B") nil (3 . "D") (4 . "E") (9 . Z))
;
;   (ALE_List_SubstNth 6 '(9 . Z) alist (reverse alist))
;   => ((0 . "A") (1 . "B") nil (3 . "D") (4 . "E") nil)
;
;   (ALE_List_SubstNth 1 nil alist (reverse alist))
;   => ((0 . "A") nil nil (3 . "D") (4 . "E") nil)
;
(defun ALE_List_SubstNth (NthPos NewItm In_Lst InRLst / LstLng OldItm)
  (cond
    ( (null In_Lst)                                nil )
    ( (zerop NthPos)        (cons NewItm (cdr In_Lst)) )
    ( (<= (setq LstLng (length In_Lst)) NthPos) In_Lst )
    ( (zerop (setq LstLng (- LstLng (1+ NthPos))))
      (append (reverse (cdr InRLst)) (list NewItm))
    )
    ( T
      (setq OldItm (nth NthPos In_Lst))
      (while
        (/=
          NthPos
          (length (setq InRLst (cdr (member OldItm InRLst))))
        )
      )
      (while
        (/=
          LstLng
          (length (setq In_Lst (cdr (member OldItm In_Lst))))
        )
      )
      (append (reverse InRLst) (cons NewItm In_Lst))
    )
  )
)

thanks for your reply however im trying to actually change the entity in my drawing not just in a list.

ronjonp

  • Needs a day job
  • Posts: 7527
What is in 'MAINLST' ?

that is my list that im trying to change out specific items in that list


So it's a list of strings ? If so, you have to have some way to tie the text to the item you're trying to modify.
« Last Edit: July 29, 2014, 12:26:16 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Andrew,
Why do you turn these post into an Easter egg hunt?

Please, please post some example of what you are after. The code snippet is too vague so we all scratch our heads and play your guessing game.
Time to wise up and help up help you on the first go around.

Thanks
CAB


I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

andrew_nao

  • Guest
Hi CAB,
I dont have any full code to post, i only have snippets. im sorry.

i do things 1 step at a time so i can understand what its supposed to do and learn while i do them.
when they dont cooperate like i think they should, i ask the experts here where im going wrong.

i apologize for being vague, i try my best to post what it is im doing by giving examples as most time i dont know how to explain what it is im after because most time its indepth and would take a bit to explain and i try to keep it brief so its not passed over, cause no one has 6 hours to read it. (figuratively speaking)

i thought me posting this code would be simple for the experts to understand what im trying to do.

can you delete this post and i will make another with a better explaination.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
No need to delete the thread just give an example of the list MAINLST & the entity EN your are dealing with.

People like to help when they can. 8)



I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
This is a strange bit of code:
Code: [Select]
(NTH (VL-POSITION "HELLO" MAINLST) MAINLST)If mainLst contains "HELLO" it will always return "HELLO".

andrew_nao

  • Guest
This is a strange bit of code:
Code: [Select]
(NTH (VL-POSITION "HELLO" MAINLST) MAINLST)If mainLst contains "HELLO" it will always return "HELLO".

i was playing around and just never fixed it before i posted it
i was trying to find the position of the word in a list and then use that nth in a selection set to entmod it.

andrew_nao

  • Guest
ok here is, i hope, a better explaination.
ive attached some pics and a sample dwg.

i have another lisp that takes the text in my BOM, makes a list called (mainlst) and then writes this list to a text file for another external program to process.

im attempting to edit the mainlst at the same time edit the text fields in my BOM.
once i get the text fields to change to my desire, i think i got the actual editing of the mainlst before its written to a text file nailed down.

what im after:
in my BOM when the inches field has a number in it
and the field under "test1" for item 1 has any number other than 93 in as the first 2 digits
i want to dash out the ft and inches field, take the numbers in ft and inches, add them together and place it at the end of "T.O.E." field
(see before and after pics)
or if there is a dash where "T.O.E." is, to replace the dash with the added number.

this would of course have to step through the whole list and change the each of fields accordingly.

i have the code to compare the inches and the first 2 digits if they arent 93, take the numbers in the ft and inches field and add them up.

my approach:
ive try to get the nth of the item in the list and entmod it by that way. it however doesnt change the nth, it grabs a random nth of the same value.
example:
Code: [Select]
(nth 60 mainlst)
will return the whatever is listed from mainlst that corresponds with the inches field for item 1 in the BOM
so if the number is 1 3/4 and somewhere else there is a 1 3/4 it will change that instead of the (nth 60 mainlst)

my issue:
getting the fields to change if the conditions are met then swap out the fields in the list before writting it to a file.
like i said i think i got the changing of the list before its written to a file down.

ive posted the code in the first post here of what i was attempting to use, most likely im approaching this the wrong way.

im looking for guidance on how to get to where im going.
im not looking for anyone to write major code for me, maybe a couple lines to help me understand where i went wrong.


i apologize for my vague posts and i hope this paints a more clear picture.

andrew_nao

  • Guest
ok i got it to do what i want. however...

in the code below, this line this is causing a repeat of the last entmod, on every line after the last number found.
Code: [Select]
(SETQ MAINLST (SUBST TXTN3 (NTH (+ ITMNUM 13) MAINLST) MAINLST))


if you notice in the new before and after pics attached, the 15/16" is repeated 3 more times when it shouldnt be repeated at all.

i would appreciate it if a fresh set of eyes could tell me why this is going on and what i can do to fix it.
thanks


Code: [Select]

(SETQ LSTLEN (ATOI (NTH (- (LENGTH MAINLST) 23) MAINLST)))
(SETQ ITMNUM 60) ;+ 23
(SETQ DS (GETVAR "DIMSCALE"))

(setq elist (entget (ssname (ssget "x" (list '(0 . "INSERT") '(2 . "bmhead*"))) 0)))
(setq ip (cdr (assoc 10 elist)))
(setq xcord 1.0197)

(REPEAT LSTLEN
(IF (AND (/= (NTH ITMNUM MAINLST) "-") (/= (SUBSTR (NTH (+ ITMNUM 8) MAINLST) 1 2) "93"))
;;IF YES
(PROGN

(setq INP (list (+ (* ds (car ip)))(+ (* ds  (- (cadr ip) xcord)))))
    (setq gp1 (list (- (car inp) (* ds 5.1929))(- (cadr inp) (* ds 0.0075))))
    (setq gp2 (list (- (car inp) (* ds 4.5336))(- (cadr inp) (* ds 0.1676))))
(setq gp3 (list (- (car inp) (* ds 5.4999))(- (cadr inp) (* ds 0.0075))))
    (setq gp4 (list (- (car inp) (* ds 5.2143))(- (cadr inp) (* ds 0.1676))))
(setq gp5 (list (- (car inp) (* ds 3.5607))(- (cadr inp) (* ds 0.2564))))
    (setq gp6 (list (- (car inp) (* ds 3.2977))(- (cadr inp) (* ds 0.3532))))


(setq feet (ssget "c" gp3 gp4 '((0 . "TEXT"))))
(if (/= (cdr (assoc 1 (entget (ssname FEET 0)))) "-")
       (SETQ FT_UNITS (* 12 (ATOI (cdr (assoc 1 (entget (ssname FEET 0)))))))
    )

(setq ent1 (ssname FEET 0))
      (SETQ EN1 (ENTGET ENT1))
      (SETQ TXTO1 (CDR (ASSOC 1 EN1)))
      (SETQ TXTN1 "-")
      (setq EN1 (subst (cons 1 TXTN1) (cons 1 TXTO1) EN1))
      (entmod EN1)


    (setq inches (ssget "c" gp1 gp2 '((0 . "TEXT"))))
(IF (/= (cdr (assoc 1 (entget (ssname inches 0)))) "-")

       (SETQ IN_UNITS (DISTOF (cdr (assoc 1 (entget (ssname inches 0)))) 2))
    )

(setq ent2 (ssname INCHES 0))
      (SETQ EN2 (ENTGET ENT2))
      (SETQ TXTO2 (CDR (ASSOC 1 EN2)))
      (SETQ TXTN2 "-")
      (setq EN2 (subst (cons 1 TXTN2) (cons 1 TXTO2) EN2))
      (entmod EN2)


(SETQ TOTAL (+ FT_UNITS IN_UNITS))

(setq remarks2 (ssget "c" gp5 gp6 '((0 . "TEXT"))))
  (setq ent3 (ssname REMARKS2 0))
      (SETQ EN3 (ENTGET ENT3))
      (SETQ TXTO3 (CDR (ASSOC 1 EN3)))
(if (= (cdr (assoc 1 (entget (ssname REMARKS2 0)))) "-")
            (SETQ TXTN3 (STRCAT (RTOS TOTAL 5) "\" LG"))
(SETQ TXTN3 (STRCAT (cdr (assoc 1 (entget (ssname REMARKS2 0))))
                    ", "
(RTOS TOTAL 5)
"\" LG"
)
)
)
      (setq EN3 (subst (cons 1 TXTN3) (cons 1 TXTO3) EN3))
      (entmod EN3)


    (SETQ MAINLST (SUBST TXTN1 (NTH (- ITMNUM 1) MAINLST) MAINLST))
(SETQ MAINLST (SUBST TXTN2 (NTH ITMNUM MAINLST) MAINLST))
    (SETQ MAINLST (SUBST TXTN3 (NTH (+ ITMNUM 13) MAINLST) MAINLST))

) ;end progn
(progn
;(ALERT "NO")
)
)

(SETQ ITMNUM (+ 23 ITMNUM))
(setq xcord (+ xcord 0.375))
 )


roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
CAB has already asked for an example of the MAINLST. Why don't you supply it? Without it nobody can test your code.

andrew_nao

  • Guest
CAB has already asked for an example of the MAINLST. Why don't you supply it? Without it nobody can test your code.

my mistake, i though i gave the code with the sample dwg and pictures in the previous post

ive attached the file that creates the list.

thanks