autocad lisp for selecting multiple objects

autocad lisp for selecting multiple objects

tomaszbartosz93
Contributor Contributor
1,240 Views
2 Replies
Message 1 of 3

autocad lisp for selecting multiple objects

tomaszbartosz93
Contributor
Contributor

Hi,
This code let user select multiple text/block objects and check if those are defined in a list  relation. If those are defined, code remember what user picks and move one to another and so on... 
If object is not in definition it ask to write what it is and then checks if the entry is correct if not is give alert to try again.

Its part of bigger autocadlisp  function which then paste various blocks defined in relation list in the exact the same order as in the selection process.

to this fragment of code i want to add those function but i struggle to 😞
1. If user selects empty field in autocad alert "You pick nothing select again" and continue the loop of picking objects( this one has to repeat even if user picks object in step 1 and then select empty)
2. Somehow a possibility to end while loop of selection ( best would be when press spacebar or write endnow in command prompt.  but i am open to sugestions)
3. A possibility to undo last selection (i want to undo multiple as well - best case scenario just pressing backspace multiple times etc.)

4. If it is possible prompts user with information what exactly was the object which was canceled in undo selection process.

 

  (setq selectionList (list))
  (while (setq selection (entsel "Wybierz napis lub blok: ")) 
    (setq entType (cdr (assoc 0 (entget (car selection)))))
    (setq entName (if (equal entType "TEXT") 
                    (cdr (assoc 1 (entget (car selection))))
                    (cdr (assoc 2 (entget (car selection))))
                  )
    )
    (setq found nil)
    (foreach relation 
      '(("pralka" . "svp_pralka")
       )

      (if (equal entName (car relation)) 
        (setq found t)
      )
    )
    (if (not found) 
      (while (not found) 
        (setq entName (getstring "Podaj nazwę napisu/bloku: "))
        (foreach relation 
          '(("pralka" . "svp_pralka")

           )

          (if (equal entName (car relation)) 
            (setq found t)
          )
        )
        (if (not found) 
          (alert "Nie ma takiego napisu/bloku w relacjach, spróbuj ponownie.")
        )
      )
    )
    (setq selectionList (append selectionList 
                                (list (cons entName (car selection)))
                        )
    )
  )

 

0 Likes
Accepted solutions (1)
1,241 Views
2 Replies
Replies (2)
Message 2 of 3

ВeekeeCZ
Consultant
Consultant
Accepted solution

Here's something to start. The process is definitely wrong because I did not find the logic in your code. Why the list of pairs '(("pralka" . "svp_pralka")) is actually a list of pairs if you never use the second member of a pair??

 

Also, next time do the translation. 

 

 

(defun c:Pairs ( / prelst done sel msg done2 str str2)
  
  (setq prelst '(("pralka" "svp_pralka")
		 ("?" "?") ; postpone
		 ))
  
  (while (not done)
    
    (setvar 'errno 0) ; reset errno - neccesary
(initget "Undo") (setq sel (car (entsel "\nSelect text or block [Undo]: "))) (cond ((setq msg "" done2 nil)) ((and (not sel) (= 7 (getvar 'errno))) (princ "\nMissed, try again.")) ((not sel) ; exit (setq sellst (reverse sellst) done T)) ((= "Undo" sel) (setq sellst (cdr sellst) msg "Last item removed. ")) ((not (wcmatch (cdr (assoc 0 (entget sel))) "TEXT,INSERT")) (princ "\nWrong selection, only TEXTs or BLOCKs supported. Try again.")) (T (setq str (cdr (assoc (if (wcmatch (cdr (assoc 0 (entget sel))) "TEXT") 1 2) (entget sel))) done2 nil) (if (assoc str prelst) (setq sellst (cons (cons str sel) sellst)
msg "Item added.") (while (not done2) (princ "\nSpecify formal block name...\n") (setq str2 (lisped str)) (cond ((numberp str2) (setq done2 T msg "Item skipped. ")) ((setq ass (assoc str2 (mapcar 'reverse prelst))) (setq sellst (cons (cons str2 sel) sellst) prelst (cons (list str2 (car ass))) ; add here for later usage as well done2 T msg "Item added. ")) (T (princ "\nWrong name. Fix it or set '?' to postpone and fix it later. "))))))) (princ (strcat "\n" msg (cond ((caar sellst)) ("")) " - " (cond ((caadr sellst)) ("")) " - " (cond ((caaddr sellst)) (""))))) (princ) )

 

Message 3 of 3

tomaszbartosz93
Contributor
Contributor

 I have a database of more like 300+ relations but i deleted it to make it easier to review. ("pion" "svp_pion")  ("A$C40B1224E" . "svp_wanna") etc. Those are block names -sanitation like, sink

bathtub etc. . for example when i have like 3 -6 bathrooms i ll select multiple sinks which then i have to generate in output in corect order.  with this pattern ("block to recognize" . "block to insert if true"). 
Thanks a lot for this rewrite i thought about that solution but i i didnt quite understand how to use cond. You helped me a lot. Great start indeed. Thanks again 🙂

Yeah sure i ll translate alerts next time and add description for parts of code:) forgot about that

0 Likes