This seems absolutly silly.
(setq p1 (getpoint))
(setq p2 (getpoint))
(setq Dist (distance p1 p2))
Why does this not work?
I'm trying to build a command to replace AutoCAD's "DI" command, to give distance and delta x, y, and z values in decimal and imperial, and keep the angles in XY plane and out of XY plane.
KP
Is there a chance that you have localised or redefined the distance symbol?
Does the error still occur if you restart AutoCAD with no customisations loaded?
This is what I've started with this morning.
I use "alert" when I build code to make that my function are being defined correctly. They do get removed in the finlal version.
(defun C:ds ()
(setq p1 (getpoint))
(setq p2 (getpoint))
(setq p1x (car p1))
(setq p1y (cadr p1))
(setq p1z (caddr p1))
(setq p2x (car p2))
(setq p2y (cadr p2))
(setq p2z (caddr p2))
(setq Dist (distance p1 p2))
(setq DistDec (rtos Dist 2 8))
(setq DistArch (rtos Dist 4 8))
(Alert DistDec)
(Alert DistArch)
(setq FirstLine (strcat "Distance = " DistArch "(" DistDec ")" " , Angle in XY Plane = "))
(Alert FirstLine)
Could you post the exact error message as it appears on the command-line?
this is wierd, now it's working
I have two more problems, that I awalys seem to have.
1. The "rubber band" when picking two points.
2. Properly displaying messages at the command prompt. I built the following code this morning, to look like autocad's "dist" command.
(setq p1 (getpoint))
(setq p2 (getpoint))
(setq p1x (car p1))
(setq p1y (cadr p1))
(setq p1z (caddr p1))
(setq p2x (car p2))
(setq p2y (cadr p2))
(setq p2z (caddr p2))
(setq Dist (distance p1 p2))
(setq AngRadian (angle p1 p2))
(setq AngDegrees (* 180 (/ AngRadian pi)))
(setq Ang (rtos AngDegrees 2 8))
(setq DistInXY (distance (list p1x p1y 0) (list p2x p2y 0)))
(setq DeltaZ (- p2z p1z))
(setq AngOutOfPlane (atan (/ DeltaZ DistInXY)))
(setq AngOutOfPlane (rtos AngOutOfPlane 2 8))
(setq DistDec (rtos Dist 2 8))
(setq DistArch (rtos Dist 4 8))
(setq DeltaX (- p2x p1x))
(setq DeltaXArch (rtos DeltaX 4 8))
(setq DeltaXDec (rtos DeltaX 2 8))
(setq DeltaY (- p2y p1y))
(setq DeltaYArch (rtos DeltaY 4 8))
(setq DeltaYDec (rtos DeltaY 2 8))
(setq DeltaZ (- p2z p1z))
(setq DeltaZArch (rtos DeltaZ 4 8))
(setq DeltaZDec (rtos DeltaZ 2 8))
(prin1 (strcat "\nDistance = "DistArch " (" DistDec ") , Angle in XY Plane = " Ang " , Angle From XY Plane = " AngOutOfPlane))
(prin1 (strcat "\nDelta X = "DeltaXArch " (" DeltaXDec "), Delta Y = " DeltaYArch " (" DeltaYDec "), Delta Z = " DeltaZArch " (" DeltaZDec ")"))
okay now I only have the "rubber band" problem
(setq p1 (getpoint))
(setq p2 (getpoint))
(setq p1x (car p1))
(setq p1y (cadr p1))
(setq p1z (caddr p1))
(setq p2x (car p2))
(setq p2y (cadr p2))
(setq p2z (caddr p2))
(setq Dist (distance p1 p2))
(setq AngRadian (angle p1 p2))
(setq AngDegrees (* 180 (/ AngRadian pi)))
(setq Ang (rtos AngDegrees 2 8))
(setq DistInXY (distance (list p1x p1y 0) (list p2x p2y 0)))
(setq DeltaZ (- p2z p1z))
(setq AngOutOfPlane (atan (/ DeltaZ DistInXY)))
(setq AngOutOfPlane (rtos AngOutOfPlane 2 8))
(setq DistDec (rtos Dist 2 8))
(setq DistArch (rtos Dist 4 8))
(setq DeltaX (- p2x p1x))
(setq DeltaXArch (rtos DeltaX 4 8))
(setq DeltaXDec (rtos DeltaX 2 8))
(setq DeltaY (- p2y p1y))
(setq DeltaYArch (rtos DeltaY 4 8))
(setq DeltaYDec (rtos DeltaY 2 8))
(setq DeltaZ (- p2z p1z))
(setq DeltaZArch (rtos DeltaZ 4 8))
(setq DeltaZDec (rtos DeltaZ 2 8))
(princ (strcat "\nDistance = "DistArch " (" DistDec ") , Angle in XY Plane = " Ang " , Angle From XY Plane = " AngOutOfPlane))
(princ (strcat "\nDelta X = "DeltaXArch " (" DeltaXDec "), Delta Y = " DeltaYArch " (" DeltaYDec "), Delta Z = " DeltaZArch " (" DeltaZDec ")"))
(princ)
I would suggest the following modifications:
(defun c:mydist ( / 2dd ang del dis pt1 pt2 ) ;; Define function, declare local variables. ;; To understand why variable localisation is important ;; see http://bit.ly/15Qw104 (if ;; IF the following expression returns a non-nil value (and ;; All of the enclosed expressions must return a non-nil value for AND to return T. ;; AND will cease evaluation when an expression returns nil. (setq pt1 (getpoint "\nPick 1st point: ")) ;; Prompt the user for the 1st point (setq pt2 (getpoint "\nPick 2nd point: " pt1)) ;; Prompt the user for the 2nd point, rubber-band to 1st point ) ;; end AND (progn ;; Evaluate the following expressions and return the result ;; of the last expression evaluated. PROGN is used as a ;; wrapper function so that we can pass multiple expressions ;; to the IF function as a single argument constituting the ;; 'then' parameter. (setq ;; Assign values to the following symbols: dis (distance pt1 pt2) ;; 3D distance ang (angle (list (car pt1) (cadr pt1)) (list (car pt2) (cadr pt2))) ;; Angle in XY 2dd (distance (list (car pt1) (cadr pt1)) (list (car pt2) (cadr pt2))) ;; 2D distance del (mapcar '- pt2 pt1) ;; Delta vector ) ;; end SETQ (princ ;; Print the following string to the command-line ;; To understand the difference between princ, prin1 & print ;; see http://bit.ly/1chjetS (strcat ;; Concatenate the following strings: "\nDistance = " (rtos dis 4 8) " (" (rtos dis 2 8) "), " "Angle in XY Plane = " (angtos ang 0 8) ", " "Angle from XY Plane = " (angtos (atan (caddr del) 2dd) 0 8) "\nDelta X = " (rtos (car del) 4 8) " (" (rtos (car del) 2 8) "), " "Delta Y = " (rtos (cadr del) 4 8) " (" (rtos (cadr del) 2 8) "), " "Delta Z = " (rtos (caddr del) 4 8) " (" (rtos (caddr del) 2 8) ")" ) ;; end STRCAT ) ;; end PRINC ) ;; end PROGN ;; If we had an 'else' parameter, we would insert it here. ) ;; end IF (princ) ;; Suppress the return of the last evaluated expression ) ;; end DEFUN
Please ask if you are unsure of any of the comments in the code.
Lee
@Lee_Mac wrote:
....
(princ
;; Print the following string to the command-line
;; To understand the difference between princ, prin1 & print
;; see http://bit.ly/1chjetS
....
Largely a matter of preference, but personally, I use the (prompt) function for this kind of thing. The (prin...) functions are built for the possibility of writing to an external file, which is overhead in this situation, and they not only print to the command line but also return the string in case you are writing it to a file. The latter of those outcomes serves no purpose here. Prompting the user is exactly what you're doing, so you may as well use the function that's intended expressly for that.