Results 1 to 8 of 8

Thread: Create a new block from an existing block in autocad

  1. #1
    Member
    Join Date
    2007-08
    Posts
    4
    Login to Give a bone
    0

    Default help...Create a new block from an existing block in autocad

    In adt it is possible to right click a block and create a new block from it. In AutoCAD ( I am using version 2008 ) this function is not available. Is there a Lisp or macro that recreates this function?

    I have googled and nothing relevant pops up.

    Thanks in advance.
    Last edited by crcampbell; 2009-06-04 at 03:33 PM.

  2. #2
    I could stop if I wanted to
    Join Date
    2007-08
    Posts
    202
    Login to Give a bone
    0

    Default Re: Create a new block from an existing block in autocad

    Hi

    Something like this ?

    Code:
    ;;; NBL (gile) 2007/05/05
    ;;; Creates a new block definition similar to the selected reference
    
    (defun c:nbl (/	old-ref	new-name AcDoc Space Blocks old-name old-block new-block
    	      obj new-ref)
      (vl-load-com)
      (while (not
    	   (and
    	     (setq old-ref (car (entsel "\nSelect the block to re-create: ")))
    	     (= "INSERT" (cdr (assoc 0 (entget old-ref))))
    	   )
    	 )
        (princ "\nInvalid object.")
      )
      (while
        (not
          (and
    	(setq
    	  new-name (getstring T "\nEnter the new block name: ")
    	)
    	(/= new-name "")
    	(null (tblsearch "BLOCK" new-name))
          )
        )
         (princ "\nInvalid name.")
      )
      (setq	AcDoc	  (vla-get-ActiveDocument (vlax-get-acad-object))
    	Space	  (if (= (getvar "CVPORT") 1)
    		    (vla-get-PaperSpace AcDoc)
    		    (vla-get-ModelSpace AcDoc)
    		  )
    	Blocks	  (vla-get-Blocks acDoc)
    	old-ref	  (vlax-ename->vla-object old-ref)
    	old-name  (if (vlax-property-available-p old-ref 'EffectiveName)
    		    (vla-get-EffectiveName old-ref)
    		    (vla-get-name old-ref)
    		  )
    	old-block (vla-item Blocks old-name)
    	new-block (vla-add Blocks
    			   (vlax-3d-point '(0 0 0))
    			   new-name
    		  )
    
      )
      (vlax-for o old-block
        (setq obj (cons o obj))
      )
      (vlax-invoke AcDoc 'CopyObjects obj new-block)
      (and (vlax-property-available-p old-block 'Units)
           (vla-put-Units new-block (vla-get-Units old-block))
      )
      (setq	new-ref
    	 (vla-insertblock
    	   Space
    	   (vlax-3d-point '(0 0 0))
    	   new-name
    	   (vla-get-XScaleFactor old-ref)
    	   (vla-get-YScaleFactor old-ref)
    	   (vla-get-ZScaleFactor old-ref)
    	   (vla-get-Rotation old-ref)
    	 )
      )
      (vla-put-Normal new-ref (vla-get-Normal old-ref))
      (vla-put-InsertionPoint
        new-ref
        (vla-get-InsertionPoint old-ref)
      )
      (if (= (vla-get-HasAttributes old-ref) :vlax-true)
        (progn
          (setq old-att (mapcar '(lambda (att) (cons (vla-get-TagString att) att))
    			    (vlax-invoke old-ref 'getAttributes)
    		    )
    	    new-att (mapcar '(lambda (att) (cons (vla-get-TagString att) att))
    			    (vlax-invoke new-ref 'getAttributes)
    		    )
          )
          (foreach att new-att
    	(foreach prop (list
    			'Alignment   'Backward	  'Color       'FieldLength
    			'Height	     'InsertionPoint	       'Invisible
    			'Layer	     'TextString  'Linetype    'LinetypeScale
    			'Lineweight  'Material	  'Normal      'ObliqueAngle
    			'Rotation    'ScaleFactor 'StyleName   'TextString
    			'Thickness   'TrueColor	  'UpsideDown  'Visible
    		       )
    	  (if (vlax-property-available-p
    		(cdr (assoc (car att) old-att))
    		prop
    	      )
    	    (vlax-put (cdr att)
    		      prop
    		      (vlax-get (cdr (assoc (car att) old-att)) prop)
    	    )
    	  )
    	)
          )
        )
      )
      (vla-delete old-ref)
      (princ (strcat "The block \"" new-name "\" has been created."))
      (princ)
    )

  3. #3
    The Silent Type RobertB's Avatar
    Join Date
    2000-01
    Location
    Seattle WA USA
    Posts
    5,859
    Login to Give a bone
    0

    Default Re: Create a new block from an existing block in autocad

    Quote Originally Posted by crcampbell View Post
    In adt it is possible to right click a block and create a new block from it. In AutoCAD ( I am using version 2008 ) this function is not available. Is there a Lisp or macro that recreates this function?

    I have googled and nothing relevant pops up.

    Thanks in advance.
    The Block Editor has a Save As option. Does the same thing as you want, just in a different interface.
    R. Robert Bell
    Design Technology Manager
    Stantec
    Opinions expressed are mine alone and do not reflect the views of Stantec.

  4. #4
    Member
    Join Date
    2007-08
    Posts
    4
    Login to Give a bone
    0

    Default Re: Create a new block from an existing block in autocad

    Thanks alot I will give that a try...

    Quote Originally Posted by 'gile' View Post
    Hi

    Something like this ?

    Code:
    ;;; NBL (gile) 2007/05/05
    ;;; Creates a new block definition similar to the selected reference
    
    (defun c:nbl (/	old-ref	new-name AcDoc Space Blocks old-name old-block new-block
    	      obj new-ref)
      (vl-load-com)
      (while (not
    	   (and
    	     (setq old-ref (car (entsel "\nSelect the block to re-create: ")))
    	     (= "INSERT" (cdr (assoc 0 (entget old-ref))))
    	   )
    	 )
        (princ "\nInvalid object.")
      )
      (while
        (not
          (and
    	(setq
    	  new-name (getstring T "\nEnter the new block name: ")
    	)
    	(/= new-name "")
    	(null (tblsearch "BLOCK" new-name))
          )
        )
         (princ "\nInvalid name.")
      )
      (setq	AcDoc	  (vla-get-ActiveDocument (vlax-get-acad-object))
    	Space	  (if (= (getvar "CVPORT") 1)
    		    (vla-get-PaperSpace AcDoc)
    		    (vla-get-ModelSpace AcDoc)
    		  )
    	Blocks	  (vla-get-Blocks acDoc)
    	old-ref	  (vlax-ename->vla-object old-ref)
    	old-name  (if (vlax-property-available-p old-ref 'EffectiveName)
    		    (vla-get-EffectiveName old-ref)
    		    (vla-get-name old-ref)
    		  )
    	old-block (vla-item Blocks old-name)
    	new-block (vla-add Blocks
    			   (vlax-3d-point '(0 0 0))
    			   new-name
    		  )
    
      )
      (vlax-for o old-block
        (setq obj (cons o obj))
      )
      (vlax-invoke AcDoc 'CopyObjects obj new-block)
      (and (vlax-property-available-p old-block 'Units)
           (vla-put-Units new-block (vla-get-Units old-block))
      )
      (setq	new-ref
    	 (vla-insertblock
    	   Space
    	   (vlax-3d-point '(0 0 0))
    	   new-name
    	   (vla-get-XScaleFactor old-ref)
    	   (vla-get-YScaleFactor old-ref)
    	   (vla-get-ZScaleFactor old-ref)
    	   (vla-get-Rotation old-ref)
    	 )
      )
      (vla-put-Normal new-ref (vla-get-Normal old-ref))
      (vla-put-InsertionPoint
        new-ref
        (vla-get-InsertionPoint old-ref)
      )
      (if (= (vla-get-HasAttributes old-ref) :vlax-true)
        (progn
          (setq old-att (mapcar '(lambda (att) (cons (vla-get-TagString att) att))
    			    (vlax-invoke old-ref 'getAttributes)
    		    )
    	    new-att (mapcar '(lambda (att) (cons (vla-get-TagString att) att))
    			    (vlax-invoke new-ref 'getAttributes)
    		    )
          )
          (foreach att new-att
    	(foreach prop (list
    			'Alignment   'Backward	  'Color       'FieldLength
    			'Height	     'InsertionPoint	       'Invisible
    			'Layer	     'TextString  'Linetype    'LinetypeScale
    			'Lineweight  'Material	  'Normal      'ObliqueAngle
    			'Rotation    'ScaleFactor 'StyleName   'TextString
    			'Thickness   'TrueColor	  'UpsideDown  'Visible
    		       )
    	  (if (vlax-property-available-p
    		(cdr (assoc (car att) old-att))
    		prop
    	      )
    	    (vlax-put (cdr att)
    		      prop
    		      (vlax-get (cdr (assoc (car att) old-att)) prop)
    	    )
    	  )
    	)
          )
        )
      )
      (vla-delete old-ref)
      (princ (strcat "The block \"" new-name "\" has been created."))
      (princ)
    )

  5. #5
    Member
    Join Date
    2007-08
    Posts
    4
    Login to Give a bone
    0

    Default Re: Create a new block from an existing block in autocad

    You know what I didn't even think of that, sometimes you cannot see the forrest for the trees.

    Thanks much.

    Quote Originally Posted by RobertB View Post
    The Block Editor has a Save As option. Does the same thing as you want, just in a different interface.

  6. #6
    Certifiable AUGI Addict
    Join Date
    2015-11
    Location
    Jo'burg SA
    Posts
    4,512
    Login to Give a bone
    0

    Default Re: Create a new block from an existing block in autocad

    This would probably not work for dynamic blocks, as neither LISP nor VBA allows for dynamic parameters inside a block definition.

    Why not simply make a WBLOCK to the new name, then INSERT from that file. It shouldn't be too difficult using LISP.

  7. #7
    The Silent Type RobertB's Avatar
    Join Date
    2000-01
    Location
    Seattle WA USA
    Posts
    5,859
    Login to Give a bone
    0

    Default Re: Create a new block from an existing block in autocad

    Quote Originally Posted by irneb View Post
    This [lisp-based solution] would probably not work for dynamic blocks, as neither LISP nor VBA allows for dynamic parameters inside a block definition.

    Why not simply make a WBLOCK to the new name, then INSERT from that file. It shouldn't be too difficult using LISP.
    Why would you go thru all that when BEdit has a SaveAs button?
    R. Robert Bell
    Design Technology Manager
    Stantec
    Opinions expressed are mine alone and do not reflect the views of Stantec.

  8. #8
    Certifiable AUGI Addict
    Join Date
    2015-11
    Location
    Jo'burg SA
    Posts
    4,512
    Login to Give a bone
    0

    Default Re: Create a new block from an existing block in autocad

    Sure it's the easiest built-in method, but sometimes (if you're using AnnoScales extensively) just opening BEdit takes quite a while. And if you're in a PS tab it first needs to go to MS then into BEdit, so regens happen just to you can make a duplicate of a block def?
    Code:
    ;;; Create a duplicate block definition
    (defun c:DupBlock (/ en ed bname nname fname fdia)
      (if (and (setq en (entsel "\nPick block: "))
               (setq ed (entget (car en)))
               (= (cdr (assoc 0 ed)) "INSERT")
               (setq nname (getstring nil "\nNew name: "))
          ) ;_ end of and
        (progn
          (setq bname (cdr (assoc 2 ed))
                fname (strcat (getvar "TEMPPREFIX") nname ".DWG")
          ) ;_ end of setq
          (if (findfile fname)
            (vl-file-delete fname)
          ) ;_ end of if
          (setq fdia (getvar "FILEDIA"))
          (setvar "FILEDIA" 0)
          (command ".-WBLOCK" fname bname)
          (command ".-INSERT" fname)
          (setvar "FILEDIA" fdia)
        ) ;_ end of progn
      ) ;_ end of if
      (princ)
    ) ;_ end of defun

Similar Threads

  1. Insert a new block using data from existing block
    By mike.43789 in forum AutoLISP
    Replies: 3
    Last Post: 2015-01-12, 11:50 PM
  2. Replies: 6
    Last Post: 2012-08-15, 04:55 PM
  3. Replies: 2
    Last Post: 2012-06-06, 12:28 PM
  4. Replies: 2
    Last Post: 2010-08-23, 02:53 PM
  5. Existing Block does not update when new Block is inserted
    By Rachel Ritchie in forum AutoCAD General
    Replies: 4
    Last Post: 2005-12-05, 03:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •