AutoLISP: Block Name

When it comes time to annotate your drawings, wouldn’t it be nice for you to be able to select a block and then have its name appear as a text object and then place that text object?

Here ya go:

  • BLOCKN <enter> to start
  • Select block
  • Place text
; places name of block as text via pick

(defun C:blockn()

(setvar "cmdecho" 0)

(setq pt(cadr(entsel"\nSelect Block:")))

(setq e1(ssget pt))

(setq e2 (entget (ssname e1 0)))

(setq blname (cdr(assoc 2 e2)))

(setq pt1 (getpoint"\nSelect point for block title:"))

(command "text" pt1 "" 0 blname)

)

Advertisement

About AutoCAD Tips

This blog serves as a knowledge base for myself (and anyone else) so that I can reference tips & tricks that I have learned and also refer others to it as well. I hope that this blog helps you learn at least one tip to make your drafting/design experience better.
This entry was posted in AutoLISP, Blocks, Text, TIPS. Bookmark the permalink.

9 Responses to AutoLISP: Block Name

  1. Marcelo says:

    Hi,
    Is that possible to have the name of the block written in a FIELD, that will be link with the block name?
    Thanks
    Marcelo

    • AutoCAD Tips says:

      I found this @ http://www.cadtutor.net/forum/showthread.php?51254-Block-s-name-to-FIELD-(based-on-existing-lisp)

      (defun c:FieldBlockName ( / *error* doc spc e p )
      (vl-load-com)
      ;; © Lee Mac 2010

      (defun *error* ( msg )
      (or (wcmatch (strcase msg) “*BREAK,*CANCEL*,*EXIT*”)
      (princ (strcat “\n** Error: ” msg ” **”)))
      (princ)
      )

      (LM:ActiveSpace ‘doc ‘spc)

      (while
      (and
      (setq e
      (LM:SelectifFoo
      (lambda ( x )
      (eq “INSERT” (cdr (assoc 0 (entget x))))
      )
      “\nSelect Block: ”
      )
      )
      (setq p (getpoint “\nPick Point for Field: “))
      )
      (LM:AddMText_MC spc p
      (strcat “%<\\AcObjProp Object(%vla-object e)) “>%).EffectiveName>%”
      )
      )
      )

      (princ)
      )

      (defun LM:AddMText_MC ( space pt str / o )
      ;; © Lee Mac 2010
      (setq o (vla-AddMtext space (vlax-3D-point pt) 0. str))
      (vla-put-AttachmentPoint o acAttachmentPointMiddleCenter)
      (vla-put-InsertionPoint o (vlax-3D-point pt))
      )

      (defun LM:GetObjectID ( doc obj )
      ;; © Lee Mac 2010
      (if (eq “X64” (strcase (getenv “PROCESSOR_ARCHITECTURE”)))
      (vlax-invoke-method (vla-get-Utility doc) ‘GetObjectIdString obj :vlax-false)
      (itoa (vla-get-Objectid obj))
      )
      )

      ;;——————–=={ ActiveSpace }==———————;;
      ;; ;;
      ;; Retrieves pointers to the Active Document and Space ;;
      ;;————————————————————;;
      ;; Author: Lee McDonnell, 2010 ;;
      ;; ;;
      ;; Copyright © 2010 by Lee McDonnell, All Rights Reserved. ;;
      ;; Contact: Lee Mac @ TheSwamp.org, CADTutor.net ;;
      ;;————————————————————;;
      ;; Arguments: ;;
      ;; *doc – quoted symbol other than *doc ;;
      ;; *spc – quoted symbol other than *spc ;;
      ;;————————————————————;;

      (defun LM:ActiveSpace ( *doc *spc )
      ;; © Lee Mac 2010
      (set *spc
      (if
      (or
      (eq AcModelSpace
      (vla-get-ActiveSpace
      (set *doc
      (vla-get-ActiveDocument
      (vlax-get-acad-object)
      )
      )
      )
      )
      (eq :vlax-true (vla-get-MSpace (eval *doc)))
      )
      (vla-get-ModelSpace (eval *doc))
      (vla-get-PaperSpace (eval *doc))
      )
      )
      )

      ;;——————-=={ Select if Foo }==——————–;;
      ;; ;;
      ;; Continuous selection prompts until the predicate function ;;
      ;; foo is validated ;;
      ;;————————————————————;;
      ;; Author: Lee McDonnell, 2010 ;;
      ;; ;;
      ;; Copyright © 2010 by Lee McDonnell, All Rights Reserved. ;;
      ;; Contact: Lee Mac @ TheSwamp.org, CADTutor.net ;;
      ;;————————————————————;;
      ;; Arguments: ;;
      ;; foo – predicate function taking ename argument ;;
      ;; str – prompt string ;;
      ;;————————————————————;;
      ;; Returns: selected entity ename if successful, else nil ;;
      ;;————————————————————;;

      (defun LM:SelectifFoo ( foo str / sel ent )
      ;; © Lee Mac 2010
      (while
      (progn
      (setq sel (entsel str))

      (cond
      (
      (vl-consp sel)

      (if (not (foo (setq ent (car sel))))
      (princ “\n** Invalid Object Selected **”)
      )
      )
      )
      )
      )
      ent
      )

  2. Thank you for this Lisp.
    I have one problem: it doesn’t work to anotate in Layouts viewports.
    Do you know how to change it or another routine that can do the same but on layout (with viewports)?

  3. 1skej8 says:

    Hi Sir Greg,

    it’s me again, Sean. I just wanted to ask for help about what’s wrong with my lisp here. I wanted to count the blocks and also displays their name. I’m very new to this. I think its my second week of learning LISP.

    I hope you can help me with this. thank you in advance. :D

    here’s my code:
    ____________________
    (defun c:BL (/ ssblks ssblks2 en ctr blk ent enlist) ;define funcntion
    (vl-load-com)

    (if (setq ssblks (ssget))) ;|selects blocks|;
    (prog

    (setq blk (sslength ssblks)) ;|get number of blocks|;

    (setq ct 0) ;|set counter to zero|;

    (repeat (ssget ssblks2)) ;|repeat the selecting blocks|;

    (ssadd (ssname ssblks2 ct) ssblks) ;|add ssname froms ssblks selection|;

    (setq en (car(entsel ssblks2)) ;|gets entity from ssblks|;

    (setq enlist(cdr (assoc 2 (entget en)))) ;|get the dxf group codes of the entity;|

    (alert (strcat “\n There are ” (itoa blk) ” blocks and these are: ” (vla-get-effectivename (vlax-ename->vla-object enlist)) )) ;|display result|;

    ) ;prog

    (princ)

    ) ;if

    (princ)

    );end defun

    (princ)

    • AutoCAD Tips says:

      Without trying to reinvent the wheel with the various ways to count blocks, I would highly recomend Lee Mac’s block counting routine. It will show a count of the blocks with the block’s name and even a preview of the block and have you place the count as an AutoCAD table. Lee’s routine can be found here: http://www.lee-mac.com/blockcounter.html

      I would also remind you of the built-in block counting tool in AutoCAD called BCOUNT. It is found in the express tools. It will simply show the name of the block and the # of occurrences in the command line

      • 1skej8 says:

        Thank you so much for this Sir Greg. Lee Mac is one of the legends in Autolisp. How does he do that? must be years of learning and has also the talent in making LISP. I’ve viewed the Lisp and it is very complicated for me to understand. haha. Yes i know about BCOUNT but for those using a lower version of AutoCAD has no bcount command yet. :) thank you once again Sir. More power and God bless. :D

  4. Thank You all… codes are very useful. i have an additional question. i wanted to know that if it could be posibble to make a mleader that writes choosen block’s name. thank you very much again…

    • AutoCAD Tips says:

      This is a good one from Alan Thompson
      Note that after selecting the block, the arrow point goes to the block’s insertion point automatically.
      found at http://www.cadtutor.net/forum/showthread.php?39301-Block-into-multileader-text&s=0d0c574e023689abfed961521d1d7402
      ;;; Alan J Thompson
      ;;; Block name to Multileader
      ;;; http://www.cadtutor.net/forum/showthread.php?39301-Block-into-multileader-text&s=0d0c574e023689abfed961521d1d7402
      (defun c:BNameLabel (/ obj lastentity ent)
      (vl-load-com)
      (if (setq obj (car (entsel "\nSelect block: ")))
      (if (eq (cdr (assoc 0 (entget obj))) "INSERT")
      (progn (setq lastentity (entlast))
      (vl-cmdf "_.mleader"
      "_non"
      (trans (vlax-get (setq obj (vlax-ename->vla-object obj)) 'InsertionPoint) 0 1)
      PAUSE
      )
      (while (eq 1 (logand 1 (getvar 'CMDACTIVE))) (vl-cmdf ""))
      (if (not (equal lastentity (setq ent (entlast))))
      (vla-put-textstring
      (vlax-ename->vla-object ent)
      (vlax-get-property
      obj
      (if (vlax-property-available-p obj 'EffectiveName)
      'EffectiveName
      'Name
      )
      )
      )
      )
      )
      (princ "\nInvalid object!")
      )
      )
      (princ)
      )

      ;;; Entsel or NEntsel with options
      ;;; #Nested - Entsel or Nentsel (T for Nentsel, nil for Entsel)
      ;;; #Message - Selection message (if nil, "\nSelect object: " is used)
      ;;; #FilterList - DXF ssget style filtering, no cons (nil if not required)
      ;;; #Keywords - Keywords to match instead of object selection (nil if not required)
      ;;; Example: (AT:Entsel nil "\nSelect MText not on 0 layer [Settings]: " '((0 . "MTEXT")(8 . "~0")) "Settings")
      ;;; Alan J. Thompson, 04.16.09
      ;;; Updated: Alan J. Thompson, 06.04.09 (changed filter coding and added layer option)
      (defun AT:Entsel (#Nested #Message #FilterList #Keywords
      / #Count #Message #Choice
      #Ent
      )
      (setvar "errno" 0)
      (setq #Count 0)
      (or #Message (setq #Message "\nSelect object: "))
      (if #Nested
      (setq #Choice nentsel)
      (setq #Choice entsel)
      ) ;_ if
      (while (and (not #Ent)
      (/= (getvar "errno") 52)
      ) ;_ and
      (and #Keywords (initget #Keywords))
      (cond
      ((setq #Ent (#Choice #Message))
      (and
      #FilterList
      (vl-consp #Ent)
      (or
      (not
      (member
      nil
      (mapcar
      '(lambda (x)
      (wcmatch
      (if
      (eq
      (type
      (cdr (assoc (car x) (entget (car #Ent))))
      ) ;_ type
      'STR
      ) ;_ eq
      (strcase
      (cdr (assoc (car x) (entget (car #Ent))))
      ) ;_ strcase
      (cdr (assoc (car x) (entget (car #Ent))))
      ) ;_ if
      (cdr x)
      ) ;_ wcmatch
      ) ;_ lambda
      #FilterList
      ) ;_ mapcar
      ) ;_ member
      ) ;_ not
      (setq #Ent nil)
      ) ;_ or
      ) ;_ and
      )
      ) ;_ cond
      (and (= (getvar "errno") 7)
      (not #Ent)
      (setq #Count (1+ #Count))
      (prompt (strcat "\nNope, keep trying! "
      (itoa #Count)
      " missed pick(s)."
      ) ;_ strcat
      ) ;_ prompt
      ) ;_ and
      ) ;_ while
      #Ent
      ) ;_ defun

  5. Liz says:

    Hi! I know this is kind of a stretch, but do you know of a lisp that would put bcount and either block name or block description into a mleader? As in, select all of X-block in a cluster (not in the whole drawing) run the lisp and then set the mleader points. Final result being: <—- # X-Block

    Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s