Lisp to place a group of blocks sorted on a similar axis

Lisp to place a group of blocks sorted on a similar axis

georg_holter
Contributor Contributor
996 Views
9 Replies
Message 1 of 10

Lisp to place a group of blocks sorted on a similar axis

georg_holter
Contributor
Contributor

Hello,

This is my first post...I'am only reading usually. 🙂 And i'm beginner in lisp! 😞

 

I used "BAL" lips and it works perfectly!! thanks to Kent1Cooper for this awesome lisp!!!

(https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-to-place-a-group-of-blocks-on-...)

 

What i wanna do is that this lisp is sorting the selected blocks by its attribute values for example "NL1/1" the next block has value "NL1/2", the next "NL1/3" and so forth... these are different blocks (and names) but they have all the same attribute tag: "STROMKREIS" with values: "NL1/1" next block "NL1/2" another block "NL1/3".....so i would have all the blocks sorted in a row... 🙂


I tried but i failed! Here's the modified code:

 

(defun C:BAL (/ osm bss pt rad len values blk n spc); = Block Align
;; for Blocks with primary Circle, with insertion point at its center
(setq osm (getvar 'osmode))
(prompt "\nBlöcke wählen, welche aufzulisten sind!")
(if
(and
(setq bss (ssget '((0 . "INSERT"))))
(setq pt (getpoint "\nLinken Startpunkt der Reihe angeben: "))
(setq rad 2)
(setq len 2)
); and
(progn ; then
(setvar 'osmode 0)
(setq values (cons (cadr
(assoc
"STROMKREIS"
(mapcar '(lambda (at)
(list (vla-get-tagstring at)(vla-get-textstring at)))
(vlax-invoke
(vlax-ename->vla-object (ssname blk (Setq i (1- i))))
'GetAttributes)
)
)
)
values
)
)
(repeat (setq n (sslength bss))
(command
"_.copy" (setq blk (ssname bss (setq n (1- n)))) ""
(cdr (assoc 10 (entget blk))) pt
); command
(setq pt (polar pt 0 rad))
); repeat
(setvar 'osmode osm)
); progn
); if
); defun

 

Do you have any ideas!? please help me!


Thank you!!!!

0 Likes
Accepted solutions (2)
997 Views
9 Replies
Replies (9)
Message 2 of 10

georg_holter
Contributor
Contributor

For better understanding, here is such sample dwg! 🙂

0 Likes
Message 3 of 10

georg_holter
Contributor
Contributor

and here my modified "BAL" lisp from @Kent1Cooper 

(hope it's ok to repost!?!?)

0 Likes
Message 4 of 10

ВeekeeCZ
Consultant
Consultant
Accepted solution

Welcome to the forums!

 

Try this code if that is what you need. 

You basically need to get the attribute value eg. by (getpropertyvalue ename tagname) and then sort it by vl-sort func. If you have some questions regarding the code, don't hesitate to ask.

 

(vl-load-com)

(defun c:BlocksToLine ( / s p i a l x)
  
  (or *btl-a* (setq *btl-a* "STROMKREIS"))
  (or *btl-d* (setq *btl-d* 2.))
  
  (if (and (setq s (ssget "_:L" '((0 . "INSERT") (66 . 1 ))))
	   (not (initget 128))
	   (setq *btl-a* (cond ((getkword (strcat "\nTag name to sort by <" *btl-a* ">: "))) 	(*btl-a*)))
	   (setq *btl-d* (cond ((getdist  (strcat "\nDistance in between <" (rtos *btl-d*) ">: "))) 	(*btl-d*)))
	   (setq p (getpoint "\nPlace them here: "))
	   )
    (repeat (setq i (sslength s))
      (setq e (ssname s (setq i (1- i))))
      (if (not (vl-catch-all-error-p (setq a (vl-catch-all-apply 'getpropertyvalue (list e *btl-a*)))))
	(setq l (cons (cons e a) l)))))
  (setq l (vl-sort l '(lambda (e1 e2) (< (cdr e1) (cdr e2)))))
  (setq x (- (car p) *btl-d*))
  (foreach e l (command "_copy" (car e) "" "_non" (trans (cdr (assoc 10 (entget (car e)))) 0 1) "_non" (list (setq x (+ x *btl-d*)) (cadr p))))
  (princ)
  )

 

Also remember, that you should always post a sample dwg, ideally 2 dwgs - state before and after.

 

Message 5 of 10

georg_holter
Contributor
Contributor
Hello BeekeeCZ!

YOUR ARE THE MASTER!!! :))) works perfectly!!
BUT one question:
now it sorts NL1/1 - NL1/10 - NL1/11 - NL1/12........NL1/2 - NL1/3.....
but should be NL1/1 - NL1/2 - NL1/3 - NL1/4........NL1/10 - NL1/11..... 🙂
My numbers are mostly to NL1/50 max.

I hope you know what i mean 🙂

THANK YOU AGAIN!!!!!!! 🙂

0 Likes
Message 6 of 10

ВeekeeCZ
Consultant
Consultant
Accepted solution

Yes, I see what you mean. Also see, that you should be the one who solves it.

 

The starting point is a string "NL1/10"

The goal is to get a number: 10.

 

HERE and HERE are functions that you need to use. 

 

The current comparison is this expression, which compares two strings:

(< (cdr e1) (cdr e2))   

 

Good luck.

 

 

Spoiler: 

Convertion function:
(atoi (substr "NL1/10" (+ 2 (vl-string-search "/" "NL1/10"))))

Comparison function:
(setq l (vl-sort l '(lambda (e1 e2) (< (atoi (substr (cdr e1) (+ 2 (vl-string-search "/" (cdr e1))))) (atoi (substr (cdr e2) (+ 2 (vl-string-search "/" (cdr e2)))))))))

 

Message 7 of 10

georg_holter
Contributor
Contributor
hello BeekeeCZ,
Thank you for your answer!!
I'am trying for hours now but i've no clue... :(((
do i have to use "atoi" instead of "cdr"?
I can’t write/create LISP programs i only can sometimes change existing lisps („really mini-mini-things“) and copy things together...sometimes for me… only trying – i’m learning. please help me further...
sorry for beeing annoying... 😞
0 Likes
Message 8 of 10

georg_holter
Contributor
Contributor
sorry! did not see your spoiler! haha 🙂 i think now i am there:

(vl-load-com)

(defun c:BTL ( / s p i a l x)

(or *btl-a* (setq *btl-a* "STROMKREIS"))
(or *btl-d* (setq *btl-d* 2.))

(if (and (setq s (ssget "_:L" '((0 . "INSERT") (66 . 1 ))))
(not (initget 128))
(setq p (getpoint "\nPlace them here: "))
)
(repeat (setq i (sslength s))
(setq e (ssname s (setq i (1- i))))
(if (not (vl-catch-all-error-p (setq a (vl-catch-all-apply 'getpropertyvalue (list e *btl-a*)))))
(setq l (cons (cons e a) l)))))
(setq l (vl-sort l '(lambda (e1 e2) (< (atoi (substr (cdr e1) (+ 2 (vl-string-search "/" (cdr e1))))) (atoi (substr (cdr e2) (+ 2 (vl-string-search "/" (cdr e2)))))))))
(setq x (- (car p) *btl-d*))
(foreach e l (command "_copy" (car e) "" "_non" (trans (cdr (assoc 10 (entget (car e)))) 0 1) "_non" (list (setq x (+ x *btl-d*)) (cadr p))))
(princ)
)

right?
0 Likes
Message 9 of 10

ВeekeeCZ
Consultant
Consultant

@georg_holter wrote:
sorry! did not see your spoiler! haha 🙂 i think now i am there:


 

Cool. Just to be clear, the goal was to figure out the code itself, not how to use my spoiler.

0 Likes
Message 10 of 10

georg_holter
Contributor
Contributor
I know... I'am currently comparing the two versions to understand why and where is the difference and meaning of the string-handling functions and convertion functions.... my brain is spinning right now... 😮

THANK YOU very much for your help!!