Copy text then edit that text

Copy text then edit that text

Anonymous
Not applicable
3,209 Views
15 Replies
Message 1 of 16

Copy text then edit that text

Anonymous
Not applicable

Hi all!

i'm super new to autolisp. I take parts here and there on the web and edit it for my purposes.

I want to copy a text, snap to insertion point automatically then click where I want it to go, then enter edit mode.

Here's what I came with:

 

(defun c:ct3 (/ IncVal TextObj NewPos TextVal NewTextObj)
(vl-load-com)
  (setq TextObj
  (vlax-ename->vla-object (car (entsel "\nSelect Mtext Object :")))
  )
  (while
    (setq NewPos (getpoint "\nSelect new position : "))
     (setq TextVal (atoi (vla-get-textstring TextObj)))
     (setq NewTextObj (vla-copy TextObj))
     (vla-move NewTextObj
        (vla-get-InsertionPoint NewTextObj)
        (vlax-3D-Point NewPos)
     )
     (vla-put-textstring NewTextObj (command "_TEXTEDIT" (entlast) ""))
     (setq TextObj NewTextObj)
  )
  (princ)
)

 

It works, but it gives me an error about ActiveX, and a non optional parameter.

 

Also, it would be nice that when I edit the text and press enter, the funtion would

start again so that I can copy/edit another text.

 

Thanks to anyone who can help!

0 Likes
Accepted solutions (2)
3,210 Views
15 Replies
Replies (15)
Message 2 of 16

DannyNL
Advisor
Advisor
Accepted solution

You cannot give a (COMMAND.... as argument to vla-put-TextString. That is causing the error.

 

But in this case you wouldn't need to use Visual LISP at all but just plain AutoLISP will do:

 

(defun c:ct3 (/ Selection BasePoint NewPos Entity)
   (if
      (setq Selection (ssget "_:S+." '((0 . "*TEXT"))))
      (progn
         (setq BasePoint (cdr (assoc 10 (entget (setq Entity (ssname Selection 0))))))
         (while            
            (setq NewPos (getpoint BasePoint  "\nSelect new position : "))
            (command "_.COPY" Entity "" BasePoint NewPos)
            (command "_.TEXTEDIT" (entlast) "")
         )
      )
   )
   (princ)
)

 

0 Likes
Message 3 of 16

Anonymous
Not applicable

Thanks for the very fast answer!

0 Likes
Message 4 of 16

Anonymous
Not applicable

why does it say unknown command "ct3" each time I copy/edit the text?

0 Likes
Message 5 of 16

ВeekeeCZ
Consultant
Consultant
Accepted solution

Little modified @DannyNL's version to see the preview...

 

(and added the UCS support and removed last "" causing an error) 

 

 

(defun c:ct3 (/ Selection BasePoint NewPos Entity)
   (if
      (setq Selection (ssget "_:S+." '((0 . "*TEXT"))))
      (progn
        (setq BasePoint (trans (cdr (assoc 10 (entget (setq Entity (ssname Selection 0))))) 0 1))
        (command "_.COPY" Entity "" "_non" BasePoint PAUSE)
        (command "_.TEXTEDIT" (entlast))))
   (princ)
)
Message 6 of 16

DannyNL
Advisor
Advisor

I don't know.

At first I thought it was the additional "" after (entlast) just as @ВeekeeCZ said, but when I remove it the code fails on my side in AutoCAD 2017. So on my side the additional "" after (entlast) just like in your original code seems to work.

 

I'll try to look into it tomorrow out of curiosity, as my working day is getting to an end at the moment Smiley Happy

 

0 Likes
Message 7 of 16

Anonymous
Not applicable

Thanks BeekeeCZ it works. I just added a while loop so the code conitnues to copy edit!

0 Likes
Message 8 of 16

ВeekeeCZ
Consultant
Consultant

@DannyNL wrote:

I don't know.

At first I thought it was the additional "" after (entlast) just as @ВeekeeCZ said, but when I remove it the code fails on my side in AutoCAD 2017. So on my side the additional "" after (entlast) just like in your original code seems to work.

 

I'll try to look into it tomorrow out of curiosity, as my working day is getting to an end at the moment Smiley Happy

 


Sorry, I tested it in Autocad 2016.

 

But the behavior of the TEXTEDIT changed in 2017 and now it depends on the TEXTEDITMODE sysvar.

Message 9 of 16

Anonymous
Not applicable

I have autocad 2015 so everything works perfectly 🙂

0 Likes
Message 10 of 16

Kent1Cooper
Consultant
Consultant

If you can count on not missing, and not picking something other than Text/Mtext, it can be reduced to a single (command) function:

 

(command

  ".copy" (car (setq test (entsel))) "" (osnap (cadr test) "ins") pause

  ".textedit" "_last"

)

 

That also gets around the quirky situation with (assoc 10) values for Text, which could lead you astray.  For Left-justified Text [and Fit and Aligned, if you ever use those], that's the insertion point, but for other justifications, the insertion point is (assoc 11), and (assoc 10) is the left end of its base-line.  The above uses Osnap INSertion, which will find the "right" one in all cases, without the need to account for the justification to decide which entity data entry to look at.  It also spares you any need to account for the possibility of a different UCS.

Kent Cooper, AIA
Message 11 of 16

Kent1Cooper
Consultant
Consultant

@DannyNL and BeekeeCZ wrote:

 

... (cdr (assoc 10 (entget ....

A potential problem -- see the after-code-suggestion comment in Post 10.

Kent Cooper, AIA
0 Likes
Message 12 of 16

DannyNL
Advisor
Advisor

Yes, the code could be compressed to shorten it or extended to do check on additional properties and/or more error checking as well.

But considering the original code (also using the InsertionPoint instead of the TextAlignmentpoint) and this new 'quick 'n dirty' solution, it does the job and works as expected.

0 Likes
Message 13 of 16

Anonymous
Not applicable

(defun c:df1 (/ Selection BasePoint NewPos Entity Last BasePoint2)
   (if
      (setq Selection (ssget "_:S+." '((0 . "*TEXT"))))
      (progn
        (setq BasePoint (trans (cdr (assoc 10 (entget (setq Entity (ssname Selection 0))))) 0 1))
        (command "_.COPY" Entity "" "_non" BasePoint PAUSE)
        (command "_.TEXTEDIT" (entlast))))
 (setq Last (entlast))
   (c:TA)
   (command "_.move" Last "" (osnap (cadr Last) "_ins") PAUSE)  <--------
   (princ)
)

 

my command move doesnt work here... It gets Last but doesn't get the basepoint.

It asks me to pick a point as basepoint.

TA gets orientation of a line and apply it to a text. Then I want to move that text with insertion point.

I'm stuck and I really tried everything...

0 Likes
Message 14 of 16

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

....
        (command "_.COPY" Entity "" "_non" BasePoint PAUSE)
        (command "_.TEXTEDIT" (entlast))))
 (setq Last (entlast))
   (c:TA)
   (command "_.move" Last "" (osnap (cadr Last) "_ins") PAUSE)  <--------
....

 

my command move doesnt work here... It gets Last but doesn't get the basepoint.

....


The 'Last' variable contains an entity name, but you're trying to get a point  from it, using (cadr Last) as if what 'Last' contained was what you get from (entsel)  [i.e. a list, containing an entity name and the point at which it was picked].

 

Since the last designated point [unless the TA command uses some (command) function that uses selected points] was the end of the Copy command above, presumably that's still where you want to Move from, and you should probably be able to do this instead:

 

   (command "_.move" Last "" "@" PAUSE)

Kent Cooper, AIA
0 Likes
Message 15 of 16

Anonymous
Not applicable

This is great!! thanks so much! How come the @ snap to insertion?

0 Likes
Message 16 of 16

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

@Anonymous is great!! thanks so much! How come the @ snap to insertion?


It doesn't -- it's a "relative displacement" with no numerical values for relative coordinates or distance and angle, i.e. it re-uses the last designated point, which in this case happens to be  the insertion point, because of the way the last command operated.  You could do the same with this equivalent:

 

   (command "_.move" Last "" (getvar 'lastpoint) PAUSE)

Kent Cooper, AIA
0 Likes