Jump to content

Error when selecting LINE or LWPOLYLINE


Recommended Posts

Posted

Please tell me how to fix the errors in the code.
When selecting a LINE:
Not enough vertices in the polyline.; error: no function definition: nil
When selecting a LWPOLYLINE:
Not enough vertices in the polyline.; error: invalid function: T

(defun c:angdeg_ln_lwln ( / selEnt entData objType coords p1 p2 ang angDeg mid v offsetVector offsetDist insPoint txtHeight txtStr x y )
                   
  (setq selEnt (car (entsel "\nSelect LINE or LWPOLYLINE: ")))
  (cond
     ((null selEnt)
     (prompt "\nNo lines or polylines are selected..")
    )
    (t
      (setq entData (entget selEnt)
            objType (cdr (assoc 0 entData))
      )
      (cond
        ;; ********* LINE ************
        ((eq objType "LINE")
         (setq p1 (cdr (assoc 10 entData))) 
               p2 (cdr (assoc 11 entData))) 
        )

        ;; *********** LWPOLYLINE **********
        ((eq objType "LWPOLYLINE")
         (setq coords '())
         (foreach d entData
           (cond
             ((= (car d) 10)
              (setq x (cdr d)))
             ((= (car d) 20)
              (setq y (cdr d))
              (setq coords (append coords (list (list x y))))))
         )

         (if (>= (length coords) 2)
           (progn
             (setq p1 (nth 0 coords)
                   p2 (nth 1 coords)))
           (prompt "\nNot enough vertices in the polyline.")
         )
        )
        ;; ********** Other type of object *********
        (t
         (prompt (strcat "\nAn object has been selected" objType
                         ",but expected LINE or LWPOLYLINE."))
         (return)  
        )
      )
      ;; angle
      (if (and p1 p2)
        (progn
               (setq ang    (angle p1 p2)
                angDeg (* ang (/ 180.0 pi)) ; radians -> degrees
          )
          ;;The middle of the segment
          (setq mid (mapcar
                      '(lambda (a b) (/ (+ a b) 2.0))
                      p1 p2))

          ;;Vector  p1  p2
          (setq v (list (- (nth 0 p2) (nth 0 p1))
                        (- (nth 1 p2) (nth 1 p1))))

          ;; Perpendicular to this vector (x, y) => (y, -x)
          (setq offsetVector (list (nth 1 v) (- (nth 0 v))))

          ;; Offset 
          (setq offsetDist 0.5)

           (defun normalize (vec)
            (let ((len (distance '(0 0) vec)))
              (if (> len 1e-9)
                (list (/ (car vec) len)
                      (/ (cadr vec) len))
                vec 
              )
            )
          )
          (setq offsetVector (normalize offsetVector))
          (setq offsetVector (mapcar '(lambda (x) (* x offsetDist)) offsetVector))
          ;; The end point of text insertion
          (setq insPoint (mapcar '+ mid offsetVector))

          (setq txtStr (rtos angDeg 2 2))  
          (setq txtHeight 2.5)        

               (command "_.TEXT" "_M" insPoint txtHeight 0.0 txtStr)
          (prompt (strcat "\nTilt angle: " txtStr "°"))
        )
      )
    )
  (princ)
)

 

Posted (edited)

Hi Nikon
The problem is that 'let' not is a function defined by the language or by you. You should either redefine the 'normalize' function so that it does not use this function or define it yourself.
The easiest is the first one

Edited by GLAVCVS
  • Thanks 1
Posted
(defun normalize (vec / len)
  (if (> (setq len (distance '(0 0) vec)) 1e-9)
    (list (/ (car vec) len)
          (/ (cadr vec) len))
    vec 
  )
)

 

  • Thanks 1
Posted

Also, the conditional clause 'cond' '(eq objType "LWPOLYLINE")' works with "POLYLINES" but not with "LWPOLYLINES". So your code will fail there.

  • Thanks 1
Posted (edited)

I think this is what you want.

 

There were a bunch of things wrong

 

;; a function that reads the coordinates of the vertices of a selected POLYLINE / LWPOLYLINE

(defun getPolylineVertexes ( pline / lst i res)				   
  (setq lst (vlax-get (vlax-ename->vla-object pline) 'coordinates))
  (setq i 0)
  (setq res (list))
  (repeat (/ (length lst) 2)
    (setq res (append res (list
	  (list (nth i lst) (nth (+ i 1) lst) )
	)))
    (setq i (+ i 2))
  )
  res
)


(defun c:angdeg_ln_lwln ( / selEnt entData objType coords p1 p2 ang angDeg mid v offsetVector offsetDist insPoint txtHeight txtStr x y )
                   
  (setq selEnt (car (entsel "\nSelect LINE or LWPOLYLINE: ")))
  (cond
	((null selEnt)
		(prompt "\nNo lines or polylines are selected..")
	)
    (t
		(setq entData (entget selEnt)
			objType (cdr (assoc 0 entData))
		)
		
		(cond		;; switch on the type of selected object
			;; ********* LINE ************
			(	(eq objType "LINE")
				(setq p1 (cdr (assoc 10 entData)) 
					p2 (cdr (assoc 11 entData)))
			) 

			;; *********** LWPOLYLINE **********
			((eq objType "LWPOLYLINE")
			 (setq coords (getPolylineVertexes selEnt))
			 
			 ;;(princ coords)

			 (if (>= (length coords) 2)
			   (progn
				 (setq p1 (nth 0 coords)
					   p2 (nth 1 coords)))
			   (prompt "\nNot enough vertices in the polyline.")
			 )
			 
			 	;;(princ p1)
				;;(princ p2)

			)
		
			;; ********** Other type of object *********
			(t
				(prompt (strcat "\nAn object has been selected" objType ",but expected LINE or LWPOLYLINE."))
			 (return)  
			)
		)
		
      ;; angle
      (if (and p1 p2)
        (progn
               (setq ang    (angle p1 p2)
                angDeg (* ang (/ 180.0 pi)) ; radians -> degrees
          )
          ;;The middle of the segment
          (setq mid (mapcar
                      '(lambda (a b) (/ (+ a b) 2.0))
                      p1 p2))

          ;;Vector  p1  p2
          (setq v (list (- (nth 0 p2) (nth 0 p1))
                        (- (nth 1 p2) (nth 1 p1))))

          ;; Perpendicular to this vector (x, y) => (y, -x)
          (setq offsetVector (list (nth 1 v) (- (nth 0 v))))

          ;; Offset 
          (setq offsetDist 0.5)
           (defun normalize (vec)
            (setq len (distance '(0 0) vec))
              (if (> len 1e-9)
                (list (/ (car vec) len)
                      (/ (cadr vec) len))
                vec 
              )
          )
		  
          (setq offsetVector (normalize offsetVector))
          (setq offsetVector (mapcar '(lambda (x) (* x offsetDist)) offsetVector))
          ;; The end point of text insertion
          (setq insPoint (mapcar '+ mid offsetVector))

          (setq txtStr (rtos angDeg 2 2))  
          (setq txtHeight 2.5)        

               (command "_.TEXT" "_M" insPoint txtHeight 0.0 txtStr)
          (prompt (strcat "\nTilt angle: " txtStr "°"))
        )
      )
    )
  )
  (princ)
)

 

Edited by Emmanuel Delay
  • Like 1
Posted
3 hours ago, Emmanuel Delay said:

There were a bunch of things wrong

Yes, it's a bad option...Thanks for the improvements.
It's better to do it in a different way and arrange the text along the line.

;; shift the text from the center of the line by 1.5 mm, the text is located along the line
(defun c:Angle_ln_lwln (/ ent ent-data pt1 pt2 pt-list dx dy angle angle-deg text-pt offset)
  (setq ent (entsel "\nSelect a line or polyline: ")) 
  (if ent
    (progn
      (setq ent-data (entget (car ent)))
      (cond
        ((= (cdr (assoc 0 ent-data)) "LINE")
         (setq pt1 (cdr (assoc 10 ent-data)))
         (setq pt2 (cdr (assoc 11 ent-data))))
        ((= (cdr (assoc 0 ent-data)) "LWPOLYLINE")
         (setq pt-list (mapcar 'cdr (vl-remove-if-not '(lambda (x) (member (car x) '(10 11))) ent-data)))
         (setq pt1 (car pt-list))
         (setq pt2 (cadr pt-list)))
        (t
         (prompt "\nThe selected object is not a line or a single-segment polyline.")
         (exit))
        )
      (setq dx (- (car pt2) (car pt1))) 
      (setq dy (- (cadr pt2) (cadr pt1))) 
      (setq angle (atan dy dx)) 
      (setq angle-deg (* (/ angle pi) 180.0)) 

           (setq text-pt (mapcar '(lambda (a b) (/ (+ a b) 2.0)) pt1 pt2))
      
            (setq offset (/ 1.5 (sqrt (+ (* dx dx) (* dy dy)))))  
      (setq text-pt (list
                     (+ (car text-pt) (* (- dy) offset))  
                     (+ (cadr text-pt) (* dx offset))    
                     0.0)) 

      (setq text-height 2.5) 
            (command "_.TEXT" "_J" "_MC" text-pt text-height (* 180 (/ angle pi)) (strcat (rtos angle-deg 2 2) "°")) 
      (princ (strcat "\nAngle: " (rtos angle-deg 2 2) "°"))
      )
    (prompt "\nThe object is not selected or the selection is canceled.")
    )
  (princ)
)


 

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