Mtext missing linebreak - adding them with LISP

Mtext missing linebreak - adding them with LISP

stijn.dedeckerM4Q3S
Participant Participant
1,785 Views
20 Replies
Message 1 of 21

Mtext missing linebreak - adding them with LISP

stijn.dedeckerM4Q3S
Participant
Participant

Hi, 

 

I have a bunch of drawings with MTEXT blocks, for some rease we are missing linebreaks, in the drawing i see it like this

 

03.01

Meetingroom 

 

but when check it in the properties mtext content its like this "03.01Meetingroom" 
I have a list off all the roomtypes like 'Meetingroom','Cafe',CoffeeCorner,.. so i try with a lisp script to add a line break, but for some reasen, it only want to put the linebreak before the roomnumber and not before the room type? 

Can someone Helpme

0 Likes
Accepted solutions (1)
1,786 Views
20 Replies
Replies (20)
Message 2 of 21

JBerns
Advisor
Advisor

@stijn.dedeckerM4Q3S,

 

Please provide a sample drawing. 

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 3 of 21

Kent1Cooper
Consultant
Consultant

If you put this in at the Command line:

(cdr (assoc 1 (entget (car (entsel "\nSelect such an Mtext object: ")))))

does it return this?

 

"03.01\nMeetingroom"

 

If so, that \n is a "soft return," which you get from Shift+Enter, and which starts a new line but not a new paragraph.  It's curious that it doesn't show up in the Properties palette Contents slot.  A "hard" return from just Enter does show up, as "\P" [which comes out as "\\P" in the entity data content entry].  But a soft return is a form of line break -- it's not missing [otherwise the Mtext would not be in two lines], just un-displayed in that one type of content display, for whatever reason.

 

Oddly, for me, with the soft return, what shows in the Contents slot if I have only picked the Mtext is just the

03.01

part, but if I pick in that slot, then I see the entire

03.01Meetingroom

Kent Cooper, AIA
0 Likes
Message 4 of 21

john.kaulB9QW2
Advocate
Advocate

Why do you need a "sample drawing"?! ...why does every post have someone asking for a sample drawing lately? This is even happening on TheSwamp too.

 

Firstly (directed at question askers), if you can't describe your problem in words then you should probably spend a little more time defining your question/problem.

Secondly (directed at question answer-ers), if you think you need a drawing or a picture, then ask more questions differently. 

Thirdly, (directed at question answer-ers), it's okay to NOT know the answer. This is not a popularity contest.

another swamper
0 Likes
Message 5 of 21

Kent1Cooper
Consultant
Consultant

@stijn.dedeckerM4Q3S wrote:

.... I have a list off all the roomtypes like 'Meetingroom','Cafe',CoffeeCorner,.. so i try with a lisp script to add a line break, but for some reasen, it only want to put the linebreak before the roomnumber and not before the room type? ....


For that issue, it would be necessary to see the form of your list of room types, and the contents of the AutoLisp routine you are using.  And if it's not clear from the routine's code, we would need to know whether all things you use it on would always start with two 2-digit numbers with a decimal point between them as in your description, or what the possible formats of the string contents could be.

 

[By the way, don't say "lisp script," because "script" has a specific meaning in AutoCAD, which is related to the kinds of things that AutoLisp can do, but is its own distinct thing.]

Kent Cooper, AIA
0 Likes
Message 6 of 21

Kent1Cooper
Consultant
Consultant

@john.kaulB9QW2 wrote:

Why do you need a "sample drawing"?! ...why does every post have someone asking for a sample drawing lately? ....


I feel your pain.  I agree that there are many requests for sample drawings that really seem unnecessary.  But certainly there are cases where having an actual drawing and a drawn object to investigate is the best or quickest way to diagnose a problem.

 

Sometimes there are enough possibilities that it would be cumbersome to ask all the possible questions, but might be very quick to find the problem from conditions that the asker may not know about and/or that don't show in an image [things won't join because they're at different elevations, something is on a locked Layer, some System Variable is set wrong, the difference between a Text object and an Attribute definition, etc.].

 

If one is unaware of the possible situation I described in Message 3 [we don't know yet whether that's the cause], I can understand wanting to have a drawing, with an object in it that exhibits the problem, or in which various settings can be looked at, for diagnosis.  But yes, most of the time I try to pose pertinent questions before asking for a sample drawing.

Kent Cooper, AIA
Message 7 of 21

stijn.dedeckerM4Q3S
Participant
Participant

I have added a sample drawing

0 Likes
Message 8 of 21

stijn.dedeckerM4Q3S
Participant
Participant

here is my drawing

0 Likes
Message 9 of 21

pendean
Community Legend
Community Legend

@stijn.dedeckerM4Q3S wrote:

I have added a sample drawing


Is this file from another program?

Who/how was the MTEXT added?

 

Your map3d-content-20-errors-removed-by-AutOCAD DWG file has MTEXT with line-breaks, they are just not showing as such in PROPERTIES and

1) you are wanting to know why?

2) Or are you assuming the line-breaks are just not there and are concerned they might go away somehow all by themselves?

3) is your content far away from 0,0,0 because you are working with a MAP3D site plan and you intend to send it back to the MAP3D intact inside their own DWG file instead of XREFing? and is that perhaps why your METER units in the DWG are being used to draw a floor plan of a structure?

 

Your file's MTEXT are an anomaly: what is their source is the answer you appear to seek.

0 Likes
Message 10 of 21

pendean
Community Legend
Community Legend

@john.kaulB9QW2 wrote:

Why do you need a "sample drawing"?! ...


Check it out and see for yourself 😉

0 Likes
Message 11 of 21

JBerns
Advisor
Advisor

@stijn.dedeckerM4Q3S,

 

Just to clarify, please explain what the text should look like.

 

Change this:

 

03.01

Meetingroom 

 

to ???

 

Please include examples for the MText objects.

 

00.C1

circulaie

 

00.15

bergruimte

 

Once we know the format you want we can better assist.

 

 

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 12 of 21

Kent1Cooper
Consultant
Consultant

@stijn.dedeckerM4Q3S wrote:

I have added a sample drawing


So yes, they have the \n variety of linebreaks.  Since they are actually not "missing linebreaks" as described in Message 1, is it just that the linebreaks are not as you expect, and do you want to change those to be "hard" instead of "soft" returns, or is it enough that they do in fact have line breaks, and can be left alone?

Kent Cooper, AIA
0 Likes
Message 13 of 21

john.kaulB9QW2
Advocate
Advocate

@pendean wrote:

@john.kaulB9QW2 wrote:

Why do you need a "sample drawing"?! ...


Check it out and see for yourself 😉


I cannot (nor would I if I could, to be honest. sorry).

 

But you're missing the point; what is the need? Think storage space (how long does that file live on a drive somewhere?). and what problem(s) are you trying to solve that are not "programming related"?

 

Simply put, the OP asked how you insert a newline in between the number and the name in a AutoLisp programming forum (not a "is my drawing corrupt" or "I don't know the difference between TEXT and ATTRIBUTES" forum).

 

more simply put, if the code (Ken spent so much time writing):
(cdr (assoc 1 (entget (car (entsel "\nSelect text object: ")))))
fails, that's their problem.

 

So let me try to build something to answer the OP (this will be wrong if the structure of the text isn't in the specific format mentioned--"#.#@@@"--but, who cares at this point).

 

We can use REGEX to pull the indexes of the number part and the alpha part and then concat them back together, but this will fail if your strings are not in that exact format. -i.e. if you have numbers after the first part, or alpha chars in your numbers then this will break.

 

;; helper function to locate the differnt parts we want.
(defun string-index-p (f s)
     (defun isnumber-p (e) (wcmatch e "#") )
     (defun isalpha-p (e) (wcmatch e "@") )
     ( (lambda (sl i)
         (while (> (setq sl (1- sl)) 0)
                (if ((eval f) (substr s sl 1))
                  (setq i (cons sl i))))
         i )
      (1+ (strlen s))
      nil) )

;; create a quick test.
(setq mystring "03.01Meetingroom")

;; locate the parts.
(setq numbers (string-index-p 'isnumber-p mystring))
(setq alpha (string-index-p 'isalpha-p mystring))

;; string them together with a new-line inbetween.
(strcat 
  (substr mystring (car numbers) (car (reverse numbers)))
  "\n"
  (substr mystring (car alpha) (car (reverse alpha)))
  )
another swamper
0 Likes
Message 14 of 21

stijn.dedeckerM4Q3S
Participant
Participant

I have added 2 print screen 

We use a facilities management information system (fmis) program that can import DWG files and read the data from the drawings in that way. Now, the problem is that, with a large number of files, it is unable to read the second line (space type) unless there is a \P between the first and second parts. You can see the differences in my screenshot. This concerns about a hundred drawings, so I'm looking for a solution, whether it's through Lisp or another method, to correct this automatically.

mtext not ok.png

mtext ok.png

 

0 Likes
Message 15 of 21

Kent1Cooper
Consultant
Consultant
Accepted solution

@stijn.dedeckerM4Q3S wrote:

.... We use a facilities management information system (fmis) program .... it is unable to read the second line (space type) unless there is a \P between the first and second parts. ....


A fairly simple substitution.  Try this [minimally tested]:

(defun C:HRLB ; = Hard Return Line Breaks
  (/ ss n str tdata)
  (if (setq ss (ssget "_X" '((0 . "MTEXT") (1 . "*\n*"))))
    (repeat (setq n (sslength ss))
      (setq str (cdr (assoc 1 (setq tdata (entget (ssname ss (setq n (1- n))))))))
      (entmod (subst (cons 1 (vl-string-subst "\\P" "\n" str)) (assoc 1 tdata) tdata))
    )
  )
  (prin1)
)

 

Kent Cooper, AIA
0 Likes
Message 16 of 21

stijn.dedeckerM4Q3S
Participant
Participant

Hi, 

This seems to work.

 

Do you have a solution to run the LISP over all dwg files in a specific folder? 

 

Tnx

Stijn

0 Likes
Message 17 of 21

Kent1Cooper
Consultant
Consultant

@stijn.dedeckerM4Q3S wrote:

.... This seems to work. Do you have a solution to run the LISP over all dwg files in a specific folder?  ....


That would need to be a Script.  I haven't done that kind of thing, but you can Search the Forum for many examples.

Kent Cooper, AIA
0 Likes
Message 18 of 21

Sea-Haven
Mentor
Mentor

 A start, an example script, just add (C:HRLB) as last line in the saved lisp so it auto runs on loading.

 

Open dwg1
(load "HRLB")
Close Y
Open dwg2
(load "HRLB")
Close Y
open dwg3
(load "HRLB")
Close Y

 

 

Message 19 of 21

stijn.dedeckerM4Q3S
Participant
Participant
Tnx, we are now using your solution

Maybe you can help with other question,

How can i change now the second part to a smaller text size, , the first part is 0.25, and i want the second part to be 0.1,

In autocad i can do it manual but i want it also automated.
0 Likes
Message 20 of 21

Kent1Cooper
Consultant
Consultant

@stijn.dedeckerM4Q3S wrote:
....
How can i change now the second part to a smaller text size, , .... In autocad i can do it manual but i want it also automated.

Do it manually and look at how it's coded into the text-string content.  You would need to use (strcat) to put the size encodings into the string in the right places.

Kent Cooper, AIA
0 Likes