Wednesday, May 22, 2013

Display of Internal Part Numbers On Symbols – AutoCAD Electrical

imageLast week a user asked me how to push the custom USER data to symbols. He created USER1 and USER2 attributes and then edited the catalog data expecting it to update. However this information is not pushed from the database. CAT and MFG are the only columns that push to symbols. The rest of the columns are pushed to reports but not to symbols.
So the question is… Is this possible? Yep. And you already have access to it. It’s in the AutoCAD Electrical help system. It’s in the form of a sample AutoLISP utility in the "API" section of the help under Section F: ace_get_cat_data Function. The text is at the bottom of this post if you can’t find it… I can’t seem to locate the API Help in release 2013.
This LISP is currently pulling USER1, USER2, and USER3 columns from the catalog and pushing them out to selected symbols with the same named attributes.
 
You could easily edit this script and modify it to push any other columns by simply modifying the existing code or adding a few new lines. The function c:ace_get_cat_data pushes out the data in list form. The help shows what order the columns are reported in. And the LISP refers to the columns by number in the order each column is listed in. USER1 is 12.
So
          (if (/= (nth 12 cat_rec) "")
captures the 12th number in the function report and
          (c:wd_modattrval ben "USER1" (nth 12 cat_rec) nil)Pushes that same 12th value into the USER1 attribute of the selected block.
I posted a copy of this LISP here:
Automatic Display of Internal Part Numbers on Electrical Components.lsp
APPLOAD it and then type user_cat_2_symbol and press enter to run the LISP. 
; Extracted from AcadE "Electrical Help" > "API" > "ace_get_cat_data"
;
(defun c:user_cat_2_symbol ( / x xx cat_rec ben blknam
                               mfg cat assycode)
  ; Utility to populate picked component with USER1/USER2/USER3
  ; values pulled from catalog lookup file
  ; Prompt user to pick each schematic or panel component to process
  (while (setq x (entsel "\nPick component:"))
    (setq ben (car x)) ; Extract entity name of picked object
    ; Look for "CAT" attribute or CAT xdata value on the entity
    (if (AND (setq cat (c:wd_get_pnlval ben "CAT"))
             (/= cat ""))
      (progn ; Non-blank CAT value found, okay to continue
        (setq mfg (c:wd_get_pnlval ben "MFG"))
        (setq assycode (c:wd_get_pnlval ben "ASSYCODE"))
        (setq blknam (c:wd_get_pnlval ben "WDBLKNAM"))
        ; If not WDBLKNAM then use the block insert's block name
        ; to use for table selection within the default_cat.mdb
        ; catalog lookup file.
        (if (OR (not blknam)(= blknam ""))
          (progn ; get actual block name
            (setq blknam (cdr (assoc 2 (entget ben))))
        ) )       
        ; Do catalog lookup on the target MFG/CAT/ASSYCODE combination
        (setq x (c:ace_get_cat_data nil mfg cat assycode blknam))         
        (if x  ; some data returned, use only first or only record
               ; returned (which will be the main part number). Discard
               ; any sub-assembly records (which would follow the first
               ; or main part number record of returned data).
          (progn
            (setq cat_rec (car x)) ; get first (or only) record
            ; nth7=catalogdesc, 8=query1, 9=query2,....14=user3
            (if (/= (nth 12 cat_rec) "") ; USER1 value
              (progn ; non-blank USER1 value, write out to USER1
                     ; attribute on block insert (assumes that
                     ; attribute name USER1 exists on the block
                     ; insert!)
                (c:wd_modattrval ben "USER1" (nth 12 cat_rec) nil)
            ) )
            (if (/= (nth 13 cat_rec) "") ; USER2 value
              (progn ; non-blank USER2 value, write out to USER2
                     ; attribute on block insert
                (c:wd_modattrval ben "USER2" (nth 13 cat_rec) nil)
            ) )
            (if (/= (nth 14 cat_rec) "") ; USER3 value
              (progn ; non-blank USER3 value, write out to USER3
                     ; attribute on block insert
                (c:wd_modattrval ben "USER3" (nth 14 cat_rec) nil)
            ) )
        ) )
    ) )
  )
  (princ)
)

4 comments:

  1. To open API Help:
    click Help (question mark in right upper corner) than Additional Resources->API Help

    ReplyDelete
  2. That worked for me in 2012. But in my 2013 the API link is straight text not a link. Any search for API only offers the default AutoCAD .NET and LISP tutorials.

    ReplyDelete
  3. OR, you can just add
    USER1=123456;USER2=$4.45
    to the TEXTVALUE-field directly in the catalog?

    ReplyDelete
  4. Rhesus is correct. The TEXTVALUE field auto-populates attributes on the symbols. However you would need to be sure to add the values in the USER1 AND USER2 fields as well so they would be there for reports if needed.

    So using this method you would enter this information twice but the script listed above would no longer be necessary.

    Great point Rhesus and thank you for the comment.

    ReplyDelete

Note: Only a member of this blog may post a comment.