Author Topic: Quick Block modification  (Read 2101 times)

0 Members and 1 Guest are viewing this topic.

asami486

  • Guest
Quick Block modification
« on: December 12, 2018, 08:56:58 PM »
Hi.
I Just wanna to add one function.
I want to use not the default block name "MyBlock" BUT a block name that I enter.
Please Help Me!


Code: [Select]
;;; Quick Block
  ;;; Tharwat 11. May. 2012 ;;
  ;;; http://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/quick-block/td-p/3454228
  ;;; Modified by HasanCAD and Grrr1337 (in theswamp)
  ;;; https://www.theswamp.org/index.php?topic=52634.0

(defun c:B (/ selectionset insertionpoint number Blockname mn mx pnts)
  (vl-load-com)
  (defun LM:ssboundingbox ( sel / idx llp ls1 ls2 obj urp ) ; Lee mac
    (repeat (setq idx (sslength sel))
        (setq obj (vlax-ename->vla-object (ssname sel (setq idx (1- idx)))))
        (if (and (vlax-method-applicable-p obj 'getboundingbox)
                 (not (vl-catch-all-error-p
(vl-catch-all-apply 'vla-getboundingbox (list obj 'llp 'urp)))))
            (setq ls1 (cons (vlax-safearray->list llp) ls1)
                  ls2 (cons (vlax-safearray->list urp) ls2))))
 
    (if (and ls1 ls2)
        (mapcar '(lambda ( a b ) (apply 'mapcar (cons a b))) '(min max) (list ls1 ls2))))
 
    (if (setq selectionset (ssget "_:L-I"))
    (progn
      (setq number    1
            Blockname (strcat "MyBlock" (itoa number))
      )
      (while (tblsearch "BLOCK" Blockname)
        (setq Blockname
               (strcat "MyBlock" (itoa (setq number (1+ number))))
        )
      )
      (or ; why? - select XLINE/RAY/POINT entity
        (setq insertionpoint (getpoint "\n Specify insertion point <Press any key for Center>: "))
        (and
          (setq insertionpoint (LM:ssboundingbox selectionset))
          (setq insertionpoint (apply 'mapcar (cons '(lambda (a b) (/ (+ a b) 2.))  insertionpoint))) ; thanks for this, Lee Mac
        )
        (setq insertionpoint (getpoint "\nYou must specify an insertion point <exit>: "))
      ); or
 
      (command "_.-Block" Blockname insertionpoint selectionset "")
      (command "_.-insert" Blockname insertionpoint "" "" "")
    )
    (princ)
  )
  (princ)
)


ronjonp

  • Needs a day job
  • Posts: 7527
Re: Quick Block modification
« Reply #1 on: December 13, 2018, 09:21:18 AM »
Try this:
Code - Auto/Visual Lisp: [Select]
  1. ;;; Quick Block
  2. ;;; Tharwat 11. May. 2012 ;;
  3. ;;; http://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/quick-block/td-p/3454228
  4. ;;; Modified by HasanCAD and Grrr1337 (in theswamp)
  5. ;;; https://www.theswamp.org/index.php?topic=52634.0
  6.  
  7. (defun c:b (/ selectionset insertionpoint number blockname mn mx pnts)
  8.   (defun lm:ssboundingbox (sel / idx llp ls1 ls2 obj urp) ; Lee mac
  9.     (repeat (setq idx (sslength sel))
  10.       (setq obj (vlax-ename->vla-object (ssname sel (setq idx (1- idx)))))
  11.       (if
  12.         (and
  13.           (vlax-method-applicable-p obj 'getboundingbox)
  14.           (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-getboundingbox (list obj 'llp 'urp))))
  15.         )
  16.          (setq ls1 (cons (vlax-safearray->list llp) ls1)
  17.                ls2 (cons (vlax-safearray->list urp) ls2)
  18.          )
  19.       )
  20.     )
  21.     (if (and ls1 ls2)
  22.       (mapcar '(lambda (a b) (apply 'mapcar (cons a b))) '(min max) (list ls1 ls2))
  23.     )
  24.   )
  25.   (if (and (setq blockname (getstring "\nEnter Block Name: "))
  26.            (snvalid blockname)
  27.            (setq selectionset (ssget "_:L-I"))
  28.       )
  29.     (progn
  30.       (setq number 0)
  31.       (while (tblsearch "BLOCK" blockname)
  32.         (setq blockname (strcat blockname (itoa (setq number (1+ number)))))
  33.       )
  34.       (or                               ; why? - select XLINE/RAY/POINT entity
  35.         (setq insertionpoint (getpoint "\n Specify insertion point <Press any key for Center>: "))
  36.         (and
  37.           (setq insertionpoint (lm:ssboundingbox selectionset))
  38.           (setq insertionpoint (apply 'mapcar (cons '(lambda (a b) (/ (+ a b) 2.)) insertionpoint)))
  39.                                         ; thanks for this, Lee Mac
  40.         )
  41.         (setq insertionpoint (getpoint "\nYou must specify an insertion point <exit>: "))
  42.       )                                 ; or
  43.       (command "_.-Block" blockname insertionpoint selectionset "")
  44.       (command "_.-insert" blockname insertionpoint "" "" "")
  45.     )
  46.     (princ)
  47.   )
  48.   (princ)
  49. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

asami486

  • Guest
Re: Quick Block modification
« Reply #2 on: December 17, 2018, 08:15:17 PM »
Thank you So much.   :-D

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Quick Block modification
« Reply #3 on: December 18, 2018, 09:37:06 AM »
You're welcome!

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC