Jump to content

Understanding Lists


CadProWannabe

Recommended Posts

Is there a resource or someone that can explain lists? I see that a point has x,y,z elements in it. when coding how do I know what variable has multiple entities in it and how to access the certain entities. I know this does not sound like I am explaining right. Lets say I want to change something having to do with MTEXT how would i know how to address the properties that make up MTEXT with lisp.

 

I assume MTEXT has textsize, style, color, layer so how would I tackle these in list to say change the textsize of a selected text entity. It seems like you have to know that x,y,z I guess would be refrenced with 0,1,2 place in a list?:?:

Link to comment
Share on other sites

Lists are pretty easy to understand and manipulate, but there are finer points that allow a true master to do some pretty funky things. The name "lisp" itself comes from "LISt Processing."

 

The short answer is, it's nothing more than a type of data. You've got your real numbers, your integers, your strings, well you've also got your lists. It's similar to an array, if you've had experience with other code languages.

 

The most common list you're going to deal with is a point, which is nothing more than a list composed of three numbers (real numbers or integers, or both). Sometimes you'll use two, and the third number (for the z coordinate) will be zero by default.

 

You can form a list using the AutoCAD function "list", something like:

 

(list 1 2 3)

There is a more advanced technique which uses a comma or the "quote" function, but that can wait.

 

To access the list data once you've created it, there are four main functions you're likely to use/see a lot:

 

car - returns the first item in a list

cadr - returns the second item in a list

caddr - returns the third item in a list

nth num - returns the item which is the "num" index

assoc sym - for an association list, returns the item of the list that begins with "sym"

 

You can consult your AutoCAD help file for a more in-depth explanation of these functions.

 

Again, there are more advanced functions, and the car, cadr, and caddr functions themselves are parts of a larger whole, however those three are what you're going to see often. Other ways to get data from a list are "last," or perhaps "member"

 

There are several functions to modify lists, a common one being "mapcar." Also, there are several interesting Visual Lisp functions which operate on lists. However, if you're going to be dealing with the location of an entity, this should be all you need.

 

Changing the properties of an entity does require list manipulation, and you can see the list you're trying to work with using this bit of code:

 

(entget (car (entsel)))

Entsel returns the entity name and the point that you picked in the form of a list. However, you just want the entity name, so you get the first item in the list (see? the "car" function). Passing the entity name to the "entget" function returns the entity properties in the form of an association list. You can get whatever property you want using "assoc" and "cdr". For example, if you wanted to get the layer of the object, you could do this:

 

(cdr (assoc 8 (entget (car (entsel)))))

because 8 is the DXF code for "layer". Once you get that, you could use "subst" to change the layer, but you'll also need to use "entmod" and "entupd". For actually changing the properties of an object, it's an awful lot easier to use the Visual Lisp functions.

Link to comment
Share on other sites

I'm not sure if this is a reference to our other thread, but here is a good reference for DXF codes:

 

http://autodesk.com/techpubs/autocad/acad2000/dxf/

 

 

As for Lists:

 

Car/Cadr/Caddr Explained:

http://ronleigh.info/autolisp/afude09.htm

http://www.theswamp.org/index.php?topic=31473.0

 

Explanation of the Apostrophe:

http://www.cadtutor.net/forum/showpost.php?p=258390&postcount=20

 

Hope this helps,

 

Lee

Link to comment
Share on other sites

You mention changing the TextHeight of the Selected Entity, here is some food for thought about how you might approach it.

 

Each of the following achieves the same result, with varying efficiency, demonstrating how one problem can be approached in many different ways.

 

 

DXF Code Modification

(defun c:tHgt1 (/ i ss Hgt ent)

 (if (and (setq i -1 ss (ssget "_:L" '((0 . "TEXT,MTEXT"))))
          (setq Hgt (getdist "\nSpecify New Height: ")))
   
   (while (setq ent (ssname ss (setq i (1+ i))))
     (entmod (subst (cons  40 Hgt)
                    (assoc 40 (entget ent)) (entget ent)))))

 (princ))

 

ActiveX Property Modification

(defun c:tHgt2 (/ i ss Hgt ent)
 (vl-load-com)

 (if (and (setq i -1 ss (ssget "_:L" '((0 . "TEXT,MTEXT"))))
          (setq Hgt (getdist "\nSpecify New Height: ")))

   (while (setq ent (ssname ss (setq i (1+ i))))
     (vla-put-Height (vlax-ename->vla-object ent) Hgt)))

 (princ))

 

Using the ActiveX SelectionSet

(defun c:tHgt3 (/ ss Hgt)
 (vl-load-com)

 (if (and (ssget "_:L" '((0 . "TEXT,MTEXT")))
          (setq Hgt (getdist "\nSpecify New Height: ")))           
   (progn
     (vlax-for obj (setq ss (vla-get-ActiveSelectionSet
                              (vla-get-ActiveDocument
                                (vlax-get-acad-object))))
       (vla-put-Height obj Hgt))

     (vla-delete ss)))

 (princ))

Selection using ActiveX

(defun c:tHgt4 (/ MakeVariant ss SelSets Hgt)
 (vl-load-com)

 (defun MakeVariant (typ lst)
   (vlax-make-variant
     (vlax-safearray-fill
       (vlax-make-safearray (eval typ)
         (cons 0 (1- (length lst)))) lst)))

 (if (not (vl-catch-all-error-p
            (setq ss
              (vl-catch-all-apply (function vla-item)
                (list (setq SelSets
                        (vla-get-SelectionSets
                          (vla-get-ActiveDocument
                            (vlax-get-acad-object)))) "TextSS")))))
   (vla-delete ss))

 (setq ss (vla-add SelSets "TextSS"))

 (vla-SelectOnScreen ss
   (MakeVariant vlax-vbInteger '(0))
     (MakeVariant vlax-vbVariant '("TEXT,MTEXT")))

 (if (and (not (zerop (vla-get-count ss)))
          (setq Hgt (getdist "\nSpecify New Height: ")))
   
   (vlax-for obj ss (vla-put-Height obj Hgt)))

 (vla-delete ss)
 (princ))

Link to comment
Share on other sites

This is makin no sense to me.

 

I think for learning perposes what if I write a program that does hte following:

 

1. Select a block, or any entity

2. Display all info about that block in list form.

 

When i look at peoples code through the list editor in Autocad different things change color and its hard to know what are dxf codes, variables, etc I am a visual learner and I have been reading and reading and this stuff is not clicking.

 

I started with something like this posted earlier, but I must be missing something.

 

 
(defun c:test ()
(entget (car (entsel)))
(princ)
)

 

is there a way to make DXF codes come up a ertain color, and system varibales a different color and user defined variables a different color?

Link to comment
Share on other sites

This is makin no sense to me.

 

I think for learning perposes what if I write a program that does hte following:

 

1. Select a block, or any entity

2. Display all info about that block in list form.

 

When i look at peoples code through the list editor in Autocad different things change color and its hard to know what are dxf codes, variables, etc I am a visual learner and I have been reading and reading and this stuff is not clicking.

 

I started with something like this posted earlier, but I must be missing something.

 

 
(defun c:test ()
(entget (car (entsel)))
(princ)
)

is there a way to make DXF codes come up a ertain color, and system varibales a different color and user defined variables a different color?

 

Not that I know of, you just have to learn them/recognise how they are used.

 

I would suggest that you read the Visual LISP Help Files or perhaps AfraLISP/Jeffrey Sanders.

Link to comment
Share on other sites

Did those other links I provided not help at all?

 

Here are a few links if you are starting LISP:

 

http://www.afralisp.net/

http://www.jefferypsanders.com/autolisptut.html

http://ronleigh.info/autolisp/index.htm

Link to comment
Share on other sites

they seem to be good sites and will help, but I just not able to understand the concept, its like they are talking medical terms with somebody that know nothing about the medical field. I need visual aids to learn I need to see what a string of code is doing letter by letter what each step is doing as it would be evaluated by the computer. I have a book on Visual Lisp and have been reading on those sites you gave me to check out. The light bulb is not coming on.

Link to comment
Share on other sites

yes that is what i have been doing, I guess we will see what my brain will retain, nust be getting old:shock:

 

I'm not sure that I can offer anything better, you've just got to have perseverence :)

Link to comment
Share on other sites

CadPro,

First set up VLIDE as follows:

From the pull down menus at the top select Debug & make sure the "Break on Error" is checked.

From the pull down menus at the top select Tools/Enviornmental Options/General

Activate the Diagnostic Tab

Check Report statistics...

Check Print notification ...

Check Echo PRINx ....

Check Inspect drawings....

 

Second Using you sample code to look at enter it into the vlide editor:

(defun c:test ()
 (entget 
   (car 
     (entsel)
   )
 )
 (princ)
)

 

Third From the pull down menus at the top select Tools/Load Text in Editor

 

Use the pictures to help you prepare for a test run.

 

VlideReturnValue.png

VlideWatchWindow.png

VlideBreakSet.png

 

 

Activate ACAD and enter test at the command line. When you hit enter you should be taken

to VLIDE with (entsel) highlited.

 

VlideStartTest.png

 

 

Note that most functions return a value. Some time you don't care what that is and some times you do.

 

VlideListWindow.png

 

That's all I have time for today.

VlideReturnValue.png

VlideWatchWindow.png

VlideBreakSet.png

VlideStartTest.png

VlideListWindow.png

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...