vla-Explode and MText?

Discussion in 'AutoCAD' started by T.Willey, Mar 4, 2005.

  1. T.Willey

    T.Willey Guest

    I have this sub-routine that get the actual bounding box for mtext, but I have to explode it to get it, and I want it to work without commands, and I haven't seen any way to explode it but with a command. I haven't found any way around it, and was wondering if anyone has, or if it can be done another way because the vla-GetBoundingBox seems to only get the box that is picked for the mtext, not the actual extents of the mtext.

    Thanks
    Tim
    ps. Here is the code I'm talking about.

    (defun GetMtextBB (Obj / LX LY tmpObj tmpEnt tmpList tmpOBj2 tmpLL tmpUR tmpLX tmpLY tmpUX tmpUY)

    (setq tmpObj (vla-Copy Obj))
    (setq tmpEnt (entlast))
    (command "_.explode" tmpEnt)
    (while (setq tmpEnt (entnext tmpEnt))
    (setq tmpList (cons tmpEnt tmpList))
    )
    (foreach Ent tmpList
    (setq tmpObj2 (vlax-ename->vla-object Ent))
    (vla-GetBoundingBox tmpObj2 'tmpLL 'tmpUR)
    (setq tmpLL (safearray-value tmpLL))
    (setq tmpUR (safearray-value tmpUR))
    (setq tmpLX (car tmpLL))
    (setq tmpLY (cadr tmpLL))
    (setq tmpUX (car tmpUR))
    (setq tmpUY (cadr tmpUR))
    (if LX
    (if (< tmpLX LX)
    (setq LX tmpLX)
    )
    (setq LX tmpLX)
    )
    (if LY
    (if (< tmpLY LY)
    (setq LY tmpLY)
    )
    (setq LY tmpLY)
    )
    (if UX
    (if (> tmpUX UX)
    (setq UX tmpUX)
    )
    (setq UX tmpUX)
    )
    (if UY
    (if (> tmpUY UY)
    (setq UY tmpUY)
    )
    (setq UY tmpUY)
    )
    (vla-Delete (vlax-ename->vla-object Ent))
    )
    (list (list LX LY) (list UX LY) (list UX UY) (list LX UY))
    )
     
    T.Willey, Mar 4, 2005
    #1
  2. T.Willey

    OLD-CADaver Guest

    DXF codes 42 and 43 are the width and height of the actual text within the mtext bounding box.

    (setq bxr (entget (entlast))
    ins (cdr (assoc 10 bxr))
    txj (cdr (assoc 71 bxr))
    txr (cdr (assoc 50 bxr))
    addr (* 0.09 (getvar "dimscale"))
    lg (+ addr (/ (cdr (assoc 42 bxr)) 2.0))
    ht (+ addr (/ (cdr (assoc 43 bxr)) 2.0))
    )
     
    OLD-CADaver, Mar 4, 2005
    #2
  3. T.Willey

    T.Willey Guest

    Cool. Thanks OC.

    Tim
     
    T.Willey, Mar 4, 2005
    #3
  4. T.Willey

    Gary Fowler Guest

    Will this help?

    Gary

    Code:
    ;;;This routine will work with individual Text, Attributes or Dimensions.
    ;;;Tom Beauford
    (defun BOXXIT  (/ myerror osm olderr txt elist e ss tb of ll ur boxsize
    boxsizex boxsizemsg)
    (defun myerror  (s) ; If an error (such as CTRL-C)
    occurs
    (if (/= s "Function cancelled") ; while this command is
    active...
    (princ (strcat "\nError: " s)))
    (setvar "qaflags" 0)
    (setvar "osmode" osm)
    (setq *error* olderr) ; Restore old *error* handler.
    (princ))
    (setq olderr  *error*
    *error* myerror
    osm     (getvar "osmode"))
    (setvar "cmdecho" 0)
    (setvar "osmode" 0)
    (setq txt 1)
    (prompt "\n* BOXX: Select Dtext, Mtext, Attribute or Dimension *")
    ;|
    (setq boxsizex (getreal "\n* Enter Box size: <2 small> thru <15 large>
    <default = 6>"))
    (cond
    ((/= boxsizex nil)
    (progn
    (setq boxsize (/ 12 boxsizex))
    ;;(setq boxsizemsg (strcat "\n* Box size = " (rtos boxsizex 2 0) "
    *"))
    ;;(princ boxsizemsg)
    )
    )
    ((= boxsize nil)
    (progn
    (setq boxsize 2)
    ;;(setq boxsizemsg (strcat "\n* Box size = 6 * Now Select Dtext,
    Attribute or Dimension:"))
    ;;(princ boxsizemsg)
    )
    )
    )
    |;
    (setq boxsize 1.5)
    (while txt
    (if (setq txt (entsel ""))
    (setq elist (entget (car txt))
    e     (cdr (assoc 0 elist)))
    (setq txt nil
    e   nil))
    (cond ((= e "INSERT")
    (progn (setq txt   (nentselp "" (cadr txt))
    elist (entget (car txt))
    e     (cdr (assoc 0 elist)))
    (if (= e "TEXT")
    (setq e nil))))
    ((= e "DIMENSION")
    (progn (command "undo" "Mark")
    (setq ss (cadr txt))
    (setvar "qaflags" 128)
    (command "EXPLODE" ss)
    (command "explode" ss)
    (setq txt   (nentselp "" (cadr txt))
    elist (cdr (entget (car txt)))
    e     (cdr (assoc 0 elist)))
    (command "undo" "Back")
    (entmake elist)
    (setq elist (entget (entlast))
    e     (cdr (assoc 0 elist))
    txt   (list (cdar elist) "DIMENSION"))))
    ((= e "ATTRIB")
    (setq elist (subst (cons 73 (cdr (assoc 74 elist))) (assoc 74
    elist) elist)
    elist (subst (cons 0 "TEXT") (assoc 0 elist) elist)
    e     "TEXT"))
    ;;(if (= e "MTEXT")(setq MTxt (entget (car txt)))(Mtext-Boxit))
    ((= e "MTEXT") (setq bxr (entget (car txt))) (MtextBoxit))
    ((= e "TEXT")
    (progn (command "undo" "BEgin")
    (command "ucs" "OBject" (car txt))
    (setq tb (textbox elist)
    of (/ (cadr (cadr tb)) boxsize)
    ll (list (- (car (car tb)) of) (- (cadr (car tb))
    of))
    ur (list (+ (car (cadr tb)) of) (+ (cadr (cadr tb))
    of)))
    (if (= (cadr txt) "DIMENSION")
    (entdel (entlast)))
    (command "rectang" ll ur)
    (command "ucs" "p")
    (command "undo" "End")))))
    (setvar "osmode" osm)
    (setq *error* olderr) ; Restore old *error* handler.
    (princ))
    
     
    Gary Fowler, Mar 4, 2005
    #4
  5. T.Willey

    T.Willey Guest

    Something came up, so I can't look right now, but I will and I do appreciate it.

    Thanks.
    Tim
     
    T.Willey, Mar 4, 2005
    #5
  6. T.Willey

    T.Willey Guest

    Here is how I ended up doing it. Thanks to everyone that posted.

    Tim

    (defun GetMTextBB (Ent / EntData EnsPt TxtJust TxtWd TxtHt TxtRot LeftX RightX TopY BottomY)
    ; Get real Bounding Box for MText
    ; Tim Willey 03/2005
    ; Thanks to OLD-CADaver for the tips on the DXF codes

    (setq EntData (entget Ent))
    (setq InsPt (value 10 EntData))
    (setq TxtJust (value 71 EntData))
    (setq TxtWd (value 42 EntData))
    (setq TxtHt (value 43 EntData))
    (setq TxtRot (value 50 EntData))
    (cond
    ((or (= TxtJust 1) (= TxtJust 4) (= TxtJust 7))
    (setq LeftX (car InsPt))
    (setq RightX (car (polar InsPt TxtRot TxtWd)))
    )
    ((or (= TxtJust 2) (= TxtJust 5) (= TxtJust 8))
    (setq LeftX (car (polar InsPt TxtRot (/ TxtWd 2))))
    (setq RightX (car (polar InsPt (+ (DTR 180) TxtRot) (/ TxtWd 2))))
    )
    ((or (= TxtJust 3) (= TxtJust 6) (= TxtJust 9))
    (setq LeftX (car (polar InsPt (+ (DTR 180) TxtRot) TxtWd)))
    (setq RightX (car InsPt))
    )
    )
    (cond
    ((or (= TxtJust 1) (= TxtJust 2) (= TxtJust 3))
    (setq TopY (cadr InsPt))
    (setq BottomY (cadr (polar InsPt (+ (DTR 270) TxtRot) TxtHt)))
    )
    ((or (= TxtJust 4) (= TxtJust 5) (= TxtJust 6))
    (setq TopY (cadr (polar InsPt (+ (DTR 90) TxtRot) (/ TxtHt 2))))
    (setq BottomY (cadr (polar InsPt (+ (DTR 270) TxtRot) (/ TxtHt 2))))
    )
    ((or (= TxtJust 7) (= TxtJust 8) (= TxtJust 9))
    (setq TopY (cadr (polar InsPt (+ (DTR 90) TxtRot) TxtHt)))
    (setq BottomY (cadr InsPt))
    )
    )
    (list (list LeftX BottomY) (list RightX BottomY) (list RightX TopY) (list LeftX TopY))
    )
     
    T.Willey, Mar 5, 2005
    #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.