Community
AutoCAD Forum
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Removing a certain part of mtext

20 REPLIES 20
Reply
Message 1 of 21
Anonymous
2827 Views, 20 Replies

Removing a certain part of mtext

hi my problem is i have number of mtext contains the following:-

 

"LINE A

E 12345

N 54321

EL+ 10000"

 

The numbers "12345" and "54321" are variable and i want to remove the middle part of the mtext to be like

 

"LINE A

EL+ 10000"

 

or remove all chacters from "LINE A" to "EL+ " so could you please help me becouse i have many sheets contain the same text.

 

thanks alot for your time.

20 REPLIES 20
Message 2 of 21
Kent1Cooper
in reply to: Anonymous

That's a matter of finding the Enters/hard-returns/new-lines in the Mtext's text content, which are \\P elements.  Keep the first one, and strip out everything through the last one.  Lightly tested:

 

(defun C:FLL (/ mtss n mtdata content start)
  ; = keep First and Last Lines only of selected mtext
  (if (setq mtss (ssget ":L" '((0 . "MTEXT"))))
    (repeat (setq n (sslength mtss))
      (setq
        mtdata (entget (ssname mtss (setq n (1- n))))
        content (cdr (assoc 1 mtdata))
        start (substr content 1 (+ (vl-string-search "\\P" content) 2))
          ; first line including Enter at end
      ); setq
      (while (wcmatch content "*\\P*"); still any more Enters?
        (setq content (substr content (+ (vl-string-search "\\P" content) 3)))
          ; remove up through first remaining Enter; end up with only portion after last Enter
      ); while
      (entmod ; update
        (subst
          (cons 1 (strcat start content)); new text content
          (assoc 1 mtdata); original text content
          mtdata
        ); subst
      ); entmod
    ); repeat
  ); if
); defun

Kent Cooper, AIA
Message 3 of 21
Anonymous
in reply to: Kent1Cooper

Hi thanks for your fast reply but unfortunately the LSP doesn’t work I don’t know why, the attached photo explains the error.
Can I change the first line to remove to be the third or the forth not the second as you can see in the attached photo I want to remove the yellow bow test which are the fourth and fifth lines and if I can how could I do it?
Finally can it work on 2014 version?
Again thanks for your time.

Message 4 of 21
Kent1Cooper
in reply to: Anonymous


@Anonymous wrote:

....
Can I change the first line to remove to be the third or the forth not the second .... Finally can it work on 2014 version? ....


It shouldn't be too hard to get it to do that if it's always and only the 3rd and 4th lines you want removed.  But if they could sometimes be like the example in your first Post, or other numbers of lines, then the routine would need to look for an Enter followed by either a W or an E.  But could the N or S line ever be above the W or E line?  Could any other line before the ones you want removed ever start with one of those letters?  If so, would the lines you want removed always, but no other lines above them ever, start with one of those followed by a space?  That would help, but it's certainly more complicated than just finding the end of the first line.

 

This is why, for future reference, the more real-life and specific you can be in examples and descriptions of what you're after, the better and quicker solutions you'll get.

 

This kind of thing shouldn't be affected by the version, though I wouldn't guarantee it.  I'll want to try it later where I have a newer version than I do here.

Kent Cooper, AIA
Message 5 of 21
Anonymous
in reply to: Kent1Cooper

thanks for your fast reply and sorry for misunderstanding or my poor illustration the thing is i have multiple sheets contain the following mtext

 

"CONT'D FROM

DWG#

3"-DW-990-0533-SS-2

E 13280

N 34488

EL +8530"

 

the charcters are variable and change as follows:

 

"XXXXXX XXXX

DWG#

XX-XXXX-XX-XXX-XX-X

X XXXXX

X XXXXX

EL +XXXXX"

 

I want to remove the 4th and 5th lines only whatever the letters in it would be, So the final mtext turn into:-

 

"CONT'D XXXX

DWG#

XX-XXXX-XX-XXX-XX-X

EL +XXXXX"

 

i hope that this illustration works, sorry again i know that each detail matters and please see the attached screenshot .

Message 6 of 21
Kent1Cooper
in reply to: Anonymous


@Anonymous wrote:

.... 

I want to remove the 4th and 5th lines only whatever the letters in it would be, ....


In the bottom Mtext object in the image, the northing/easting lines are the 3rd & 4th, rather than the 4th & 5th [no "DWG#" line].  Is that a mistake, or does that possibility need to be accounted for?  If it really would always be the 4th & 5th you want removed, that would be a lot easier than if it might be different lines that would need to be found based on their content.

Kent Cooper, AIA
Message 7 of 21
Anonymous
in reply to: Kent1Cooper

thanks for your fast reply and yes you are right but it not amistake i know it will be easer to just remove the 4th and 5th lines and that is what i need but if there is a way to remove any line from mtext contains the following:-

 

"E XXXXX"

or

"N XXXXX"

or

"S XXXXX"

or

"W XXXXX"

 

I will be gradefull and that will make my process in every sheet much easier and thanks for you of course.

Message 8 of 21
Kent1Cooper
in reply to: Anonymous


@Anonymous wrote:

....it not amistake .... if there is a way to remove any line from mtext contains the following:-

"E XXXXX" or "N XXXXX" or "S XXXXX" or "W XXXXX"

....


Not as complicated as I was expecting....  The following should remove any such lines [starting with one of those four capital letters followed by a space], and doesn't care where they occur in the Mtext or in what order or whether they're adjacent lines, as long as there aren't more than one starting with the same letter in the same Mtext object [which doesn't seem likely, given the kind of information they represent].  No bells and whistles yet, and lightly tested, but it seems to work:

 

(defun C:NER (/ mtss n mtdata content start mid end)
  ; = Northing/Easting Removal of lines from selected Mtext
  (if (setq mtss (ssget ":L" '((0 . "MTEXT"))))
    (repeat (setq n (sslength mtss))
      (setq
        mtdata (entget (ssname mtss (setq n (1- n))))
        content (cdr (assoc 1 mtdata))
      ); setq
      (foreach dir '("E" "N" "S" "W")
        (setq teststr (strcat "\\P" dir " "))
          ; Enter followed by compass direction letter + space
        (if (wcmatch content (strcat "*" teststr "*")); contains a line for that direction
          (progn ; then
            (setq
              pos (vl-string-search teststr content)
              start (substr content 1 (+ pos 2))
                ; content through Enter at end of preceding line
              mid (substr content (+ pos 3))
                ; middleman content this line through end
              end (substr mid (+ (vl-string-search "\\P" mid) 3))
                ; content after this line
            ); setq
            (entmod ; update
              (subst
                (cons 1 (setq content (strcat start end))); new text content
                (assoc 1 mtdata); original text content
                mtdata
              ); subst
            ); entmod
          ); progn
        ); if
      ); foreach
    ); repeat
  ); if
); defun

Kent Cooper, AIA
Message 9 of 21
Anonymous
in reply to: Kent1Cooper

again thanks for your fast reply but still the lISP donot work and the following is shown as the attached photo 

and i donot know why? 

Message 10 of 21
Anonymous
in reply to: Anonymous

please tell me if there are any thing must be done before coping the code in new notepad text and change the extension to LISP and load it into AutoCad
Message 11 of 21
doni49
in reply to: Anonymous

The following code is based on Kent's code.  I made a few changes so that it will look at every line and if it begins with an N, E, W or S followed by a space and five numeric digits, that line will be excluded and then the whole thing will eventually be re-written.

 

(defun C:FLL ();/ mtss n mtdata content start)
  ; = keep First and Last Lines only of selected mtext
  (if (setq mtss (ssget ":L" '((0 . "MTEXT"))))
    (repeat (setq n (sslength mtss))
      (setq
        mtdata (entget (ssname mtss (setq n (1- n))))
        content (cdr (assoc 1 mtdata))
        start (substr content 1 (+ (vl-string-search "\\P" content) 2))
          ; first line including Enter at end
      ); setq
      (while (wcmatch content "*\\P*"); still any more Enters?
        (setq content (substr content (+ (vl-string-search "\\P" content) 3)))


                (setq cline nil)
                (if (vl-string-search "\\P" content)(setq cline(substr content 1 (+ (vl-string-search "\\P" content) 2)))(setq cline content))
                ;(if cline
                  (if (not(wcmatch cline "*[NEWS] #####*\\P"))
                     (setq start (strcat start cline))
                  )
                  ;(setq start content)
                ;)
        
      ); while
      (entmod ; update
        (subst
          (cons 1 start); new text content
          (assoc 1 mtdata); original text content
          mtdata
        ); subst
      ); entmod
    ); repeat
  ); if
); defun

 



Don Ireland
Engineering Design Technician




If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.


Please do not send a PM asking for assistance. That's what the forums are for. This allows everyone to benefit from the question asked and the answers given.

Message 12 of 21
Anonymous
in reply to: doni49

thanks for your response but still the same problem running the LISP I feel that something is wrong during loading the LISP, the attached photo illustrating AutoCAD response , i just copy the code and putting it into anew notepad and load it so could you please tell me what am i doing wrong to load and run the lisp correctly?,

finaly if the text has no space i.e "NXXXXX" what part of the code should be changed? and into what?

again thanks foryour responce.

Message 13 of 21
Kent1Cooper
in reply to: Anonymous

It worked for me in both 2004 and 2015 versions.  I have found that in copying/pasting code from this website, it sometimes "arrives" in Notepad with an oddball character somewhere, or extraneous blank lines.  Here it is put into a .lsp file that works for me.  Save that somewhere and use APPLOAD to load it.

Kent Cooper, AIA
Message 14 of 21
Kent1Cooper
in reply to: Anonymous


@Anonymous wrote:

.... if the text has no space i.e "NXXXXX" what part of the code should be changed? and into what?

....


That should be easy enough to account for by taking the space out of doni49's test:

 

(if (not(wcmatch cline "*[NEWS]#####*\\P"))

 

Or if they might sometimes have the space, but not always, check for either:

 

(if (not (or

  (wcmatch cline "*[NEWS] #####*\\P")

  (wcmatch cline "*[NEWS]#####*\\P")

))

 

But I have to ask:  Would it always and only be five digits?  That doesn't seem likely, unless that's some kind of a standard and it's always padded out to five digits with leading zeros when needed.

Kent Cooper, AIA
Message 15 of 21
Anonymous
in reply to: Kent1Cooper

-Thanks for your reply the lisp is still couldnot remove them but the error this time shows as "nil" massage in command line as shown in the attached photo and i have applied the lisp on the attached test DWG.

- yes almost all the sheets have the same numbers of coordinates , but some times the number reduced to be 4 but not in my current case then it will be helpful to use a LISP which remove all MTEXT lines beginig with "E " or "W " or "N " or "S ", If it couldnot be i will change the number of "#" to match the case but again the LISP you gave me will be generally used becouse it is not depend on the number of coordinates and depend only on the beginning of mtext line if it works so please tell me what is wrong in appling the LISP in the attached test.dwg file

 

 

 

Message 16 of 21
doni49
in reply to: Anonymous

Are those MLEADER objects? If so, this function doesn't see any mtext objects to process.

That would produce the nil.


Don Ireland
Engineering Design Technician




If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.


Please do not send a PM asking for assistance. That's what the forums are for. This allows everyone to benefit from the question asked and the answers given.

Message 17 of 21
Anonymous
in reply to: doni49

thanks for your reply and yes it is MLEADER objects with mtext generated from Autodesk Plant 3D Program to ISO dwg as shown in the test.dwg and even after i explode the all the leader the answer is "nil" so please tell me what i shall do to overcaome this problem to make the 2 lisps work

Message 18 of 21
Kent1Cooper
in reply to: Anonymous


@Anonymous wrote:

thanks for your reply and yes it is MLEADER objects ... and even after i explode the all the leader the answer is "nil" ....


I'm at my place with too old a version to have Mleaders, but if all these are true:

1)  they contain their textual content in the same way [DXF code 1 entry], and

2)  Exploding them doesn't merely separate them into a Leader arrow and Mtext but also Explodes the Mtext into separate plain-Text objects [which would explain your nil return after Exploding], and

3)  their entity type name [DXF code 0 entry] is "MLEADER"

then this change ought to work:  Change

 

(if (setq mtss (ssget ":L" '((0 . "MTEXT"))))

to

(if (setq mtss (ssget ":L" '((0 . "MTEXT,MLEADER"))))

 

If "MLEADER" isn't the right entity type name, substitute whatever that is ["MULTILEADER" perhaps?].

Kent Cooper, AIA
Message 19 of 21
Anonymous
in reply to: Kent1Cooper

thanks for your fast reply but No it doesn't change the content of the desired mtext so what could you please tell me what i shall do to solve this problem and make the 2 lisps work and is there is away to convert the generated text to simple mtext so the lisp can handle please see the attached dwg of both texts , i noticed a difference in the content cell when i have compared the two texts content so i find this characters in the beginnig of the generated mtext   "{\Q0;" . at some text I Found it and for other text I amnot ,I donot know if it is relevent or can help 

again thanks for your fast reply 

Message 20 of 21
Anonymous
in reply to: Anonymous

thanks alot Mr dane49 and mr kentcooper for your help there is acode correction mrs pli have given to me to let the code see that mtext the modified code at follows put i cannot apply this modification on the fll lisp sent to me from mr dane49 so could any one modify the fll code forme and send it back to me becouse i need both.

the modified ner lisp for this kind of mleader text is

 

(defun C:NER (/ mtss n mtdata content start mid end)
; = Northing/Easting Removal of lines from selected Mtext
(if (setq mtss (ssget ":L" '((0 . "MTEXT,MLEADER"))))
(repeat (setq n (sslength mtss))
(setq
mtdata (entget (ssname mtss (setq n (1- n))))
content (cdr (assoc 1 mtdata))
); setq
(foreach dir '("E" "N" "S" "W")
; (setq teststr (strcat "\\P" dir " "))
(setq teststr (strcat "\n" dir " ")) ; find \n code in mtext string
; Enter followed by compass direction letter + space
(if (wcmatch content (strcat "*" teststr "*")); contains a line for that direction
(progn ; then
(setq
pos (vl-string-search teststr content)
; start (substr content 1 (+ pos 2))
start (substr content 1 (+ pos 1)) ; reduce # by 1
; content through Enter at end of preceding line
; mid (substr content (+ pos 3))
mid (substr content (+ pos 2)) ; reduce # by 1
; middleman content this line through end
; end (substr mid (+ (vl-string-search "\\P" mid) 3))
end (substr mid (+ (vl-string-search "\n" mid) 2)) ; reduce # by 1
; content after this line
); setq
(entmod ; update
(subst
(cons 1 (setq content (strcat start end))); new text content
(assoc 1 mtdata); original text content
mtdata
); subst
); entmod
); progn
); if
); foreach
); repeat
); if
); defun

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


AutoCAD Beta