Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

um, not as simple as I thought. . .

14 REPLIES 14
Reply
Message 1 of 15
Anonymous
196 Views, 14 Replies

um, not as simple as I thought. . .

Guys,

I could really use some help. I'm trying to write a little lisp ( I know, I
should leave it to the pros - Judging from past experience) and it is not
working out. I'm kind of scrapping from other things you all have helped me
with, but I guess I just don't know enough - yet - to paste it all together.
I've spent much to much time this morning not understanding how it works.
It's like this: I can follow a program, just not lead it. Please, take a
look.

I'm trying to be able to select a circle and then an attribute - quick and
dirty - and be able to write the circle's radius into the attribute. Having
the little ' at the end of the 30' would be nice, but dreamy. Here's my
latest version


(defun c:r2a ( / )
;get radius
(while (or (not ss)
(> (sslength ss) 1))
(princ "\nSelect 1 circle")
(setq ss (ssget)))
(and ss
(setq en (ssname ss 0)
ed (entget en)
rd (cdr (assoc 40 ed)));rd holds value of radius now
)
;now - change the attribute
(if (setq att (nentsel "\nSelect attribute to change: "))
;if makes it so you don't get shut out?!
(if (= "ATTRIB" (cdr (assoc 0 (entget (car old-rad)))));check if you
select att or not
(setq ent (subst (cons 1 (rtos old-rad 2 2)) (assoc 1 ent)
ent));set ent to att value
)
(entmod ent)
(entupd (car att));update att entity
(princ "must select an attribute")
);if=att
);if select
;or from geotools
;; !
***************************************************************************
;; ! BL_WriteAttrNo
;; !
***************************************************************************
;; ! Function : Write a value into a specified attribute at a given position
;; ! in a block
;; ! Argument : 'ename' - entity name of the block
;; ! 'pos' - position of the attribute in the block
;; ! 'Value' - Value to be updated
;; ! Returns : The updated block entity name if succesful otherwise nil
;; ! Updated : November 12, 1998
;; ! Copyright: (C) 2000, Four Dimension Technologies, Singapore
;; ! Contact : rakesh.rao@4d-technologies.com for help/support/info

(defun BL_WriteAttrNo ( ename pos value / aname entl )
(setq aname (BL_FindAttrNo ename pos)) ; Search for attribute
(if aname
(progn
(setq
entl (entget aname)
entl (subst (cons 1 value) (assoc 1 entl) entl)
)
(entmod entl)
(entupd ename)
))
ename
)

Thanks for any help you can offer. I hope its ok to post all that from
4dimt - I like how their stuff is organized, just how do it work?

Matt Fridell
saladays@gwtc.net
14 REPLIES 14
Message 2 of 15
Anonymous
in reply to: Anonymous

ATTRIBs are part of INSERTs. You can't just pick 1. Any block in
particular your trying to get to? -David


"sundog" wrote in message
news:BAACB3666F23AA3A7FCF78CD62B0D6AC@in.WebX.maYIadrTaRb...
> Guys,
>
> I could really use some help. I'm trying to write a little lisp ( I know,
I
> should leave it to the pros - Judging from past experience) and it is not
> working out. I'm kind of scrapping from other things you all have helped
me
> with, but I guess I just don't know enough - yet - to paste it all
together.
> I've spent much to much time this morning not understanding how it works.
> It's like this: I can follow a program, just not lead it. Please, take a
> look.
>
> I'm trying to be able to select a circle and then an attribute - quick and
> dirty - and be able to write the circle's radius into the attribute.
Having
> the little ' at the end of the 30' would be nice, but dreamy. Here's my
> latest version
>
>
> (defun c:r2a ( / )
> ;get radius
> (while (or (not ss)
> (> (sslength ss) 1))
> (princ "\nSelect 1 circle")
> (setq ss (ssget)))
> (and ss
> (setq en (ssname ss 0)
> ed (entget en)
> rd (cdr (assoc 40 ed)));rd holds value of radius now
> )
> ;now - change the attribute
> (if (setq att (nentsel "\nSelect attribute to change: "))
> ;if makes it so you don't get shut out?!
> (if (= "ATTRIB" (cdr (assoc 0 (entget (car old-rad)))));check if you
> select att or not
> (setq ent (subst (cons 1 (rtos old-rad 2 2)) (assoc 1 ent)
> ent));set ent to att value
> )
> (entmod ent)
> (entupd (car att));update att entity
> (princ "must select an attribute")
> );if=att
> );if select
> ;or from geotools
> ;; !
>
***************************************************************************
> ;; ! BL_WriteAttrNo
> ;; !
>
***************************************************************************
> ;; ! Function : Write a value into a specified attribute at a given
position
> ;; ! in a block
> ;; ! Argument : 'ename' - entity name of the block
> ;; ! 'pos' - position of the attribute in the block
> ;; ! 'Value' - Value to be updated
> ;; ! Returns : The updated block entity name if succesful otherwise nil
> ;; ! Updated : November 12, 1998
> ;; ! Copyright: (C) 2000, Four Dimension Technologies, Singapore
> ;; ! Contact : rakesh.rao@4d-technologies.com for help/support/info
>
> (defun BL_WriteAttrNo ( ename pos value / aname entl )
> (setq aname (BL_FindAttrNo ename pos)) ; Search for attribute
> (if aname
> (progn
> (setq
> entl (entget aname)
> entl (subst (cons 1 value) (assoc 1 entl) entl)
> )
> (entmod entl)
> (entupd ename)
> ))
> ename
> )
>
> Thanks for any help you can offer. I hope its ok to post all that from
> 4dimt - I like how their stuff is organized, just how do it work?
>
> Matt Fridell
> saladays@gwtc.net
>
>
Message 3 of 15
Anonymous
in reply to: Anonymous

Well, there are several blocks that I may pick. However, I know that you
*can* select just the attribute as there are several routines out there
that I use regularily that do just this. Here's an example of one I was
just looking at that does this.


"David Bethel" wrote in message
news:D31655BB2D2C079D651C9342F1ED33E8@in.WebX.maYIadrTaRb...
> ATTRIBs are part of INSERTs. You can't just pick 1. Any block in
> particular your trying to get to? -David

;**********************************************************************
; Program Name: C:CA
; Description: Match text values in attributes or text strings. Pick
; Source entity (Attribute or Text) then pick destination
; entity, it will copy the text value from the source to
; the destination.
; Date: 4-23-99
; Version: 1.00
; Author: Micah Nerren (714) 556-4454
;**********************************************************************

(defun C:CA ( / source_ent source_text dest_ent)

(setq source_ent nil)
(setq source_text nil)

(princ "\nPlease Select Source Text (Attribute or Text) ")

(setq source_ent (car (nentsel)))

(if (OR (= (cdr (assoc 0 (entget source_ent))) "ATTRIB")
(= (cdr (assoc 0 (entget source_ent))) "TEXT"))
(progn
(setq source_text (cdr (assoc 1 (entget source_ent))))
(princ (strcat "\nSource Text (Attribute or Text) " source_text))
(princ "\nPlease Select Text to Change (Attribute or Text) ")
(setq dest_ent (car (nentsel)))

(if (OR (= (cdr (assoc 0 (entget dest_ent))) "ATTRIB")
(= (cdr (assoc 0 (entget dest_ent))) "TEXT"))
(progn
(setq dest_ent (entget dest_ent))

(setq dest_ent
(subst (cons 1 source_text)
(assoc 1 dest_ent)
dest_ent
) ; subst
) ; setq

(entmod dest_ent)
(entupd (cdr (assoc -1 dest_ent)))
) ; progn
(princ "\nMust be either an Attribute or Text!")

) ; if

(princ)
) ; progn
(progn
(princ "\nEntity must be an Attribute or Text object!")
(princ)
) ; progn

) ; if

(princ)

) ; defun C:CA
Message 4 of 15
Anonymous
in reply to: Anonymous

> (defun c:r2a ( / )

You should set ss as a local variable or initialize it to nil here.
Don't assume it won't be already bound. It is a popular variable name.

> ;get radius
> (while (or (not ss)
> (> (sslength ss) 1))
> (princ "\nSelect 1 circle")
> (setq ss (ssget)))


If you are just looking for circles, then add a filter for your selection set here
(ssget (list (cons 0 "circle")))
or better yet for AC2000+ add single selection and filter
(ssget ":S" (list (cons 0 "circle")))
That will assure that the selection set will be exactly 1 element
so no other test is necessary


> (and ss

at this point ss must be bound to something so nothing is gained by
testing that it is bound.


> (setq en (ssname ss 0)
> ed (entget en)
> rd (cdr (assoc 40 ed)));rd holds value of radius now
> )

RD is now a real number. The new value you want is now RD

> ;now - change the attribute
> (if (setq att (nentsel "\nSelect attribute to change: "))

Your variable name for the attribute entity is ATT.

> ;if makes it so you don't get shut out?!
> (if (= "ATTRIB" (cdr (assoc 0 (entget (car old-rad)))));check if you
> select att or not

Where did OLD-RAD come from?
It should be
(if (= "ATTRIB" (cdr (assoc 0 (setq ent (entget att)))))

You needed to set ent to the entity info list.

> (setq ent (subst (cons 1 (rtos old-rad 2 2)) (assoc 1 ent)
> ent));set ent to att value

Again where does OLD-RAD come from?
It should be
(setq ent (subst (cons 1 (rtos RD 2 2) (assoc 1 ent) ent))


I didn't look any farther. Good luck.

Doug
Message 5 of 15
Anonymous
in reply to: Anonymous

I normally assume ATTRIBs to be empty. Can't (nentsel) an empty 1.

Change old-rad to (rtos rd 2)

-David
Message 6 of 15
Anonymous
in reply to: Anonymous

Matt,

I copy & pasted this thing together, but you can get the idea of where it
should go. This one will loop until you've selected a circle and then will
loop until your finger falls off from selected attribs...or until you run
out of attribs, whichever comes first:-)

Good luck.
--
Bobby C. Jones
http://www.acadx.com
Message 7 of 15
Anonymous
in reply to: Anonymous

Bobby,

Thank you! Man. Works so well!

One question on this - what restarts the loop? Is it the "t"?

;;;Once an attribute has been selected
;;;Modify the value and restart the loop
;;;-------------------------------------
(t
(setq entLst (entget (car ent)))
(entmod (subst (cons 1 (rtos rad 2)) (assoc 1 entLst) entLst))
(entupd (cdr (assoc 330 entLst)))
(setvar "errno" 7)
)
); _end of cond
); _ end of while
); _end of progn
); _end of if
(*error* nil)
)

Matt
Message 8 of 15
Anonymous
in reply to: Anonymous

Thanks for the input. Very helpful. I think I will try to do a new one
myself too.


Matt
>
Message 9 of 15
Anonymous
in reply to: Anonymous

Thanks. I don't know enough about the rules to have an intelligent
discussion debating it, so I believe you, and I think it is magic.

Matt
"David Bethel" wrote in message
news:5053B57B6632095390E1D1E814FF3D81@in.WebX.maYIadrTaRb...
>
>
> I normally assume ATTRIBs to be empty. Can't (nentsel) an empty 1.
>
> Change old-rad to (rtos rd 2)
>
> -David
>
>
>
Message 10 of 15
Anonymous
in reply to: Anonymous

It is a while loop. Look back up the code to the while statement.
Nice coding Bobby. I like the exit condition 52.

Doug

"sundog" wrote in message news:E32234A5343C89E798DB7148DE5CDA1F@in.WebX.maYIadrTaRb...
> Bobby,
>
> Thank you! Man. Works so well!
>
> One question on this - what restarts the loop? Is it the "t"?
>
> ;;;Once an attribute has been selected
> ;;;Modify the value and restart the loop
> ;;;-------------------------------------
> (t
> (setq entLst (entget (car ent)))
> (entmod (subst (cons 1 (rtos rad 2)) (assoc 1 entLst) entLst))
> (entupd (cdr (assoc 330 entLst)))
> (setvar "errno" 7)
> )
> ); _end of cond
> ); _ end of while
> ); _end of progn
> ); _end of if
> (*error* nil)
> )
>
> Matt
>
>
>
>
>
>
Message 11 of 15
Anonymous
in reply to: Anonymous

Matt,

The T is a little trick that is commonly used with a COND statement. COND
evaluates each test case that you provide in order from the first to the
last. It stops evaluating test expressions as soon as one evaluates to
True. Once you understand that principal, you can play around with the
order of the test expressions to achieve a desired result. In this
instance, our first test checks the "ERRNO" system variable. If it is 7,
then the user has 'mis-picked'. This is an important test for us, because
our loop is based on this system variable. I'll come back to that in a
minute. Notice that our COND statement doesn't actually do anything on a
user mispick. It just simply stops evaluating test expressions. Our next
test also checks the "ERRNO" system variable. If it is 52, then the user
has pressed the "Enter" key. We're assuming that this means the user is
tired of picking ATTRIBS (this will end the loop). Again our COND statement
doesn't do anything here, except stop evaluating test expressions. Our next
test determines whether or not the selected entity is actually an ATTRIB.
We know that the variable 'ent' actually contains something, because COND
never even gets to this test unless the user actually selects an entity.
Our previous two tests ensure this. If the entity is not an attribute, we
let the user know, then we set the ERRNO system variable to 7. I told you
earlier that I'd tell you why this was important to do, so be patient with
me, I'm almost there 🙂 Finally, we get to the infamous 't'. Remember
from above that the COND statement evaluates each test expression from
beginning to end and only stops when it finds one that evaluates to True, or
until it reaches the end of your test expressions. So what happens when
COND tries to evaluate our test expression T. You guessed it. It evaluates
to True (every single time, I promise) and executes all of the ensuing
statements. At this point we are positive that the user has selected an
actual attribute because whatever the user selected passed all of the
previous tests. This type of setup allows us to easily filter data and only
execute certain statements when specific criteria are met. These are
generally the statements where the actual work is to be done. In our case
it was to modify an attribute value. Also, note that the last statement
evaluated after the attrib has been set, re-sets the ERRNO system variable
back to 7. Why does it do this you ask/demand?!? Because this is what our
WHILE loop is keying on. Our COND statement is wrapped in a WHILE loop that
executes as long as the ERRNO system variable is set to 7. It is set to 7
when a user 'mis-picks' while attempting to select an entity with ENTSEL.
Look real hard at the loop structure. Do you see how/why it's looping? If
not, just let me know and I'll see if I can babble on for a bit longer on
the subject. HINT - If you only want to allow the user to select a single
attribute to modify, remove the (setvar "errno" 7) from the 't' test case.
That way it won't bomb when the user mis-picks or selects something that's
not an attribute, but will exit after they successfully select an attribute.
--
Bobby C. Jones
http://www.acadx.com
Message 12 of 15
Anonymous
in reply to: Anonymous

Ok, ok, ok. I can follow it! Clever. Man. This lisp is versatile. And
smart. This is the *- turn the paradigm on its head -* kind of stuff I
like. My dog is chewing on my leg, telling me to go inside, but after 10
minutes of puzzling, Here's my question.
How does the following function, and why this order? Shouldn't the 7 be
second?
;;;Stop testing on mispick or enter key
;;;-------------------------------------
((= (getvar "errno") 7))
((= (getvar "errno") 52))

I thank you again. That kind of walk through of the program is very helpful
to me. I can stare at the help files all day, stare at other code, and
puzzle til my eyes pop out of my head, and still not get it. Thanks. I'll
try a version on the next project and see if you approve!

Matt

"Bobby C. Jones" wrote in message
news:3E5758B3472ED0A0927EBDB5F845ADEB@in.WebX.maYIadrTaRb...
> Matt,
>
> The T is a little trick that is commonly used with a COND statement. COND
> evaluates each test case that you provide in order from the first to the
> last. It stops evaluating test expressions as soon as one evaluates to
> True. Once you understand that principal, you can play around with the
> order of the test expressions to achieve a desired result. In this
> instance, our first test checks the "ERRNO" system variable. If it is 7,
> then the user has 'mis-picked'. This is an important test for us, because
> our loop is based on this system variable. I'll come back to that in a
> minute. Notice that our COND statement doesn't actually do anything on a
> user mispick. It just simply stops evaluating test expressions. Our next
> test also checks the "ERRNO" system variable. If it is 52, then the user
> has pressed the "Enter" key. We're assuming that this means the user is
> tired of picking ATTRIBS (this will end the loop). Again our COND
statement
> doesn't do anything here, except stop evaluating test expressions. Our
next
> test determines whether or not the selected entity is actually an ATTRIB.
> We know that the variable 'ent' actually contains something, because COND
> never even gets to this test unless the user actually selects an entity.
> Our previous two tests ensure this. If the entity is not an attribute, we
> let the user know, then we set the ERRNO system variable to 7. I told you
> earlier that I'd tell you why this was important to do, so be patient with
> me, I'm almost there 🙂 Finally, we get to the infamous 't'. Remember
> from above that the COND statement evaluates each test expression from
> beginning to end and only stops when it finds one that evaluates to True,
or
> until it reaches the end of your test expressions. So what happens when
> COND tries to evaluate our test expression T. You guessed it. It
evaluates
> to True (every single time, I promise) and executes all of the ensuing
> statements. At this point we are positive that the user has selected an
> actual attribute because whatever the user selected passed all of the
> previous tests. This type of setup allows us to easily filter data and
only
> execute certain statements when specific criteria are met. These are
> generally the statements where the actual work is to be done. In our case
> it was to modify an attribute value. Also, note that the last statement
> evaluated after the attrib has been set, re-sets the ERRNO system variable
> back to 7. Why does it do this you ask/demand?!? Because this is what
our
> WHILE loop is keying on. Our COND statement is wrapped in a WHILE loop
that
> executes as long as the ERRNO system variable is set to 7. It is set to 7
> when a user 'mis-picks' while attempting to select an entity with ENTSEL.
> Look real hard at the loop structure. Do you see how/why it's looping?
If
> not, just let me know and I'll see if I can babble on for a bit longer on
> the subject. HINT - If you only want to allow the user to select a single
> attribute to modify, remove the (setvar "errno" 7) from the 't' test case.
> That way it won't bomb when the user mis-picks or selects something that's
> not an attribute, but will exit after they successfully select an
attribute.
> --
> Bobby C. Jones
> http://www.acadx.com
>
>
>
Message 13 of 15
Anonymous
in reply to: Anonymous

Doesn't really matter. It's like going down a checklist.
Could have also been done like
((member (getvar "errno") '( 7 52))
but that would have made the program a little more
difficult to change if a later decision was made to handle the
two conditions in different ways later. The result of both
conditions is that nothing is done. This leaves the main
check of the errno in place. When the main check is
done at the while statement, the exit will occur.

A decision to arrange the order of checks should be
made on the basis of
1) check for things that could cause errors in later checks
2) check for things that require less computation first after 1)
3) check for things expected often should happen before other checks.

There could also be other rules dependent on the situation.

Doug

"sundog" wrote in message
news:443A9E5DAB24AF511001B5E075226D45@in.WebX.maYIadrTaRb...
> Ok, ok, ok. I can follow it! Clever. Man. This lisp is versatile. And
> smart. This is the *- turn the paradigm on its head -* kind of stuff I
> like. My dog is chewing on my leg, telling me to go inside, but after 10
> minutes of puzzling, Here's my question.
> How does the following function, and why this order? Shouldn't the 7 be
> second?
> ;;;Stop testing on mispick or enter key
> ;;;-------------------------------------
> ((= (getvar "errno") 7))
> ((= (getvar "errno") 52))
>
> I thank you again. That kind of walk through of the program is very helpful
> to me. I can stare at the help files all day, stare at other code, and
> puzzle til my eyes pop out of my head, and still not get it. Thanks. I'll
> try a version on the next project and see if you approve!
>
> Matt
>
> "Bobby C. Jones" wrote in message
> news:3E5758B3472ED0A0927EBDB5F845ADEB@in.WebX.maYIadrTaRb...
> > Matt,
> >
> > The T is a little trick that is commonly used with a COND statement. COND
> > evaluates each test case that you provide in order from the first to the
> > last. It stops evaluating test expressions as soon as one evaluates to
> > True. Once you understand that principal, you can play around with the
> > order of the test expressions to achieve a desired result. In this
> > instance, our first test checks the "ERRNO" system variable. If it is 7,
> > then the user has 'mis-picked'. This is an important test for us, because
> > our loop is based on this system variable. I'll come back to that in a
> > minute. Notice that our COND statement doesn't actually do anything on a
> > user mispick. It just simply stops evaluating test expressions. Our next
> > test also checks the "ERRNO" system variable. If it is 52, then the user
> > has pressed the "Enter" key. We're assuming that this means the user is
> > tired of picking ATTRIBS (this will end the loop). Again our COND
> statement
> > doesn't do anything here, except stop evaluating test expressions. Our
> next
> > test determines whether or not the selected entity is actually an ATTRIB.
> > We know that the variable 'ent' actually contains something, because COND
> > never even gets to this test unless the user actually selects an entity.
> > Our previous two tests ensure this. If the entity is not an attribute, we
> > let the user know, then we set the ERRNO system variable to 7. I told you
> > earlier that I'd tell you why this was important to do, so be patient with
> > me, I'm almost there 🙂 Finally, we get to the infamous 't'. Remember
> > from above that the COND statement evaluates each test expression from
> > beginning to end and only stops when it finds one that evaluates to True,
> or
> > until it reaches the end of your test expressions. So what happens when
> > COND tries to evaluate our test expression T. You guessed it. It
> evaluates
> > to True (every single time, I promise) and executes all of the ensuing
> > statements. At this point we are positive that the user has selected an
> > actual attribute because whatever the user selected passed all of the
> > previous tests. This type of setup allows us to easily filter data and
> only
> > execute certain statements when specific criteria are met. These are
> > generally the statements where the actual work is to be done. In our case
> > it was to modify an attribute value. Also, note that the last statement
> > evaluated after the attrib has been set, re-sets the ERRNO system variable
> > back to 7. Why does it do this you ask/demand?!? Because this is what
> our
> > WHILE loop is keying on. Our COND statement is wrapped in a WHILE loop
> that
> > executes as long as the ERRNO system variable is set to 7. It is set to 7
> > when a user 'mis-picks' while attempting to select an entity with ENTSEL.
> > Look real hard at the loop structure. Do you see how/why it's looping?
> If
> > not, just let me know and I'll see if I can babble on for a bit longer on
> > the subject. HINT - If you only want to allow the user to select a single
> > attribute to modify, remove the (setvar "errno" 7) from the 't' test case.
> > That way it won't bomb when the user mis-picks or selects something that's
> > not an attribute, but will exit after they successfully select an
> attribute.
> > --
> > Bobby C. Jones
> > http://www.acadx.com
> >
> >
> >
>
>
Message 14 of 15
Anonymous
in reply to: Anonymous

Thanks Doug, You may recognize this code from our BREAK @ thread from a
while back. This little routine was a simple copy -n- paste job.
BTW - That was some very nice advice on the COND function. I may tape that
list to my monitor. One less thing to remember 🙂
--
Bobby C. Jones
http://www.acadx.com
Message 15 of 15
Anonymous
in reply to: Anonymous

Glad to help.

Doug

"Bobby C. Jones" wrote in message
news:EF954456AB5FFE70627796060D8101DC@in.WebX.maYIadrTaRb...
> Thanks Doug, You may recognize this code from our BREAK @ thread from a
> while back. This little routine was a simple copy -n- paste job.
> BTW - That was some very nice advice on the COND function. I may tape that
> list to my monitor. One less thing to remember 🙂
> --
> Bobby C. Jones
> http://www.acadx.com
>
>
>

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost