bad argument type: consp #<variant 8197 ...>

bad argument type: consp #<variant 8197 ...>

longorialoera
Community Visitor Community Visitor
632 Views
4 Replies
Message 1 of 5

bad argument type: consp #<variant 8197 ...>

longorialoera
Community Visitor
Community Visitor

some one can help me with this code pls, im rlly new in this 

(defun c:AUTEXT ()
(setq spaces '())
(setq text-to-insert "100")

(setq acadObj (vlax-get-acad-object))
(setq doc (vla-get-ActiveDocument acadObj))

(vlax-for obj (vla-get-ModelSpace doc)
(if (= "AcDbPolyline" (vla-get-objectname obj))
(setq spaces (cons obj spaces))
)
)

(foreach space spaces
(setq points (vlax-get-property space 'Coordinates))
(setq centroid (mapcar '+ (car points) (last points)))
(setq text-entity (vla-addText (vla-get-ModelSpace doc) "AUTEXT" (vlax-3D-point centroid) 1.0))
)

(princ)
)

0 Likes
633 Views
4 Replies
Replies (4)
Message 2 of 5

Kent1Cooper
Consultant
Consultant

My guess, without trying it:

 

The Coordinates property of a Polyline is an undifferentiated list of numbers, not a list of vertex locations -- it's just:

(x1 y1 x2 y2 x3 y3), not ((x1 y1) (x2 y2) (x3 y3))

So your (mapcar '+  is trying to add just the X coordinate of the first vertex to the Y coordinate of the last one, as if they were lists rather than just numbers.  [Even if it was adding vertex lists, that won't give you anything like the centroid of the Polyline.  You would want to do something like add all the vertices, and divide the coordinates of the result by the number of vertices.  You can find numerous routines around here that will find the centroid by similar methods, or the middle of the bounding box, or maybe other approaches.]

Kent Cooper, AIA
0 Likes
Message 3 of 5

paullimapa
Mentor
Mentor

if you're looking for a method to find the center point of a pline to insert your TEXT "100", try this code:

  ;; function to get geometric center of closed pline
  ;; For Example:
  ;; (_getgeoc en) 
  ;; Where en is a Pline entity
  ;; Returns cooridinate point location
  (defun _getgeoc (en / allx ally ctr el vertex x y)
   (setq el (entget en) ; get entity data
         allx 0
         ally 0
         ctr  0   
   )
   (while (assoc 10 el) ; cycle through vertices
    (setq	vertex (cdr (assoc 10 el)) 
          ctr    (+ ctr 1)           ; keep count of # of vertices 
          x      (car vertex)        ; get x coordinate
          y      (cadr vertex)       ; get y coordinate
          allx   (+ allx x)          ; add up x coordinates
          ally   (+ ally y)          ; add up y coordinates
          EL     (cdr (member (assoc 10 el) el)) ; get next vertex
    )
   )
   (setq x (/ allx ctr) ; divide sum of x with # of x to get average
         y (/ ally ctr) ; divide sum of y with # of y to get average
   )
   ; return coordinate
   (list x y) 
  )

since the argument "en" to feed to this function "_getgeoc" is an entity you'll need to convert the pline vl objects to entity by using this function: (vlax-vla-object->ename 

see your revised code:

; autext places text in center of plines
; https://forums.autodesk.com/t5/forums/replypage/board-id/130/message-id/452863
(defun c:AUTEXT ; ()
  (/ _getgeoc acadObj centroid doc points spaces text-to-insert) ; localize functions & variables
  ;; function to get geometric center of closed pline
  ;; For Example:
  ;; (_getgeoc en) 
  ;; Where en is a Pline entity
  ;; Returns cooridinate point location
  (defun _getgeoc (en / allx ally ctr el vertex x y)
   (setq el (entget en) ; get entity data
         allx 0
         ally 0
         ctr  0   
   )
   (while (assoc 10 el) ; cycle through vertices
    (setq	vertex (cdr (assoc 10 el)) 
          ctr    (+ ctr 1)           ; keep count of # of vertices 
          x      (car vertex)        ; get x coordinate
          y      (cadr vertex)       ; get y coordinate
          allx   (+ allx x)          ; add up x coordinates
          ally   (+ ally y)          ; add up y coordinates
          EL     (cdr (member (assoc 10 el) el)) ; get next vertex
    )
   )
   (setq x (/ allx ctr) ; divide sum of x with # of x to get average
         y (/ ally ctr) ; divide sum of y with # of y to get average
   )
   ; return coordinate
   (list x y) 
  ) 
 (setq spaces '())
 (setq text-to-insert "100")

 (setq acadObj (vlax-get-acad-object))
 (setq doc (vla-get-ActiveDocument acadObj))

 (vlax-for obj (vla-get-ModelSpace doc)
  (if (= "AcDbPolyline" (vla-get-objectname obj))
   (setq spaces (cons obj spaces))
  )
 )

 (foreach space spaces
;  (setq points (vlax-get-property space 'Coordinates))
;  (setq centroid (mapcar '+ (car points) (last points)))
  (setq centroid (_getgeoc (vlax-vla-object->ename space))) ; convert vl obj to entity to get center point
  (setq text-entity (vla-addText (vla-get-ModelSpace doc) "AUTEXT" (vlax-3D-point centroid) 1.0))
 )
(princ)
)

  


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 4 of 5

Sea-Haven
Mentor
Mentor

The average and centroid are 2 different values, for a simple rectang they are the same, for multi segment plines may be different. thanks to kent Cooper for this

(setq pt (osnap (vlax-curve-getStartPoint obj) "gcen")

I found it in label multi pline areas.lsp, many examples out there.

0 Likes
Message 5 of 5

paullimapa
Mentor
Mentor

Yes, I like @Kent1Cooper too. But I find that at times with very complicated polygonal shapes that version ends up with a point outside of the polygon shape whereas the average tends to always fall inside the polygon.


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes