Results 1 to 10 of 13

Thread: Mtext Command in Lisp

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Active Member
    Join Date
    2015-11
    Location
    Sydney Australia
    Posts
    91
    Login to Give a bone
    0

    Default Mtext Command in Lisp

    I am trying to write a lisp routine that converts multiple text entities to multiple mtext entities while preserving the insertion point (express tools txt2mtxt wont do this)

    I dont know much about lisp ... but have managed to write something that works for a single entity ...

    Rather than try to change the text to mtext i thought it would be easier to extract all the relevant information from the text entity, erase it and use the command function to create the mtext, however, it crashes after the first entity because i am not able to end the mtext command.

    Any help would be greatly appreciated


    Code:
    ;;;
    ;;; convert multiple text entities to multiple mtext entities maintaining the insertion point
    ;;;
    
    ;;; basically create a selection set of the entities to change, extract all the relevant information 
    ;;;    ie size, style, rotation angle, insertion point, justification
    ;;; erase the text and create a piece of mtext
    ;;;
    
    
    
    
    (defun C:Tx2M (/ ss itm elist H V hnd ent ins)
    
    
    ;;;
    ;;;--------------------------------------
    ;;; convert acad polar angle to a bearing
    ;;; polar angle must be in decimal degrees
    ;;;______________________________________
    ;;;
    (defun polar_ang_to_bng (a)
      (setq a (- 450 a) )
      (if (>= a 360)
             (setq a (- a 360)) (setq a a) 
      ) ;if
    ) ; 
    
    
    
    
    	(prompt "\nSelect Text objects to convert to MText: ")
    	(setq ss (ssget (list (cons 0 "TEXT"))))
    	(setq itm 0)
    	(if	ss
    		(repeat	(sslength ss)
    			
    			(setq hnd (ssname ss itm)) ; get the handle for itm
    			(setq ent (entget hnd))		 ; 
    
    			(setq 
    		    en_type   (cdr (assoc 0  ent))
    		    en_txt    (cdr (assoc 1  ent))	; the text value
    		    en_lyr    (cdr (assoc 8  ent))	; the layer the text is in
    		    en_size   (cdr (assoc 40 ent))	; text size
    		    en_ang    (cdr (assoc 50 ent))	; text angle
    		    en_style  (cdr (assoc 7  ent))	; text style
    				
    				en_H 			(cdr (assoc 72 ent)) ; get the horizontal alignment of the text
    				en_V			(cdr (assoc 73 ent)) ; get the vertical alignment of the text
    			); setq
    	
    			(if (and (= en_H 0) (= en_V 0))	  ; if the horiz and vert just = 0 then 
    			  (setq en_ins (cdr (assoc 10 ent))) ; we have the default insertion point 
    			  (setq en_ins (cdr (assoc 11 ent))) ; otherwise get the insertion point
    			)
    			
    
    			(setq en_just "")
    
    			(cond
    					( (= en_V 3) (setq en_just "T" ) )
    					( (= en_V 2) (setq en_just "M" ) )
    					( (= en_V 1) (setq en_just "B" ) )
    			)
    
    			(cond
    					( (= en_H 0) (setq en_just (strcat en_just "L" )) )
    					( (= en_H 1) (setq en_just (strcat en_just "C" )) )
    					( (= en_H 2) (setq en_just (strcat en_just "R" )) )
    			)
    
    ;
    ; angle is POLAR in radians - 0 horizontal increase anticlockwise
    ; convert to degrees & bearings - 0 vertical increase clockwise
    ;
    		  	(setq en_ang (/ (* en_ang 180.0) Pi) )
    		  
    			(setq en_ang (Polar_ang_to_Bng en_ang)) ; convert polar to bearing
    
    			(setq en_ang (- en_ang 90.0)) ; subtract 90 because mtext is different 
    
    		  
    		  
    		  	;(while (> en_ang 180.0) (setq en_ang (- en_ang 180.0)))
    			  
    
    			
    
    			(command "ERASE" hnd "")
    			(command "Layer" "Set" en_lyr "")
    			(command "MTEXT" en_ins "Height" en_size "Justify" en_just "Rotation" en_ang "Style" en_style "Width" "0" en_txt "")
    			
    		); repeat
    		
    	); if
      
    ); function
    Greg B

  2. #2
    100 Club
    Join Date
    2003-11
    Location
    Dublin, Ireland.
    Posts
    152
    Login to Give a bone
    0

    Default Re: Mtext Command in Lisp

    This seems to work now.
    I don't know how to color the revisions that i made, but I put comments on them.



    Code:
    ;;;
    ;;; convert multiple text entities to multiple mtext entities maintaining the insertion point
    ;;;
    
    ;;; basically create a selection set of the entities to change, extract all the relevant information 
    ;;;    ie size, style, rotation angle, insertion point, justification
    ;;; erase the text and create a piece of mtext
    ;;;
    
    
    
    
    (defun C:Tx2M ();;/ ss itm elist H V hnd ent ins)
    
    
    ;;;
    ;;;--------------------------------------
    ;;; convert acad polar angle to a bearing
    ;;; polar angle must be in decimal degrees
    ;;;______________________________________
    ;;;
    (defun polar_ang_to_bng (a)
      (setq a (- 450 a) )
      (if (>= a 360)
             (setq a (- a 360)) (setq a a) 
      ) ;if
    ) ; 
    
    
    
    
    	(prompt "\nSelect Text objects to convert to MText: ")
    	(setq ss (ssget (list (cons 0 "TEXT"))))
    	(setq itm 0)
    	(if	ss
    		(repeat	(sslength ss)
    			
    ;;;			(setq hnd (ssname ss itm)) ; get the handle for itm
    ;;;			(setq ent (entget hnd))		 ;
    		  
    (setq ent (entget (ssname ss itm)))   ;;Get the entity
    (setq EntName (cdr (assoc -1 ent)))  ;;Get the entity name
    
    		  
    			(setq
    			  en_type  (cdr (assoc 0 ent))
    			  en_txt   (cdr (assoc 1 ent)) ; the text value
    			  en_lyr   (cdr (assoc 8 ent))
    					; the layer the text is in
    			  en_size  (cdr (assoc 40 ent)) ; text size
    			  en_ang   (cdr (assoc 50 ent)) ; text angle
    			  en_style (cdr (assoc 7 ent)) ; text style
    
    			  en_H	   (cdr (assoc 72 ent))
    					; get the horizontal alignment of the text
    			  en_V	   (cdr (assoc 73 ent))
    					; get the vertical alignment of the text
    			)		; setq
    	
    			(if (and (= en_H 0) (= en_V 0))	  ; if the horiz and vert just = 0 then 
    			  (setq en_ins (cdr (assoc 10 ent))) ; we have the default insertion point 
    			  (setq en_ins (cdr (assoc 11 ent))) ; otherwise get the insertion point
    			)
    			
    
    			(setq en_just "")
    
    			(cond
    					( (= en_V 3) (setq en_just "T" ) )
    					( (= en_V 2) (setq en_just "M" ) )
    					( (= en_V 1) (setq en_just "B" ) )
    			  		( (= en_V 0) (setq en_just "B" ) )   ;;Check for value = 0
    			)
    
    			(cond
    					( (= en_H 0) (setq en_just (strcat en_just "L" )) )
    					( (= en_H 1) (setq en_just (strcat en_just "C" )) )
    					( (= en_H 2) (setq en_just (strcat en_just "R" )) )
    			)
    
    ;
    ; angle is POLAR in radians - 0 horizontal increase anticlockwise
    ; convert to degrees & bearings - 0 vertical increase clockwise
    ;
    		  	(setq en_ang (/ (* en_ang 180.0) Pi) )
    		  
    			(setq en_ang (Polar_ang_to_Bng en_ang)) ; convert polar to bearing
    
    			(setq en_ang (- en_ang 90.0)) ; subtract 90 because mtext is different 
    
    		  
    		  
    		  	;(while (> en_ang 180.0) (setq en_ang (- en_ang 180.0)))
    			  
    
    			
    ;;;
    ;;;			(command "ERASE" hnd "")    ;Take this line out
    			(command "Layer" "Set" en_lyr "")
    			(command "-MTEXT" en_ins "Height" en_size "Justify" en_just "Rotation" en_ang "Style" en_style "Width" "0" en_txt "")
    ;;Use the command line version of Mtext
    		  
    		  (entdel EntName)   ;;Delete the old TEXT entity
    		  (setq itm (1+ itm))  ;;Increase the counter
    			
    		); repeat
    		
    	); if
      
    ); function

  3. #3
    Active Member
    Join Date
    2015-11
    Location
    Sydney Australia
    Posts
    91
    Login to Give a bone
    0

    Default Re: Mtext Command in Lisp

    Quote Originally Posted by jmcshane View Post
    This seems to work now.
    I don't know how to color the revisions that i made, but I put comments on them.
    Thanks
    ... Did not think of trying the command line version of Mtext, actually did not know there was one, AND incrementing the counter helps a lot also ...

    Thanks again for the help

    GregB

  4. #4
    Active Member
    Join Date
    2015-11
    Location
    Sydney Australia
    Posts
    91
    Login to Give a bone
    0

    Default Re: Mtext Command in Lisp

    I still have one problem ... the mtext command seems to be ignoring the text size and uses whatever the current mtext size is

    ie current mtext size =5 size of chosen text entity = 10 ... then the new mtext has a size of 5 not 10


    GregB

  5. #5
    Certifiable AUGI Addict
    Join Date
    2001-03
    Location
    Tallahassee, FL USA
    Posts
    3,667
    Login to Give a bone
    0

    Default Re: Mtext Command in Lisp

    Maybe the text size is specifyed in the Text Style? Height is set to 0 in all my styles and I've never seen that problem.

    Try this multiple text entities to multiple mtext entities routine:
    Code:
     (defun c:mtxt2mtxt( / *error* ss ss1)
      (defun *error* (msg)
        (if docobj (vla-endundomark docobj))
        (cond
          ((member msg
             '("Function cancelled"
               "quit / exit abort"
               "console break"
              )
           )
          )
          ((princ (strcat " Error: " msg)))
        )
        (princ)
      )
      (prompt "\nSelect Text items to convert to Mtext.")
      (setq ss(ssget '((0 . "Text")))
          count(sslength ss)
        docobj(vlax-get-property (vlax-get-acad-object) "ActiveDocument")
      )
      (vla-startundomark docobj)
      (while (>= count 0)
        (setq ss1(ssname ss count)
              count(- count 1)
        )
        (command "txt2mtxt" ss1 "")
        (princ)
      )
      (vla-endundomark docobj)
      (princ)
    )

  6. #6
    Active Member
    Join Date
    2015-11
    Location
    Sydney Australia
    Posts
    91
    Login to Give a bone
    0

    Default Re: Mtext Command in Lisp

    Quote Originally Posted by Tom Beauford View Post
    Maybe the text size is specifyed in the Text Style? Height is set to 0 in all my styles and I've never seen that problem.
    The Height is set to 0 for the style ....

    I was clutching at straws and eventually changed the position in the command where I set the height

    Originally was ...
    (command "-MTEXT" en_ins "Height" en_size "Justify" en_just "Rotation" en_ang "Style" en_style "Width" "0" en_txt "")

    Then changed it to ....
    (command "-MTEXT" en_ins "Justify" en_just "Rotation" en_ang "Style" en_style "Height" en_size "Width" "0" en_txt "")

    and for some unknown reason it now works.


    Thanks for your help ... much simpler coding with error trapping also

    guess i should try and learn visual lisp

Similar Threads

  1. Move MText at a certain Z value LISP
    By djin_fre3449006 in forum AutoLISP
    Replies: 5
    Last Post: 2013-11-28, 08:17 AM
  2. Lisp and Annotative MTEXT
    By bowtle in forum AutoLISP
    Replies: 1
    Last Post: 2010-02-20, 06:01 PM
  3. Replies: 3
    Last Post: 2009-10-22, 10:19 PM
  4. Mtext in LISP
    By ticad02 in forum AutoLISP
    Replies: 4
    Last Post: 2009-04-21, 01:59 PM
  5. Replies: 6
    Last Post: 2007-04-23, 01:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •