Author Topic: mtext fractions  (Read 3877 times)

0 Members and 1 Guest are viewing this topic.

paul_s

  • Guest
mtext fractions
« on: June 19, 2009, 09:32:48 AM »
Hello,

We been using this special font file to do fractions, but get into
trouble when we forget the send that font file with the drawings.

We've decided to stop using that font & use mtext instead. I'm trying
to replace my fraction lisp routines and need a little help.
These are examples for "1/2" and "1 1/2" fractions.

Can somebody please show me the "mtext" way?

(defun c:one-half ( / pt)     ;1/2"
(setq pt (getpoint"\nSpecify start point of text: "))
(command "text" pt "" "" (strcat "%%158"(chr 34)))
)

(defun c:one&one-half ( / pt)  ;1 1/2"
(setq pt (getpoint"\nSpecify start point of text: "))
(command "text" pt "" "" (strcat "1%%158"(chr 34)))
)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: mtext fractions
« Reply #1 on: June 19, 2009, 10:23:42 AM »
This may help you - I'm not sure if its the correct way to do it, but I wrote it a while back

Code: [Select]
(defun stacknum (num den typ)
  (cond ((= typ "D")
(setq txt (strcat "\{\\H0.7x;\\S" (itoa num) "#" (itoa den) ";\}")))
((= typ "H")
(setq txt (strcat "\{\\H0.7x;\\S" (itoa num) "/" (itoa den) ";\}")))))

(defun c:Fractionizer  (/ ss n1 n2 eLst txt ans)
  (if (and (setq ss (ssget '((0 . "MTEXT"))))
   (setq n1 (getint "\nSpecify Numerator: "))
   (setq n2 (getint "\nSpecify Denominator: ")))
    (progn
      (initget 1 "D H")
      (setq ans (getkword "\nStacked Horizontally or Vertically? [H/D] : "))
      (cond ((= ans "H")
     (stacknum n1 n2 "H"))
    ((= ans "D")
     (stacknum n1 n2 "D")))
      (setq eLst (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss))))
      (foreach e  eLst
(entmod (subst (cons 1 txt) (assoc 1 (entget e)) (entget e)))))
    (princ "\n<!> No Text Selected! <!>"))
  (princ))

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: mtext fractions
« Reply #2 on: June 19, 2009, 10:33:18 AM »
Another way. 8-)
Code: [Select]
(defun c:m1/2 (/ pt)      ;1/2"
  (setq pt (getpoint "\nSpecify start point of text: "))
  (MakeMtext pt "{\\H0.5x;\\S1/2;}\"")
)

(defun c:m11/2 (/ pt)  ;1 1/2"
  (setq pt (getpoint "\nSpecify start point of text: "))
  (MakeMtext pt "1{\\H0.5x;\\S1/2;}\"")
)


(defun MakeMtext (pt str)
  (entmakex
    (list
      '(0 . "MTEXT")
      '(100 . "AcDbEntity")
      '(100 . "AcDbMText")
      (cons 10 pt)
      (cons 1 str)
    )
  )
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

hermanm

  • Guest
Re: mtext fractions
« Reply #3 on: June 19, 2009, 10:38:48 AM »
Here is what I use:

Code: [Select]
(defun tktn_make_scaled_mtext1 (textval textlyr inspt width attach rot / ent)
  (entmake
    (list
        '(0 . "MTEXT")
        '(100 . "AcDbEntity")
         (cons 8  textlyr)
        '(100 . "AcDbMText")
         (cons 10 inspt)
         (cons 40 (* (tktn_getscl) (getvar "DIMTXT")))
         (cons 41 width)
         (cons 71 attach)
        '(72 . 5)
         (cons 1 textval)
         (cons 7 (getvar "TEXTSTYLE"))
        '(210 0.0 0.0 1.0)
        '(11 1.0 0.0 0.0)
         (cons 50 rot)
    )
  )
inspt;return insertion point
);tktn_make_scaled_mtext1


Here is the scaling function:

Code: [Select]
;;--------------------- tktn_getscl--------------------------------------
;;      Purpose: calculate scale factor
;;      Uses:    nothing
;;      Returns: sf
;;-----------------------------------------------------------------
(defun tktn_getscl( )
  (cond ((= 1 (getvar "TILEMODE")) (getvar "DIMSCALE"));tiled space
        ((= 1 (getvar "CVPORT")) 1.0);in paper space
     ;; in a viewport
        (T
          (last (trans '(0 0 1.0) 3 2));calc scale factor
        )
  );cond
);end tktn_getscl

Convert real to MTEXT string:

Code: [Select]
;;--------------rtomtftin.lsp-----------------------------
;;; Author:  Herman Mayfarth
;;; Date:    7 May 2003
;;; Purpose: convert real to MTEXT representation of ft & inches
;;;          w/ choice of fraction style
;;; Params:  num: real number to convert to MTEXT string
;;;          fracht: real specifying the relative ht. of fractions
;;;          barsty: string specifying fraction bar style
;;;                  this must be one of '("/" "^" "#")
;;; Returns: string as above if successful, nil otherwise
;;; Calling function must provide the vertical alignment code
;;;------------------------------------------------------
(defun rtomtftin (num fracht barsty /
                             add
                             count
                             curent
                             instrg
                             outstrg
                             start_format
                             suffix
                )
(setq instrg (rtos num 4 4))
(if (member barsty '("/" "^" "#"));character is allowed
(progn ;call the function
;;three cases exist:
;;Case 1:
;;if we have > 1" and have a fraction, the string will contain a space before
;;the fraction, and we need to substitute the start of the MTEXT format for
;;the space character
;;Case 2:
;;we have >1" but no fraction => no / will be found, and no MTEXT formatting
;;Case 3:
;;we have a fraction <1" => MTEXT formatting prefixes the input string
;setup the format string for the start of the MTEXT fraction
(setq start_format (strcat "{\\H" (rtos fracht 2 2) "x;\\S")
;initialize a counter for the position to test in the input string
      count 1
      suffix 0
;and bind a null string to a symbol for the output string
      outstrg "")
;now walk down the input string one character at a time
 (repeat (strlen instrg)
;set a flag to indicate the current character should be kept
    (setq add 1
;and get the current character from the input string
          curent (substr instrg count 1))
;;first, test for a space character
      (cond ((= curent " ");found a space (Case 1),
;; so substitute the fraction format, clear the add flag, and set a flag
;; to let us know we need to close the format at the end
             (setq outstrg (strcat outstrg start_format) add nil suffix 1))
;;else test for fraction bar
            ((= curent "/") ;found a fraction bar
          (progn
;;;test for suffix
            (if (= suffix 0) (setq suffix 2));need a prefix
;;substitute the value of barsty if necessary, clear the add flag
            (if (/= barsty "/");rtos always uses /
             (setq outstrg (strcat outstrg barsty) add nil )
            )
          );progn
        );pred
        );cond
    ;if add is bound, there was no substitution, so just add the
    ;current character to the front of the output string
    (if add (setq outstrg (strcat outstrg curent)))
    (setq count (1+ count))
 );repeat
;;if we found a fraction, we need to close the MTEXT format
  (cond ((= suffix 1) (setq outstrg (strcat outstrg ";}")))
        ((= suffix 2) (setq outstrg (strcat start_format outstrg ";}")))
        (T nil);otherwise, do nothing
  )
 outstrg
);progn
;;do nothing, disallowed separator
);if
);rtomtftin


/code]

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: mtext fractions
« Reply #4 on: June 19, 2009, 10:49:51 AM »
Changed my mind, I like this better.
Code: [Select]
(defun c:m1/2 (/ pt)      ;1/2"
  (setq pt (getpoint "\nSpecify start point of text: "))
  (MakeMtext pt "" "1/2")
)

(defun c:m11/2 (/ pt)  ;1 1/2"
  (setq pt (getpoint "\nSpecify start point of text: "))
  (MakeMtext pt "1" "1/2")
)


(defun MakeMtext (pt hole fract)
  (entmakex
    (list
      '(0 . "MTEXT")
      '(100 . "AcDbEntity")
      '(100 . "AcDbMText")
      (cons 10 pt)
      (cons 1 (strcat hole "{\\H0.5x;\\S" fract ";}\""))
    )
  )
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

paul_s

  • Guest
Re: mtext fractions
« Reply #5 on: June 19, 2009, 10:53:31 AM »
Wow...Thank you everyone!!!!

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: mtext fractions
« Reply #6 on: June 19, 2009, 01:26:27 PM »
Just as a side note, as a general rule:

A text string in MTEXT in the format:

Code: [Select]
"\{\\H[color=blue]0.7[/color]x;\\S" [color=red]<Numerator>[/color] "[color=green]#[/color]" [color=red]<Denominator>[/color] ";\}"

Will return a Diagonally Stacked Fraction, whereas:

Code: [Select]
"\{\\H[color=blue]0.7[/color]x;\\S" [color=red]<Numerator>[/color] "[color=green]/[/color]" [color=red]<Denominator>[/color] ";\}"

Will return a Horizontally Stacked Fraction. The 0.7 means that the fraction text is 0.7x the height of the text before it.

paul_s

  • Guest
Re: mtext fractions
« Reply #7 on: June 23, 2009, 01:10:02 PM »
One more question..the mtext created with the sample code below
defaults to "top left" justification. Is there a way to make the
justification "middle center"?

(entmakex
    (list
     '(0 . "MTEXT")
     '(100 . "AcDbEntity")
     '(100 . "AcDbMText")
     (cons 7 (getvar "TEXTSTYLE"))
    (cons 10 .....)
    (cons 1 .....)
   )
)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: mtext fractions
« Reply #8 on: June 23, 2009, 01:14:05 PM »

paul_s

  • Guest
Re: mtext fractions
« Reply #9 on: June 23, 2009, 01:24:40 PM »
Thanks for link Lee. This is really very helpful for me!

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: mtext fractions
« Reply #10 on: June 23, 2009, 01:33:03 PM »
No problem Paul, the full document can be found here for future reference:

http://autodesk.com/techpubs/autocad/acad2000/dxf/