Lisp to Copy Mtext but paste only the first line

Lisp to Copy Mtext but paste only the first line

tiwari1211
Enthusiast Enthusiast
3,747 Views
50 Replies
Message 1 of 51

Lisp to Copy Mtext but paste only the first line

tiwari1211
Enthusiast
Enthusiast

Dear Lisp experts.  I hope you all are doing well. 

 

Do we have any Lisp to copy only the first line of MTEXT. I have many drawings where i need to copy only the first line to other position. presently i use copy command and then manually delete the remaining bottom lines. I dont want to explode the MTEXT. 

 

Can anyone help me in this. Thank you very much 

0 Likes
Replies (50)
Message 21 of 51

tiwari1211
Enthusiast
Enthusiast
Hi pbejse,

Thanks a lot, your solution also works. Can you also help and add option for rotation of MTEXT. So, when i copy the Mtext it ask for rotation angle. by default 0 deg. Thank you very much.

0 Likes
Message 22 of 51

Kent1Cooper
Consultant
Consultant
Accepted solution

@tiwari1211 wrote:
.... will it be possible for you to add option for text rotation too in your code ?
So, when i copy the Mtext it ask for rotation angle. by default 0 deg. ....

Try this [minimally tested]:

 

;| CopyMTextLine1-ht10.lsp [command name: CMTL1]
To Copy Mtext only up to the first Enter [if any] encountered, with
  height of 10 units regardless of current height, and at User-specified
  rotation [default 0].
Creates new Mtext with [if containing Enter(s)] truncated content,
  on same Layer, with same justification, etc., and leaves User in
  Move command to specify new location.
If Mtext contains no Enter(s), simply Copies it [with new height if
  not originally 10 units].
NOTE:  Width of defining Mtext box is NOT changed, so word
  wrapping can occur in result if height is increased, or if original had
  new lines from word wrapping before first Enter, word wrapping
  can change.
Kent Cooper, 15 March 2021
|;
(defun C:CMTL1 ; = Copy MText Line 1 only
  (/ mt mtdata txt hard soft newtxt)
  (if
    (and
      (setq mt (car (entsel "\nMText object to Copy 1st line of: ")))
      (member '(0 . "MTEXT") (setq mtdata (entget mt)))
    ); and
    (progn ; then
      (initget 6); no zero, no negative
      (if (wcmatch (setq txt (cdr (assoc 1 mtdata))) "*\\P*,*\n*"); contains Enter(s)
        (setq ; then
          hard (vl-string-search "\\P" txt) ; first "hard" Enter if any
          soft (vl-string-search "\n" txt) ; first "soft" Enter [Shift+Enter] if any
          newtxt (substr txt 1 (apply 'min (vl-remove nil (list hard soft))))
            ; from beginning to first type of Enter encountered
        ); setq
        (setq newtxt txt); no Enter(s) -- keep
      ); if
      (initget 4); no negative
      (setq rot
        (cond
          ((getangle "\nText rotation <0>: ")); nil on Enter
          (0.0); User Enter
        ); cond
      ); setq
      (entmake
        (append
          (subst (cons 1 newtxt) (assoc 1 mtdata) mtdata); truncated text
          '((8 . "0") (40 . 10.0)); Layer 0 & fixed height
          (list (cons 50 rot))
        ); append
      ); entmake
      (command "_.move" (entlast) "" "_none" (cdr (assoc 10 mtdata)))
    ); progn
  ); if
); defun

 

I suppose it could be altered to offer the current rotation as an option, or to save your latest rotation choice as the default, instead of always 0.

Kent Cooper, AIA
0 Likes
Message 23 of 51

tiwari1211
Enthusiast
Enthusiast

@Kent1Cooper 

 

Hi Kent,  I hope you are doing fine..

 

Your above code copy the 1st line of Mtext. 

but In my case i am saving solidworks drawings to Autocad. In solidworks if the text is single line then it save to Mtext in Acad and Multiple line text to Block. Because of this problem i am unable to use above code as thought in Acad.  I dont know if there is any way to save the solidworks multiple line text to MTEXT in autocad. Hence i am planning to change my text in solidworks to Single line eg. 9384-001.1b/1.4404/316L-1x 

 

Now my new requirement is to copy only bold text and not -1x

 

Can you please help me in updating your code to copy only the blue text from Mtext and ignore the red part. 

also is it possible to have the new text layer in yellow color ?

Rest everything in your previous code (rotation, new layer, text size etc) should remain the same.

 

Will it be possible for you to please help. Thank you very much!

I have also attached the lisp for your reference. 

 

Greetings. 

0 Likes
Message 24 of 51

Kent1Cooper
Consultant
Consultant

@tiwari1211 wrote:

... i am saving solidworks drawings to Autocad. In solidworks if the text is single line then it save to Mtext in Acad and Multiple line text to Block. ....  I dont know if there is any way to save the solidworks multiple line text to MTEXT in autocad. Hence i am planning to change my text in solidworks to Single line eg. 9384-001.1b/1.4404/316L-1x 

 

Now my new requirement is to copy only bold text and not -1x

.... 


What happens if you EXPLODE the Block that Solidworks exports from Multi-line Text?  Does it result in Mtext or in multiple plain Text objects?

 

What are the criteria for differentiating what you want to copy and what you want omitted?  Copy everything until the last hyphen found, and leave out the rest?  Copy everything except the last three characters?  Copy everything except characters connected with an 'x'?  If hyphen-based, since there are more than one hyphen in your example, might the cut-off ever need to be at one other than the last one?  Etc.

Kent Cooper, AIA
0 Likes
Message 25 of 51

tiwari1211
Enthusiast
Enthusiast

Hi Kent,  Thanks for your reply. 

 

- if I explode the block it result in multiple plain text objects. 

 

the text (9384-001.1b/1.4404/316L-1x) is explained as below

 

9384-001.1b/1.4404/316L = Part no./material/material

-1x = required qty.

 

I want to copy everything except the qty.  i.e. Copy everything until the last hyphen found, and leave out the rest.

Also i require the copied text in Yellow color. 

 

 

Thank you 🙂

0 Likes
Message 26 of 51

tiwari1211
Enthusiast
Enthusiast

@Kent1Cooper 

 

Hi Kent, Did you had chance to look at my request. Can you please help. Thanks 

0 Likes
Message 27 of 51

Kent1Cooper
Consultant
Consultant

Try this [untested]:

(defun C:CMTNQ; = Copy MText with No Quantity suffix
  (/ mt mtdata txt newtxt rot)
  (command "_.layer" "_make" "YourLayerName" "_color" 2 "" "")
  (if
    (and
      (setq mt (car (entsel "\nMText object to Copy with No Quantity suffix: ")))
      (member '(0 . "MTEXT") (setq mtdata (entget mt)))
    ); and
    (progn ; then
      (if (wcmatch (setq txt (cdr (assoc 1 mtdata))) "*-*"); contains hyphen(s)
        (setq newtxt (substr txt 1 (vl-string-position 44 txt) T))
            ; from beginning to last hyphen encountered
        (setq newtxt txt); else [no Enter(s)] -- keep
      ); if
      (initget 4); no negative
      (setq rot
        (cond
          ((getangle "\nText rotation <0>: ")); nil on Enter
          (0.0); User Enter
        ); cond
      ); setq
      (entmake
        (append
          (subst (cons 1 newtxt) (assoc 1 mtdata) mtdata); truncated text
          '((8 . "YourLayerName") (40 . 10.0)); Layer 0 & fixed height
          (list (cons 50 rot))
        ); append
      ); entmake
      (command "_.move" (entlast) "" "_none" (cdr (assoc 10 mtdata)))
    ); progn
  ); if
); defun
Kent Cooper, AIA
0 Likes
Message 28 of 51

tiwari1211
Enthusiast
Enthusiast

@Kent1Cooper 

 

Hi Kent, 

 

I tried below code but it is giving error as below in Autocad and stop- 

Can you please have a look and suggest. Thank you very much!!

 

 

Command: CMTNQ
_.layer
Current layer: "0"
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: _make
Enter name for new layer (becomes the current layer) <0>: Lasermarkierung Enter
an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: _color
New color [Truecolor/COlorbook] : 2
Enter name list of layer(s) for color 2 (yellow) <Lasermarkierung>: Enter an
option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]:
Command:
MText object to Copy with No Quantity suffix: ; error: too many arguments

 

Command:

0 Likes
Message 29 of 51

Kent1Cooper
Consultant
Consultant

@tiwari1211 wrote:

... it is giving error as below in Autocad and stop- 

Can you please have a look and suggest. Thank you very much!!

....

MText object to Copy with No Quantity suffix: ; error: too many arguments

....


It looks like I had a parenthesis in the wrong place.  Try changing this line:

(setq newtxt (substr txt 1 (vl-string-position 44 txt) T))

to this:

(setq newtxt (substr txt 1 (vl-string-position 44 txt T)))

 

Kent Cooper, AIA
0 Likes
Message 30 of 51

tiwari1211
Enthusiast
Enthusiast

Hi @Kent1Cooper 

 

Thanks for your reply but still I get the error. Please see below -

 

Command: CMTNQ
_.layer
Current layer: "0"
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: _make
Enter name for new layer (becomes the current layer) <0>: Lasermarkierung Enter
an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: _color
New color [Truecolor/COlorbook] : 2
Enter name list of layer(s) for color 2 (yellow) <Lasermarkierung>: Enter an
option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]:
Command:
MText object to Copy with No Quantity suffix: ; error: bad argument type:
fixnump: T

0 Likes
Message 31 of 51

tiwari1211
Enthusiast
Enthusiast
Hi @Kent1Cooper 



Thanks for your reply but still I get the error. Please see below -



Command: CMTNQ
_.layer
Current layer: "0"
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: _make
Enter name for new layer (becomes the current layer) <0>: Lasermarkierung Enter
an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: _color
New color [Truecolor/COlorbook] : 2
Enter name list of layer(s) for color 2 (yellow) <Lasermarkierung>: Enter an
option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]:
Command:
MText object to Copy with No Quantity suffix: ; error: bad argument type:
fixnump: T
0 Likes
Message 32 of 51

Kent1Cooper
Consultant
Consultant

One more try, same line:

(setq newtxt (substr txt 1 (vl-string-position 44 txt 1 T)))
Kent Cooper, AIA
0 Likes
Message 33 of 51

tiwari1211
Enthusiast
Enthusiast

Hi @Kent1Cooper 

 

Thanks i tried again..  Now it copy the complete Mtext and not only till the *-* hyphen. 

also the color changed to ByBlock (White) and in not in Bylayer (yellow). 

 

Regards

Tiwari1211

0 Likes
Message 34 of 51

Kent1Cooper
Consultant
Consultant
Accepted solution

@tiwari1211 wrote:

....  Now it copy the complete Mtext and not only till the *-* hyphen. ....


Sorry about that....  The where-to-stop code came from something that was looking for commas, and I forgot to change the ASCII index number.  Change the 44 to 45.

Kent Cooper, AIA
0 Likes
Message 35 of 51

Kent1Cooper
Consultant
Consultant
Accepted solution

@tiwari1211 wrote:

.... the color changed to ByBlock (White) and in not in Bylayer (yellow). ....


There wasn't anything about color before, so I expect it wasn't "changed" but came that way.  But it's easily fixed -- add the '((62 . 256)) line for "ByLayer" color:

      (entmake
        (append
          (subst (cons 1 newtxt) (assoc 1 mtdata) mtdata); truncated text
          '((8 . "YourLayerName") (40 . 10.0)); Layer 0 & fixed height
          (list (cons 50 rot))
          '((62 . 256))
        ); append
      ); entmake

 

Kent Cooper, AIA
0 Likes
Message 36 of 51

tiwari1211
Enthusiast
Enthusiast
Accepted solution

Hi @Kent1Cooper 

 

Thank you very much for your support. now the code works perfectly as required.

I am coping the complete code below so in case someone else need can follow.  

 

(defun C:CMTNQ; = Copy MText with No Quantity suffix
(/ mt mtdata txt newtxt rot)
(command "_.layer" "_make" "Lasermarkierung" "_color" 2 "" "")
(if
(and
(setq mt (car (entsel "\nMText object to Copy with No Quantity suffix: ")))
(member '(0 . "MTEXT") (setq mtdata (entget mt)))
); and
(progn ; then
(if (wcmatch (setq txt (cdr (assoc 1 mtdata))) "*-*"); contains hyphen(s)
(setq newtxt (substr txt 1 (vl-string-position 45 txt 1 T)))
; from beginning to last hyphen encountered
(setq newtxt txt); else [no Enter(s)] -- keep
); if
(initget 4); no negative
(setq rot
(cond
((getangle "\nText rotation <0>: ")); nil on Enter
(0.0); User Enter
); cond
); setq
(entmake
(append
(subst (cons 1 newtxt) (assoc 1 mtdata) mtdata); truncated text
'((8 . "Lasermarkierung") (40 . 10.0)); Layer 0 & fixed height
(list (cons 50 rot))
'((62 . 256))
); append
); entmake
(command "_.move" (entlast) "" "_none" (cdr (assoc 10 mtdata)))
); progn
); if
); defun

 

Thanks once again!!

May God shine sunshine and happiness on you. May good things find there way to you 🙂

0 Likes
Message 37 of 51

tiwari1211
Enthusiast
Enthusiast

Hi @Kent1Cooper , 

 

I hope you are doing well & once again thanks a lot for all your help you did in past. 

Your lisp file is so far working fine for me but I am looking for one improvement in the same.  

Presently from text (9384-001.1b/1.4404/316L-1x) the lisp copies the text in blue and ignore the red one. 

 

Now i want to reduce the text shown in blue to (9384-001.1b/1.4404/316L-1x) while copying. so from Text (9384-001.1b/1.4404/316L-1x)  only (9384-001.1b) should be copied. 

 

Can you help me in making it possible ?

 

Thanks again. below is the Lisp i am using. 

 

(defun C:TT; = Copy MText with No Quantity suffix
(/ mt mtdata txt newtxt rot)
(command "_.layer" "_make" "Lasermarkierung" "_color" 2 "" "")
(if
(and
(setq mt (car (entsel "\nMText object to Copy with No Quantity suffix: ")))
(member '(0 . "MTEXT") (setq mtdata (entget mt)))
); and
(progn ; then
(if (wcmatch (setq txt (cdr (assoc 1 mtdata))) "*-*"); contains hyphen(s)
(setq newtxt (substr txt 1 (vl-string-position 45 txt 1 T)))
; from beginning to last hyphen encountered
(setq newtxt txt); else [no Enter(s)] -- keep
); if
(initget 4); no negative
(setq rot
(cond
((getangle "\nText rotation <0>: ")); nil on Enter
(0.0); User Enter
); cond
); setq
(entmake
(append
(subst (cons 1 newtxt) (assoc 1 mtdata) mtdata); truncated text
'((8 . "Lasermarkierung") (40 . 10.0)); Layer 0 & fixed height
(list (cons 50 rot))
'((62 . 256))
); append
); entmake
(command "_.move" (entlast) "" "_none" (cdr (assoc 10 mtdata)))
); progn
); if
); defun

 

 

0 Likes
Message 38 of 51

tiwari1211
Enthusiast
Enthusiast

Hi @Kent1Cooper , 

 

I hope you are doing well & once again thanks a lot for all your help you did in past. 

Your lisp file is so far working fine for me but I am looking for one improvement in the same.  

Presently from text (9384-001.1b/1.4404/316L-1x) the lisp copies the text in blue and ignore the red one. 

 

Now i want to reduce the text shown in blue to (9384-001.1b/1.4404/316L-1x) while copying. so from Text (9384-001.1b/1.4404/316L-1x)  only (9384-001.1b) should be copied. 

 

Can you help me in making it possible ?

 

Thanks again. below is the Lisp i am using. 

 

(defun C:TT; = Copy MText with No Quantity suffix
(/ mt mtdata txt newtxt rot)
(command "_.layer" "_make" "Lasermarkierung" "_color" 2 "" "")
(if
(and
(setq mt (car (entsel "\nMText object to Copy with No Quantity suffix: ")))
(member '(0 . "MTEXT") (setq mtdata (entget mt)))
); and
(progn ; then
(if (wcmatch (setq txt (cdr (assoc 1 mtdata))) "*-*"); contains hyphen(s)
(setq newtxt (substr txt 1 (vl-string-position 45 txt 1 T)))
; from beginning to last hyphen encountered
(setq newtxt txt); else [no Enter(s)] -- keep
); if
(initget 4); no negative
(setq rot
(cond
((getangle "\nText rotation <0>: ")); nil on Enter
(0.0); User Enter
); cond
); setq
(entmake
(append
(subst (cons 1 newtxt) (assoc 1 mtdata) mtdata); truncated text
'((8 . "Lasermarkierung") (40 . 10.0)); Layer 0 & fixed height
(list (cons 50 rot))
'((62 . 256))
); append
); entmake
(command "_.move" (entlast) "" "_none" (cdr (assoc 10 mtdata)))
); progn
); if
); defun

 

 

0 Likes
Message 39 of 51

Kent1Cooper
Consultant
Consultant
Accepted solution

Try:
(if (wcmatch (setq txt (cdr (assoc 1 mtdata))) "*/*"); contains slash(es)
(setq newtxt (substr txt 1 (vl-string-position 47 txt 1))) ;; without the T
; from beginning to first slash encountered

Kent Cooper, AIA
Message 40 of 51

tiwari1211
Enthusiast
Enthusiast

Hi Kent, 

Thanks this works very well. 

Also, I generate a part list using this text (1497-1/1.4000/410S-2x) using a excel macro. Macro reads all the Mtext available in model space in "E-text" layer and creates table as shown below. After that i copy this table in Autocad. 

Is it possible to generate this table directly in Autocad ?

I can share the excel macro if require. 

tiwari1211_0-1666724265379.png

 

Would be great if i get your help in this. 

 

Many thanks 

0 Likes