Jump to content

How to return value Autolisp function


techjunkie09

Recommended Posts

probably an obvious answer, but i can't figure out how to get an autolisp function to return a value unless i do a

(princ return_value)

however this also causes the value to display on the command window everytime, any way around that?

Link to comment
Share on other sites

  • Replies 32
  • Created
  • Last Reply

Top Posters In This Topic

  • Se7en

    10

  • techjunkie09

    8

  • Lee Mac

    7

  • The Buzzard

    6

Top Posters In This Topic

Posted Images

Type !VARIABLENAME at the command prompt providing your variable have not been declared local.

I assume you mean variable values, If I am incorrect ignore this post.

Link to comment
Share on other sites

thanks for the suggestion, however i think you misunderstood the problem... i'm used to coding in other languages and am trying to split my autolisp code into multiple functions that work together... a short example of what i'm trying to do is something like

(defun compareString(string1 string2)
 (if (= (string1) (string2))
 (return "true")
 (return "false")
 )
)

(defun c:do_something(string1 string2)
  (if (= ((compareString string1 string2) "true")
  (progn
  ;do some stuff
  )
  (progn
  ;else do this stuff
  )
  )
)

i realize its sillly to do it that way in the example but thats the concept of what i'm trying to do, in other words using custom function return values within a different custom function

Link to comment
Share on other sites

thanks for the suggestion, however i think you misunderstood the problem... i'm used to coding in other languages and am trying to split my autolisp code into multiple functions that work together... a short example of what i'm trying to do is something like

(defun compareString(string1 string2)
 (if (= (string1) (string2))
 (return "true")
 (return "false")
 )
)

(defun c:do_something(string1 string2)
  (if (= ((compareString string1 string2) "true")
  (progn
  ;do some stuff
  )
  (progn
  ;else do this stuff
  )
  )
)

i realize its sillly to do it that way in the example but thats the concept of what i'm trying to do, in other words using custom function return values within a different custom function

 

I had a feeling about that the last minute. Sorry

Link to comment
Share on other sites

thanks for the suggestion, however i think you misunderstood the problem... i'm used to coding in other languages and am trying to split my autolisp code into multiple functions that work together... a short example of what i'm trying to do is something like

(defun compareString(string1 string2)
 (if (= (string1) (string2))
 (return "true")
 (return "false")
 )
)

(defun c:do_something(string1 string2)
  (if (= ((compareString string1 string2) "true")
  (progn
  ;do some stuff
  )
  (progn
  ;else do this stuff
  )
  )
)

i realize its sillly to do it that way in the example but thats the concept of what i'm trying to do, in other words using custom function return values within a different custom function

 

 

Check out this link, It may explain better:

 

http://www.afralisp.net/lispa/lisp13.htm

 

 

http://www.afralisp.net/lisp/conif.htm

Link to comment
Share on other sites

oddly, that link isn't working for me it just has this

(If) is probably the most important and widely use condition statement.

Unlike other languages though, you can match only one (if) statement with a then statement. The syntax is as follows :

and the rest of the page is blank. However i doubt the page will be of much use, my problem isn't in the if statement itself, i'll give yet another example

(defun inner()
(princ 1)
)

(defun c:outer()
(setq x (inner))
(princ (+ x 1))
(princ )
)

outputs

1

2

 

i'd like to not have it show the "1" from the inner function's (princ)

Link to comment
Share on other sites

oddly, that link isn't working for me it just has this

and the rest of the page is blank. However i doubt the page will be of much use, my problem isn't in the if statement itself, i'll give yet another example

(defun inner()
(princ 1)
)

(defun c:outer()
(setq x (inner))
(princ (+ x 1))
(princ )
)

outputs

1

2

 

i'd like to not have it show the "1" from the inner function's (princ)

 

You must be using firefox or something other than IE if the page is blank. I not sure of the procedure to view it in firefox, But there are others here that do.

Link to comment
Share on other sites

oddly, that link isn't working for me it just has this

and the rest of the page is blank. However i doubt the page will be of much use, my problem isn't in the if statement itself, i'll give yet another example

(defun inner()
(princ 1)
)

(defun c:outer()
(setq x (inner))
(princ (+ x 1))
(princ )
)

outputs

1

2

 

i'd like to not have it show the "1" from the inner function's (princ)

 

Hope this is useful to you.

 

 

Print Commands Prompt

This command is used simply to print a message on the command line. A line feed (\n) is not included so two consecutive prompt commands will print both messages on the same line. Therefore , any printing after the prompt must have the (terpri) or \n command. (terpri)

 

This is a line feed that causes the next printed text to appear on the next line. It generally follows the prompt command. eg:

  • (prompt "Hello, how are you ?")(terpri) or
  • (prompt "\nHello, how are you ?")

Prin1

 

This function prints the expression on the screen and returns the expression. It can be any expression and need not only be a string. The expression can also be written to a file and will appear in the file exactly as it would on the screen.

  • (prin1 "Hello") would print Hello
  • (prin1 a) would print the value of variable a
  • (prin1 a f) would print the value of variable a to an open file named in variable f

Princ

Is the same as prinl except that the control characters ("") are not printed. Can also be used to print a blank line by using no statement after princ.Print

 

Same as prinl except that a new line is printed before the expression and a space is printed after the expression. eg:

  • (print "Hello") would return "Hello" "Hello"

Link to comment
Share on other sites

lol yep firefox :), viewed in IE and it loads fine, however it still doesn't answer the question, i misled u a bit with the 'if' bit in my example... let me give this one more try

(setq x (+ 1 1))
(princ x)

outputs 2, the (+ 1 1) evaluates to '2' but doesn't display it on the screen until the (princ) command, it just uses that value internally. The only way i can figure out to do that with custom functions / custom return values is

(defun compare(num1 num2)
  (if (= num1 num2)
  (princ "true")
  (princ "false")
  )
)

(defun test(num1 num2)
   (setq test (compare num1 num2))
   (princ test)
   (princ )
)

(test 3 4) outputs

false

false

when i run (test 3 4) i want the inner function (compare 3 4) to evaluate to "false" in this case and use the value internally and not have to output it to the screen

Link to comment
Share on other sites

The Buzzard,

I dont think (s)he needs that kind of info.

 

techjunkie09,

I got cha. I think i know what your thinking of. Its not hard at all. AutoLisp can use either Bound or Free variables to pass/return. BTW, what languages?

 

If you want; give me a sec and i will try and send you a primer i wrote for autoLisp newbies (most Autolispers are people who never programmed before so i wrote it as a beginner tut to use proper technique) you can use to glean the info you want from there.

 

Autolisp is similar to Scheme. There are a few Gotcha's (like Schemes Boole's return different then Autolisp's do but we can go over those as you run into them).

Link to comment
Share on other sites

The Buzzard,

I dont think (s)he needs that kind of info.

 

techjunkie09,

I got cha. I think i know what your thinking of. Its not hard at all. AutoLisp can use either Bound or Free variables to pass/return. BTW, what languages?

 

If you want; give me a sec and i will try and send you a primer i wrote for autoLisp newbies (most Autolispers are people who never programmed before so i wrote it as a beginner tut to use proper technique) you can use to glean the info you want from there.

 

Autolisp is similar to Scheme. There are a few Gotcha's (like Schemes Boole's return different then Autolisp's do but we can go over those as you run into them).

 

Thanks Se7en,

No problem, But I think the link I provided has the information the OP is looking for, But could not view it. I am still not sure at this point anyway.

 

All yours

Link to comment
Share on other sites

thanks se7en, as far as languages go mostly java and php but i've dabbled a little in c++, vba, and now my least favorite autolisp lol, it just takes some getting used to. but yeah that primer would be great

Link to comment
Share on other sites

I'm sure Se7en has a wealth of information to provide you with, but I'll add my 2 cents for what its worth :)

 

AutoLISP will return the result of the last evaluated expression, whether that be a String, Variable or other data type.

 

So, in your example:

 

(defun compare(num1 num2)
  (if (= num1 num2) 
    "true"
    "false"
  )
)

(defun test(num1 num2)
   (setq test (compare num1 num2))
   (princ test)
   (princ )
)

 

This should be what you are after - The IF statement will return the true string as the THEN statement, or the false string as the ELSE statement.

 

I'm sure that Se7en could elaborate on this quite a bit :P

 

Lee

Link to comment
Share on other sites

thanks se7en, ... but yeah that primer would be great

 

Beginners primer attached.

 

Quick Synopsis:

You will use either one of these three basic models 95% of the time.

 

;; subs
(defun subfunction( a / )
   (dosomething a)
)

;; main
(defun c:mainroutine ( / 
                      ;; varialbe localization
                      e
                    )


 (setq e (dosomething))
 (subfunction e)
)

 

;; main
(defun c:mainroutine ( / 
                      ;; function localization
                      subfunction

                      ;; varialbe localization
                      e
                    )
 ;; subs (local)
 (defun subfunction( a / )
     (dosomething a)
 )

 (setq e (dosomething))
 (subfunction e)
)

 

;;  note no arguments passed to the subfunctions yet 
;;       the variable was still redefined
;;       via lexical scope
(defun c:mainroutine ( / 
                       ;; function localization
                       subfunction
                       
                       ;; variable localization
                       a
                       )
 ;; subs (local) 
 (defun subfunction ( / )
      (setq a "1"))

 (setq a "0")
 (princ (strcat "\n`a' is set to: " a))
 (subfunction)
 (princ (strcat "\n`a' is set to: " a))
 (princ)
)

Methods.zip

Link to comment
Share on other sites

thanks se7en just finished looking over the pdf and GREAT WORK i'm definitely hanging on to that :)

however, i've got one more question lol, in an unrelated bit, i have a script that draws a series of boxes using the basic structure

(setq pt1 (getpoint "\nClick the location of the top-left corner"))
(setq pt2 (list (+ width (car pt1)) (cadr pt1) 0.0))
(setq pt3 (list (+ width (car pt1)) (- (cadr pt1) height) 0.0))
(setq pt4 (list (car pt1) (- (cadr pt1) height) 0.0))
(command ".pline" pt1 pt2 pt3 pt4 "Close")

with subsequent boxes being based on the location of that start point, i've been having the points print to the command window as u can see in the attached screen shot and they are always correct. Basically the screen shot is of the same function run twice and in one it operates as expected and has 4 boxes with spacing between them, in the other it for some reason increases the size of some of the boxes and makes them all connect, its driving me nuts because its the EXACT SAME FUNCTION just executed at two different coordinates. Any ideas?

 

p.s. also i've already tried

(command "line" pt1 pt2 pt3 pt4 "Close")
(command "line" pt1 pt2 pt3 pt4 pt1 "Close")
(command "line" pt1 pt2 pt3 pt4 pt1 "Cancel")

plus the mtext labels are based off the top left corner and they always appear in the right spots, so i'm 99.9% sure its not a problem with the numbers i'm feeding it

acad_oddity.jpg

Link to comment
Share on other sites

huh? no idea.

 

We can try to create a line with Visual Lisp (You get to play with an object model) instead of passing control to the Command Line (command...) if you want to try that.

Link to comment
Share on other sites

thanks se7en just finished looking over the pdf and GREAT WORK i'm definitely hanging on to that :)

...

 

heh, your the ONLY one who seems to have appreciated that doc. I wrote it back in 05 and most if not all i got was a `meh, it's nice'. So thank you very much!

Link to comment
Share on other sites

works for me, i'm not really too picky, i just need it to draw lots of boxes and the spacing MUST be there

heh, your the ONLY one who seems to have appreciated that doc. I wrote it back in 05 and most if not all i got was a `meh, it's nice'. So thank you very much!

:) well it served its purpose as a basic intro and if i had been able to google a similar document i wouldn't have needed to start a thread to begin with

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