eimimitu 发表于 2022-7-5 15:38:51

What's wrong with this LI

I pieced together this routine which draws a line from its midpoint..
I added error handler and echo off...
 
The LISP executes fine but it gives me an "; error: AutoCAD variable setting rejected: CMDECHO nil" message... not sure how to fix it. It also spits out some coordinates at the end of the routine that I'd like to get rid of (which echo off should take care of right?)
 
Currently it allows me to pick a point and displays the default dashed rubber band/cursor tracer line. I would like for it to be dynamic where it shows the preview of the actual line in both directions from starting point and the rubber band/tracer line as well... Similar to when you draw a circle and type "D" for diameter: it shows a preview of said circle and the rubber band/tracer line extends past it the length of the radius...
 
Thanks in advance...
 
One last thing... I'm super new at LISPing so please be as detailed as possible... Thanks again
MidLine LISP:

(defun c:MidLine (/ *error* pt1 pt1w pt2 pt2w pt0 ang len ed)(defun *error* ( msg )       (if ocmd (setvar 'CMDECHO ocmd))       (if acdoc (_EndUndoMark acdoc))       (if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*"))         (princ (strcat "\nError: " msg))       )       (princ)   )   (setq acdoc (vla-get-activedocument (vlax-get-acad-object)))   (_StartUndoMark acdoc)   (setq acdoc (vla-get-activedocument (vlax-get-acad-object)))   (_StartUndoMark acdoc)   (setq ocmd (getvar 'CMDECHO))   (setvar 'CMDECHO 0) (setq pt1 (getpoint "\nMIDLINE Specify midpoint of line: "))   (setq pt1w (trans pt1 1 0)         pt2(getpoint pt1 "\nMIDLINE Specify endpoint of line: ")         pt2w (trans pt2 1 0)   )   (setq ang (angle pt2w pt1w))   (setq len (/ (distance pt1w pt2w) 2.0))   (setq pt0 (polar pt1w ang len))   (setq ang (angle pt1w pt2w))   (setq pt2 (polar pt1w ang len))   (setq ed (list '(0 . "LINE") (cons 10 pt0) (cons 11 pt2)))   (entmake ed) )(defun _StartUndoMark ( doc )   (_EndUndoMark doc)   (vla-StartUndoMark doc))(defun _EndUndoMark ( doc )   (if (= 8 (logand 8 (getvar 'UNDOCTL)))       (vla-EndUndoMark doc)   ))   (setvar 'CMDECHO ocmd)   (_EndUndoMark acdoc)(princ)

BIGAL 发表于 2022-7-5 15:45:51

I would add some force osnaps "_mid" or use osmode variables get existing osmode then set for MID & END

Tharwat 发表于 2022-7-5 15:50:08

Hi,
Not sure if this goal if the program is what you are after but its as per your original codes.
 
Besides that, this is just for your learning and its more than enough.
 

(defun c:midline (/ pt1 pt1w pt2 pt2w pt0 ang len) (and (setq pt1 (getpoint "\nMIDLINE Specify midpoint of line: "))      (setq pt2 (getpoint pt1 "\nMIDLINE Specify endpoint of line: "))      (setq pt1w (trans pt1 1 0))      (setq pt2w (trans pt2 1 0))      (setq ang (angle pt2w pt1w))      (setq len (/ (distance pt1w pt2w) 2.0))      (setq pt0 (polar pt1w ang len))      (setq ang (angle pt1w pt2w))      (setq pt2 (polar pt1w ang len))      (entmake (list '(0 . "LINE") (cons 10 pt0) (cons 11 pt2)))      ) (princ))

Grrr 发表于 2022-7-5 15:54:48

Note that the _EndUndoMark subfunction should be wrapped within a while loop, so substitute the if function with while - it was explained somewhere why (so I won't bother).

eimimitu 发表于 2022-7-5 15:56:15

Thanks for the responses...
 
You got rid of the weird coordinates it was displaying... GREAT! Thank you!.....
 
Now how do I properly add error handling and echo off? and how do i make it display a dynamic preview of the line to be drawn as described above?

Tharwat 发表于 2022-7-5 16:02:45

 
You are welcome.
 
 
Can you please answer the following questions?
 
1- Why do you want to use error handler function ?
2- What's the use of system variable 'CMDECHO and why do you want use it with that simple routine?

eimimitu 发表于 2022-7-5 16:03:15

I guess I don't need CMDECHO for your simplified version since it stopped displaying those weird coordinates... as for error handling: so that it won't display the "error; function cancelled" message if I press escape key mid command..
I was just under the impression that these two elements are standard procedure when LISPing... forgive my ignorance if that's completely false but...
Ultimately the routine works as intended... I'm just trying to fine tune it to my preference and to try to grasp some of the concepts of this awesome programming language

Tharwat 发表于 2022-7-5 16:07:11

 
If you add the princ function at the end then this should prevent that to happen.
 
 
Yes you can add the error handler but if you want to cancel the command from continuing then a sample hit on the space bar or enter should exit the command quietly and safely.
 
No worries, you are doing just well with your initial believes & thoughts about programming.

SLW210 发表于 2022-7-5 16:11:16

Please read the Code Posting Guidelines and edit your Code to be included in Code Tags.
Your Code Here =
Your Code Here

eimimitu 发表于 2022-7-5 16:15:03

Upon further research I discovered that i need to add GRREAD to my code in order to get a dynamic preview of the line... I just don't have a clue how to incorporate it and have it display the line dynamically in both directions from starting mid-point. Any insight on this would be greatly appreciated.
页: [1] 2
查看完整版本: What's wrong with this LI