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

How to call a function in Autocad and return a value by reference

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
Anonymous
1105 Views, 5 Replies

How to call a function in Autocad and return a value by reference

Hello,

I have been programming in Autolisp for many years. I am used to writing functions that don't need to return a value to the calling routine.

Now, my programming assignment requires that I write a function that does return a value to the calling routine, (and that was modified inside

of the function).

 

My first programming attempt does not work. It fails to return the true value of the modified variable. I tried looking this up on the internet, and had absolutely no luck, because the Autolisp documentation that is available on the internet has it's own peculiar style of communicating how it works, and I am used to the conventions associated with C# programming, which are based on the theory of Object Oriented Programming.

 

So, I am uploading my "test development program" here and letting you, (whoever you may be), to try to modify it so that it does successfully return

the modified value from the function.

5 REPLIES 5
Message 2 of 6
marko_ribar
in reply to: Anonymous

From what I saw in your posted code, it looks that you just want to obtain point by clicking with mouse left button... The simple way of doing this is just using (getpoint) function... If you're looking to pass point data list to other defined function and get back modified data list, I just don't see where are you specyfing argument inside function you are calling from main function that is to be modified and returned back as modifed value... To explain what I mean, I'll try to ilustrate what your 2 defined function should look like in following example...

 

(defun modifoo ( pt / gr modpt )

  (setq gr (grread))

  (setq modpt (mapcar '+ pt (cadr gr)))

  modpt

)

 

(defun c:mainfoo ( / pt modpt )

  (setq pt '(0.0 0.0 0.0))

  (setq modpt (modifoo pt))

  (prompt "\nCoordinates of mouse position in moment of calling main function are : ") (princ modpt)

  (princ)

)

 

Hope it shows what should (midifoo) function contain - argument "pt" that is modified and returned back as "modpt" in main (c:mainfoo) function...

Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 3 of 6
Anonymous
in reply to: marko_ribar

First of all, thank you very much for taking your time to answer my question.

 

Now, to explain what is going on in this issue:

 

My task was to take an existing program that already worked right and make it more "user friendly" (according to the criteria that my father, the

owner of this company, thinks that it's more user friendly).

 

So, the existing program was using "getpoint" just as you recommended to me. But the new task was to make a copy of the program and call it

"program-2" etc.... and make it work with the new "user friendly" way. So, the way I understood my task, I needed to make my own substitute

for the "getpoint" function that is built into Autolisp.

 

Now, it got to the issue that since I don't know how to get the parameter back from my calling function, that it would be very good for me to

learn what I am doing wrong in the general case, because getting a parameter back from a function is one of those basic skills that I should have

mastered a long time ago.

 

To add to my frustration, NONE of the documentation to Autolisp, either proprietary or third party, will "guide you by the hand" to understand what I am doing wrong.

 

I think that your coding exampe give me the answer to this question, and if it does, I am going to keep it in a special place in my files, so whenever

I need to be able to get a parameter back from a function that I can do it.

Message 4 of 6
Kent1Cooper
in reply to: Anonymous

Here's another way of going about your routines that I think does what you're looking for.  I made it to store the latest picked point as a replacement for the previous one, so it can "chain" a series of locations, but you can leave it always based from 0,0.  I also made it draw a line in (getpoint) fashion from the previous to the cursor location.  See additional comments in the code.

 

;;----------------------------------------------------------------------------
;; getclick2 - to return when one click has been entered from the mouse
;;----------------------------------------------------------------------------

(defun getclick2 (pt1 / getting cur)
  (setq getting T); to start
  (while
    (and
      getting ; until nil-ed out below
      (setq cur (grread T 12 0)); read cursor movement
    ); and
    (redraw); so lines don't pile up
    (grdraw pt1 (cadr cur) 253); dynamic line [edit color as desired]
    (if (= (car cur) 3); picked point
      (setq getting nil) ; to end (while) loop
    ); if
  ); while
  (cadr cur); return to test1
); defun

;;----------------------------------------------------------------------------
;; test1.lsp
;;----------------------------------------------------------------------------

(defun c:test1 ()
  (if (not pt1) (setq pt1 '(0 0))); continues from previous, rather than always 0,0
  (setq pt1 (getclick2 pt1))
;  (print "debug: debug1")
  (redraw); omit this to retain last dynamic line until next Redraw/Regen/Pan/Zoom
  (print pt1)
  (princ)
); defun

Kent Cooper, AIA
Message 5 of 6
Anonymous
in reply to: marko_ribar

Hello Marko,

 

Now, I have an even more bizarre question to ask you. My task has led me to the point where I need to take your "mainfoo" routine and turn it into

a "reactor" routine that will respond "asynchronously" whenever someone clicks, (simultaneous with my other code).

 

As usual, when I look this type of thing up in my system of paperback books, I don't get an answer. Do you know how to do it?

Message 6 of 6
Anonymous
in reply to: marko_ribar

Hello Marko,

I had posted my question and I see that you decided not to answer it. I would like to propose to you that if you could spare some of your time

to answer my question, that my boss has given me the approval to give you a free copy of the program that we are working on when we finish

getting it to work. That would only be fair, because your insight into the coding is very much needed to finish what we are doing.

 

I could try to finish it myself, but that would require me to use "brute force programming" (meaning that I try all of the combinations of the things

that I could type exhaustively), and that would be a very costly and unwise use of time.

 

We would very much appreciate it you could help us.

 

Leonard Gojer

214-679-5554

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

Post to forums  

Autodesk Design & Make Report

”Boost