Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: LISP routine to grab an attribute value from a block (which varies)

  1. #1
    Member
    Join Date
    2017-06
    Posts
    6
    Login to Give a bone
    0

    Default LISP routine to grab an attribute value from a block (which varies)

    I was wondering if someone might be able to help with a bit of code / point me in the right direction. I’m a complete novice and haven’t written anything, I’ve gotten by through modifying routines online.

    I’m after a lisp routine to grab an attribute value from a block (the block name may vary but the attribute tag remains constant). In doing so I want to return this value as part of a pdf file name (i.e. the revision number of the drawing). Is this something that is possible?

    Code:
    (defun c:ppdf()
    (COMMAND "-PLOT"  "Y"  "" "dwg to Pdf"
    "Iso full bleed A3 (420.00 x 297.00 MM)"
    "M"
    "LANDSCAPE"
    "N"
    "W"
    "-24.061, -4.029"
    "816.939, 589.971"
    "1=2"
    "C"
    "Y"
    "monochrome.ctb"
    "Y"
    "N"
    "N"
    "N"
    (strcat (substr (getvar "dwgname") 1 (- (strlen (getvar "dwgname")) 4)) "-REV" "ZZZ")
    "N"
    "Y"
    		))

    Where the “ZZZ” is above I’d like to have that as the attribute value.

    Thanks in advance for any help!
    Last edited by rkmcswain; 2017-07-24 at 12:25 PM. Reason: Added [CODE] tags

  2. #2
    Member
    Join Date
    2015-09
    Posts
    5
    Login to Give a bone
    0

    Default Re: LISP routine to grab an attribute value from a block (which varies)

    First thing, when you are posting some code, use bbcode:
    Code:
    Your code
    And now, I will try to help you.
    Function you will find below, will get all blocks with attributes in current layout. If such blocks were found Then will proceed through selection and return list of revision attribute value.
    Code:
    (defun getrev ( / ss blklst res )
      (if
        (and
          (setq ss
            (ssget 
              "_X"
              (list
                (cons 0 "INSERT")         ; select all blocks
                (cons 66 1)               ; with attributes
                (cons 410 (getvar 'CTAB)) ; in current layout
              )
            )
          )
          (setq blklst (cd:SSX_Convert ss 1)) ; convert selection set to vla-obj list
        )
        ; if T, return list of revision attribute values
        (setq res (mapcar '(lambda (%) (cd:BLK_GetAttValueVLA % "REV")) blklst)); where REV is your att name
        ; else return nil
        (setq res nil)
      )
      res
    )
    ; =========================================================================================== ;
    ; Pobiera wartosc atrybutu / Gets the attribute value                                         ;
    ;  Obj [ENAME/VLA-Object] - entycja lub obiekt VLA / entity name or VLA-Object                ;
    ;  Tag [STR] - etykieta atrybutu / attribute tag                                              ;
    ; ------------------------------------------------------------------------------------------- ;
    ; (cd:BLK_GetAttValueVLA (car (entsel)) "VIEW_NUMBER")                                        ;
    ; =========================================================================================== ;
    (defun cd:BLK_GetAttValueVLA (Obj Tag)
      (if (= (type Obj) (quote ENAME))
        (setq Obj (vlax-ename->vla-object Obj))
      )
      (vl-some
        (function
          (lambda (%)
            (if (eq (strcase tag) (strcase (vla-get-TagString %)))
              (vla-get-TextString %)
            )
          )
        )
        (vlax-invoke Obj (quote GetAttributes))
      )
    )
    ; =========================================================================================== ;
    ; Zmienia PICKSET na liste obiektow / Convert PICKSET to list of objects                      ;
    ;  Ss   [PICKSET] - zbior wskazan / selection sets                                            ;
    ;  Mode [INT]     - typ zwracanych obiektow / type of returned objects                        ;
    ;                   0 = ENAME, 1 = VLA-OBJECT, 2 = SAFEARRAY                                  ;
    ; ------------------------------------------------------------------------------------------- ;
    ; (cd:SSX_Convert (ssget) 1)                                                                  ;
    ; =========================================================================================== ;
    (defun cd:SSX_Convert (Ss Mode / n res)
      (if
        (and
          (member Mode (list 0 1 2))
          (not
            (minusp
              (setq n
                (if Ss (1- (sslength Ss)) -1)
              )
            )
          )
        )
        (progn
          (while (>= n 0)
            (setq res
              (cons
                (if (zerop Mode)
                  (ssname Ss n)
                  (vlax-ename->vla-object (ssname Ss n))
                )
                res
              )
                  n (1- n)
            )
          )
          (if (= Mode 2)
            (vlax-safearray-fill
              (vlax-make-safearray 9
                (cons 0 (1- (length res)))
              ) res
            )
            res
          )
        )
      )
    )

  3. #3
    I could stop if I wanted to
    Join Date
    2005-06
    Location
    CORDOBA-ARGENTINA
    Posts
    275
    Login to Give a bone
    0

    Default Re: LISP routine to grab an attribute value from a block (which varies)

    Hi NZ. Di you try with ATTOUT, a simple acad command it give you a txt file .

  4. #4
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,106
    Login to Give a bone
    0

    Default Re: LISP routine to grab an attribute value from a block (which varies)

    Here is a place to start.

    The syntax is

    Code:
     (TitleBlockValue strBlockname strTagString)
    and it will return the textstring value of the first attribute with the specified tagstring in the first instance of the strBlockName block

    P=

    Code:
    ;___________________________________________________________________________________________________________|
    ;
    ; Written By: Peter Jamtgaard copyright 2017 All Rights Reserved
    ;___________________________________________________________________________________________________________|
    ;
    ; Abstract: This set of functions are designed to get an attribute textstring from the first instance of a 
    ; (or selected) block reference i 
    ;___________________________________________________________________________________________________________|
    ;
    ; Command Line Function Header List
    ;___________________________________________________________________________________________________________|
    
    ;* C:TitleBlockValue
    ;* Command Line Function to get a value from a block reference and a tagstring (test function)
    
    ;___________________________________________________________________________________________________________|
    ;
    ; General Function Header List 
    ;___________________________________________________________________________________________________________|
    
    ;  Function, Arguments and Description
    
    ;* (AttributeSublists objBlockReference)
    ;* Function to create a list of sublists from a block reference of its attribute tagstring, textstring and handle
    
    ;* (AttributeTextString objSelection strTagString)
    ;* Function to get a textstring value from a block reference and a tagstring
    
    ;* (TitleBlockValue strBlockname strTagString)
    ;* Function to get a value from the first instance of a block reference
    
    ;$ End Header
    
    ;___________________________________________________________________________________________________________|
    ;
    ; Command line Function to get an attribute value from a selected block and entered attribute tagstring.
    ;___________________________________________________________________________________________________________|
    
    (defun C:TitleBlockValue (/ entSelection objSelection ssSelections strTagString)
     (if (and (princ "\nSelect Title Block: ")
              (setq ssSelections (ssget ":S:E" (list (cons 0 "INSERT")(cons 66 1))))
              (setq strTagString (getstring "\nEnter TagString: "))
              (setq entSelection (ssname ssSelections 0))
              (setq objSelection (vlax-ename->vla-object entSelection))
         )
      (AttributeTextString objSelection strTagString)
     )
    )
    
    ;___________________________________________________________________________________________________________|
    ;
    ; Function to create a list of sublists from a block reference of its attribute tagstring, textstring and handle
    ;___________________________________________________________________________________________________________|
    
    (defun AttributeSublists (objBlockReference / lstAttributes objAttribute)
     (if (and (vlax-method-applicable-p objBlockReference "getattributes")
              (setq lstAttributes (vlax-invoke objBlockReference "getattributes"))
         )
      (setq lstOfSublists (mapcar '(lambda (objAttribute)(list (strcase (vla-get-tagstring objAttribute))
                                                               (vla-get-textstring objAttribute)
                                                               (vla-get-handle objAttribute)
                                                         )
                                   ) 
                                   lstAttributes
                          )
      )
     )
    )
    
    ;___________________________________________________________________________________________________________|
    ;
    ; Function to get a textstring value from a block reference and a tagstring
    ;___________________________________________________________________________________________________________|
    
    (defun AttributeTextString (objSelection strTagString)
     (if (setq lstOfSublists (AttributeSublists objSelection))
      (cadr (assoc (strcase strTagString) lstOfSublists))
     )
    )
    
    ;___________________________________________________________________________________________________________|
    ;
    ; Function to get a value from the first instance of a block reference
    ;___________________________________________________________________________________________________________|
    
    (defun TitleBlockValue (strBlockname strTagString / entSelection objSelection ssSelections)
     (if (and (setq ssSelections    (ssget "x" (list (cons 2 strBlockName)(cons 66 1))))
              (setq entSelection    (ssname ssSelections 0))
              (setq objSelection    (vlax-ename->vla-object entSelection))
         )
      (AttributeTextString objSelection strTagString)
     )
    )
    
    (princ "!")
    (vl-load-com)
    Attached Files Attached Files
    Last edited by peter; 2017-07-25 at 12:18 AM.
    AutomateCAD

  5. #5
    All AUGI, all the time
    Join Date
    2003-07
    Posts
    555
    Login to Give a bone
    0

    Default Re: LISP routine to grab an attribute value from a block (which varies)

    Nz is there a reason your title block name is not known makes the search for block a lot easier. All our automated plot routines have a hard coded title block.

    As an example plot a range of layouts to individual pdf's and combine into one as a bonus. Projectxxx D01-D08.pdf would be 8 pdf's Projectxxx D01, D02etc.

  6. #6
    Member
    Join Date
    2017-06
    Posts
    6
    Login to Give a bone
    0

    Default Re: LISP routine to grab an attribute value from a block (which varies)

    Quote Originally Posted by BIG-AL View Post
    Nz is there a reason your title block name is not known makes the search for block a lot easier.
    Ideally it would be but I'd like to make the routine full proof.

    ziele_o2k's code works perfectly for returning the value, but I don't know how to get this to replace the "ZZZ" in my basic plot code.

  7. #7
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,106
    Login to Give a bone
    0

    Default Re: LISP routine to grab an attribute value from a block (which varies)

    Ideally it would be but I'd like to make the routine full proof.
    What is the name of the title block and what is the tagstring of the attribute.

    If the titleblock is called "MyTitleBlock" and the Attribute Tag is "REVISION" this is how you use the code I supplied to name the drawing.


    (TitleBlockValue "MyTitleBlock" "REVISION")

    would return "7/30/2017"

    so your code would be

    Code:
    (defun C:PPDF (/ strRevision)
     (and
      (setq strRevision (TitleBlockValue "MyTitleBlock" "REVISION"))
      (COMMAND "-PLOT"  
               "Y"  
               "" 
               "dwg to Pdf"
               "Iso full bleed A3 (420.00 x 297.00 MM)"
               "M"
               "LANDSCAPE"
               "N"
               "W"
               "-24.061, -4.029"
               "816.939, 589.971"
               "1=2"
               "C"
               "Y"
               "monochrome.ctb"
               "Y"
               "N"
               "N"
               "N"
               (strcat (substr (getvar "dwgname") 1 (- (strlen (getvar "dwgname")) 4)) "-REV" strRevision)
               "N"
               "Y"
      )
     )
    )
    AutomateCAD

  8. #8
    All AUGI, all the time
    Join Date
    2003-07
    Posts
    555
    Login to Give a bone
    0

    Default Re: LISP routine to grab an attribute value from a block (which varies)

    This is a universal method for any block with attributes, when you look at output you can see its in attribute order so you can use I want attribute 3, the only little gotcha is that attributes start at 0. So use 3-1. Its example code to illustrate the method you would use it as a library routine without the princ.

    You just enter the attribute order number. In a more advanced method I use a dcl list so you can pick it. It also allows for multiple picking where you want multiple attributes pulled from multiples of the same block name.

    Code:
    (defun c:test ( / obj num x )
    (setq num (- (getint "Enter the attribute order number") 1))
    (setq obj (vlax-ename->vla-object (car (entsel "\nPick object"))))
    (if (and (vlax-property-available-p obj 'hasAttributes)(eq (vla-get-HasAttributes obj) :vlax-true))
    (progn
    (setq x 0)
    (foreach att (vlax-invoke Obj 'GetAttributes)
    (princ ( strcat (rtos x 2 0) "= " (vla-get-Textstring att) "\n"))
    ;(princ ( strcat (rtos x 2 0) "= " (vla-get-Tagstring att) "\n"))
    (if (= x num)(setq ans (vla-get-Textstring att)))
    (setq x (+ x 1))
    )
    )
    )
    (alert (strcat "You picked " (rtos num 2 0) " attribute value is "  ans))
    )

  9. #9
    Member
    Join Date
    2017-06
    Posts
    6
    Login to Give a bone
    0

    Default Re: LISP routine to grab an attribute value from a block (which varies)

    Quote Originally Posted by peter View Post
    What is the name of the title block and what is the tagstring of the attribute.
    Thanks for that. The problem I have is that the title block name varies (these are typically renamed from job to job) but the attribute tag is always the same, so hence I’d like it to work for all title block names. The attribute tag is called “REVISION” as you have it and its value will either be a letter or a number.

    It's something I'd like to be able to batch with a script program so pausing to pick the title block is not a viable option.

  10. #10
    All AUGI, all the time
    Join Date
    2003-07
    Posts
    555
    Login to Give a bone
    0

    Default Re: LISP routine to grab an attribute value from a block (which varies)

    You can use ssname and get a block from a selection set, then use something like this, I change around 5 attributes in one go in one routine. Like sheet x of Y as it goes through all the layouts updating the title block.

    Code:
    (foreach att (vlax-invoke (vlax-ename->vla-object (ssname SS 0 )) 'getattributes)
     
           (if (= "REVISION" (strcase (vla-get-tagstring att)))
            (vla-put-textstring att newstr) 
            ) ; end if
    ) ; foreach

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 2016-11-03, 03:13 PM
  2. Set file name from block attribute with lisp routine
    By email.dnewton396831 in forum AutoLISP
    Replies: 0
    Last Post: 2013-07-03, 08:51 PM
  3. Replies: 9
    Last Post: 2006-07-11, 11:09 PM
  4. Attribute block switching routine
    By jason.cyr13 in forum AutoLISP
    Replies: 10
    Last Post: 2006-03-28, 07:22 PM
  5. how to grab and modify enities within a block
    By clintonc in forum AutoLISP
    Replies: 3
    Last Post: 2004-09-01, 09:30 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
  •