Jump to content

Sort table whith block attributes


henrique

Recommended Posts

 

I have a list with block attributes "L1 320 12" "L2 270 5" "L3 320 12" ..... "L10 320 12" "L11 330 12" .... "L20 400 18"

I need to sort by the first item: 

 

"L1 320 12" "L10 320 12" "L11 330 12"  "L2 270 5" "L20 400 18" "L3 320 12"

 

but I need

 

"L1 320 12" "L2 270 5" "L3 320 12".... "L10 320 12" "L11 330 12"..... "L20 400 18" 

 

image.thumb.png.7ccc2332cf84e9466e6b032a06ef8007.png

 

(defun c:tlaje (/ lst int sel ent ord qty itm un get tag str ins tbl row col)
  (and
    (if (= (getint "\nDigite ENTER para todos ou 1 seleção manual:") 1)
    (progn 
    (setq int -1 sel (ssget '((0 . "INSERT") (66 . 1))))
    )
    (progn
    (princ "\nSelecione os blocos: ")
    (setq int -1 sel (ssget "x" (list (cons 0 "INSERT") (cons 410 "Model"))))
    )
    )
    (while (setq int (1+ int) ent (ssname sel int))
      (setq ord nil qty nil itm nil)
      (while
        (/= (cdr (assoc 0 (setq get (entget (setq ent (entnext ent))))))
            "SEQEND"
        )
         (setq tag (cdr (assoc 2 get))
               str (cdr (assoc 1 get))
         )
         (vl-some (function (lambda (j k) (and (= tag j) (set k str))))
                  '("CÔMODO" "QTD" "COMPRIMENTO")
                  '(ord qty itm)
         )
      )
      (and ord
           itm
           qty
           (or (vl-some
                 '(lambda (u)
                    (and (eq (car u) ord)
                         (eq (cadr u) itm)
                         (setq
                           lst (subst (list ord
                                            itm
                                            (vl-princ-to-string
                                              (+ (read qty) (read (caddr u)))
                                            )  
                                      )
                                      u
                                      lst
                               )
                         )
                    )
                  )
                 lst
               )
               (setq lst (cons (list ord itm qty) lst))
           )
      )
      lst
    )     
    (setq ins (getpoint "\nSelecione o local da tabela: "))
    (setq tbl (vla-addtable
                (vla-get-modelspace
                  (vla-get-activedocument (vlax-get-acad-object))
                )
                (vlax-3d-point ins)
                (1+ (length lst))
                3
                145
                46
              )
          row 2
          col -1
    )
    (progn
      (mapcar
        '(lambda (w) (vla-setcolumnwidth tbl (setq col (1+ col)) w))
        '(100 100 200)
      )
      (setq col -1)
      (vla-put-RegenerateTableSuppressed tbl :vlax-true)
      (vla-put-Vertcellmargin tbl 10.55)
      (vla-put-Horzcellmargin tbl 10.55)
      (vla-unmergecells tbl 0 0 0 2)
      (mapcar '(lambda (s c)
                 (Set:text:contents_ tbl 0 c s acMiddlecenter)
                 (vla-setrowheight tbl 0 45)
               )
              '(" " "Comprimento" "Qtd.")
              '(0 2 1)
      )
      (setq row 1
            col -1
      )
      (foreach itm 
                   (vl-sort      
                     lst
                     (function
                       (lambda (j k) (< (car j) (car k))) 
      		       ) 
                   ) 
        (mapcar '(lambda (s c a)
                   (Set:text:contents_ tbl row c s (eval a))
                 )
                itm
                '(0 2 1)
                '(acMiddlecenter acMiddleRight acMiddlecenter acMiddlecenter)
        )
        (vla-setrowheight tbl row 1.0)
        (setq row (1+ row)
              col -1
        )
      )
      (vla-put-RegenerateTableSuppressed tbl :vlax-false)
    )
  )
  (princ)
) (vl-load-com)
(defun Set:text:contents_ (o_ r_ c_ v_ a_) 
  (vla-settext o_ r_ c_ (strcat "{\\fcalibri|b0|i0|c0|p34;" v_ "}"))
  (vla-setcelltextheight o_ r_ c_ 18.5)
  (vla-setcellalignment o_ r_ c_ a_) 
)

 

Link to comment
Share on other sites

You need to use VL-sort and sort the list of values to go into the table before putting those values into cells. I redid the list as individual items then it sorts on the alpha characters.

 

(setq lst (list '("L1" 320 12) '("L10" 320 12) '("L11" 330 12) '("L2" 270 5) '("L20" 400 18) '("L3" 320 12)))

; By pbejse forums/autodesk

(defun _n (str / s)
(setq s (vl-string-right-trim "0123456789" str))
(list s (atoi (substr str (1+ (strlen s)))))
)

(Defun ThisFunc (l)
(Vl-sort l'(lambda (n m)
		(setq n (_n (Car n)) m (_n (Car m)))
			(cond
			  ((< (Car n) (car m)))
			  ((eq (Car n) (car m)) (< (Cadr n) (cadr m)))
			)
		)
	 )
)

(setq lst thisfunc lst)

 

(ThisFunc  lst)
(("L1" 320 12) ("L2" 270 5) ("L3" 320 12) ("L10" 320 12) ("L11" 330 12) ("L20" 400 18))

 

I may have another go at Bubblesort an oldy but was written in Basic I converted it to Lisp, but long gone. Should handle the original list.

 

Edited by BIGAL
Link to comment
Share on other sites

On 11/26/2023 at 12:15 AM, henrique said:

 

I have a list with block attributes "L1 320 12" "L2 270 5" "L3 320 12" ..... "L10 320 12" "L11 330 12" .... "L20 400 18"

I need to sort by the first item: 

 

"L1 320 12" "L10 320 12" "L11 330 12"  "L2 270 5" "L20 400 18" "L3 320 12"

 

but I need

 

"L1 320 12" "L2 270 5" "L3 320 12".... "L10 320 12" "L11 330 12"..... "L20 400 18" 

 

image.thumb.png.7ccc2332cf84e9466e6b032a06ef8007.png

 

(defun c:tlaje (/ lst int sel ent ord qty itm un get tag str ins tbl row col)
  (and
    (if (= (getint "\nDigite ENTER para todos ou 1 seleção manual:") 1)
    (progn 
    (setq int -1 sel (ssget '((0 . "INSERT") (66 . 1))))
    )
    (progn
    (princ "\nSelecione os blocos: ")
    (setq int -1 sel (ssget "x" (list (cons 0 "INSERT") (cons 410 "Model"))))
    )
    )
    (while (setq int (1+ int) ent (ssname sel int))
      (setq ord nil qty nil itm nil)
      (while
        (/= (cdr (assoc 0 (setq get (entget (setq ent (entnext ent))))))
            "SEQEND"
        )
         (setq tag (cdr (assoc 2 get))
               str (cdr (assoc 1 get))
         )
         (vl-some (function (lambda (j k) (and (= tag j) (set k str))))
                  '("CÔMODO" "QTD" "COMPRIMENTO")
                  '(ord qty itm)
         )
      )
      (and ord
           itm
           qty
           (or (vl-some
                 '(lambda (u)
                    (and (eq (car u) ord)
                         (eq (cadr u) itm)
                         (setq
                           lst (subst (list ord
                                            itm
                                            (vl-princ-to-string
                                              (+ (read qty) (read (caddr u)))
                                            )  
                                      )
                                      u
                                      lst
                               )
                         )
                    )
                  )
                 lst
               )
               (setq lst (cons (list ord itm qty) lst))
           )
      )
      lst
    )     
    (setq ins (getpoint "\nSelecione o local da tabela: "))
    (setq tbl (vla-addtable
                (vla-get-modelspace
                  (vla-get-activedocument (vlax-get-acad-object))
                )
                (vlax-3d-point ins)
                (1+ (length lst))
                3
                145
                46
              )
          row 2
          col -1
    )
    (progn
      (mapcar
        '(lambda (w) (vla-setcolumnwidth tbl (setq col (1+ col)) w))
        '(100 100 200)
      )
      (setq col -1)
      (vla-put-RegenerateTableSuppressed tbl :vlax-true)
      (vla-put-Vertcellmargin tbl 10.55)
      (vla-put-Horzcellmargin tbl 10.55)
      (vla-unmergecells tbl 0 0 0 2)
      (mapcar '(lambda (s c)
                 (Set:text:contents_ tbl 0 c s acMiddlecenter)
                 (vla-setrowheight tbl 0 45)
               )
              '(" " "Comprimento" "Qtd.")
              '(0 2 1)
      )
      (setq row 1
            col -1
      )
      (foreach itm 
                   (vl-sort      
                     lst
                     (function
                       (lambda (j k) (< (car j) (car k))) 
      		       ) 
                   ) 
        (mapcar '(lambda (s c a)
                   (Set:text:contents_ tbl row c s (eval a))
                 )
                itm
                '(0 2 1)
                '(acMiddlecenter acMiddleRight acMiddlecenter acMiddlecenter)
        )
        (vla-setrowheight tbl row 1.0)
        (setq row (1+ row)
              col -1
        )
      )
      (vla-put-RegenerateTableSuppressed tbl :vlax-false)
    )
  )
  (princ)
) (vl-load-com)
(defun Set:text:contents_ (o_ r_ c_ v_ a_) 
  (vla-settext o_ r_ c_ (strcat "{\\fcalibri|b0|i0|c0|p34;" v_ "}"))
  (vla-setcelltextheight o_ r_ c_ 18.5)
  (vla-setcellalignment o_ r_ c_ a_) 
)

 

 

Why did you remove the author name from the codes ?

 

 

 

 

 

 

  • Like 2
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...