Jump to content

Combining text second below first and place in desired location.


lucky9

Recommended Posts

(defun c:comtext ()
  (setq ent1 (entsel "Select first text entity: "))
  (setq ent2 (entsel "Select second text entity: "))
  (setq text1 (cdr (assoc 1 (entget (car ent1)))))
  (setq text2 (cdr (assoc 1 (entget (car ent2)))))
  (setq point (getpoint "Select location for combined text: "))
  (entmake (list (cons 0 "MTEXT") (cons 10 point) (cons 1 (strcat text1 "\P" text2)))))
  (princ)
)

 

Can anyone help me on this code I wrote. The purpose of the code is to combine two text entities combining as a MText deleting the selected and placing in desired location. When I run the above code it does not work 

The code runs with the following Error: 

 

Select first text entity: Select second text entity: Select location for combined text: nil

 

Please help, thank you 

Link to comment
Share on other sites

I have a library of entmake routines, so I don't need to copy and paste them every time I want one. get it working once and it will work every time after.

 

Using that instead of your code gives me the below: Try that. Also added a couple of princ to report what the user clicks

 

 

Problem with your code I think you need the cons 100s:  (cons 100 "AcDbEntity")(cons 100 "AcDbMText")

 

 

(defun M-Text (pt str)
  (entmakex (list (cons 0 "MTEXT")         
                  (cons 100 "AcDbEntity")
                  (cons 100 "AcDbMText")
                  (cons 10 pt)
                  (cons 1 str)))
)


(defun c:comtext ()
  (setq ent1 (entsel "\nSelect first text entity: "))
  (princ (setq text1 (cdr (assoc 1 (entget (car ent1))))))
  (setq ent2 (entsel "\nSelect second text entity: "))
  (princ (setq text2 (cdr (assoc 1 (entget (car ent2))))))
  (setq point (getpoint "\nSelect location for combined text: "))
  (M-Text point (strcat text1 "\P" text2))  
  (princ)
)

 

 

  • Like 1
Link to comment
Share on other sites

Thank you for the code, How do I retain the original text formatting for example color, height, font etc. of selected text.   

 

 

image.thumb.png.d6b93f1cb6dfc529b8dc00455bfe8df5.png

Edited by lucky9
Link to comment
Share on other sites

Building on what you have done., 

 

(setq text1 (cdr (assoc 1 (entget (car ent1)))))

 

Will give you the text string (text 1 here), if you use other assoc numbers you can get the rest, then add them into the entmake mtext.

For example:

8 for layer

62 for colour

40 for height

7 for style

 

 

EDIT:

Try this which will take the values from the text1 - see below from MHUPP, if they are different you might want to do some rule to work out what to use.

 

Noting that the order you create an entity with can be important, you can view the entity description with

(entget(car(entsel)))

and select something - the command line will tell you what it all is and in the order that CAD likes (it isn't numerical order)

 

 

(defun M-Text ( layer colour pt height str font / )
  (entmakex (list (cons 0 "MTEXT")         
                  (cons 100 "AcDbEntity")
                  (cons 8 layer)
                  (cons 62 colour)
                  (cons 100 "AcDbMText")
                  (cons 10 pt)
                  (cons 40 height)
                  (cons 1 str)))
                  (cons 7 font)
)
(defun c:comtext ( / ent1 MyEnt1 text1 layer colour height font ent2 text2 CombinedText point)
  (setq ent1 (entsel "\nSelect first text entity: "))
  (setq MyEnt1 (entget (car ent1)))
;;  (princ (setq text1 (cdr (assoc 1 (entget (car ent1))))) )

  (princ (setq text1 (cdr (assoc 1 MyEnt1))) )

  (setq layer (cdr (assoc 8 MyEnt1)))
  (setq colour (cdr (assoc 62 MyEnt1)))
  (setq height (cdr (assoc 40 MyEnt1)))
  (setq font (cdr (assoc 7 MyEnt1)))

  (setq ent2 (entsel "\nSelect second text entity: "))
  (princ (setq text2 (cdr (assoc 1 (entget (car ent2))))) )

  (princ (setq CombinedText (strcat text1 "\P" text2)))

  (princ (setq point (getpoint "\nSelect location for combined text: ")))
  (M-Text layer colour point height (strcat text1 "\P" text2) font)  
  (princ)
)

 

Edited by Steven P
  • Like 1
Link to comment
Share on other sites

This what i came up with a few weeks back. will work on multiple lines of text or mtext. will create mtext at the current 'textsize variable & keep the formatting. if you don't pick a point it will copy text to the clipboard.

 

;;----------------------------------------------------------------------------;;
;; COMBINE MULTIPLE TEXT INTO ONE MTEXT
(defun c:CMT (/ done lst str)
  (while (not done)
    (setvar 'errno 0)
    (setq e (car (nentsel "\nSelect Text to join: ")))
    (if
      (and
        (= (getvar 'errno) 0)
        (wcmatch (cdr (assoc 0 (entget e))) "*TEXT")
      )
      (progn
        (if (not (member (vlax-ename->vla-object e) lst))
          (setq lst (cons (vlax-ename->vla-object e) lst))
        )  ; end if
        (redraw e 3)
      )
      (if (= (getvar 'errno) 52)
        (setq done T)
        (prompt "\nNothing selected --")
      )
    )
  )
  (setq lst (reverse lst))
  (setq str (vlax-get (car lst) 'TextString))
  (setq lst (cdr lst))
  (foreach x lst
    (setq str (strcat str "\\P" (vlax-get x 'TextString)))
  )
  (if (setq pt (getpoint "\nLocation: ")) ;if you pick a point will create mtext if not will copy to clipboard
    (entmake (list '(0 . "MTEXT")
                   '(100 . "AcDbEntity")
                   '(100 . "AcDbMText")
                   (cons 10 pt)
                   (cons 1 str)
                   '(71 . 5) ;mid center justify
             )
    )
    (progn
      (setq str (vl-string-subst " " "\\P" str))
      (vlax-invoke (vlax-get (vlax-get (setq html (vlax-create-object "htmlfile")) 'ParentWindow) 'ClipBoardData) 'setData "Text" str)
      (vlax-release-object html)
    )
  )
  (vla-Regen Drawing acactiveviewport)
  (princ)
)

 

  • Like 1
Link to comment
Share on other sites

45 minutes ago, lucky9 said:

Thank you for the code, How do I retain the original text formatting for example color, height, font etc. of selected text.  

 

if they are just text they don't have any formatting. you would have to pull other info like stevenp says. Tricky part is when calculating the text height.

 

You have to look at all text first to see their sizes. if they are different in anyway you then have to inject some math into the mtext string.

 

Using the smallest textsize as the "Base" (cons 40 h)

TextString = "\\C5;\\fConsola.ttf|b1|i0|c0|p0;test\\P\\H2x;\\C1;bigtest"

bigtest is 2x time bigger the orginal test text

 

layer multiple witch one to choose?

same with style if multiple.

Edited by mhupp
Link to comment
Share on other sites

  • 2 months later...
On 26.01.2023 at 22:18, mhupp said:
Это то, к чему я пришел несколько недель назад. будет работать с несколькими строками текста или mtext. создаст mtext в текущей переменной textsize и сохранит форматирование. если вы не выберете точку, текст будет скопирован в буфер обмена.

В этом лиспе при указании точки вставки текста, текст съезжает далеко вправо, можно это исправить? Спасибо.

Возможно ли отдельные тексты  выбирать рамкой (а не по одному) и создавать мтекст в таком же порядке, как расположены отдельные тексты.

Link to comment
Share on other sites

16 minutes ago, Nikon said:

In this script, when specifying the insertion point of the text, the text slides far to the right, can I fix this? Thank you.

Is it possible to select individual texts with a frame (rather than one at a time) and create mtext in the same order as the individual texts.

 

- Yes, is your text justified in some way perhaps, could be right justified? 

- Yes, that should be possible, creating a list of the text as you select it will keep it in the same order. I don't think I'll have time to look at this today though

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...