Text to Mleader

Text to Mleader

Hamza.itani
Contributor Contributor
351 Views
2 Replies
Message 1 of 3

Text to Mleader

Hamza.itani
Contributor
Contributor

Hi, the following code converts text objects to Mleaders, however after each execution, it returns the following line " Select source text: Unknown command "\" " and shows the mleader text as "\". selecting the created Mleader afterwards fixes it. Can anyone help troubleshoot please:

(defun c:t2ml ( / oobj nobj nstrg oheight tScale overallScale oWidth)
  (vl-load-com) ; Load the Visual LISP extensions
  (setq oobj (vlax-ename->vla-object (car (nentsel "\nSelect source text: "))))

  (cond
    ((= (vlax-get-property oobj 'ObjectName) "AcDbMText") ; Check if it's MText
      (setq nstrg (vlax-get-property oobj 'TextString))
      (setq oheight (vlax-get-property oobj 'Height)) ; Get the text height
      (setq oWidth (vlax-get-property oobj 'Width)) ; Get the text width from MText
    )
    ((= (vlax-get-property oobj 'ObjectName) "AcDbText") ; Check if it's DText
      (setq nstrg (vlax-get-property oobj 'TextString))
      (setq oheight (vlax-get-property oobj 'Height)) ; Get the text height
      ; For DText, width is not applicable as DText is a single line of text, hence no width property
    )
    (t
      (exit) ; Exit if it's neither MText nor DText
    )
  )

  (command "_MLEADER") ; Start MLEADER command
  (while (= 1 (logand (getvar "CMDACTIVE") 1)) (command PAUSE)) ; Wait for MLEADER command to finish

  (setq nobj (vlax-ename->vla-object (entlast))) ; Get the last created entity, which should be the MLeader

  (if (= (vlax-get-property nobj 'ObjectName) "AcDbMLeader") ; Check if it's MLeader
    (progn
      (setq tScale (vlax-get-property nobj 'TextHeight)) ; Get the text height of MLeader
      (setq overallScale (/ oheight tScale)) ; Compute the overall scale based on height
      (vla-put-ScaleFactor nobj overallScale) ; Set the overall scale of MLeader
      (vla-put-TextWidth nobj oWidth) ; Set the width of the MLeader to match MText's width
      (vlax-put-property nobj 'TextString nstrg) ; Set the text of MLeader
    )
  )

  (entdel (vlax-vla-object->ename oobj)) ; Delete the original text object

  (princ) ; Clean exit
)

 

0 Likes
Accepted solutions (1)
352 Views
2 Replies
Replies (2)
Message 2 of 3

hosneyalaa
Advisor
Advisor

try

(defun c:t2ml ( / oobj nobj nstrg oheight tScale overallScale oWidth)
  (vl-load-com) ; Load the Visual LISP extensions
  (setq oobj (vlax-ename->vla-object  (setq pten (car (nentsel "\nSelect source text: ")))) )

  (cond
    ((= (vlax-get-property oobj 'ObjectName) "AcDbMText") ; Check if it's MText
      (setq nstrg (vlax-get-property oobj 'TextString))
      (setq oheight (vlax-get-property oobj 'Height)) ; Get the text height
      (setq oWidth (vlax-get-property oobj 'Width)) ; Get the text width from MText
    )
    ((= (vlax-get-property oobj 'ObjectName) "AcDbText") ; Check if it's DText
      (setq nstrg (vlax-get-property oobj 'TextString))
      (setq oheight (vlax-get-property oobj 'Height)) ; Get the text height
      ; For DText, width is not applicable as DText is a single line of text, hence no width property
    )
    (t
      (exit) ; Exit if it's neither MText nor DText
    )
 )
;;;
;;;  (command "_MLEADER") ; Start MLEADER command
;;;  (while (= 1 (logand (getvar "CMDACTIVE") 1)) (command PAUSE)) ; Wait for MLEADER command to finish

    
      (setq exyz (cdr (ASSOC 10 (ENTGET pten))))
  (command "._MLEADER" exyz  (list (+ 0.5 (car exyz)) (+ 0.2 (cadr exyz)) (caddr exyz)) "")
    

  (setq nobj (vlax-ename->vla-object (entlast))) ; Get the last created entity, which should be the MLeader

  (if (= (vlax-get-property nobj 'ObjectName) "AcDbMLeader") ; Check if it's MLeader
    (progn
      (setq tScale (vlax-get-property nobj 'TextHeight)) ; Get the text height of MLeader
      (setq overallScale (/ oheight tScale)) ; Compute the overall scale based on height
      (vla-put-ScaleFactor nobj overallScale) ; Set the overall scale of MLeader
      ;(vla-put-TextWidth nobj oWidth) ; Set the width of the MLeader to match MText's width
      (vlax-put-property nobj 'TextString nstrg) ; Set the text of MLeader
    )
  )

  (entdel (vlax-vla-object->ename oobj)) ; Delete the original text object

  (princ) ; Clean exit
)

 

7.gif

Message 3 of 3

komondormrex
Mentor
Mentor
Accepted solution

hey,

check the following

(defun c:t2ml ( / oobj nobj nstrg oheight tScale overallScale oWidth)
  (vl-load-com) ; Load the Visual LISP extensions
  (setq oobj (vlax-ename->vla-object (car (nentsel "\nSelect source text: "))))

  (cond
    ((= (vlax-get-property oobj 'ObjectName) "AcDbMText") ; Check if it's MText
      (setq nstrg (vlax-get-property oobj 'TextString))
      (setq oheight (vlax-get-property oobj 'Height)) ; Get the text height
      (setq oWidth (vlax-get-property oobj 'Width)) ; Get the text width from MText
    )
    ((= (vlax-get-property oobj 'ObjectName) "AcDbText") ; Check if it's DText
      (setq nstrg (vlax-get-property oobj 'TextString))
      (setq oheight (vlax-get-property oobj 'Height)) ; Get the text height
      ; For DText, width is not applicable as DText is a single line of text, hence no width property
    )
    (t
      (exit) ; Exit if it's neither MText nor DText
    )
  )

  (command "_MLEADER" (getpoint) pause nstrg) ; draw MLEADER

  (setq nobj (vlax-ename->vla-object (entlast))) ; Get the last created entity, which should be the MLeader

  (if (= (vlax-get-property nobj 'ObjectName) "AcDbMLeader") ; Check if it's MLeader
    (progn
      (setq tScale (vlax-get-property nobj 'TextHeight)) ; Get the text height of MLeader
      (setq overallScale (/ oheight tScale)) ; Compute the overall scale based on height
      (vla-put-ScaleFactor nobj overallScale) ; Set the overall scale of MLeader
      (vl-catch-all-apply 'vla-put-TextWidth (list nobj oWidth)) ; Set the width of the MLeader to match MText's width
    )
  )

  (vla-erase oobj) ; Delete the original text object
  (princ) ; Clean exit
)