Code Red > AutoLISP (Vanilla / Visual)

Adding block type to a list

(1/2) > >>

jerryman:
Hello everyone,

First post, thanks for your time in advance.

I have a command that will, like many others I've seen, allow selection of blocks, ask for an additional point and print them. My goal was to utilize the list data to draw polylines, but I want to add additional fields to the list for later use. I've simplified some of this just to show what code I have that functions properly, I'm having issues understanding how to add the other items to the list or sublist. This is totally out of ignorance but I'm trying to learn more, if I wanted to introduce this option what functions would be the best to use?

Concept:
Check blockname list for AA, if AA exist then AA is arc.

Modified example of current output of the code below (it would normally only have one userpt as intended):
Block Coordinates and Names:
((BLOCK1_XXAAXX (921.866 1042.21 246.0)) (UserPt (860.866 992.962 246.0)))
((BLOCK1_XXBBXX (860.866 992.962 246.0)) (UserPt (440.866 992.962 246.0)))

Desired output:
Block Coordinates and Names:
((BLOCK1_XXAAXX (921.866 1042.21 246.0)) (UserPt (860.866 992.962 246.0)) arc)
((BLOCK1_XXBBXX (860.866 992.962 246.0)) (UserPt (440.866 992.962 246.0)) straight)


--- Code: ---(defun c:mycmd ( / ss ent inscoords blockname coordlist userpt)
  (setq ss (ssget '((0 . "INSERT"))))
  (setq coordlist (list))
  (if ss 
    (progn
      (setq ent (ssname ss 0))
      (while ent
        (setq inscoords (cdr (assoc 10 (entget ent))))
        (setq blockname (cdr (assoc 2 (entget ent))))
        (setq coordlist (append coordlist (list (list blockname inscoords))))
        (setq ss (ssdel ent ss))
        (setq ent (ssname ss 0))
      )
      (setq userpt (getpoint "\nSelect end point of last block selected: "))
      (setq coordlist (append coordlist (list (list "UserPt" userpt))))
      (princ "\nBlock Coordinates and Names: ")
      (princ coordlist)
    )
    (princ "\nNo blocks selected.")
  )
  (princ) ; Ensure function returns nil
)

--- End code ---

Thanks and regards,
jerryman

BIGAL:
Fairly simple use the COND function to compare block name with type.


--- Code: ---(cond
((= "BLOCK1_XXAAXX" blockname)(setq typ "Arc"))
((= "BLOCK1_XXBBXX" blockname)(setq typ "Straight"))
((= "BLOCK1_XXCCXX" blockname)(setq typ "ZIGZAG"))
)
--- End code ---

Tharwat:
Hopefully this meets what you are after.
NOTE: You can add more block names & types as declared in the following codes in 'grp' list.

--- Code - Auto/Visual Lisp: ---(defun c:Test (/ grp pnt int sel ent get fnd lst)  ;;----------------------------------------------------;;  ;;    Author : Tharwat Al Choufi                      ;;  ;; website: https://autolispprograms.wordpress.com    ;;  ;;----------------------------------------------------;;  (setq grp '(("XX_AA_XX" "Arc")              ("XX_BB_XX" "Straight")             )  )  (and    (setq pnt (getpoint "\nSelect end point of last block selected: "))    (princ "\nSelect your desired blocks : ")    (or (setq int -1              sel (ssget '((0 . "INSERT")))        )        (princ "\nNo blocks selected.")    )    (or (while (setq int (1+ int)                     ent (ssname sel int)               )          (and (setq fnd (assoc (cdr (assoc 2 (setq get (entget ent)))) grp))               (setq                 lst (cons (list pnt (cdr (assoc 10 get)) (cadr fnd)) lst)               )          )        )        t    )    (and lst (princ lst))  )  (princ))

jerryman:
Thanks @BIGAL and @Tharwat

@Tharwat, what section of your code would check the list to see if the block name contains these matches and then adds the block type to the list?

I noticed the block names are very long but have a few obvious and defining factors in their names. Below the block names, "have been changed to protect the innocent" but these are well thought out alternatives. As you can see there is quite a bit of overlap in the block names, I've added the spaces to the block names to make them easier to read.

The arc blocks, while many other block names are arcs, they would all contain these in their name. Maybe treat these as static if block name contained an exact match, then arc? I think I can treat the straight blocks the same, if they contain these exact matches, then straight.
commonalities:
arc:
abc xy de
abc de

I think I can treat the straight blocks the same, if they contain these exact matches, then straight.
straight:
123 abc
123 abc xy
45 abc
45 abc xy
456 abc
456 abc xy
457 abc
457 abc xy


I tried to get this to work previously but was unsuccessful:

--- Code: ---        (cond
            ((or
                (wcmatch blockname "*abc xy de*")
                (wcmatch blockname "*abc de*")
            )
            (setq blocktype "arc"))
            ((or
                (wcmatch blockname "*123 abc*")
                (wcmatch blockname "*123 abc xy*")
                (wcmatch blockname "*45 abc*")
                (wcmatch blockname "*45 abc xy*")
                (wcmatch blockname "*456 abc*")
                (wcmatch blockname "*456 abc xy*")
                (wcmatch blockname "*457 abc*")
                (wcmatch blockname "*457 abc xy*")
            )
            (setq blocktype "straight"))
        )

--- End code ---

ribarm:
Since you are repeating block structure with same syntaxes from arc to straight types, these 2 are wrongly written :
(wcmatch blockname "*abc xy de*")
(wcmatch blockname "*abc de*")

You should change them to names without starting *...
(wcmatch blockname "abc xy de*")
(wcmatch blockname "abc de*")

Navigation

[0] Message Index

[#] Next page

Go to full version