TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: jaydee on July 14, 2011, 12:51:56 AM

Title: Using ssget "X" to Get block name if block contain more then 20 lines of attribu
Post by: jaydee on July 14, 2011, 12:51:56 AM
Hi.
At the moment i got this lisp that work well with block attributes using (ssget "X" '(0 . "insert")(2 . "blockname"))

The only issue is that i have to provide the block name (sheet title attributes) .
every so often someone decided to rename this block to suit project.
therefore im thinking is there a way to do this without providing a blockname in my lisp.
finding a tagname wont work, because im not confident that the tagname is unigue. therefore i still need the blockname.

What i would like to achieve is, do a ssget "x" on layout
if a block with more then 20 line of attdef, (im very sure theres only 1 block with that many tag)
then return the block name.

Thankyou
Title: Re: Using ssget "X" to Get block name if block contain more then 20 lines of attribu
Post by: Lee Mac on July 14, 2011, 09:11:06 AM
The ssget function can only search the drawing database for primary entities, not subentities such as attributes/vertices.

Therefore, you have two options: the first would be to iterate through all attributed blocks in the drawing, then single out those that have more than 20 attributes:

Code: [Select]
(defun GetBlocksWithMoreThan20Attribs ( / at en i ss )
  (if (setq ss (ssget "_X" '((0 . "INSERT") (66 . 1))))
    (repeat (setq i (sslength ss))
      (setq en (ssname ss (setq i (1- i)))
            at 0
      )
      (while (eq "ATTRIB" (cdr (assoc 0 (entget (setq en (entnext en))))))
        (setq at (1+ at))
      )
      (if (< at 20)
        (ssdel (cdr (assoc 330 (entget en))) ss)
      )
    )
  )
  ss
)

The second would be to iterate through the block table, and collect the names of blocks with more than 20 attribute definitions, then create a selection set of these blocks.

Code: [Select]
(defun GetBlocksWithMoreThan20AttDefs ( / at bd be bl nm )
  (while (setq bd (tblnext "BLOCK" (null bd)))
    (if (= 2 (logand 2 (cdr (assoc 70 bd))))
      (progn
        (setq at 0
              nm (cdr (assoc 2 bd))
              be (tblobjname "BLOCK" nm)
        )
        (while (setq be (entnext be))
          (if (eq "ATTDEF" (cdr (assoc 0 (entget be))))
            (setq at (1+ at))
          )
        )
        (if (<= 20 at)
          (setq bl (cons "," (cons nm bl)))
        )
      )
    )
  )
  (ssget "_X" (list '(0 . "INSERT") '(66 . 1) (cons 2 (apply 'strcat (cdr bl)))))
)

The second method will fail however if attributes have been added to the block outside of the block definition, but this is an unlikely case.

Title: Re: Using ssget "X" to Get block name if block contain more then 20 lines of attribu
Post by: jaydee on July 14, 2011, 07:27:21 PM
Thankyou Lee Mac.

These codes are very valuable to me.

Please explain
"if attributes have been added to the block outside of the block definition"

Its my shout. cheers
Title: Re: Using ssget "X" to Get block name if block contain more then 20 lines of attribu
Post by: Lee Mac on July 14, 2011, 07:48:59 PM
Please explain
"if attributes have been added to the block outside of the block definition"

When entmake'ing a Block Reference (Insert), attributes which do not belong to the Block Definition can be programmatically added to the Insert entity using entmake.