Jump to content

Recommended Posts

Posted

I’m working on part nesting in AutoCAD and I need to save each part as a block named after the part. Is there a way to create a block and automatically use a selected text object as the block name, instead of typing the name manually?

Posted (edited)

Based of this code by Lee Mac.

I changed it to do what you ask for.

 

- copy-paste this code to a file named select-text-as-block.lsp (or whatever, just make sure it has the .lsp extension),

- Drag this file to your drawing

- type command otbs, press enter, then follow the instructions.  (select the objects; select the text object, select a point on screen)

 

(I hope you don't need attributes; they seem to be somewhat of a problem with this code)

 

;; Objects to Block  -  Lee Mac
;; Converts a selection of objects to a block reference.
;; adapted by Emmanuel Delay : obj2blk separated from c:obj2blk 
  
  (defun add_attdef ( / ip tag prmpt dfault)
    
    (entmake
      (list
        (cons 0 "ATTDEF")
        (cons 8 "0")
        (cons 10 '(0.3 4.3 0.0))
        (cons 70 0)
        (cons 1 "")
        (cons 2 "NUM")
        (cons 3 "Number: ")
        (cons 40 2.0)
      )
    )
    
  )
 
;; @params: s: selection, n: name, p: point 
(defun obj2blk (s n p / e i l s x )
  (if
    (and 
      s
      (progn
        (while
          (not
            (or (= "" n)
              (and
                (snvalid n)
                (null (tblsearch "BLOCK" n))
              )
            )
          )
          (princ "\nBlock name invalid or already exists.")
        )
        (if (= "" n)
          (setq n "*U")
        )
        p
      )
    )
    (progn
      (entmake
        (list
           '(0 . "BLOCK")
          (cons 10 (trans p 1 0))
          (cons 02 n)
          (cons 70 (if (wcmatch n "`**") 1 0))
        )
      )
      (repeat (setq i (sslength s))
        (entmake (entget (setq e (ssname s (setq i (1- i))))))
        (if (= 1 (cdr (assoc 66 (entget e))))
          (progn
            (setq x (entnext e)
                l (entget  x)
            )
            (while (/= "SEQEND" (cdr (assoc 0 l)))
              (entmake l)
              (setq x (entnext x)
                  l (entget  x)
              )
            )
            (entmake l)
          )
        )
        (entdel e)
      )
      ;;(add_attdef)
      (if (setq n (entmake '((0 . "ENDBLK"))))
        (entmake
          (list
             '(0 . "INSERT")
            (cons 02 n)
            (cons 10 (trans p 1 0))
          )
        )
      )
    )
  )
  (princ "\nBlock made: ")
  (princ n)
)


(defun c:otb ( / s n p)

  (setq s (ssget "_:L" '((-4 . "<NOT") (0 . "ATTDEF,VIEWPORT") (-4 . "NOT>"))))
  ;;(setq s (ssget "_:L" '((-4 . "<NOT") (0 . "VIEWPORT") (-4 . "NOT>"))))
  (setq n (getstring t "\nSpecify Block Name <Anonymous>: "))
  (setq p (getpoint "\nSpecify Base Point: "))
  
  (obj2blk s n p)
  
  (princ)
)

(defun c:otbs ( / s n p)

  (setq s (ssget "_:L" '((-4 . "<NOT") (0 . "ATTDEF,VIEWPORT") (-4 . "NOT>"))))
  ;;(setq s (ssget "_:L" '((-4 . "<NOT") (0 . "VIEWPORT") (-4 . "NOT>")))) ;;;
  
  (setq n (cdr (assoc 1 (entget (car (entsel "\nSelect a text object that holds the name of the block: "))))))
  (setq p (getpoint "\nSpecify Base Point: "))
  
  (obj2blk s n p)
  
  (princ)
)

 

 

Edited by Emmanuel Delay

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...