Author Topic: Stored variable ?  (Read 10172 times)

0 Members and 1 Guest are viewing this topic.

V-Man

  • Bull Frog
  • Posts: 343
  • I exist therefore I am! Finally Retired!
Stored variable ?
« on: February 20, 2004, 09:53:13 AM »
I have a question. I have a variable that hold the value of a command that you can type in at the command prompt. How can I initiate the command from the stored variable?
AutoCAD 9 - 2023, AutoCADMap 2008 - 2010, Revit 2012 - 2022, Autocad Civil 3D 2023

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Stored variable ?
« Reply #1 on: February 20, 2004, 09:57:30 AM »
I dont understand what your saying so im just going to start stabbin' in the dark.

See the value of a var from the command line with the " ! " symbol. Like this:
(setq a 1)
!a
1

Or store a "command" in a varaible.
(setq a (princ "\nHave a nice day!"))
(eval a)
Have a nice day!


any of those answer you?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Stored variable ?
« Reply #2 on: February 20, 2004, 09:57:46 AM »
I don't follow you, can you post some code to look at?
TheSwamp.org  (serving the CAD community since 2003)

V-Man

  • Bull Frog
  • Posts: 343
  • I exist therefore I am! Finally Retired!
Stored variable ?
« Reply #3 on: February 20, 2004, 10:09:09 AM »
I have a custom command called DOWORK

For example...

Command: (Setq test "line")
"line"

Command: !test
"line"

now in my lisp the "test" is holding the value of DOWORK.

Can I start the command from the variable?
AutoCAD 9 - 2023, AutoCADMap 2008 - 2010, Revit 2012 - 2022, Autocad Civil 3D 2023

daron

  • Guest
Stored variable ?
« Reply #4 on: February 20, 2004, 10:14:39 AM »
According to what you are showing, "test" is holding the value of "line". I don't see where DOWORK is being held anywhere. As far as the last question goes, I don't think so.

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Stored variable ?
« Reply #5 on: February 20, 2004, 10:18:45 AM »
Like this?
Quote
Command: (defun dowork () (+ 5 2))
DOWORK

Command: (setq test '(dowork))
(DOWORK)

Command: (eval test)
7
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Stored variable ?
« Reply #6 on: February 20, 2004, 10:27:28 AM »
What about this:
Code: [Select]
(command test)
TheSwamp.org  (serving the CAD community since 2003)

V-Man

  • Bull Frog
  • Posts: 343
  • I exist therefore I am! Finally Retired!
Stored variable ?
« Reply #7 on: February 20, 2004, 10:30:39 AM »
Code: [Select]

(defun C:samp5 ();( / REGION# DWGTYPE RNAMES DCL_ID) ;define function
  (setq REGION# "R3") ;*preset Region
  (setq DWGTYPE "FP") ;*preset FP
  (setq RNAMES '("R1" "R3" "R4" "R7" "R8" "R9" "R10" "R11"))
  (setq dcl_id (load_dialog "samp5.dcl")) ;load dialog
  (if (not (new_dialog "samp5" dcl_id))
    (exit) ;exit if no dialog
  )
  (start_list "selections") ;*start the list box
  (mapcar 'add_list RNAMES) ;*fill the list box
  (end_list) ;*end list
  (action_tile "rb1" "(setq DWGTYPE \"FP\")") ;store DWGTYPE type
  (action_tile "rb2" "(setq DWGTYPE \"SR\")") ;store DWGTYPE type
    (action_tile
    "cancel" ;if cancel button pressed
    "(done_dialog) (setq userclick nil)" ;close dialog, set flag
    );action_tile
  (action_tile
    "accept" ;if O.K. pressed
    (strcat ;string 'em together
      "(progn
       (setq REGION# (atof (get_tile \"selections\")))" ;*get list selection
      " (done_dialog)(setq userclick T))" ;close dialog, set flag
    );strcat
  );action tile
  (start_dialog) ;start dialog
  (unload_dialog dcl_id) ;unload
   (if userclick ;*check O.K. was selected
    (progn
      (setq REGION# (fix REGION#)) ;*convert to integer
      (setq REGION# (nth REGION# RNAMES)) ;*get the REGION#
    );progn
  );if userclick
  (DO_WORK5)
(princ)
)
(princ)

  (defun DO_WORK5()
     (if (= DWGTYPE "FP") (FP-DWG))
     (if (= DWGTYPE "SR") (SR-DWG)
     )
    (while (equal 8 (logand 8 (getvar "undoctl")))
         (command "_.undo" "_end")
    );while        
    (while (not (equal 8 (logand 8 (getvar "undoctl"))))
         (command "_.undo" "_begin")                
    );while
)

(defun FP-DWG ()
(C:INSERTDIM)
(setq RLAYER (strcat REGION# DWGTYPE))  <---This is where I'm combining 2 items to make up the command that is in another lisp. I want to be able to call the RLAYER so that it will initiate the command from the 2nd lisp file. In this case it is DOWORK
(command "limmax" "10000,10000")
(command ".regen")
(princ)
)
AutoCAD 9 - 2023, AutoCADMap 2008 - 2010, Revit 2012 - 2022, Autocad Civil 3D 2023

V-Man

  • Bull Frog
  • Posts: 343
  • I exist therefore I am! Finally Retired!
Stored variable ?
« Reply #8 on: February 20, 2004, 10:42:19 AM »
(command test)

The above comes up with

LISP command is not available.[/quote]
AutoCAD 9 - 2023, AutoCADMap 2008 - 2010, Revit 2012 - 2022, Autocad Civil 3D 2023

SMadsen

  • Guest
Stored variable ?
« Reply #9 on: February 20, 2004, 10:44:43 AM »
If RLAYER will hold the string "DOWORK" and your function is defined as (defun DOWORK ... then you can convert RLAYER to a symbol and run it like this:

(eval (list (read rlayer)))

If your function is defined as a command like (defun C:DOWORK ... and RLAYER is still holding the string "DOWORK" then you can merely put a "C:" in front and run it like this:

(eval (list (read (strcat "C:" rlayer))))

I don't see RLAYER holding anything but "R1FP" or such, though

V-Man

  • Bull Frog
  • Posts: 343
  • I exist therefore I am! Finally Retired!
Stored variable ?
« Reply #10 on: February 20, 2004, 10:47:09 AM »
That's it you goy it. That what I was trying to accomplish.
AutoCAD 9 - 2023, AutoCADMap 2008 - 2010, Revit 2012 - 2022, Autocad Civil 3D 2023

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Stored variable ?
« Reply #11 on: February 20, 2004, 10:52:16 AM »
Ive written down some terms.

Expression: 10

Primitive procedure: +, -, * etc. (These are ofter refered to as "functions")

Combination: (+ 1 2)

Procedure:
Code: [Select]
(defun MyProcedure ()
  (princ "\nThis is my first procedure My name is ")
  (princ (+ (+ 1 2) 4))
 (princ)
)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

SMadsen

  • Guest
Stored variable ?
« Reply #12 on: February 20, 2004, 10:53:51 AM »
Good :)

Quote from: dvarino
now in my lisp the "test" is holding the value of DOWORK.


Just to make it easier on yourself, please be more specific when showing something like the above. Most here would read the value of the variable test as a quoted symbol where it, in fact, was meant as a string.
No big deal - just an observation.

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Stored variable ?
« Reply #13 on: February 20, 2004, 10:54:11 AM »
Oh, good, i see stig got it. Good job!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

V-Man

  • Bull Frog
  • Posts: 343
  • I exist therefore I am! Finally Retired!
Stored variable ?
« Reply #14 on: February 20, 2004, 10:55:20 AM »
Thanks for the help and advice. Much appreciated.

Don
AutoCAD 9 - 2023, AutoCADMap 2008 - 2010, Revit 2012 - 2022, Autocad Civil 3D 2023

V-Man

  • Bull Frog
  • Posts: 343
  • I exist therefore I am! Finally Retired!
Stored variable ?
« Reply #15 on: February 20, 2004, 12:48:41 PM »
How can I get this code to do nothing if the cancel button is selected on the dialog box? Where in the code should I place it?


Code: [Select]

(defun C:GO ()
  (setq DWG-NAME (strcase (getvar "DWGNAME")))
  (setq DWG-SEQ (substr DWG-NAME 3 2))
  (if (or (= T (wcmatch DWG-SEQ "FP")) (= T (wcmatch DWG-SEQ "SR")))
    (CONTINUE-ON)
    (alert
      "\nThis will not work in the drawing you are in. ABORTING!!!!!!!!!"
    )
  )
  (princ)
)

(defun CONTINUE-ON ()
  (setq REGION# "R3")
  (setq DWGTYPE "FP")
  (setq RNAMES '("R3" "R4" "R8" "R9" "R11"))
  (setq dcl_id (load_dialog "Gset.dcl"))
  (if (not (new_dialog "Gset" dcl_id))
    (exit)
  )
  (start_list "selections")
  (mapcar 'add_list RNAMES)
  (end_list)
  (action_tile "rb1" "(setq DWGTYPE \"FP\")")
  (action_tile "rb2" "(setq DWGTYPE \"SR\")")
  (action_tile
    "cancel"
    "(done_dialog) (setq userclick nil)"
  )
  (action_tile
    "accept"
    (strcat
      "(progn
       (setq REGION# (atof (get_tile \"selections\")))"
      " (done_dialog)(setq userclick T))"
    )
  )
  (start_dialog)
  (unload_dialog dcl_id)
  (if userclick
    (progn
      (setq REGION# (fix REGION#))
      (setq REGION# (nth REGION# RNAMES))
    )
  )
  (DO_WORK5)
  (setq REGION# nil
DWGTYPE nil
RNAMES nil
DCL_ID nil
  )
  (princ)
)

(defun DO_WORK5 ()
  (if (= DWGTYPE "FP") (FP-DWG))
  (if (= DWGTYPE "SR") (SR-DWG))
  (while (equal 8 (logand 8 (getvar "undoctl")))
    (command "_.undo" "_end")
  )        
  (while (not (equal 8 (logand 8 (getvar "undoctl"))))
    (command "_.undo" "_begin")
  )
)

(defun FP-DWG ()
  (DO-ALL)
  (C:INSERTDIM)
  (setq RLAYER (strcat REGION# DWGTYPE))
  (eval (list (read (strcat "C:" RLAYER))))
  (princ)
)

(defun SR-DWG ()
  (DO-ALL)
  (setq RLAYER (strcat REGION# DWGTYPE))
  (eval (list (read (strcat "C:" RLAYER))))
  (princ)
)

(defun DO-ALL ()
  (command "audit" "y" "")
  (setq *doc* (vla-get-ActiveDocument (vlax-get-acad-object)))
  (vla-AuditInfo *doc* :vlax-true)
  (repeat 3
    (vla-PurgeAll *doc*)
  )
  (command "limmax" "10000,10000")
  (command ".regen")
  (princ)
)
[/quote]
AutoCAD 9 - 2023, AutoCADMap 2008 - 2010, Revit 2012 - 2022, Autocad Civil 3D 2023

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Stored variable ?
« Reply #16 on: February 20, 2004, 01:00:40 PM »
(quit) or (exit) to call your error trap.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

SMadsen

  • Guest
Stored variable ?
« Reply #17 on: February 20, 2004, 02:02:02 PM »
Don, here are some commented alterations to the code you posted. You might just find some answers to your questions in there.

Code: [Select]
(defun CONTINUE-ON (/ REGION# DWGTYPE RNAMES DCL_ID myAction)
  (setq REGION# "R3")
  (setq DWGTYPE "FP")
  (setq RNAMES '("R3" "R4" "R8" "R9" "R11"))
  (setq dcl_id (load_dialog "Gset.dcl"))
 
  ;; I like to use COND instead of (if (not (new_dialog ...))(exit)).
  ;; First off, I don't like EXIT or QUIT and secondly, you get a
  ;; nice wrap-around of all dialog-based calls
  (cond
    ((new_dialog "Gset" dcl_id)

     (start_list "selections")
     (mapcar 'add_list RNAMES)
     (end_list)
     (action_tile "rb1" "(setq DWGTYPE \"FP\")")
     (action_tile "rb2" "(setq DWGTYPE \"SR\")")
     ;; If you're using the default OK & Cancel buttons, there's
     ;; no need to assign actions - they are precoded.
     ;; You do need to set REGION#, though. But you are setting
     ;; a default value at the beginning so there's no need to
     ;; read it if the user didn't select anything else.
     ;; Why not put assigment of REGION# where it belongs: the
     ;; popup (or list_box?) where the user selects it?
     ;; This will  assign REGION# to whichever item the user
     ;; selects - WHEN he/she makes a selection:
     (action_tile "selections" "(setq REGION# (nth (atoi $value) RNAMES))")

     ;; How to get the button hit on exit?
     ;; START_DIALOG returns a value that is normally defined by
     ;; DONE_DIALOG. But the predefined OK and Cancel buttons return
     ;; 1 and 0, respectively (if they've not been overriden by other
     ;; tiles)
     (setq myAction (start_dialog))
    )
  )
  (unload_dialog dcl_id)
  ;; Check if OK was hit. If so do your stuff, otherwise do nawt
  ;; Pass local vars as arguments to DO_WORK5
  (if (= myAction 1)
    (DO_WORK5 REGION# DWGTYPE)
  )
  ;; no need to set vars to nil if they're localized
  ;; (setq REGION# nil .. etc. deleted
  (princ)
)

(defun DO_WORK5   (REGION# DWGTYPE)
  ;; After inspecting the DWGTYPE argument, pass
  ;; the arguments to the lucky function
  (if (= DWGTYPE "FP") (FP-DWG REGION# DWGTYPE))
  (if (= DWGTYPE "SR") (SR-DWG REGION# DWGTYPE))
  ;; This below I'm won't comment
  (while (equal 8 (logand 8 (getvar "undoctl")))
    (command "_.undo" "_end")
  )
  (while (not (equal 8 (logand 8 (getvar "undoctl"))))
    (command "_.undo" "_begin")
  )
)

(defun FP-DWG (REGION# DWGTYPE)
  (DO-ALL)
  ;; If you're using REGION# or DWGTYPE in C:INSERTDIM
  ;; then make sure they are passed as arguments
  (C:INSERTDIM)
  (setq RLAYER (strcat REGION# DWGTYPE))
  (eval (list (read (strcat "C:" RLAYER))))
  (princ)
)

(defun SR-DWG (REGION# DWGTYPE)
  (DO-ALL)
  (setq RLAYER (strcat REGION# DWGTYPE))
  (eval (list (read (strcat "C:" RLAYER))))
  (princ)
)

;; Important: one of the changes here makes the document
;; object a local variable that is released after use! If you
;; are using it as a global variable somewhere else then make
;; sure you are getting the doc in those routines, also.
;; Alternatively, don't adopt the changes!
(defun DO-ALL (/ doc)
  (command "audit" "y" "")
  (setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (vla-AuditInfo doc :vlax-true)
  (repeat 3
    (vla-PurgeAll doc)
  )
  ;; No need to use the slow COMMAND for this one
  (setvar "LIMMAX" '(10000.0 10000.0))
  ;; Why not regen with the document already at hand?
  (vla-regen doc acAllViewports)
  ;; Don't go without cleaning up
  (vlax-release-object doc)
  ;; If this is not the last routine then it'd be good
  ;; to remove the PRINC if it's included somewhere else
  ;; (and it is :)
  (princ)
)

V-Man

  • Bull Frog
  • Posts: 343
  • I exist therefore I am! Finally Retired!
Stored variable ?
« Reply #18 on: February 20, 2004, 03:34:58 PM »
Thanks SMadsen for the critique. I'm always looking for better (smarter) ways for doing something. I'm still very new to Visual Lisp. I'm still learning to walk before I can run.

Thanks again..

Don
AutoCAD 9 - 2023, AutoCADMap 2008 - 2010, Revit 2012 - 2022, Autocad Civil 3D 2023

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Stored variable ?
« Reply #19 on: February 20, 2004, 11:43:55 PM »
Quote from: dvarino
.....I'm still learning to walk before I can run.....


Many of us here are still learning to crawl .... and have not yet progressed enough to even call ourselves crawlers.

The fact is that you will never know ALL there is to know about programming in any particular language. Some of the more elegant and resourceful coders like Stig and Matt would likely agree. I have been coding in Lisp for 10 years and have written applications that are around 1 meg in lisp. Very very complex routines, yet I still find new ways to implement code that I surprise myself regularly.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

V-Man

  • Bull Frog
  • Posts: 343
  • I exist therefore I am! Finally Retired!
Stored variable ?
« Reply #20 on: February 23, 2004, 03:48:14 PM »
I'm modifying the code below to add additional features. Now I'm getting "too few arguments". Am I just missing something here. Any comments welcome.


Code: [Select]

(defun C:GSA (/ REGION# DWGTYPE DWGTYPE1 RNAMES DCL_ID myAction)
(setvar "CMDECHO" 0)
 (setq REGION# "R3")
 (setq DWGTYPE "FP")
  (setq RNAMES '("R3" "R4" "R8" "R9" "R11"))
  (setq dcl_id (load_dialog "Gset.dcl"))
    (cond
    ((new_dialog "Gset" dcl_id)
     (start_list "selections")
     (mapcar 'add_list RNAMES)
     (end_list)
     (mode_tile "tog1" 1)
     (action_tile "rb1" "(setq DWGTYPE \"FP\")(mode_tile \"tog1\" 0)")
     (action_tile "rb2" "(setq DWGTYPE \"SR\")(mode_tile \"tog1\" 1)")
     (action_tile "selections" "(setq REGION# (nth (atoi $value) RNAMES))")
     (action_tile "tog1" "(setq DWGTYPE1 \"SP\")(mode_tile \"rb2\" 1)")
     (setq myAction (start_dialog))
    )
  )
  (unload_dialog dcl_id)
  (if (= myAction 1)
    (DO_WORK5 REGION# DWGTYPE)
  )
  (princ)
)

(defun DO_WORK5   (REGION# DWGTYPE)
  (if (= DWGTYPE "FP") (FP-DWG REGION# DWGTYPE))
  (if (= DWGTYPE "SR") (SR-DWG REGION# DWGTYPE))
)

(defun FP-DWG (REGION# DWGTYPE)
  (C:INSERTDIM)
  (DO-ALL)
      (if (/= DWGTYPE1 NIL)
      (INSERT_SITE_LAYERS)
              (DO-FP)
      )
  )
 
    (defun DO-FP (REGION# DWGTYPE)
  (setq RLAYER (strcat REGION# DWGTYPE))
  (eval (list (read (strcat "C:" RLAYER))))
  (princ)
)

(defun SR-DWG (REGION# DWGTYPE)
  (DO-ALL)
  (setq RLAYER (strcat REGION# DWGTYPE))
  (eval (list (read (strcat "C:" RLAYER))))
  (princ)
)

(defun DO-ALL (/ doc)
  (C:DELNUL)
  (C:FIXSEQEND)
  (command "audit" "y" "")
  (setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (vla-AuditInfo doc :vlax-true)
  (repeat 3
    (vla-PurgeAll doc)
  )
  (setvar "LIMMIN" '(0 0))
  (setvar "LIMMAX" '(10000.0 10000.0))
  (vla-regen doc acAllViewports)
  (vlax-release-object doc)
  (princ)
)
 
  (defun INSERT_SITE_LAYERS ()
  (setq SITEL "site")
    (setq RLAYER1 (strcat REGION# SITEL))
    (eval (list (read (strcat "C:" RLAYER1))))
    (princ)
    )
AutoCAD 9 - 2023, AutoCADMap 2008 - 2010, Revit 2012 - 2022, Autocad Civil 3D 2023

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Stored variable ?
« Reply #21 on: February 23, 2004, 03:53:06 PM »
you have this (DO-FP) but it looks like it needs some arguments   (defun DO-FP (REGION# DWGTYPE)
TheSwamp.org  (serving the CAD community since 2003)

V-Man

  • Bull Frog
  • Posts: 343
  • I exist therefore I am! Finally Retired!
Stored variable ?
« Reply #22 on: February 23, 2004, 03:57:07 PM »
Thanks for that. I forgot I put those in there.
(defun DO-FP () <-----Corrected.
AutoCAD 9 - 2023, AutoCADMap 2008 - 2010, Revit 2012 - 2022, Autocad Civil 3D 2023

SMadsen

  • Guest
Stored variable ?
« Reply #23 on: February 24, 2004, 10:19:55 AM »
The idea of using local variables fades away if you don't pass arguments to DO-FP. So, if I were you, I would leave the definition of DO-FP as it is and instead call it in FP-DWG as (DO-FP REGION# DWGTYPE) - thereby passing the right arguments to DO-FP.

Also in the last function, INSERT-SITE-LAYERS, you are using REGION# without it being declared as an argument. This does not trigger a stringp error because REGION# is exposed from FP-DWG but it is risky business to use local variables exposed from a calling function.