Jump to content

Recommended Posts

Posted (edited)

New to the site and need a little help with a lisp routine I wrote . I cannot figure out how to round my results to show only two decimal places and also to get my text results to print on one line with a comment and the variable with out repeating the text command .

:?

My routine

(defun C:MH ()
(setq pnt (getpoint "\n Pick Structure :"))
(setq el (caddr pnt))
(setq in1 (getreal " \n Enter Inv. in: " ))
(setq out1 (getreal " \n Enter Inv. out: " ))
(setq in (- el in1))
(setq out (- el out1))
(command "TEXT" (getpoint) "0" "TOP = ")
(command "TEXT" "" el)
(command "TEXT" "" "In =")
(command "TEXT" "" in )
(command "TEXT" "" "Out =")
(command "TEXT" "" out)

Edited by SLW210
Posted

(rtos variable 2 2)

(strcat "string" "string")

(setvar 'cmdecho 0)(princ/print)

(setvar "dimzin 3)

 

Welcome to the forum Djames :)

Posted

Welcome to CADTutor djames, hope you like it here.

 

Firstly, regarding posting code, have a read of this.

 

I have slightly modified your code and added some comments to explain my reasoning; if you have any questions about my modifications, just ask.

 

([color=BLUE]defun[/color] c:mh ( [color=BLUE]/[/color] cmd el in out pt tp )

   [color=GREEN];; Define function and localise variables.[/color]

   [color=GREEN];; Variable localisation is very important and a[/color]
   [color=GREEN];; good practice to get into. Not sure why? See:[/color]

   [color=GREEN];; www.lee-mac.com/localising.html[/color]

   ([color=BLUE]if[/color]

       [color=GREEN];; If the following expression returns a non-nil[/color]
       [color=GREEN];; value (anything not equal to nil)[/color]

       ([color=BLUE]and[/color]

           [color=GREEN];; All of the following expressions must return a[/color]
           [color=GREEN];; non-nil expression for AND to return T;[/color]
           [color=GREEN];; (and hence for the IF statement to be validated).[/color]

           [color=GREEN];; AND will stop evaluating expressions when an expression[/color]
           [color=GREEN];; returns nil.[/color]

           ([color=BLUE]setq[/color] pt ([color=BLUE]getpoint[/color] [color=MAROON]"\nPick Structure: "[/color]))

           [color=GREEN];; If the user has picked a point, getpoint will[/color]
           [color=GREEN];; return that point, otherwise getpoint will return nil.[/color]

           ([color=BLUE]setq[/color] in ([color=BLUE]getreal[/color] [color=MAROON]"\nEnter Inv. in: "[/color]))

           [color=GREEN];; If the user enters a number, getreal will return that[/color]
           [color=GREEN];; number, otherwise getreal will return nil and the AND[/color]
           [color=GREEN];; statement will stop evaluating expressions.[/color]

           ([color=BLUE]setq[/color] out ([color=BLUE]getreal[/color] [color=MAROON]"\nEnter Inv. out: "[/color]))

           [color=GREEN];; Again, if the user enters a number, getreal will return that[/color]
           [color=GREEN];; number, otherwise getreal will return nil.[/color]

           ([color=BLUE]setq[/color] tp ([color=BLUE]getpoint[/color] [color=MAROON]"\nPick Point for Text: "[/color]))

           [color=GREEN];; Same as previous getpoint statement. If this expression[/color]
           [color=GREEN];; returns a non-nil value (a point), then all the expressions[/color]
           [color=GREEN];; have returned a non-nil value and the AND function will return T.[/color]

       ) [color=GREEN];; END AND[/color]

       ([color=BLUE]progn[/color]

           [color=GREEN];; Wrap the following expressions into a single expression[/color]
           [color=GREEN];; so that a single expression can be passed to the IF statement[/color]
           [color=GREEN];; as the 'THEN' argument.[/color]

           ([color=BLUE]setq[/color]
               
               el  ([color=BLUE]caddr[/color] pt)
               in  ([color=BLUE]-[/color] el in)
               out ([color=BLUE]-[/color] el out)
               
           ) [color=GREEN];; END SETQ[/color]

           ([color=BLUE]setq[/color] cmd ([color=BLUE]getvar[/color] 'CMDECHO))

           [color=GREEN];; Store the value of the CMDECHO System Variable so[/color]
           [color=GREEN];; that we may reset it later.[/color]

           [color=GREEN];; In the future, consider adding an *error* handler to this[/color]
           [color=GREEN];; routine, so that the CMDECHO System Variable is reset if[/color]
           [color=GREEN];; the routine errors.[/color]

           ([color=BLUE]setvar[/color] 'CMDECHO 0)

           [color=GREEN];; Set CMDECHO to zero so that we don't see the command calls.[/color]

           ([color=BLUE]command[/color]
               
               [color=MAROON]"_.text"[/color] [color=MAROON]"_non"[/color] tp 0.0 ([color=BLUE]strcat[/color] [color=MAROON]"TOP = "[/color] ([color=BLUE]rtos[/color] el 2 2))
               [color=MAROON]"_.text"[/color] [color=MAROON]""[/color] ([color=BLUE]strcat[/color] [color=MAROON]"In = "[/color]  ([color=BLUE]rtos[/color] in  2 2))
               [color=MAROON]"_.text"[/color] [color=MAROON]""[/color] ([color=BLUE]strcat[/color] [color=MAROON]"Out = "[/color] ([color=BLUE]rtos[/color] out 2 2))
               
           ) [color=GREEN];; END COMMAND[/color]

           [color=GREEN];; Note that I use the "_" prefix to allow for other[/color]
           [color=GREEN];; language versions of CAD and the "." prefix to ensure the[/color]
           [color=GREEN];; default TEXT command is used, not a redefinition of the TEXT command[/color]

           ([color=BLUE]setvar[/color] 'CMDECHO cmd)

           [color=GREEN];; Reset the CMDECHO System Variable to its previous value[/color]
           [color=GREEN];; stored earlier.[/color]

       ) [color=GREEN];; END PROGN[/color]

       [color=GREEN];; The 'ELSE' expression is optional, and we don't really need it[/color]
       [color=GREEN];; for this program.[/color]
       
   ) [color=GREEN];; END IF[/color]

   ([color=BLUE]princ[/color])

   [color=GREEN];; Print a null symbol as the return of the function[/color]
   [color=GREEN];; this ensures the command 'exits cleanly'.[/color]

) [color=GREEN];; END DEFUN[/color]

There are still many parts of the program that could be improved; for example, an error handler could be added to reset the environment should the routine error. (See my tutorial on Error Handling here).

 

Also, you could include the trans function to allow for changes in the UCS; but we'll start of with the basics first.

 

I hope this helps!

 

Lee

Posted

thanks for the help . tried to edit post . but no pound sign in editor , wont happen again .

Posted
thanks for the help . tried to edit post . but no pound sign in editor , wont happen again .

 

Click the Go Advanced after the Edit Post to add code tags. :thumbsup:

 

I'll get it this time for you, at least you tried. :thumbsup:

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