Jump to content

Recommended Posts

Posted

I've become a bit rusty when it comes to Lisp functions.

 

Is there a function that will strip the quotation marks from a variable?

 

I have a rountine with a variable set to "C:NAME". To use the variable within my Lisp routine, I need to remove the quotation marks.

 

Is there a simple way to do this (compatible with AutoCAD14)?

 

Thanks,

 

Razoo

Posted

Hi,

 

The read function ?

 

(read "C:NAME") returns C:NAME

Posted
Hi,

 

The read function ?

 

(read "C:NAME") returns C:NAME

 

Ah yes, I remember now :)

 

Thanks for the quick reply.

 

Razoo

Posted

Hmm, can't get it to work - yet!

 

I'm trying to define a routine for loading lisp routines on demand.

 

Basically, when the lisp routine is called from the menu, a check is made to see whether the routine is already loaded. If it is, then just the command name is called. If it isn't, then the lisp file is loaded first, followed by the command name being called.

 

(if (not C:NAME) (load "NAME")... etc.

 

NAME is stored as "NAME" within the variable fn

 

My cunning plan was to use:

 

(if (not (read (strcat "C:" fn))) (load fn)....

 

but it doesn't work and (not (read (strcat "C:" fn)) always returns nil.

 

(read (strcat "C:" fn)) correctly returns C:NAME

 

Any ideas?

 

Razoo

Posted

Try this:

 

(if (not (read (strcat "(C:" fn ")")))
 (load fn)
)

 

Or something along those lines..

 

Lee

Posted

Hi,

 

These are LISP basics (not only AutoLISP, all LISP languages).

eval returns a symbol, if you want to know if this symbol is bounded to a function you can use eval function :

 

(if (not (eval (read (strcat "c:" fn))))
 (load fn)
)

Posted

Thanks Lee & gile, I will try your suggestions.

 

Regards,

 

Razoo

Posted

Lee,

 

(not (read (strcat "(c:" fn ")"))) should always return nil, whatever fn because (read (strcat "(c:" fn ")")) always returns a list (not evaluated) which contains a symbol (not evaluated too).

 

Razoo,

 

Instead of eval, you can use boundp too

(boundp (read (strcat "c:" fn))) should return T if the symbol (c: + fn value) is bounded to a value, nil otherwise.

Posted
Instead of eval, you can use boundp too

(boundp (read (strcat "c:" fn))) should return T if the symbol (c: + fn value) is bounded to a value, nil otherwise.

 

Progress (slow) is being made!

 

Where fn is set to "NAME" :

 

(boundp (read (strcat "C:" fn))) works fine both when the Lisp routine isn't loaded (returns nil), and when it is (returns T).

 

(eval (read (strcat "C:" fn))) only works when the Lisp routine is not loaded (returns nil). When the Lisp routine is loaded, it appears to echo most of the Lisp file.

 

I now have a similar problem with running the Lisp routine after loading. Normally (C:NAME) will run NAME.LSP

 

Unfortunately (read (strcat "C:" fn)) does not run the Lisp routine, even though it returns C:NAME

 

Any ideas how I can initiate the Lisp routine using the "NAME" stored in the variable fn?

 

Thanks,

 

Razoo (who thought this Lisp routine would be a doddle to write!)

Posted

Razoo,

 

You asked first for removing quotation matks.

read function was, I think, the right reply.

 

Then it appears you want to evaluate if a LISP function is loaded

Both eval and boundp works the way you wanted to use them, try:

(if (not (eval (read (strcat "c:" fn))))
 (alert "Not loaded !")
 (alert "Already loaded !")
)

and

(if (not (boundp (read (strcat "c:" fn))))
 (alert "Not loaded !")
 (alert "Already loaded !")
)

they work both the same because of if statement which doesn't need the condition to return T but a non nil value.

 

To run the command, the problem is the the same, you have to force evaluation:

(eval (read (strcat "(c:" fn ")")))

(strcat "(c:" fn ")") returns "(c:NAME)"

(read "(c:NAME)") returns (c:NAME)

(eval (c:NAME)) will run the function.

 

 

PS: If you explained right from the start all you want to do, I'll certainly replid you:

"Did you look at the autoload function ?"

(autoload fn '(fn))

 

Posted
You asked first for removing quotation marks.

read function was, I think, the right reply.

 

It was.

 

Then it appears you want to evaluate if a LISP function is loaded

Both eval and boundp works the way you wanted to use them.

 

Agreed (within an 'IF' statement). I'm using boundp.

 

To run the command, the problem is the the same, you have to force evaluation:

(eval (read (strcat "(c:" fn ")")))

 

I did know that, but pilot error crept in, causing my test code to fail. See my reply to Lee.

 

PS: If you explained right from the start all you want to do, I'll certainly replied you:

"Did you look at the autoload function ?"

 

With fn set to "NAME" (autoload fn '(fn)) returns "error: bad argument type".

 

I expect that is resolvable, but I prefer the 'Load on Demand' Lisp routine I have written because it gives me more control and allows me to specify any error messages.

 

I didn't explain the full task at the outset because:

1. I wasn't expecting this much trouble
:(
I thought my only problem was remembering which function stripped quotation marks (I haven't written any Lisp for the past 3 years or so and my Lisp bible has gone AWOL).

 

2. I like to work out as much of a Lisp routine as I can myself.

 

3. I was timing you and Lee to see who was quickest off the mark (I think Lee won!) :wink:

Thanks,

 

Razoo

Posted

3. I was timing you and Lee to see who was quickest off the mark (I think Lee won!) :wink:

 

I disagree, Gile is far more knowledgeable than I.
:)

Posted

For autoload, this should work:

 

(autoload fn (list fn))

 

As the apostrophe (') means the list is not evaluated, and hence "fn" is an invalid argument type. :)

Posted
Like this?

 

(eval (read (strcat "(C:" fn ")")))

 

Thanks Lee,

 

I was going to post back that I had already tried that code and it didn't work - then I realised I had tried it direct from the command line without first setting the variable fn to "NAME" (fn had reset to nil when the lisp routine terminated).

 

My 'Load on Demand' Lisp routine is running just fine now.

 

Thanks to everyone who offered guidance.

 

Until the next time!

 

Regards,

 

Razoo

Posted
I disagree, Gile is far more knowledgeable than I. :)

[/indent]

 

I was referring to speed of reply, not depth of knowledge :lol:

 

Razoo

Posted
For autoload, this should work:

 

(autoload fn (list fn))

 

Yes, the revised code works fine.

 

I have many Lisp routines, but never run them all during an AutoCAD session.

 

I much prefer using my predefined (in Acad.lsp) 'Load on Demand' Lisp function (C:Runlsp).

 

If I add a new Lisp command to a menu or toolbar, all I have to do is type the macro:

^C^C^P(RUNLSP "NAME" nil)(princ)

if 'filename' = 'command name', or:

^C^C^P(RUNLSP "NAME" "NAME")(princ)

otherwise.

 

Razoo

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...