Converting text

Converting text

BrendaRichardson
Enthusiast Enthusiast
2,188 Views
16 Replies
Message 1 of 17

Converting text

BrendaRichardson
Enthusiast
Enthusiast

I don't think this is possible in lisp, but here goes. 

 

We have a program that creates our drawings - all 2D, all done in model space. The problem is, it creates them all in english and we need to convert them to metric. Converting the dimensions is easy; but we have tables with values in them. Is there a way those can be converted? I am pretty sure it would have to be a select a text item, pull the value of that item, perform the calculation (simply multiply the number X 25.4) and put that value in as the new text string. 

 

Thoughts to get me moving in the right direction?

~Brenda~
0 Likes
Accepted solutions (1)
2,189 Views
16 Replies
Replies (16)
Message 2 of 17

dlanorh
Advisor
Advisor

You can use the TABLEEXPORT command to export the table to a csv. This can then be altered by lisp or in excel and then re-imported into the table

I am not one of the robots you're looking for

0 Likes
Message 3 of 17

dlanorh
Advisor
Advisor

I've found a very old lisp that reformats table cell values. This could easily be adapted to convert table cells, however I don't know the format of the imperial table values.

 

Multiplying by 25.4 converts inches to mm, however with Imperial units the text could also be in decimal feet or feet and inches. Feet and inches would probable be the worst case scenario, but not insurmountable. Any chance of a table csv, exported using the TABLEEXPORT command?    

I am not one of the robots you're looking for

0 Likes
Message 4 of 17

BrendaRichardson
Enthusiast
Enthusiast

I say tables; but they aren't actually tables created in AutoCAD; they are just table of text and lines. So, exporting isn't really an option. 

 

Some sort of an advanced find and replace would work for 75% of the numbers, as they are usually the same 5 or 6 different numbers. So, something like find A, replace with B, find C, replace with D.  But, the other 25% wouldn't be able to have something similar to a lookup table in excel. 

 

Something that would actually do the calculation would be needed.

 

Like I said, I think this is probably a pretty long shot, but I figured I'd throw it out there. 

 

Oh, and the conversion is always inches to mm; it's just the way we do it. 

~Brenda~
0 Likes
Message 5 of 17

CodeDing
Advisor
Advisor

@BrendaRichardson ,

 

Perhaps you could get more assistance if you posted an example .dwg file? It would be easier for others to test and help come to a solution.

 

Best,

~DD

0 Likes
Message 6 of 17

Moshe-A
Mentor
Mentor

@BrendaRichardson  hi,

 

Here a lisp to convert text measurement from inch to millimeters

but it works under these terms

 

1. it let you select only TEXT objects (not MTEXT)

2. the text should represent a length measurement in Engineering or Architectural units format with 1 unit precision.

 

enjoy

moshe

 

 

; convert texts from inch to mm
(defun c:inch2mm (/ ss elist)
 (if (setq ss (ssget '((0 . "text"))))
  (foreach ename (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
   (setq elist (entget ename))
   (setq text (cdr (assoc '1 elist)))
   (if (distof text 3) 
    (entmod (subst (cons '1 (* (distof text 3) 25.4)) (assoc '1 elist) elist))
   )
  ); foreach
 ); if

 (princ)
)
0 Likes
Message 7 of 17

BrendaRichardson
Enthusiast
Enthusiast

Thanks. I've attached one of the drawings I'm thinking of. Anything dimensioned is either adjusted through the dimstyle, or redone (because the program we use doesn't do them correctly. The numbers I'm specifically talking about are in the table in the bottom left (this one is particularly small, we have some that are rather large. 

 

they should be converted to nearest 10th mm

~Brenda~
0 Likes
Message 8 of 17

CodeDing
Advisor
Advisor

@BrendaRichardson ,

 

Thank you for the dwg. It appears there are 2 columns that will need converting. this column might appear to cause some trouble unless we can confirm a pattern:

image.png

 

Could you help me confirm a pattern for this column so that our conversions are consistent across drawings? For example:

- Is this column ALWAYS laid out this same way?

- or Is the dimension ALWAYS immediately following the diameter symbol?

- or Will the dimension ALWAYS come immediately before the word "HOLES"?

- Will there ever be more than 1 dimension to convert in this column?

 

I believe this will get us on the right track toward a solution.

 

Best,

~DD

0 Likes
Message 9 of 17

Moshe-A
Mentor
Mentor

here is my version

 

; convert texts from inch to mm
(defun c:inch2mm (/ numeric_string iRound ; local functions
		    ss elist text p num)

 (defun numeric_string (str p / ch s)
  (setq ch (substr str p 1) s "")
  (while (and (/= ch "")
 	       (or
	        (eq ch ".")
	        (and
	         (>= (ascii ch) 48)
	         (<= (ascii ch) 57)
	        )
	       )
	 )
   (setq s (strcat s ch))
   (setq p (1+ p) ch (substr str p 1))		 
  ); while

  s
 ); numeric_string

 (defun iRound (n)
  (if (>= (rem n 10) 5)
   (* (1+ (fix (/ n 10))) 10)
   (* (fix (/ n 10)) 10)
  )
 ); round
  
 (setvar "cmdecho" 0)
 (command "._undo" "_begin")
  
 (if (setq ss (ssget '((0 . "text,mtext"))))
  (foreach ename (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
   (setq elist (entget ename))
   (setq text (cdr (assoc '1 elist)))

   (cond
    ((setq p (vl-string-search "%%C" (strcase text)))
     (setq p (+ p 4))
    ); case
    ( t
     (setq p 1)
    )
   ); cond

   (if (and
	 (/= (setq num (numeric_string text p)) "")
	 (distof num 3)
       )
    (progn
     (setq text (vl-string-subst (rtos (iRound (* (distof num 3) 25.4)) 2 0) num text))
     (entmod (subst (cons '1 text) (assoc '1 elist) elist))
    ); progn
   ); if
    
  ); foreach
 ); if

 (command "._undo" "_end")
 (setvar "cmdecho" 1) 
  
 (princ)
)
0 Likes
Message 10 of 17

BrendaRichardson
Enthusiast
Enthusiast

@CodeDing 

 

Actually, it will almost always be diameter 0.748 holes. There are times when there will be multiple dimensions there (slot sizes), but those can be converted manually if needed. 

 

It's the number of holes that's going to throw it off, right? 

 

 

~Brenda~
0 Likes
Message 11 of 17

BrendaRichardson
Enthusiast
Enthusiast

@Moshe-A 

 

Can you help me understand this function you've defined? I ran your routine, and it converted 1.250 to 30, when, in fact, it should be 31.75; and what we would like is 31.8.

 

If I can understand how this is working, I think I can adjust it to what we need.

 

Thanks!!

 

 

~Brenda~
0 Likes
Message 12 of 17

Moshe-A
Mentor
Mentor

@BrendaRichardson ,

 


@BrendaRichardson wrote:

@Moshe-A 

 

Can you help me understand this function you've defined? I ran your routine, and it converted 1.250 to 30, when, in fact, it should be 31.75; and what we would like is 31.8.

that's because you requested this:

"they should be converted to nearest 10th mm"

hey, it's 'dangers' here, every thing is documented Smiley LOL

 

If I can understand how this is working, I think I can adjust it to what we need.

 

Thanks!!

 

 


Nevertheless, i made the correction cause it was involve other settings.

see line 16 i comment the previous line and line 17 is the new and if you regret, reverse them.

 

enjoy

moshe

 

; convert texts from inch to mm
(defun c:inch2mm (/ numeric_string iRound ; local functions
		    savDimzin ss elist text p num)

 (defun numeric_string (str p / ch s)
  (setq ch (substr str p 1) s "")
  (while (and (/= ch "")
 	       (or
	        (eq ch ".")
	        (and
	         (>= (ascii ch) 48)
	         (<= (ascii ch) 57)
	        )
	       )
	 )
   (setq s (strcat s ch))
   (setq p (1+ p) ch (substr str p 1))		 
  ); while

  s
 ); numeric_string

 (defun iRound (n)
  (if (>= (rem n 10) 5)
   (* (1+ (fix (/ n 10))) 10)
   (* (fix (/ n 10)) 10)
  )
 ); iRound
  
 (setvar "cmdecho" 0)
 (command "._undo" "_begin")

 (setq savDimzin (getvar "dimzin"))
 (setvar "dimzin" 8)
  
 (if (setq ss (ssget '((0 . "text,mtext"))))
  (foreach ename (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
   (setq elist (entget ename))
   (setq text (cdr (assoc '1 elist)))

   (cond
    ((setq p (vl-string-search "%%C" (strcase text)))
     (setq p (+ p 4))
    ); case
    ( t
     (setq p 1)
    )
   ); cond

   (if (and
	 (/= (setq num (numeric_string text p)) "")
	 (distof num 3)
       )
    (progn
    ; (setq text (vl-string-subst (rtos (iRound (* (distof num 3) 25.4)) 2 1) num text))
     (setq text (vl-string-subst (rtos (* (distof num 3) 25.4) 2 1) num text))
     (entmod (subst (cons '1 text) (assoc '1 elist) elist))
    ); progn
   ); if
    
  ); foreach
 ); if

 (setvar "dimzin" savDimzin)
  
 (command "._undo" "_end")
 (setvar "cmdecho" 1) 
  
 (princ)
)
0 Likes
Message 13 of 17

CodeDing
Advisor
Advisor
Accepted solution

@BrendaRichardson ,

 

Thanks for the updates. Here's my go at it. I couldn't think of a cool command name, so it's default is still "TEST".

(defun GetNum (txt / finished numStarted c exNum return cnt)
;returns number we are looking for in our text
;remove this character in our variable, it causes problems when searching TEXT
(if (vl-string-search "∅" txt)
  (setq txt (vl-string-subst " " "∅" txt))
);if
(setq len (strlen txt) cnt len finished nil numStarted nil exNum "")
(while (and (not finished) (> cnt 0))
  (setq c (substr txt cnt 1))
  (cond
    ((or (distof c 2) (eq "." c))
      (setq numStarted t exNum (strcat c exNum))
    );cond 1
    (t
      (if numstarted (setq finished t))
    );cond t
  );cond
  (setq cnt (1- cnt))
);while
(setq return nil)
(if (> (strlen exNum) 0)
  (if (distof exNum 2)
    (setq return exNum)
    (prompt (strcat "...bad number to convert: " exNum))
  );if
  (prompt "...no number to convert.")
);if
return
);defun

(defun c:TEST ( / e eg txt tmp n)
;assumes all text dimensions are in inches
;converts by 25.4 to mm
(while (setq e (car (entsel "\nSelect text (Enter to exit): ")))
  (setq eg (entget e) txt (cdr (assoc 1 eg)))
  (cond
    ((not (or (eq "MTEXT" (cdr (assoc 0 eg))) (eq "TEXT" (cdr (assoc 0 eg)))))
      (prompt "...Invalid object selected.")
    );cond 1
    ((setq n (GetNum txt))
      (setq tmp (rtos (* 25.4 (distof n 2)) 2 1))
      (if (setq txt (vl-string-subst tmp n txt))
        (progn
(entmod (subst (cons 1 txt) (assoc 1 eg) eg))
(prompt (strcat "Converted: " n " -> " tmp))
);progn (prompt "...number substitution unsuccessful.") );if );cond 2 );cond (setq e nil) );while (princ) );defun

Best,

~DD

0 Likes
Message 14 of 17

Sea-Haven
Mentor
Mentor

One of the things I get worried about is when converting feet to metric it can be a exact, but you can not buy a bolt of an imperial size, a 25.4 bolt does not exist a 25 does. Here in AUS you can buy both. 

 

Another example is that when metric came in sizes changed if I want 1 foot wide timber boardI would buy 300mm wide but its 4.8mm different thats a lot. 

 

What I am getting at if product is to be locally sourced you may need to rethink original sizes.

 

 

0 Likes
Message 15 of 17

john.uhden
Mentor
Mentor

Very good point!

It may be better to skip the mathematics and use a conversion list for each of the hole sizes.  That would also enable a different list for each source (sizing nomenclature).

John F. Uhden

0 Likes
Message 16 of 17

BrendaRichardson
Enthusiast
Enthusiast

Not an issue here; we have been doing this for years. We know what to convert, and how to convert it. We've got the hardware issue down pat (depending on what our customers want, that's what they get). And, outside of I-Beams (which are not drawn by this program, and specified by us based on availability), everything is cut to size sheet metal.

 

But, I appreciate your comments.

~Brenda~
0 Likes
Message 17 of 17

Moshe-A
Mentor
Mentor

@BrendaRichardson 

 

if  you got an answer? please make it as your solution.

 

thank  you

moshe

 

0 Likes