Please help me fix this code.

Please help me fix this code.

hungrd
Observer Observer
599 Views
5 Replies
Message 1 of 6

Please help me fix this code.

hungrd
Observer
Observer
Please help me fix this code. The target will move the text to the line position directly above it. as shown in the picture below error run code
Command: MOVETEXTTOLINE
Select objects: Specify opposite corner: 6 found
Select objects:  ; error: bad SSGET list
Command:
 
 
hungrd_1-1712492178720.png

 

 

 
(defun c:MoveTextToLine ()
  (setq ss (ssget '((0 . "TEXT,LINE,POLYLINE"))))
  (if ss
    (progn
      (setq i 0)
      (repeat (sslength ss)
        (setq ent (ssname ss i))
        (setq entdata (entget ent))
        (setq enttype (cdr (assoc 0 entdata)))
        (if (= enttype "TEXT")
          (progn
            (setq textpoint (cdr (assoc 10 entdata)))
            (setq ss2 (ssget "X" (list '(0 . "LINE,POLYLINE") `(cons 10 (cons (car textpoint) (cdr textpoint))))))
            (if ss2
              (progn
                (setq ent2 (ssname ss2 0))
                (setq entdata2 (entget ent2))
                (setq linepoint (cdr (assoc 10 entdata2)))
                (setq newpoint (list (car textpoint) (cadr linepoint)))
                (entmod (subst (cons 10 newpoint) (assoc 10 entdata) entdata))
              )
            )
          )
        )
        (setq i (1+ i))
      )
      (ssdel ss)
    )
  )
  (princ)
)
0 Likes
600 Views
5 Replies
Replies (5)
Message 2 of 6

paullimapa
Mentor
Mentor

That error refers to this line of code

(setq ss2 (ssget "X" (list '(0 . "LINE,POLYLINE") `(cons 10 (cons (car textpoint) (cdr textpoint))))))

read up on how to include cons structure

 https://entercad.ru/acad_alg.en/ws73099cc142f4875516d84be10ebc87a53f-7a31.htm


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

hungrd
Observer
Observer
I don't understand autolisp very well. My code is written by AI. Please help me correct it.
0 Likes
Message 4 of 6

paullimapa
Mentor
Mentor

Perhaps these examples that do similar steps would help:

1. https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/rotate-text-to-line-and-keep-in-posi...

2. https://www.cadtutor.net/forum/topic/560-rotate-text-to-align-line/

;;  Text Rotated to the selected object angle
;;  Modifications:
;;  1. Text is moved to near point selected on curve
;;  2. Text justification changed to Mid Center
;;  3. If Curve angle is = 180 or = pi then angle is set to 0
; https://www.cadtutor.net/forum/topic/560-rotate-text-to-align-line/#comment-4179
(defun c:TRA() (c:TextRotate2Angle))

(defun c:TextRotate2Angle (/ ss lst pt ang obj get_pt_and_angle)
  (vl-load-com)


  ;;  User selection of curve object
  ;;  return pick point & average angle of curve at pick point
  (defun get_pt_and_angle (prmpt / ent p@pt parA parB pt ang)
    (if (and (setq ent (entsel prmpt))
             (not (vl-catch-all-error-p
                    (setq pt (vl-catch-all-apply
                               'vlax-curve-getClosestPointTo
                               (list (car ent) (cadr ent))
                             )
                    )
                  )
             )
        )
      (progn
        (setq ent  (car ent)
              p@pt (vlax-curve-getParamAtPoint ent pt)
              parA (max 0.0 (- p@pt 0.05))
              parB (min (+ p@pt 0.05) (vlax-curve-getEndParam ent))
              ang (angle (vlax-curve-getPointAtParam ent parA)
                         (vlax-curve-getPointAtParam ent ParB)
                  )
        )
        (list pt ang)
      )
    )
  )

  
  ;;  Get Text to align & object to alignment angle
  ;;  Text is moved & rotated to the alignment angle
  ;;  Object must have curve data
  (prompt "\nSelect text object to align.")
  (if (and (or (setq ss  (ssget "_+.:E:S" '((0 . "Text,Mtext"))))
               (prompt "\n**  No Text object selected.  **"))
           (or (setq lst (get_pt_and_angle "\nSelect point on object to label."))
               (prompt "\n**  Missed or no curve data for object."))
       )
    (progn
      (setq pt  (car lst)
            ;; ang (FixTextAngle (cadr lst))
            ang (cadr lst)
            obj (vlax-ename->vla-object (ssname ss 0))
            pos (vla-get-alignment obj)
      )
 ; if line object angle is 180 degree then set angle to 0
      (if(= ang pi)(setq ang 0.0))
 ; match text rotation angle
      (vla-put-rotation Obj ang)
 ; change text justify to midcenter
      (vla-put-alignment obj 10)
 ; move txt object to match with point picked
      (vla-put-InsertionPoint Obj (vlax-3D-point pt)) 
      (vla-put-textalignmentpoint obj (vlax-3d-point pt))
    )
  )
  (princ)
)

 


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

Kent1Cooper
Consultant
Consultant

@hungrd wrote:
.... My code is written by AI. ....

AI simply doesn't know how to work with AutoLisp well enough yet -- we've seen numerous examples.

 

First off, that ` reverse apostrophe has no place in that position in AutoLisp code.  Even without that, I see at least one other odd-ball-ness about that (cons) function, BUT FIRST:

 

What is the intent for finding a Line/Polyline?  It looks like the code is meant to find a Line or Polyline which starts at the Text's left end of baseline [whether that's the insertion point or not depends on what the justification is].  Clearly in your image, that is not going to find one in the conditions on the left.  What is it supposed to be looking for to find the right Line/Polyline?

 

If the Text justification would ever be other than Left, even what I think it's trying to do would not work.  In other justifications, the 10 entry cannot be assigned, but is only a result of the insertion point at 11 in relation to the height, rotation, font and content.

Kent Cooper, AIA
0 Likes
Message 6 of 6

hungrd
Observer
Observer

thanks to @paullimapa  and @Kent1Cooper . 

I already have the code. I asked someone to do it for me. Compared to AI code, it is very different. By the way, anyone who needs it can use it.

(defun c:Mtt (/ lstline lsttxt)
  (princ "\nCHon cac doi tuong")
  (setq ss (ssget '((0 . "TEXT,LINE,LWPOLYLINE"))))
  (setq ssline (ssadd)
sstxt  (ssadd)
  )
  (setq i 0)
  ;;==== loc doi tuong
  (While (< i (sslength ss))
    (setq name (ssname ss i))
    (if (= (cdr (assoc 0 (entget name))) "TEXT")
      (setq sstxt (ssadd name sstxt))
      (setq ssline (ssadd name ssline))
    )
    (setq i (1+ i))
  )
  ;; dong while
  (setq i 0)
  (while (< i (sslength sstxt))
;;;===== them cac ssname  cua tap hop txt vao danh sach lsttxt
    (setq name_txt (ssname sstxt i)
  p    (cdr (assoc 10 (entget name_txt)))
  datatxt  (list name_txt p)
  lsttxt   (append lsttxt (list datatxt))
    )
;;;===== them cac ssname  cua tap hop line vao danh sach lstline
    (setq name_line (ssname ssline i)
  p1     (cdr (assoc 10 (entget name_line)))
  dataline  (list name_line p1)
  lstline   (append lstline (list dataline))
    )
    (setq i (1+ i))
  )
  ;;===== sap xep cac danh sach theo x.y 
  (setq lsttxt
(vl-sort lsttxt
  '(lambda (x y)
     (if (equal (cadr (cadr x)) (cadr (cadr y)) 1e-4)
       (< (car (cadr x)) (car (cadr y)))
       (> (cadr (cadr x)) (cadr (cadr y)))
     )
   )
)
  )
 
  (setq lstline
(vl-sort lstline
  '(lambda (x y)
     (if (equal (cadr (cadr x)) (cadr (cadr y)) 1e-4)
       (< (car (cadr x)) (car (cadr y)))
       (> (cadr (cadr x)) (cadr (cadr y)))
     )
   )
)
  )
  ;;======== ket thuc sap xep  
 
  (setq i 0)
  (While (< i (length lsttxt))
    (setq x (car (cadr (nth i lsttxt)))
  y (cadr (cadr (nth i lstline)))
    )
    (setq dt (car (nth i lsttxt)))
    (setq newp (list x y))
    (command ".move" dt "" (cadr (nth i lsttxt)) newp)
   
    (setq i (1+ i))
 
  )
)
0 Likes