Jump to content

Fixing My Plot Command


AQucsaiJr

Recommended Posts

I have written a real quick LISP to use as a command to plot with. I want to add a little somthing to it. It works perfectly fine the way it is written so I am not looking for it to be re-written, but I would like to add a way to give the user a choice of plot size. I only need two choices, Letter and 11X17. Can somone show me what to add to make this choice available?

This is the original code:

(DEFUN C:WP ()
(COMMAND "-PLOT" "YES" "model" "HP LaserJet M5035 MFP PCL (BackOffice).pc3" "11x17" "INCHES" "LANDSCAPE" "NO" "WINDOW" PAUSE PAUSE "FIT" "CENTER" "YES" "MONOCHROME.CTB" "YES" "WIREFRAME" "NO" "NO" "YES")
(PRINC)
)

I think I need to add a user defined variable "size" and place it in the spot that is now labeled 11x17. I just am not sure how to write this code in.

This is my best attempt, but I get an error with it:

(DEFUN C:WPO (/ size )
(setq size (getkword "\n Plot Size [Letter / 11x17]: <Letter> "))
(COMMAND "-PLOT" "YES" "model" "HP LaserJet M5035 MFP PCL (BackOffice).pc3" size "INCHES" "LANDSCAPE" "NO" "WINDOW" PAUSE PAUSE "FIT" "CENTER" "YES" "MONOCHROME.CTB" "YES" "WIREFRAME" "NO" "NO" "YES")
(PRINC)
)

 

I should mention it is a window plot.

Link to comment
Share on other sites

This is what I got:

 

(DEFUN C:WP (/ size p1 p2 )
   (initget "Letter 11x17 ")
   (setq size (getkword "Paper Size: [Letter/11x17]: ")
       p1 (getpoint "\nPick first corner of window: \n")
       p2 (getcorner p1 "\nPick opposite corner of window: \n")) 
(COMMAND "-PLOT" "YES" "model" "HP LaserJet M5035 MFP PCL (BackOffice).pc3" size "INCHES" "LANDSCAPE" "NO" "WINDOW" P1 P2 "FIT" "CENTER" "YES" "MONOCHROME.CTB" "YES" "WIREFRAME" "NO" "NO" "YES")
(PRINC)
)

Link to comment
Share on other sites

Nice one mate - looks like you got the hang of it :)

 

Shall we see if we can get a default in there? o:) I'll guide you :)

 

Note that the return of getkword if the user hits enter is nil - so we need to construct some logic that sets the option to the default if the getkword returns nil.

Link to comment
Share on other sites

Here a little example:

(DEFUN C:WP (/ size p1 p2 )
 (initget "Letter 11x17 ")
 (and
   (or (setq size (getkword "Paper Size: [Letter/11x17] <Letter>: "))
       (setq size "Letter"))
   (setq p1 (getpoint "\nPick first corner of window: \n"))
   (setq p2 (getcorner p1 "\nPick opposite corner of window: \n"))
   (COMMAND "-PLOT" "YES" "model" "HP LaserJet M5035 MFP PCL (BackOffice).pc3" size "INCHES" "LANDSCAPE" "NO" "WINDOW" P1 P2 "FIT" "CENTER" "YES" "MONOCHROME.CTB" "YES" "WIREFRAME" "NO" "NO" "YES")
   )
(PRINC)
)

Link to comment
Share on other sites

So if I did this I would have a default to Letter with no problems?

 

(DEFUN C:WPO (/ size p1 p2 )
   (initget "Letter 11x17 ")
   (or(setq size (getkword "Paper Size: [Letter/11x17]:<Letter> "))
      (setq size "Letter"))
       (setq p1 (getpoint "\nPick first corner of window: \n"))
       (setq p2 (getcorner p1 "\nPick opposite corner of window: \n")) 
(COMMAND "-PLOT" "YES" "model" "HP LaserJet M5035 MFP PCL (BackOffice).pc3" size "INCHES" "LANDSCAPE" "NO" "WINDOW" P1 P2 "FIT" "CENTER" "YES" "MONOCHROME.CTB" "YES" "WIREFRAME" "NO" "NO" "YES")
(PRINC)
)

 

What about if I used the (if) rather than (or)?

 

(DEFUN C:WPO (/ size p1 p2 )
   (initget "Letter 11x17 ")
   (if(setq size (getkword "Paper Size: [Letter/11x17]:<Letter> "))
      (setq size "Letter"))
       (setq p1 (getpoint "\nPick first corner of window: \n"))
       (setq p2 (getcorner p1 "\nPick opposite corner of window: \n")) 
(COMMAND "-PLOT" "YES" "model" "HP LaserJet M5035 MFP PCL (BackOffice).pc3" size "INCHES" "LANDSCAPE" "NO" "WINDOW" P1 P2 "FIT" "CENTER" "YES" "MONOCHROME.CTB" "YES" "WIREFRAME" "NO" "NO" "YES")
(PRINC)
)

 

Would this still work?

Link to comment
Share on other sites

Here's a little more information on the topic of logic and defaults, there are many ways to do these things, so this post is by no means complete.

 

Firstly, you need to remember that getkword will return nil if the user presses enter, so to control defaults, we exploit this fact.

 

There are many ways to approach this, but here are a few methods that I've quickly put together.

 

The easiest examples are when you want a "set" default that will never change (like a yes/no, that always defaults to yes):

 

(initget "Yes No")
(if (not (setq ans (getkword "\nYes or No <Yes> : ")))
 (setq ans "Yes"))

(initget "Yes No")
(setq ans (cond ((getkword "\nYes or No <Yes> : "))
               ("Yes")))

(initget "Yes No")
(or (setq ans (getkword "\nYes or No <Yes> : "))
   (setq ans "Yes"))

But we can take this a step further and perhaps have a default that will change to the last entered value.

 

For example, when retrieving a non-zero positive number:

 

(if (not *def*) (setq *def* 3.0))

(initget 6)
(if (setq tmp (getreal (strcat "\nEnter a Number <" (rtos *def*) "> : ")))
 (setq *def* tmp))

Here we set up an initial default "*def*", (which will remain global - i.e. not localised), and, if the user enters a new number on the prompt, we will set the number to our default. The variable *def* will then be used by our program for whatever purpose.

 

Hence, if the user hits enter on the prompt, the value of *def* will not change, and the default value will be used by our program.

 

Other ways to accomplish this same result:

 

(or *def* (setq *def* 3.0))

(initget 6)
(and (setq tmp (getreal (strcat "\nEnter a Number <" (rtos *def*) "> : ")))
    (setq *def* tmp))

(or *def* (setq *def* 3.0))

(initget 6)
(setq *def* (cond ((getreal (strcat "\nEnter a Number <" (rtos *def*) "> : ")))
                 (*def*)))

For those who haven't seen the functions "and" & "or" used in this way, to help clarify it, think about the way these functions are evaluated.

 

With the use of or, the statements are evaluated until a statement evaluates to true. At which point evaluation stops.

 

With the use of and, the statements are evaluated until a statement evaluates to nil (false). At which point evaluation stops.

 

Also, bear in mind that true is anything non-nil.

 

 

I hope this helps you understand,

 

Lee

Link to comment
Share on other sites

That helps alot Lee, thanks... i could not find any information out on the proper use of (and) and (or) functions.

 

Does the (if) statment always have to be fallowed by a (not) statement? I can't find any examples that do not fallow this order.

Link to comment
Share on other sites

Andrew,

 

Look in the Visual LISP Editor Help files. They contain all the information you are every likely to need at this stage on using the LISP functions.

 

Some of it is written for VBA, but can easily be adapted for LISP - but at this stage, unless you decide to venture into Visual LISP, you should be fine.

Link to comment
Share on other sites

I have a question... I write all my LISP in Notepad... Would it be better to write them in the editor within ACAD?

 

Definitely.

 

The editor in AutoCAD is 100x better than Notepad - the syntax highlighting and help is indispensable.

 

The other route is Notepad++ or other main-stream code editors such as Vim, but these don't offer as much on the LISP help.

Link to comment
Share on other sites

On top of all that I have just said, the Visual LISP Editor (VLIDE) in AutoCAD, has debugging and formatting options, which can set out the code for you, and also let you know where problematic areas are.

 

There are also "auto word complete" options, variable watching/tracing and loads more.

 

More info on how to use it here:

 

http://www.afralisp.net/vl/vlisp.htm

 

http://www.afralisp.net/vl/vl-edit.htm

 

http://midpointcad.com/au/docs/lakose_The_Visual_LISP_Developers_Bible.pdf

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