Silvercloak Posted March 22, 2012 Posted March 22, 2012 Hi there, I'm freshly minted AutoLISP noob here. My boss has asked if I could write a LISP that would take a dimension like 10,000 and fix it to look like 10 000 instead. It's been a great little exercise, stretching my skills, but I've hit a roadblock I can't figure a way around. I've converted the dimension into a list which is ready to be turned into a string, but I just can't seem to MAKE it do that. I've tried STRCAT but I keep getting errors. Can anyone help? I'm sure I can turn it into an ENTMOD and finish the rest but I just need this part figured out. It needs to be without () or "" marks in it. Thanks! Here's what I've done, (defun c:DIMCHG () (defun GetDim () (setq DIMEN (car (entsel))) (setq OLDDIMEN (entget dimen)) (setq CHANGE (assoc 42 OLDDIMEN)) (setq CHANGE2 (cdr CHANGE)) (setq CHANGE3 (fix CHANGE2)) (setq DIMLTH (strlen (itoa CHANGE3))) (setq CHANGE4 (itoa CHANGE3)) ) ;End GetDim (defun BreakItDown () (setq POSITION 1) (setq BrkDwn nil) ;remove when finished coding (repeat DIMLTH (setq BrkDwn (cons (substr CHANGE4 POSITION 1) BrkDwn)) (setq BrkDwn (cons " " BrkDwn)) (setq POSITION (+ 1 POSITION)) ) ;End repeat ) ;End BreakItDown (defun ReAsmbl () (setq RSMBL nil) (while BrkDwn (cond ((= POSITION 4) (setq RSMBL (cons " " RSMBL)) (setq POSITION 1) ) ((= (car BrkDwn) " ") (setq BrkDwn (cdr BrkDwn))) ((/= (car BrkDwn) " ") (setq RSMBL (cons (car BrkDwn) RSMBL)) (setq BrkDwn (cdr BrkDwn)) (setq POSITION (+ POSITION 1)) ) ) ;End Cond ) ;End While ) ;End ReAsmbl (defun ltos () (setq gfile (open "acad.grp" "w")) ;open a file on disk (write-line RSMBL gfile) ;print list to file (close gfile) ;close file (setq gfile (open "acad.grp" "r")) ;open file (setq strname (read-line gfile)) ;read list from file (close gfile) ;close file strname ;return converted list ) (GetDim) (BreakItDown) (setq POSITION 1) (ReAsmbl) (vl-princ-to-string Rsmbl);I got this from the web, but it still doesn't look right ) Quote
marko_ribar Posted March 22, 2012 Posted March 22, 2012 Maybe, try this : (defun c:dimch ( / dimss n dim dimval d dimvalstr dimvalstrnew ) (setq dimss (ssget '((0 . "DIMENSION")) )) (repeat (setq n (sslength dimss)) (setq dim (ssname dimss (setq n (1- n)))) (setq dimval (cdr (assoc 42 (setq d (entget dim))))) (setq dimvalstr (rtos dimval)) (setq dimvalstrnew (vl-string-translate "." " " dimvalstr)) (entmod (subst (cons 1 dimvalstrnew) (assoc 1 d) d)) ) (princ) ) M.R. Quote
Silvercloak Posted March 22, 2012 Author Posted March 22, 2012 Maybe, try this : (defun c:dimch ( / dimss n dim dimval d dimvalstr dimvalstrnew ) (setq dimss (ssget '((0 . "DIMENSION")) )) (repeat (setq n (sslength dimss)) (setq dim (ssname dimss (setq n (1- n)))) (setq dimval (cdr (assoc 42 (setq d (entget dim))))) (setq dimvalstr (rtos dimval)) (setq dimvalstrnew (vl-string-translate "." " " dimvalstr)) (entmod (subst (cons 1 dimvalstrnew) (assoc 1 d) d)) ) (princ) ) M.R. WTF... I'm trying to understand half of what you did and I'm afraid it'll take me a while to study that and understand it... You've got the gist of what I'm trying to do. I'm trying to round it so that no decimal places comes on board. And it's broken in groups of three... for example a dimension showing: 9785642.34 would become 9 785 642 I'm afraid my coding is still really basic as I haven't quite mastered anything in autolisp yet... Yours is like AutoLISP wizardry. Silvercloak Quote
Silvercloak Posted March 22, 2012 Author Posted March 22, 2012 Oh COOL, I think I'm getting it, you used selection sets. Very ingenious, I never thought of doing it this way. Although, I've never heard of vl-string-translate. I think I'm getting the idea of what you're trying to do though. I really like how much more secure and tidy this way is. Thank you! Could you show me how to break the dimension down into groups of three without decimal places? Quote
Lee Mac Posted March 22, 2012 Posted March 22, 2012 This is quickly written, no doubt there's a better way to add the separators: (defun c:fixdim ( / e i s x ) (vl-load-com) (if (setq s (ssget "_:L" '((0 . "DIMENSION")))) (repeat (setq i (sslength s)) (setq e (entget (ssname s (setq i (1- i)))) x 0) (entmod (list (assoc -1 e) (cons 1 (vl-string-left-trim " " (vl-list->string (reverse (apply 'append (mapcar (function (lambda ( c ) (if (zerop (rem (setq x (1+ x)) 3)) (list c 32) (list c) ) ) ) (reverse (vl-string->list (rtos (cdr (assoc 42 e)) 2 0))) ) ) ) ) ) ) ) ) ) ) (princ) ) Note that all of the solutions so far are using a Dimension Text Override, hence the Dimension value will not be updated when the Dimension is modified. Ideally, the Dimension Style needs to allow a thousands separator option. Quote
Silvercloak Posted March 22, 2012 Author Posted March 22, 2012 This is quickly written, no doubt there's a better way to add the separators: (defun c:fixdim ( / e i s x ) (vl-load-com) (if (setq s (ssget "_:L" '((0 . "DIMENSION")))) (repeat (setq i (sslength s)) (setq e (entget (ssname s (setq i (1- i)))) x 0) (entmod (list (assoc -1 e) (cons 1 (vl-list->string (reverse (apply 'append (mapcar (function (lambda ( c ) (if (zerop (rem (setq x (1+ x)) 3)) (list c 32) (list c) ) ) ) (reverse (vl-string->list (rtos (cdr (assoc 42 e)) 2 0))) ) ) ) ) ) ) ) ) ) (princ) ) You rock!!! That's exactly what I'm shooting for. Thank you, now I'm going to go over it and learn how you did it. Thanks again to everyone who helped, feel free to add your ideas as well. I want to learn and get better. Silvercloak Quote
Lee Mac Posted March 22, 2012 Posted March 22, 2012 You rock!!! That's exactly what I'm shooting for. Thank you, now I'm going to go over it and learn how you did it. Thanks again to everyone who helped, feel free to add your ideas as well. I want to learn and get better. You're welcome Silvercloak - if you have any questions about the code, just ask. BTW, I've updated my above post and code to trim leading spaces for dimension values Quote
Stefan BMR Posted March 22, 2012 Posted March 22, 2012 Another way, no code needed: - To set number formats in Windows, go to Control Panel -> Region and Language -> Formats [tab] -> Additional Settings and set Space for Digit grouping symbol. - In AutoCAD, open DimStyle dialog box, edit your Dimstyle and, in Units tab, set Units Format to Windows Desktop. Quote
Lee Mac Posted March 22, 2012 Posted March 22, 2012 Another way, no code needed:- To set number formats in Windows, go to Control Panel -> Region and Language -> Formats [tab] -> Additional Settings and set Space for Digit grouping symbol. - In AutoCAD, open DimStyle dialog box, edit your Dimstyle and, in Units tab, set Units Format to Windows Desktop. I knew there must be a better way than Text Override - nice one Stefan Quote
Lee Mac Posted March 22, 2012 Posted March 22, 2012 @Silvercloak: You might want to read the forum Code Posting Guidlines and edit your first post to frame your code. Quote
Silvercloak Posted March 22, 2012 Author Posted March 22, 2012 Another way, no code needed:- To set number formats in Windows, go to Control Panel -> Region and Language -> Formats [tab] -> Additional Settings and set Space for Digit grouping symbol. - In AutoCAD, open DimStyle dialog box, edit your Dimstyle and, in Units tab, set Units Format to Windows Desktop. Hmm, I can't do that here with Windows XP and AutoCAD 2010. Not that I can see anyways. Silvercloak Quote
Silvercloak Posted March 22, 2012 Author Posted March 22, 2012 @Silvercloak: You might want to read the forum Code Posting Guidlines and edit your first post to frame your code. Ok, will do. And text override was exactly what I was shooting for. I just didn't think this little exercise would get this complicated. Silvercloak Quote
marko_ribar Posted March 22, 2012 Posted March 22, 2012 (edited) All I managed after Lee is : (defun 3digspace ( str len / k kk kkk 3dig stdig newstr ) (setq newstr "") (setq k (fix (/ (float len) 3.0))) (setq kk (rem (float len) 3.0)) (if (eq kk 0.0) (setq k (- k 1))) (setq kkk 0) (repeat k (setq kkk (1+ kkk)) (setq 3dig (substr str (+ (- len (* kkk 3)) 1) 3)) (if (eq k kkk) (setq stdig (substr str 1 (- len (* kkk 3))))) (setq newstr (strcat " " 3dig newstr)) ) (setq newstr (strcat stdig newstr)) newstr ) (defun c:dimch ( / dimss n dim dimval d dimvalstr dimvalstrl dimvalstrnew ) (setq dimss (ssget '((0 . "DIMENSION")) )) (repeat (setq n (sslength dimss)) (setq dim (ssname dimss (setq n (1- n)))) (setq dimval (cdr (assoc 42 (setq d (entget dim))))) (setq dimvalstr (itoa (fix dimval))) (setq dimvalstrl (strlen dimvalstr)) (setq dimvalstrnew (3digspace dimvalstr dimvalstrl)) (entmod (subst (cons 1 dimvalstrnew) (assoc 1 d) d)) ) (princ) ) M.R. Edited March 22, 2012 by marko_ribar Quote
Stefan BMR Posted March 22, 2012 Posted March 22, 2012 Hmm, I can't do that here with Windows XP and AutoCAD 2010. Not that I can see anyways. Silvercloak Space symbol may not be available in the popup list for settings Digit Grouping Symbol. Try to type it. Just click on the edit box and press Space then Apply. I can't test in AutoCAD 2010. Do you have Windows Desktop option for Units Format? Quote
Silvercloak Posted March 22, 2012 Author Posted March 22, 2012 Space symbol may not be available in the popup list for settings Digit Grouping Symbol. Try to type it. Just click on the edit box and press Space then Apply.I can't test in AutoCAD 2010. Do you have Windows Desktop option for Units Format? Ah yes, apparently I do. Quote
BIGAL Posted March 23, 2012 Posted March 23, 2012 DIMLUNIT 6 to quote help "Microsoft Windows Desktop (decimal format using Control Panel settings for decimal separator and number grouping symbols)" normal 2 Quote
Recommended Posts
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.