Jump to content

Add list to existing list


aaryan

Recommended Posts

Hi All,

 

Please help me to solve this problem.

I am trying to write a lisp routine in which it will read lines from txt file and go through the test expression and if it meets the condition it will add the lines to an existing list. Shown below is a part of the routine.

 

(setq lst '())
(repeat (length Pdata)
 (setq Ppoint (nth cntr Pdata)
Vpoint (list (car Ppoint) (cadr Ppoint))
Xbat (caddr Ppoint))
  (setq chkoffst
       (distance (vlax-curve-getClosestPointTo Kproute Vpoint)
		 Vpoint
       )
)
  (if (<= chkoffst gap)
    (setq batlst (append (list (car Vpoint) (cadr Vpoint) Xbat) lst)))
 (setq cntr (1+ cntr)))

 

Am i doing it correctly or missing something. I am asking this because I know there should be more than 10 lines which should be added to new list but it contains only one. How?

 

Thanks and Regards

Aaryan

Link to comment
Share on other sites

Please pay attention to variables names:

(setq [color=red]batlst[/color] '()
...
(setq batlst (append [color=red](list[/color] (list (car Vpoint) (cadr Vpoint) Xbat)[color=red])[/color] [color=red]batlst[/color]))

Link to comment
Share on other sites

Sorry Mircea I am back again with almost same problem. Am I missing something here because I am getting all list as nil.

My file is something like this

500,200,23.5,H1

200,220,23.4,H2

500,150,22.8,H3

450,250,21.9,H4

400,300,22.2,H5

600,400,23.3,H6

 

Please Reply.

 

(setq   Ref1lst '()
 	Ref2lst '()
 	Ref3lst '()
 	Ref4lst '()
 	Ref5lst '()
 	Ref6lst '())
 (repeat (length SBPdata)
 (setq SBPpoint (nth contr SBPdata);[color="red"]Easting, Northing and Elevation[/color]
SVPpoint (list (car SBPpoint) (cadr SBPpoint));[color="red"]Easting and Northing[/color]
SBPbat (caddr SBPpoint);[color="red"]Elevation[/color]
Reflect (cadddr SBPpoint);[color="red"]could be anyone from H1, H2, H3, H4, H5 ,H6[/color]
counter 0)
(if (= Reflect "H1")
 (setq Ref1lst (append (list SBPpoint) Ref1lst)))
(if (= Reflect "H2")
 (setq Ref2lst (append (list SBPpoint) Ref2lst)))
(if (= Reflect "H3")
 (setq Ref3lst (append (list SBPpoint) Ref3lst)))
(if (= Reflect "H4")
 (setq Ref4lst (append (list SBPpoint) Ref4lst)))
(if (= Reflect "H5")
 (setq Ref5lst (append (list SBPpoint) Ref5lst)))
(if (= Reflect "H6")
 (setq Ref6lst (append (list SBPpoint) Ref6lst)))
(setq contr (1+ contr)))

 

Regards

Aaryan

Link to comment
Share on other sites

Again, please pay attention to the names of variables. Second, the counter should be defined when you use it, and also should be initiated outside cycle. If initiated inside cycle it will be reset each time and only the first item from list will be evaluated.

[color=red](setq counter 0)
[/color](repeat (length SBPdata)
 (setq SBPpoint (nth [color=red]counter[/color] SBPdata);Easting, Northing and Elevation
SVPpoint (list (car SBPpoint) (cadr SBPpoint));Easting and Northing
SBPbat (caddr SBPpoint);Elevation
Reflect (cadddr SBPpoint));could be anyone from H1, H2, H3, H4, H5 ,H6

(if (= Reflect "H1")
 (setq Ref1lst (append (list SBPpoint) Ref1lst)))
(if (= Reflect "H2")
 (setq Ref2lst (append (list SBPpoint) Ref2lst)))
(if (= Reflect "H3")
 (setq Ref3lst (append (list SBPpoint) Ref3lst)))
(if (= Reflect "H4")
 (setq Ref4lst (append (list SBPpoint) Ref4lst)))
(if (= Reflect "H5")
 (setq Ref5lst (append (list SBPpoint) Ref5lst)))
(if (= Reflect "H6")
 (setq Ref6lst (append (list SBPpoint) Ref6lst)))
(setq [color=red]counter[/color] (1+ [color=red]counter[/color])))

Link to comment
Share on other sites

(setq SBPdata  '("500,200,23.5,H1"
               "200,220,23.4,H2"
               "500,150,22.8,H3"
               "450,250,21.9,H4"
               "400,300,22.2,H5"
               "600,400,23.3,H6")
     Data    '((H1 "Ref1lst")
               (H2 "Ref2lst")
               (H3 "Ref3lst")
               (H4 "Ref4lst")
               (H5 "Ref5lst")
               (H6 "Ref6lst")))

    (foreach
            itm
               SBPdata
           (if (setq fle (assoc (last
                                      (setq lst  (read (strcat
                                                             "("
                                                             (vl-string-translate "," " " itm)
                                                             ")"))))
                                Data))
                 (set (read (cadr fle))
                      (cons (list (car lst) (cadr lst))
                            (eval (read (cadr fle)))
                            ))))

 

EDIT: includes building the point lis

Edited by pBe
Link to comment
Share on other sites

Aaryan, you may optimize a little the above code:

(setq   Ref1lst '()
  Ref2lst '()
  Ref3lst '()
  Ref4lst '()
  Ref5lst '()
  Ref6lst '())
(foreach SBPpoint SBPdata
 (setq SVPpoint (list (car SBPpoint) 
                      (cadr SBPpoint))   ;Easting and Northing
       SBPbat   (caddr SBPpoint)         ;Elevation
       Reflect  (cadddr SBPpoint))       ;could be anyone from H1, H2, H3, H4, H5 ,H6

(cond
 ((= Reflect "H1")
  (setq Ref1lst (append (list SBPpoint) Ref1lst)))
 ((= Reflect "H2")
  (setq Ref2lst (append (list SBPpoint) Ref2lst)))
 ((= Reflect "H3")
  (setq Ref3lst (append (list SBPpoint) Ref3lst)))
 ((= Reflect "H4")
  (setq Ref4lst (append (list SBPpoint) Ref4lst)))
 ((= Reflect "H5")
  (setq Ref5lst (append (list SBPpoint) Ref5lst)))
 ((= Reflect "H6")
  (setq Ref6lst (append (list SBPpoint) Ref6lst)))
)
)

Link to comment
Share on other sites

@pBe Please tell me how will i create a list which contains all H1, H2..seperately.

 

@Mircea I am still getting an empty lists.

 

Please help me as i am stuck only at this point.

 

(while  (setq  SBPinfo (read-line opnSBP))
 (setq SBPdata
                         (cons (read (strcat "(" (vl-string-translate
                                                   ","  " " SBPinfo)
                                             ")"))
                               SBPdata)))
     (close opnSBP)

     (setq counter 0)
 (setq Ref1lst '()
 	Ref2lst '()
 	Ref3lst '()
 	Ref4lst '()
 	Ref5lst '()
 	Ref6lst '())
Below Your codes

 

I actually need to create seperate lists which contains easting,northing,elevation,tag.

eg. Ref1lst should contains (500 200 23.5 H1)

Ref2lst should contains (600 300 23.4 H2)

etc..

 

Regards

Aaryan

Link to comment
Share on other sites

Demo

(defun c:demo ()
(setq Data  '((H1 "Ref1lst") (H2 "Ref2lst") (H3 "Ref3lst")
             (H4 "Ref4lst") (H5 "Ref5lst") (H6 "Ref6lst")))
(if (setq file (getfiled "\nSelect file" "" "txt" 16))
   (progn
         (setq opnSBP (open file "r"))
     (while  (setq  SBPinfo (read-line opnSBP))
  (if (setq fle (assoc (last (setq lst
                                     (read (strcat "(" (vl-string-translate
                "," " " SBPinfo) ")")))) Data))
        (set (read (cadr fle)) lst))
                     )
         (close opnSBP)
         )
   )
;;; for viewing the results only      ;;;
     [color=blue](foreach itm  (mapcar 'cadr data)[/color]
[color=blue] (print (eval (Read itm)))(princ itm))[/color]
(princ)
     )

 

 

Ref1lst should contains (500 200 23.5 H1)

 

On your example

First/2nd element are integers

3rd element is a real number

Last number is a symbol

 

And will there be more than 1 H1/H2..... so will that be ((500 200 23.5 H1)(552 2050 14.5 H1)....?

Listen, before assigning values to a list, tell us, what are you planning to do with it.

Link to comment
Share on other sites

Thanks a Lot pBe its all clear now.

Well i trying to make a lisp routine which will generate a Seabed Profile by asking user to select XYZ file and then the Route along which the profile will be generated.

I have completed the routine and tested it by selecting the straight polyline as route but when i select the curvy polyline it generates but messes the polyline with many vertices. Can You Please check it out.

If you want i can share the routine tomorrow morning as my office hours are over now.

 

Regards

Aaryan.

Link to comment
Share on other sites

Sorry for being late pBe.

I understood the problem.

It is actually messing the polyline because of csv file i am selecting as the coordinates in csv file are not according to the selected polyline vertices coordinates along which the profile will be drawn.

I have attached the drawing and the csv file.

Test.dwg

Test.csv

Can i sort the csv file as the coordinates of polyline (i.e in ascending order).

 

Thanks and Regards

Aaryan

Test.dwg

Link to comment
Share on other sites

What happen to the last item aaryan? H1,H2....

Where would the individual variable name comes to play?

Sorting is easy.

 

On your sample csv.

 

(defun c:demo ( / pts)
(if (setq pts nil file (getfiled "\nSelect file" "" "csv" 16))
   (progn
      (setq opnSBP (open file "r"))
     (while  (setq  SBPinfo (read-line opnSBP))
  (if (setq fle (setq lst (read (strcat "(" (vl-string-translate
                "," " " SBPinfo) ")"))))
        (setq pts (cons fle pts)))
                     )
         (close opnSBP)
      (command  "_PLine"
           (mapcar 'command
                   (vl-sort pts
                         (function
                               (lambda (x y)
                                     (< (car x) (car y)))))))
         )
   )
     )

Link to comment
Share on other sites

What happen to the last item aaryan? H1,H2....

Where would the individual variable name comes to play?

Actually the symbol was an additional inputs for the profile as reflectors which I stopped as i found errors on my routine, without rectifying this error i could not add those symbol profile.

 

Sorting is easy.

Thanks a Lot

 

Regards

Aaryan.

Edited by aaryan
Link to comment
Share on other sites

Thanks a Lot without rectifying this error i could not add those symbol profile.

 

What error are you refering to? sorting?

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