简体   繁体   中英

Draftsight LISP, problems building a list of x coordinates with the (cons ) function

I am trying to loop through some rectangles and put the x and y coordinates separately in a list. However the list I am trying to fill looks terrible, the code:

(setq sspnls (ssget '((0 . "LWPOLYLINE"))))
    (while (= 1 (getvar 'cmdactive))
            (command  pause)
            )
    (setq pnlslength (sslength sspnls))
    (setq xlist (list))
    (setq ylist (list)) 
    (setq pnlname(ssname sspnls 0))
    (setq sssort (ssadd))
    (setq tllr 0)
    (while (< tllr pnlslength)
        (setq pnlname(ssname sspnls tllr))
        (Command "hatch" "S" pnlname "")
        (setq xs (cadr (assoc 10 (entget pnlname)))); y cordinate
        (setq ys (caddr (assoc 10 (entget pnlname)))); x cordinate
        (setq xlist (cons xlist xs))
        (setq ylist (cons ylist ys))
        (setq tllr (+ tllr 1))
        
    );while end

The xlist or y list comes out like:

((((nil . 12057.63625954) . 12057.63625954) . 10345.63625954) . 10345.63625954)

I need it to look like:

(12057.63625954 12057.63625954 10345.63625954 10345.63625954)

What am I doing wrong? Please note I am using Draftsight, I also used the append function but the append function does not work at all?

Thanks a lot for your help!

The main issue is that you are using the cons function to create a dotted pair of the list and the X & Y coordinate values, instead of pushing the X & Y coordinate values onto the head of the existing list.

For example, your code is doing this:

_$ (cons '(1 2 3 4) 5)
((1 2 3 4) . 5)

Instead of this:

_$ (cons 5 '(1 2 3 4))
(5 1 2 3 4)

In general, your code could be reduced to the following:

(if (setq sel (ssget '((0 . "LWPOLYLINE"))))
    (repeat (setq idx (sslength sel))
        (setq idx (1- idx)
              ent (ssname sel idx)
              enx (entget ent)
              xls (cons (cadr  (assoc 10 enx)) xls)
              yls (cons (caddr (assoc 10 enx)) yls)
        )
        (command "_.hatch" "_S" ent "")
    )
)

Note also that you are only retrieving the coordinates of the first polyline vertex - I'm unsure whether or not this is intentional.

Was a bit hesitant to try at first but CMS IntelliCAD turned out to be a good alternative for me. The free trial worked immediately, which was good to try it out before paying for it. All the features it offers are user-friendly and easy to work with, which was another win for me. Good job.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM