Jump to content

Split text string into 45 character


subodh_gis

Recommended Posts

We have some text objects that having long text strings like 500 character so i need a lisp that can split the text into 45-45 characters and make them separate object of 45 character. the reason of this is we have to export the text in other software and that software don't support more than 45 character string. so now we are manually spiting them.

Link to comment
Share on other sites

  • Replies 31
  • Created
  • Last Reply

Top Posters In This Topic

  • subodh_gis

    11

  • pBe

    6

  • ReMark

    4

  • BIGAL

    4

Top Posters In This Topic

Posted Images

Easy done using (substr new_line X Y) where x is start and y is the number of characters in your case Y=45 x=1 x=46 etc you need to just (Repeat for the number of characters in a line divide by 45 last Y is balance. I dont have a length of text characters answer someone will help then code is easy.

Link to comment
Share on other sites

I am new to lisp please can u code it for me .length may vary. we can get the string length by selecting it.

Link to comment
Share on other sites

Found it STRLEN string if I can find some time will do very easy, but need to know what to do with the 45 long texts do you want answer exported to a text file, lines of text in cad ? Are you picking multiple text ?

Link to comment
Share on other sites

This will put it in a list:

 

(defun c:test ()
  (setq str "This is a test of a function to divide a long string in a series of shoter string.  The new shorter string must be a maximum of 45 characters.  The OP has not specified if the new string must be separated on a logical break like SPACE or punctuation mark.  Anyway here goes")

  ; Here without logical separation  ;

  (setq x 1 len 45 strlst nil strlst2 nil)
  (repeat (+ (/ (strlen str) 45) 1)
    (setq strlst (cons (substr str x (- len 1)) strlst)
               x (+ x len -1)
    )
  )
  (setq strlst (reverse strlst))
  (foreach s strlst
    (print s)
  )
)

 

Would probably be a good idea to remove any "\n" for your string prior

to processing.

 

ymg

Link to comment
Share on other sites

ymg3 same thoughts if on 1 line the different text styles and just the words make seperating correctly quite difficult as text will overlap or have different spacing.

 

A line by line is easiest, at (Print S) adding pick pt, text ht, then adjust XY adding text. Subodh_gis your turn now.

Link to comment
Share on other sites

Thanks for both of you.

But code is not doing what i need actually. code should first ask to select the text and then split it.

Link to comment
Share on other sites

1st part

Thanks for both of you. But code is not doing what i need actually. code should first ask to select the text and then split it.
Good time to start learning lisp the line (setq str is a sample line for testing purposes, do a bit of searching about entsel or ssget, entget & assoc these work together to allow you to pick the text and return the text string which you would set to str in the code above. just read a few of the lisp examples you will see how very quickly.

 

2nd part read Tharwat post, then final solution will come.

Link to comment
Share on other sites

(Defun c:m45 (/ str ent ll [t 45:s txt ur)
;;;		pBeMay2014		;;;
 (princ "\nSelect Text objects")
 (if (setq strs (ssget "_:L" '((0 . "TEXT"))))
   (repeat (Setq i (sslength strs))
     (setq ent	(entget (ssname strs (setq i (1- i))))
    pt	(cdr (assoc 10 ent))
    str	(cdr (assoc 1 ent)) cl 1
     )
     (repeat (1+ (/ (strlen str) 45))
(setq 45:S (substr str 1 45))
(setq txt (entmakex (list (cons 0 "TEXT")
			  (cons 10 pt)
			  (Cons 1 45:S)
			  (assoc 7 ent)
			  (assoc 40 ent)
			  (cons 62 cl);<-color for identification
		    )
	  )
)
(vla-getboundingbox (vlax-ename->vla-object txt) 'll 'ur)
(setq ur (vlax-safearray->list ur))
(setq pt  (list (car ur) (cadr pt))
      cl  (1+ cl)
      str (substr str 46)
)
     )
     (entdel (ssname strs i))
   )
 )
 (princ)
))

 

But i do agree with BIGAL

 

Good time to start learning lisp

 

@ymg

Watch out:

(setq strlst (cons (substr str x [b](- len 1)[/b]) strlst) x [b](+ x len -1[/b]))

 

Produce a list of 44 characters string per item

 

Not sure but should that be just

(setq strlst (cons (substr str x [b]len[/b]) strlst)  x[b] (+ x len )[/b])

Edited by pBe
Link to comment
Share on other sites

But code is not doing what i need actually. code should first ask to select the text and then split it.

Link to comment
Share on other sites

Are you telling me you need to REPLACE the selected TEXT entity with DIVIDED 45 CHARACTER string on the same SPOT? Because it doesnt look that way on your posted image at post 13 and someplace else in the world.

 

The good in me will only give you ONE more chance at this subodh_gis . As tharwat already requested, PLEASE POST A SAMPLE DRAWING

Link to comment
Share on other sites

The problem with a lisp routine like this is one is liable to get results that look like this...

 

The language commonly used to describe the ba

ttle going on inside the Republican Party is wron

g and misleading. The fights this spring are not

between the "grass roots" and "the establishme

nt," but between two establishment fractions sp

ending vast sums to gain the upper hand. Thei

r confrontation has little to do with the long-ter

m philosophical direction of the GOP. Very rich

See what I mean? Words would be cut off unnaturally and no hyphens would be inserted, as in normal written text, as they should be. In my opinion it is a bad idea.

Link to comment
Share on other sites

Here is my variation but have no time to test the code cause I have to leave the office now .:D

 

(defun c:Test (/ _l ss)
 ;;    Tharwat 18. May. 2014        ;;
 (defun _l (e / a b l)
   (vla-getboundingbox (vlax-ename->vla-object e) 'a 'b)
   (setq l (mapcar 'vlax-safearray->list (list a b)))
   (distance (car l) (cadr l))
 )
 (if (setq ss (ssget "_:L" '((0 . "TEXT"))))
   ((lambda (i / sn st pt e en j n)
      (while (setq sn (ssname ss (setq i (1+ i))))
        (if (> (strlen (setq st (cdr (assoc 1 (setq en (entget sn)))))) 45)
          (progn (setq pt (cdr (assoc 10 en))
                       e  (cdr (assoc -1 (entmod (append en (list (cons 1 (substr st 1 45)))))))
                       st (substr st 46)
                 )
                 (repeat (1+ (/ (strlen st) 45))
                   (setq
                     e (entmakex (append (vl-remove-if-not '(lambda (x) (member (car x) '(0 8 62 40 41 50))) (entget e))
                                         (list (cons 1 (substr st 1 45))
                                               (cons 10 (setq pt (polar pt (cdr (assoc 50 (entget e))) (_l e))))
                                         )
                                 )
                       )
                   )
                   (setq st (substr st 46))
                 )
          )
        )
      )
    )
     -1
   )
 )
 (princ)
) (vl-load-com)

Link to comment
Share on other sites

See what I mean? Words would be cut off unnaturally and no hyphens would be inserted, as in normal written text, as they should be. In my opinion it is a bad idea.

 

I know what you mean ReMark ... but our "Last Chance" OP did say:

....the reason of this is we have to export the text in other software and that software don't support more than 45 character string....

 

I guess it would still "look" normal if the program will split the string onto the same spot.

 

EDIT: hang on. i believe the OP did say export the result to a external file. maybe you have somethng there ReMark, [now i'm confused]

 

FWIW: code updated at post# 14

 

@Tharwat

Throw in 7 in there for TEXTSTYLE

...(member (car x) '(0 [b]7[/b] 8 62 40 41 50))..

Edited by pBe
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...