+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 16
  1. #1
    Junior Member thorny's Avatar
    Computer Details
    thorny's Computer Details
    Operating System:
    Windows 7
    Using
    AutoCAD 2010
    Join Date
    Apr 2010
    Location
    Maidstone, UK
    Posts
    10

    Default Copy block attributes from one block to another

    Registered forum members do not see this ad.

    test.lspTitle Blocks.dwg

    Hi. I am hoping that someone may be able to help me achieve copying and pasting attributes from one title block to another.

    I have found a lisp which works to a certain degree, but has 2 issues:
    1. You cannot copy and paste from a block in layout 1 to layout 2.
    2. Our blocks have some duplicated attribute names eg 3xTitle & 2xSite
    I have attached a dwg containing 2 of our borders and the lisp to help explain.

    I understand that the duplicated attributes within each border may posses a challenge to overcome, but we have quite a lot of borders for various tasks and would be great if we didn't have to change the attribute names to eg. Title 1, Title 2, Title 3 etc...

    Obviously if this cannot be overcome, but you can copy attributes across various layouts, that would be hugely advantagous in itself.

    Cheers

  2. #2
    Forum Deity Tharwat's Avatar
    Discipline
    Mechanical
    Tharwat's Discipline Details
    Occupation
    MEP AutoCAD Draftsman
    Discipline
    Mechanical
    Using
    AutoCAD 2014
    Join Date
    Oct 2009
    Location
    Lives in Abu Dhabi
    Posts
    2,631

    Default

    This may help you with it , So check it out Buddy .

    Code:
    (defun c:Action (/ ss ssObj ss1 ssObj1 ssTag1)
     ;;;; Tharwat 02. 03. 2011
      (vl-load-com)
      (if (eq (progn (initget "Copy Paste")
                     (setq ops (getkword "\n What to do [Copy,Paste]:"))
              )
              "Copy"
          )
        (progn
          (setq ss (car (nentsel "\n Select Block with Attributes: ")))
          (setq ssObj (vlax-ename->vla-object ss))
          (setq ssTag (vla-get-TextString ssObj))
        )
        (progn
          (setq ss1 (car (nentsel "\n Select Block to change: ")))
          (setq ssObj1 (vlax-ename->vla-object ss1))
          (setq ssTag1 (vla-put-TextString ssObj1 ssTag))
        )
      )
      (vla-regen (vla-get-activedocument (vlax-get-acad-object))
                 acActiveViewport
      )
      (princ)
    )
    Tharwat

  3. #3
    Junior Member thorny's Avatar
    Computer Details
    thorny's Computer Details
    Operating System:
    Windows 7
    Using
    AutoCAD 2010
    Join Date
    Apr 2010
    Location
    Maidstone, UK
    Posts
    10

    Default

    Hi Tharwat

    Thanks for the code. However I cannot seem to run it. I have loaded it in using APPLOAD, but when I close the Appload Window I get "; error: malformed list on input" at the command line.
    I have tried running typing "Action," but it doesn't work. Am I missing something simple here?

  4. #4
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,741

    Default

    Hi Thorny,

    Nice to see a few other Brits around here

    To my knowledge, you can't use LISP to manually select blocks in different layouts since upon switching layouts, the LISP is cancelled.

    However, this offers another approach which will allow you to select a block and update all blocks with the same name (it should also hopefully work with your blocks with multiple tags with the same name).

    Call with 'test':

    Code:
    (defun c:test ( / _remove1st source ss i ) (vl-load-com)
      ;; © Lee Mac 2011
    
      (defun _remove1st ( item lst )
        (if lst
          (if (equal item (car lst))
            (cdr lst)
            (cons (car lst) (_remove1st item (cdr lst)))
          )
        )
      )
    
      (if
        (setq source
          (LM:Select "\nSelect Source Attributed Block: "
            (function
              (lambda ( x )
                (and (eq "INSERT" (cdr (assoc 0 (entget x)))) (= 1 (cdr (assoc 66 (entget x)))))
              )
            )
            entsel
          )
        )
        (if
          (progn
            (ssdel source
              (setq ss
                (ssget "_X"
                  (list (cons 0 "INSERT") (assoc 2 (entget source)) (cons 66 1))
                )
              )
            )
            (< 0 (setq i (sslength ss)))
          )
          (
            (lambda ( attribs / attribdata )
              (repeat i (setq attribdata attribs)
                (mapcar
                  (function
                    (lambda ( attrib / item )
                      (if (setq item (assoc (vla-get-TagString attrib) attribdata))
                        (progn
                          (vla-put-TextString attrib (cdr item))
                          (setq attribdata (_remove1st item attribdata))
                        )
                      )
                    )
                  )
                  (vlax-invoke (vlax-ename->vla-object (ssname ss (setq i (1- i)))) 'GetAttributes)
                )
              )
              (princ (strcat "\n--> " (itoa (sslength ss)) " Block(s) Updated."))
            )
            (mapcar
              (function
                (lambda ( attrib )
                  (cons (vla-get-TagString attrib) (vla-get-TextString attrib))
                )
              )
              (vlax-invoke (vlax-ename->vla-object source) 'GetAttributes)
            )
          )
          (princ "\n--> Only one instance of Source Block.")
        )
      )
    
      (princ)
    )
     
    (defun LM:Select ( msg pred fun / e ) (setq pred (eval pred))
      ;; © Lee Mac 2011
      
      (while
        (progn (setvar 'ERRNO 0) (setq e (car (fun msg)))      
          (cond
            ( (= 7 (getvar 'ERRNO))
    
              (princ "\n** Missed, Try again **")
            )
            ( (eq 'ENAME (type e))
    
              (if (and pred (not (pred e)))
                (princ "\n** Invalid Object Selected **")
              )
            )
          )
        )
      )
      e
    )
    
    (princ)
    Regards,

    Lee
    Last edited by Lee Mac; 2nd Mar 2011 at 01:09 am.
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

  5. #5
    Forum Deity
    Using
    Civil 3D 2013
    Join Date
    Dec 2005
    Location
    GEELONG AUSTRALIA
    Posts
    3,786

    Default

    Heres the same thing almost in VBA the original idea behind it was a single title block change no matter how many title blocks the extra code allows for pick a block that identifies the block name hope all this makes sense. Just run the add_project_number it will work with your block, change DA1DRTXT to your block name.

    Code:
    Public Sub add_project_number()
    ' This Updates the project number
    Dim SS As AcadSelectionSet
    Dim Count As Integer
    Dim FilterDXFCode(1) As Integer
    Dim FilterDXFVal(1) As Variant
    Dim attribs, newtext As Variant
    Dim BLOCK_NAME As String
    'On Error Resume Next
    Dim startCH As Double
    newtext = ThisDrawing.Utility.GetString(True, "Enter new project code : ")
    FilterDXFCode(0) = 0
    FilterDXFVal(0) = "INSERT"
    FilterDXFCode(1) = 2
    FilterDXFVal(1) = "DA1DRTXT"
    BLOCK_NAME = "DA1DRTXT"
    Set SS = ThisDrawing.SelectionSets.Add("issued")
    SS.Select acSelectionSetAll, , , FilterDXFCode, FilterDXFVal
    
    For Cntr = 0 To SS.Count - 1
       attribs = SS.Item(Cntr).GetAttributes
            
            
            attribs(1).TextString = newtext
                    attribs(1).Update
             
    Next Cntr
    ThisDrawing.SelectionSets.Item("issued").Delete
    
    End Sub
    and
    Code:
    Function Getpitname(Newpitname As String) As String
    Dim PitNameSelect As AcadObject
    Dim pitattribs As Variant
    
    ThisDrawing.Utility.GetEntity PitNameSelect, basepnt, "pick pit name : "
    If PitNameSelect.ObjectName = "AcDbText" Then
      Getpitname = PitNameSelect.TextString
    End If
    If PitNameSelect.ObjectName = "AcDbBlockReference" Then
      pitblname = PitNameSelect.Name   ' RETURNS BLOCK NAME
      pitattribs = PitNameSelect.GetAttributes
      Getpitname = pitattribs(0).TextString
    End If
    
    End Function
    you need to change the line newtext = ThisDrawing.Utility.GetString(True, "Enter new project code : ") to

    Code:
    Dim PitNameSelect As AcadObject
    ThisDrawing.Utility.GetEntity PitNameSelect, basepnt, "pick pit name : "
    Newpitname = "1"  'dummy to pass then return changed
    pitname = Getpitname(Newpitname)
    'Call Getpitname(pitname)
    MsgBox "pitname selected is " & pitname

  6. #6
    Forum Deity Tharwat's Avatar
    Discipline
    Mechanical
    Tharwat's Discipline Details
    Occupation
    MEP AutoCAD Draftsman
    Discipline
    Mechanical
    Using
    AutoCAD 2014
    Join Date
    Oct 2009
    Location
    Lives in Abu Dhabi
    Posts
    2,631

    Default

    Quote Originally Posted by thorny View Post
    Hi Tharwat

    Thanks for the code. However I cannot seem to run it. I have loaded it in using APPLOAD, but when I close the Appload Window I get "; error: malformed list on input" at the command line.
    I have tried running typing "Action," but it doesn't work. Am I missing something simple here?
    That's for sure that you have missed one of the parenthesis while copying codes to your Cad , and that might be the last bracket I guess .

    Retry it and good luck

    Tharwat

  7. #7
    Luminous Being alanjt's Avatar
    Using
    Civil 3D 2011
    Join Date
    Apr 2008
    Posts
    6,015
    DropBox | finding the light...
    Seann: ...it went crazy ex-girlfriend on me...
    eric_monceaux...its pretty funny seeing two AutoCAD Gods give each other flak...

  8. #8
    Junior Member thorny's Avatar
    Computer Details
    thorny's Computer Details
    Operating System:
    Windows 7
    Using
    AutoCAD 2010
    Join Date
    Apr 2010
    Location
    Maidstone, UK
    Posts
    10

    Default

    Thanks for all the replies. Unfortunately nothing is quite working specifically for what we need although the lisps you have provided are great in their own rights. The bit that is not quite working is copying and pasting attibutes from one block to another (with different names), whilst swithing layouts. Swithing the layouts always terminates the lisps. The closest routine I found was http://www.cadtutor.net/forum/showth...all-attributes. Thanks alanjt for this. Is there anyway of switching between layouts whilst in a lisp? Or is there a way to update the attibutes globally (even if the blocks have different names), something similar to the ATTEDIT command, but not having to select the block?

  9. #9
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,741

    Default

    Thorny,

    Mine will currently update blocks of the same name, but you can add other block names to the filter:

    Code:
    (defun c:test ( / _remove1st source ss i ) (vl-load-com)
      ;; © Lee Mac 2011
    
      (defun _remove1st ( item lst )
        (if lst
          (if (equal item (car lst))
            (cdr lst)
            (cons (car lst) (_remove1st item (cdr lst)))
          )
        )
      )
    
      (if
        (setq source
          (LM:Select "\nSelect Source Attributed Block: "
            (function
              (lambda ( x )
                (and (eq "INSERT" (cdr (assoc 0 (entget x)))) (= 1 (cdr (assoc 66 (entget x)))))
              )
            )
            entsel
          )
        )
        (if
          (progn
            (ssdel source
              (setq ss
                (ssget "_X"
                  (list (cons 0 "INSERT") (cons 2 (strcat (cdr (assoc 2 (entget source))) ",BLOCK2,BLOCK3")) (cons 66 1))
                )
              )
            )
            (< 0 (setq i (sslength ss)))
          )
          (
            (lambda ( attribs / attribdata )
              (repeat i (setq attribdata attribs)
                (mapcar
                  (function
                    (lambda ( attrib / item )
                      (if (setq item (assoc (vla-get-TagString attrib) attribdata))
                        (progn
                          (vla-put-TextString attrib (cdr item))
                          (setq attribdata (_remove1st item attribdata))
                        )
                      )
                    )
                  )
                  (vlax-invoke (vlax-ename->vla-object (ssname ss (setq i (1- i)))) 'GetAttributes)
                )
              )
              (princ (strcat "\n--> " (itoa (sslength ss)) " Block(s) Updated."))
            )
            (mapcar
              (function
                (lambda ( attrib )
                  (cons (vla-get-TagString attrib) (vla-get-TextString attrib))
                )
              )
              (vlax-invoke (vlax-ename->vla-object source) 'GetAttributes)
            )
          )
          (princ "\n--> Only one instance of Source Block.")
        )
      )
    
      (princ)
    )
     
    (defun LM:Select ( msg pred fun / e ) (setq pred (eval pred))
      ;; © Lee Mac 2011
      
      (while
        (progn (setvar 'ERRNO 0) (setq e (car (fun msg)))      
          (cond
            ( (= 7 (getvar 'ERRNO))
    
              (princ "\n** Missed, Try again **")
            )
            ( (eq 'ENAME (type e))
    
              (if (and pred (not (pred e)))
                (princ "\n** Invalid Object Selected **")
              )
            )
          )
        )
      )
      e
    )
    
    (princ)
    Shown in red is the name of the selected block, and shown in green are additional blocks to be updated.
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

  10. #10
    Luminous Being alanjt's Avatar
    Using
    Civil 3D 2011
    Join Date
    Apr 2008
    Posts
    6,015

    Default

    Registered forum members do not see this ad.

    Quote Originally Posted by thorny View Post
    Thanks for all the replies. Unfortunately nothing is quite working specifically for what we need although the lisps you have provided are great in their own rights. The bit that is not quite working is copying and pasting attibutes from one block to another (with different names), whilst swithing layouts. Swithing the layouts always terminates the lisps. The closest routine I found was http://www.cadtutor.net/forum/showth...all-attributes. Thanks alanjt for this. Is there anyway of switching between layouts whilst in a lisp? Or is there a way to update the attibutes globally (even if the blocks have different names), something similar to the ATTEDIT command, but not having to select the block?
    On mine, you could just type "ALL" when it comes to the multiple selection of blocks to update. This is a little shaky when you are NOT filtering based on matching block name, and will update if has matching attribute tag.
    DropBox | finding the light...
    Seann: ...it went crazy ex-girlfriend on me...
    eric_monceaux...its pretty funny seeing two AutoCAD Gods give each other flak...

Similar Threads

  1. Replace block and its attributes with new block and attributes
    By SPACECADET in forum AutoCAD Drawing Management & Output
    Replies: 9
    Last Post: 11th Feb 2011, 03:10 pm
  2. Copy a block with Attributes
    By Olhado_ in forum AutoLISP, Visual LISP & DCL
    Replies: 4
    Last Post: 13th Oct 2009, 01:02 pm
  3. Block Attribute answers ability to change block Physical Attributes??
    By Kaidoz in forum AutoLISP, Visual LISP & DCL
    Replies: 6
    Last Post: 19th Mar 2008, 10:25 pm
  4. Copy a Block with Attributes to a new file
    By Kirk Mac in forum AutoLISP, Visual LISP & DCL
    Replies: 5
    Last Post: 2nd Mar 2007, 04:53 pm
  5. Getting Attributes for one block reference w/many block inserts?
    By muck in forum AutoLISP, Visual LISP & DCL
    Replies: 2
    Last Post: 17th Oct 2006, 03:38 am

Bookmarks

Posting Permissions

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