Author Topic: AutoLisp (cons)  (Read 6106 times)

0 Members and 1 Guest are viewing this topic.

ChrisCarlson

  • Guest
AutoLisp (cons)
« on: July 24, 2013, 10:04:03 AM »
I'm drawing a blank, maybe it's because it's hump day  :ugly:

I have a attribute in a dynamic block named COL_ID with a value that is prompted upon insertion. Pretty typical no big deal however I have a lisp which changes the value of attributes but I need to set it so it only changes if attribute = COL_ID.

I borrowed this from lee-mac and changed it around so I could learn how things were working but here is the important section. How ever I am not sure why this is proving difficult this morning.  :pissed:

Code: [Select]
       (if
(and
           curText
          (member(cdr(assoc 0(entget curText))) '("ATTRIB"))
           ); end and
         (progn
(vla-put-TextString
(vlax-ename->vla-object curText)curStr)
(setq grnum_seq(1+ grnum_seq))
         ); end progn
        (princ "\n--- THIS IS NOT THE COL_ID ATTRIBUTE ---- ")
        ); end if

I used to have a bookmark which was for a website listing all the Autocad cons, their value and what they did, eg; 0 = entitiy, 5 = layer, etc. Does anyone know the website I'm talking about?

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: AutoLisp (cons)
« Reply #1 on: July 24, 2013, 10:15:10 AM »
I used to have a bookmark which was for a website listing all the Autocad cons, their value and what they did, eg; 0 = entitiy, 5 = layer, etc. Does anyone know the website I'm talking about?

Here is a link to the latest DXF Reference:

http://exchange.autodesk.com/autocad/enu/online-help/browse#WS73099cc142f48755a52158612bd434e551-7fdd.htm

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: AutoLisp (cons)
« Reply #2 on: July 24, 2013, 10:16:11 AM »
You're looking for DXF codes.  The AutoDesk website (and old-style stand-alone CHM help) has them listed along with the LISP function help.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

ronjonp

  • Needs a day job
  • Posts: 7529
Re: AutoLisp (cons)
« Reply #3 on: July 24, 2013, 10:22:30 AM »
I'd use visual lisp:


Code: [Select]
(defun _putattributevalue (block tag value / att)
  (if (and (setq block (cond ((= (type block) 'ename) (vlax-ename->vla-object block))
     ((= (type block) 'vla-object) block)
       )
   )
   (vlax-property-available-p block 'hasattributes)
      )
    (foreach att (vlax-invoke block 'getattributes)
      (if (eq (strcase tag) (strcase (vla-get-tagstring att)))
(vla-put-textstring
  att
  (if (= (type value) 'str)
    value
    (vl-princ-to-string value)
  )
)
      )
    )
  )
)
(_putattributevalue (car (entsel)) "COL_ID" "1")

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ChrisCarlson

  • Guest
Re: AutoLisp (cons)
« Reply #4 on: July 24, 2013, 10:55:28 AM »
Could I simply add (= (list(cons 2)) "COL_ID") within an IF statement?

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: AutoLisp (cons)
« Reply #5 on: July 25, 2013, 12:58:29 AM »
Could I simply add (= (list(cons 2)) "COL_ID") within an IF statement?
That's not going to work, you want the reverse of cons. Cons is how you create the list. To inspect the list you use the car / cdr / nth functions (or a combination of those). Also the DWF data is inside what's known as an association list, the 0, 2, etc. being the key value to which it is associated.

It works much the same as dictionary data structures. E.g. with dictionaries you'd add an item like this:
Code - Visual Basic: [Select]
  1. DictVar.Add(<Key1>, <Value1>)
  2. DictVar.Add(<Key2>, <Value2>)
Then to extract it you'd use something like this:
Code - Visual Basic: [Select]
  1. Val = DictVar.Get(<Key1>)

In Lisp's association list you use the assoc function to do the same thing. E.g.
Code - Auto/Visual Lisp: [Select]
  1. (setq dataList '((0 . "EntityType") (1 . "Contents") (2 . "Name") ... ))
  2. (assoc 2 dataList) ;Returns the 1st item starting with 2 - in this case (2 . "Name")
  3. ;; To get it's value you use cdr to return the "rest" of the list excluding the 1st position
  4. ;; In this case that's a dotted pair, so cdr returns only the value
  5. (cdr (assoc 2 dataList)) ;Returns "Name"
So in your check you can use this:
Code - Auto/Visual Lisp: [Select]
  1. (if (and curText ;Check if an ename was obtained
  2.          (setq curData (entget curText)) ;Get the DXF data list from the entname of the object
  3.          (= (cdr (assoc 0 curData)) "ATTRIB") ;Check if it is an attribute
  4.          (= (cdr (assoc 2 curData)) "COL_ID") ;Check if the attribute's name is COL_ID
  5.     )
  6.   (progn ;Start you code on the COL_ID attribute here
  7.   )
  8.   (progn ;Else if any one of those tests failed do something else
  9.   )
  10. )
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

ChrisCarlson

  • Guest
Re: AutoLisp (cons)
« Reply #6 on: July 25, 2013, 08:21:30 AM »
Could I simply add (= (list(cons 2)) "COL_ID") within an IF statement?
That's not going to work, you want the reverse of cons. Cons is how you create the list. To inspect the list you use the car / cdr / nth functions (or a combination of those). Also the DWF data is inside what's known as an association list, the 0, 2, etc. being the key value to which it is associated.

It works much the same as dictionary data structures. E.g. with dictionaries you'd add an item like this:
Code - Visual Basic: [Select]
  1. DictVar.Add(<Key1>, <Value1>)
  2. DictVar.Add(<Key2>, <Value2>)
Then to extract it you'd use something like this:
Code - Visual Basic: [Select]
  1. Val = DictVar.Get(<Key1>)

In Lisp's association list you use the assoc function to do the same thing. E.g.
Code - Auto/Visual Lisp: [Select]
  1. (setq dataList '((0 . "EntityType") (1 . "Contents") (2 . "Name") ... ))
  2. (assoc 2 dataList) ;Returns the 1st item starting with 2 - in this case (2 . "Name")
  3. ;; To get it's value you use cdr to return the "rest" of the list excluding the 1st position
  4. ;; In this case that's a dotted pair, so cdr returns only the value
  5. (cdr (assoc 2 dataList)) ;Returns "Name"
So in your check you can use this:
Code - Auto/Visual Lisp: [Select]
  1. (if (and curText ;Check if an ename was obtained
  2.          (setq curData (entget curText)) ;Get the DXF data list from the entname of the object
  3.          (= (cdr (assoc 0 curData)) "ATTRIB") ;Check if it is an attribute
  4.          (= (cdr (assoc 2 curData)) "COL_ID") ;Check if the attribute's name is COL_ID
  5.     )
  6.   (progn ;Start you code on the COL_ID attribute here
  7.   )
  8.   (progn ;Else if any one of those tests failed do something else
  9.   )
  10. )

Wow, that actually makes quite a bit of sense you can't extract data unless you put data in.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: AutoLisp (cons)
« Reply #7 on: July 25, 2013, 08:31:15 AM »
Wow, that actually makes quite a bit of sense you can't extract data unless you put data in.
8-)
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.