Jump to content

Lisp -text millimeters to feet


Hsanon

Recommended Posts

hi

i'd worked on this routine many years ago,and have been using it consistently, but now i want to upgrade it. However,i have lost my touch at programming :( age is showing...

basically it was a routine to replace written text numbers in millimeters to feet /inch. to upgrade it i would like to choose a big selection of the drawing, and filter out the text with numbers 1*,2*,3* etc, and x*,X* . then convert these to real numbers , multiply them by the factor and replace them as text strings in feet and inches. this routine will make my life easier....

 

;*****************************************************************
   (defun calcu () 
   (setq txt (atof txt))
   (setq old1 (* txt 0.039372))
   (setq old2 (rtos old1 4 0))
   )
   (defun revx ()
   (if (or (= xx "x")(= xx "X"))
       (setq old2 (strcat "x" old2))
   )  ;  end if
   (setq old2 (cons 1 old2))
   (entmod (subst old2 aa bb))
   )
  (defun C:mm2ft ()
(prompt "\nExisting text shall be overwritten ... copy to a different layer")
  (setq count 0)
  (prompt "\nPick text to edit : ")

(setq pt1
(ssget '((0 . "TEXT,MTEXT")(1 ."1*,2*,3*,4*,5*,6*,7*,8*,9*,x*,X*")))
(while (< count (sslength pt1))
  (setq bb (entget (ssname pt1 count)))
  (setq aa (assoc 1 bb))         ; extract as (1 . "text")
  (setq txt (cdr aa))            ; extract "text"
  (setq xx (substr txt 1 1))     ; extract first character


  (if (or (= xx "x")(= xx "X"))
      (progn (setq len (strlen txt))
             (setq txt (substr txt 2))
             (calcu)
             (revx)
      )  ; end progn 
      (progn (calcu)  ; other "then" of if...
             (revx)
      )  ;end progn 
 )    ; end if 
  (setq count (+ count 1))
)  ; end while
  ;(setq check (getstring "\nReturn to continue"))
    (princ)
);end defun

Edited by SLW210
Added Code Tags!!!
Link to comment
Share on other sites

  • Replies 24
  • Created
  • Last Reply

Top Posters In This Topic

  • Hsanon

    8

  • Tharwat

    4

  • Lee Mac

    3

  • Ajmal

    3

hi hsanon,

welcome to forum

since you are newbie read this

http://www.cadtutor.net/forum/showthread.php?9184-Code-posting-guidelines

 

 

 

(defun calcu ()

(setq txt (atof txt))

(setq old1 (* txt 0.039372))

(setq old2 (rtos old1 4 0))

)

 

You can use autolisp function cvunit to convert units too :)

(defun [color="blue"]_calcu[/color] ( str ) 
(rtos ([color="blue"]cvunit[/color] (atof str) "[color="red"]mm[/color]" "[color="red"]in[/color]") 4 0)
)

[color="gray"];example:[/color]
(setq txt "123.456")

([color="blue"]_calcu[/color]  txt ) [color="gray"]; alternative sub function[/color]
"5\""

([color="blue"]calcu[/color]) [color="gray"]; your sub function[/color]
"5\""

Link to comment
Share on other sites

hi hsanon,

welcome to forum

since you are newbie read this

http://www.cadtutor.net/forum/showthread.php?9184-Code-posting-guidelines

 

 

 

 

 

You can use autolisp function cvunit to convert units too :)

(defun [color="blue"]_calcu[/color] ( str ) 
(rtos ([color="blue"]cvunit[/color] (atof str) "[color="red"]mm[/color]" "[color="red"]in[/color]") 4 0)
)

[color="gray"];example:[/color]
(setq txt "123.456")

([color="blue"]_calcu[/color]  txt ) [color="gray"]; alternative sub function[/color]
"5\""

([color="blue"]calcu[/color]) [color="gray"]; your sub function[/color]
"5\""

 

:thumbsup: Thanks my dear friend !

Link to comment
Share on other sites

thanks for your help, but when apploading it, the command line says it is successfully loaded (which it said earlier too) but an autocad error box flashes that "you have cancelled the function" sign. (which it did earlier too)

 

And hey ,.... does the cvunit mean we can convert text strings back and forth to MM and ft/in accurately ??? thats great....

Link to comment
Share on other sites

Try my program and let me know .

 

(defun c:mm2in (/ in ss)
 ;; ==================================================    ;;
 ;;    Author : Tharwat Al Shoufi .Date: 22.Nov.2014    ;;
 ;;    Converts Text Strings from Millimeter to Inch    ;;
 ;; ==================================================    ;;
 (if (setq in (/ 1. 25.4)
           ss (ssget "_:L"
                     '((0 . "TEXT,MTEXT")
                       (1 . "1*,2*,3*,4*,5*,6*,7*,8*,9*,x*,X*")
                      )
              )
     )
   ((lambda (x / sn e s a)
      (while (setq sn (ssname ss (setq x (1+ x))))
        (entmod
          (subst
            (cons
              1
              (if (wcmatch
                    (setq
                      a (strcase
                          (substr
                            (setq
                              s (cdr (assoc 1 (setq e (entget sn))))
                            )
                            1
                            1
                          )
                        )
                    )
                    "X*"
                  )
                (if (/= (atof (substr s 2)) 0.)
                  (strcat a (rtos (* (atof (substr s 2)) in) 4 0))
                  s
                )
                (rtos (* (atof s) in) 4 0)
              )
            )
            (assoc 1 e)
            e
          )
        )
      )
    )
     -1
   )
 )
 (princ)
)

Link to comment
Share on other sites

now i realize why i am a newbie and you are a deity ;-)

it works perfectly...... !!!!

dont want to get greedy...., but does it reverse accurately back to mm if required ????

 

Thanks for the work done.... its going to save me a lot of time. :D

appreciate it greatly.

Link to comment
Share on other sites

now i realize why i am a newbie and you are a deity ;-)

it works perfectly...... !!!!

dont want to get greedy...., but does it reverse accurately back to mm if required ????

 

Thanks for the work done.... its going to save me a lot of time. :D

appreciate it greatly.

 

haha :lol: you are welcome .

 

Give a go to this program and let me know .

 

(defun c:Test (/ *error* dz ss f)
 ;; ==================================================    ;;
 ;;    Author : Tharwat Al Shoufi .Date: 22.Nov.2014    ;;
 ;;    Converts conversely Text Strings from        ;;
 ;;    Millimeter to Inch                 ;;
 ;; ==================================================    ;;
 (defun *error* (msg)
   (if dz
     (setvar 'DIMZIN dz)
   )
   (if (not (wcmatch msg "*CANCEL*,*EXIT*,*BREAK*"))
     (princ (strcat "\n ** Error : " msg " **"))
   )
 )
 (princ
   "\n Select Texts that either start with numbers or X character :"
 )
 (if (and (setq ss (ssget "_:L"
                          '((0 . "TEXT,MTEXT")
                            (1 . "1*,2*,3*,4*,5*,6*,7*,8*,9*,x*,X*")
                           )
                   )
          )
          (progn
            (initget 6 "Inch Millimeter")
            (setq *formula*
                   (cond
                     ((getkword
                        (strcat
                          "\n Specify convertion formula [inch/ Millimeter] < "
                          (if *formula*
                            *formula*
                            (setq *formula* "Inch")
                          )
                          " > :"
                        )
                      )
                     )
                     (*formula*)
                   )
            )

          )
          (if (eq *formula* "Inch")
            (setq f (list (/ 1.0 25.4) 4 0))
            (setq f (list 25.4 2 2))
          )
     )
   (progn
     (setq dz (getvar 'DIMZIN))
     (setvar 'DIMZIN 0)
     ((lambda (x / sn e s a)
        (while (setq sn (ssname ss (setq x (1+ x))))
          (entmod
            (subst
              (cons
                1
                (if
                  (wcmatch
                    (setq
                      a (strcase
                          (substr
                            (setq
                              s (cdr (assoc 1 (setq e (entget sn))))
                            )
                            1
                            1
                          )
                        )
                    )
                    "X*"
                  )
                   (if (/= (atof (substr s 2)) 0.)
                     (strcat a
                             (rtos (* (atof (substr s 2)) (car f))
                                   (cadr f)
                                   (caddr f)
                             )
                     )
                     s
                   )
                   (rtos (* (atof s) (car f)) (cadr f) (caddr f))
                )
              )
              (assoc 1 e)
              e
            )
          )
        )
      )
       -1
     )
     (setvar 'DIMZIN dz)
   )
 )
 (princ)
)

Link to comment
Share on other sites

The Conversion to Inch works fine, but when you convert to MM the figures go all wrong. Was trying to figure it out in the code, but havent been able to do so as yet.

 

But thanks for your help, the one way change from mm to inches is what I needed, if the reverse takes place too its great, !!!!

And I cant thank you enough ....

Link to comment
Share on other sites

The Conversion to Inch works fine, but when you convert to MM the figures go all wrong.

What was wrong with the conversion ?

Show me an example ( BEFORE and AFTER ) and what is the correct outcome as you wish to be able to get your point clearly ?

Link to comment
Share on other sites

conversion%20sample.jpg

 

to test it, i had made out a rectangle (as in the image) of 3050 x 3650

ON running the program, it perfectly converted the millimeters to ft .... i.e. to 10' x 12'

on running the mm option, it replaced it to 254.00 x 304.80

 

i think maybe the 10' needs to be x12 into inches and then x25.4 (the multiplication factor needs to be 304.8)

 

(and .... can the answer (if required) be rounded off to the nearest 50mm??)

Link to comment
Share on other sites

i think maybe the 10' needs to be x12 into inches and then x25.4 (the multiplication factor needs to be 304.8)

 

Remove the decimal numbers that after the 25 which represent the inch factor in the program and try again .

(setq f (list (/ 1.0 25[b][color=red].4[/color][/b]) 4 0))

And if you also want to keep the number without decimal numbers , replace the 2 number to 0

 

(setq f (list 25.4 2 [color=red][b]2[/b][/color]))

 

To upload any file , just press the button on right side hand in the bottom and from the button attach you can attach any file if its format is available in the list .

Link to comment
Share on other sites

Hi,

Apologies for the late response... have noticed that if we change the

(setq f (list 305 2 0)) ; this multiplies the feet portion to the correct mm but not the "inch" figure

 

we can get the feet portion to be corrected. However it does not sense the "inch" portion ...

example for 11'6" , it will take the 11' and multiply by 305 to get 3355, but it ignores the 6" and we dont get 3505 as the answer.

Link to comment
Share on other sites

Hi Hsanon,

 

Try the following program:

(defun c:mm2in nil (converttext (/ 1.0 25.4) 4 0))
(defun c:in2mm nil (converttext 25.4 2 2))

(defun converttext ( f u p / e i n s x y )
   (if (setq s (ssget "_:L" '((0 . "TEXT,MTEXT") (1 . "[1-9xX]*"))))
       (repeat (setq i (sslength s))
           (setq e (entget (ssname s (setq i (1- i))))
                 x (cdr (assoc 1 e))
           )
           (if (wcmatch x "[xX]*")
               (setq y (chr (ascii x)) x (substr x 2))
               (setq y "")
           )
           (if (setq n (distof x 4))
               (entmod (subst (cons 1 (strcat y (rtos (* n f) u p))) (assoc 1 e) e))
           )
       )
   )
   (princ)
)

The commands are mm2in & in2mm.

 

Lee

Edited by Lee Mac
Link to comment
Share on other sites

  • 1 month later...

Hi Lee...

Apologies for the late reply..... Had been travelling and (partially) vacationing.

The program works perfectly...

And its a great example for me for short precise programming.

Many Thanks once again.

 

regards

 

harsh

Link to comment
Share on other sites

  • 2 years later...

Hi Lee,

How we can convert IN frac. to In decimal 2'4" >> 2.33 something like this ..

 

Regards,

 

 

 

 

Hi Hsanon,

 

Try the following program:

([color=BLUE]defun[/color] c:mm2in [color=BLUE]nil[/color] (converttext ([color=BLUE]/[/color] 1.0 25.4) 4 0))
([color=BLUE]defun[/color] c:in2mm [color=BLUE]nil[/color] (converttext 25.4 2 2))

([color=BLUE]defun[/color] converttext ( f u p [color=BLUE]/[/color] e i n s x y )
   ([color=BLUE]if[/color] ([color=BLUE]setq[/color] s ([color=BLUE]ssget[/color] [color=MAROON]"_:L"[/color] '((0 . [color=MAROON]"TEXT,MTEXT"[/color]) (1 . [color=MAROON]"[1-9xX]*"[/color]))))
       ([color=BLUE]repeat[/color] ([color=BLUE]setq[/color] i ([color=BLUE]sslength[/color] s))
           ([color=BLUE]setq[/color] e ([color=BLUE]entget[/color] ([color=BLUE]ssname[/color] s ([color=BLUE]setq[/color] i ([color=BLUE]1-[/color] i))))
                 x ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 1 e))
           )
           ([color=BLUE]if[/color] ([color=BLUE]wcmatch[/color] x [color=MAROON]"[xX]*"[/color])
               ([color=BLUE]setq[/color] y ([color=BLUE]chr[/color] ([color=BLUE]ascii[/color] x)) x ([color=BLUE]substr[/color] x 2))
               ([color=BLUE]setq[/color] y [color=MAROON]""[/color])
           )
           ([color=BLUE]if[/color] ([color=BLUE]setq[/color] n ([color=BLUE]distof[/color] x 4))
               ([color=BLUE]entmod[/color] ([color=BLUE]subst[/color] ([color=BLUE]cons[/color] 1 ([color=BLUE]strcat[/color] y ([color=BLUE]rtos[/color] ([color=BLUE]*[/color] n f) u p))) ([color=BLUE]assoc[/color] 1 e) e))
           )
       )
   )
   ([color=BLUE]princ[/color])
)

The commands are mm2in & in2mm.

 

Lee

Link to comment
Share on other sites

This should be a good starting point it needs some extra checking for feet only input etc

(setq str "2'8")
(if (= (wcmatch str "*'*") T)
(setq pos (vl-string-search "'" str 1))
)
(setq feet (substr STR 1 pos))
(setq inchs (substr str (+ pos 2) ))
(alert (strcat "You have " feet " feet " " inchs  " inches"))

Edited by SLW210
Fixed Code Tags
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...