Automated Centering of Text

Discussion in 'AutoCAD' started by colin1234, Sep 14, 2004.

  1. colin1234

    colin1234 Guest

    Can anyone help with some programming code to automatically centre text vertically and horizontally by the use of one click.

    Thanks in advance for your help
     
    colin1234, Sep 14, 2004
    #1
  2. colin1234

    Jürg Menzi Guest

    Hi Colin
    Do you wish to change the text alignment to Center or MiddleCenter (1 click),
    or moving the text 'centered' to a selectable point (2 clicks)?

    Cheers
     
    Jürg Menzi, Sep 14, 2004
    #2
  3. colin1234

    colin1234 Guest

    Middle Centered please,

    Thank you for your help
     
    colin1234, Sep 14, 2004
    #3
  4. colin1234

    Jürg Menzi Guest

    Hi Colin

    Try this (untested):
    Code:
    (defun C:CenterText ( / AcaDoc CurEnt CurObj TmpPnt)
    (vl-load-com)
    (setq CurEnt (car (entsel "\nSelect Text: ")))
    (if Curent
    (progn
    (setq CurObj (vlax-ename->vla-object CurEnt)
    AcaDoc (vla-get-ActiveDocument (vlax-get-acad-object))
    )
    (if (eq (vla-get-ObjectName CurObj) "AcDbText")
    (progn
    (vla-StartUndoMark AcaDoc)
    (setq TmpPnt (vla-get-InsertionPoint CurObj))
    (vla-put-Alignment CurObj acAlignmentMiddleCenter)
    (vla-put-TextAlignmentPoint CurObj TmpPnt)
    (vla-EndUndoMark AcaDoc)
    )
    )
    )
    (princ "selected object is not a text. ")
    )
    (princ)
    )
    
    Cheers
     
    Jürg Menzi, Sep 14, 2004
    #4
  5. colin1234

    Rick Keller Guest

    Try this....

    (defun c:jcm (/ e p11 p10 horn vertn vert hor en y)
    (setq e (entget (car (entsel)))
    p11 (assoc 11 e)
    p10 (assoc 10 e)
    hor (assoc 72 e)
    vert (assoc 73 e)
    en e
    )
    (if (= (cdr hor) 0)
    (setq p11n (cons 11 (cdr p10)))
    )
    (if (= (cdr hor) 0)
    (setq en (subst p11n p11 en))
    )
    (setq horn 1
    horn (cons 72 horn)
    en (subst horn hor en)
    )
    (setq vertn 2
    vertn (cons 73 vertn)
    en (subst vertn vert en)
    )


    (entmod en)
    (setq y "y")
    (if (= (strcase y) "N")
    (entmod e)
    )
    (c:jcm)
    )


    Rick
     
    Rick Keller, Sep 14, 2004
    #5
  6. colin1234

    Jürg Menzi Guest

    Hi Colin

    Improved and tested:
    Code:
    (defun C:CenterText ( / AcaDoc CurEnt CurObj ExLoop TmpPnt)
    (vl-load-com)
    (setq AcaDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
    (while (not ExLoop)
    (initget " ")
    (setq CurEnt (entsel "\nSelect text to center <exit>: "))
    (cond
    ((eq Curent "") (setq ExLoop T))
    (CurEnt
    (setq CurObj (vlax-ename->vla-object (car CurEnt)))
    (if (not (eq (vla-get-ObjectName CurObj) "AcDbText"))
    (princ "selected object is not a text. ")
    (if (= (vla-get-Alignment CurObj) acAlignmentMiddleCenter)
    (princ "selected object is already centered. ")
    (progn
    (vla-StartUndoMark AcaDoc)
    (setq TmpPnt (vla-get-InsertionPoint CurObj))
    (vla-put-Alignment CurObj acAlignmentMiddleCenter)
    (vla-put-TextAlignmentPoint CurObj TmpPnt)
    (vla-EndUndoMark AcaDoc)
    )
    )
    )
    )
    ((princ "1 selected, 0 found. "))
    )
    )
    (princ)
    )
    
    Cheers
     
    Jürg Menzi, Sep 15, 2004
    #6
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.