View Full Version : Adding number to a text string

3rd May 2009, 05:08 am
I would like to create a routine to add the current dimscale variable to the end of text string.


3rd May 2009, 09:25 am
Would the dimscale need to be concurrent with the live dimscale? i.e will you need the text string to be updated to reflect changes to the dimscale value, automatically?

If you don't need it to be automatically updated you could just add a field that is assigned the value of the dimscale system variable.

Lee Mac
3rd May 2009, 11:31 am
Quick fix:

(defun c:AddDim (/ ss)
(if (setq ss (ssget (list (cons 0 "*TEXT")
(if (getvar "CTAB")
(cons 410 (getvar "CTAB"))
(cons 67 (- 1 (getvar "TILEMODE")))))))
(foreach x (mapcar 'entget
(vl-remove-if 'listp
(mapcar 'cadr (ssnamex ss))))
(entmod (subst (cons 1 (strcat (cdr (assoc 1 x)) (chr 32)
(rtos (getvar "DIMSCALE"))))
(assoc 1 x) x))))
(princ "\<!> No Text Selected <!>"))

3rd May 2009, 04:10 pm
I will better describe my intent:

I am trying to automate the process of inserting a block and have it inserted on the correct layer, grab the correct scale factor (based on the current dimscale). Our layers are standard with a suffix for a particular scale.

The layer the block is inserted on is static, except for the scale suffix, (i.e. Layer-25, Layer-50) from drawing to drawing.

Ideally, if the layer does not exist I would like it to be created. Most likely the layer will exist in the drawing the block is being inserted into.

I was thinking that I could read the current dimscale and place it at the end of the layer name and make that layer current prior to inserting the block.


Lee Mac
3rd May 2009, 04:32 pm
Ok, I think I understand your intents, will see what I can do :thumbsup:

Lee Mac
3rd May 2009, 04:37 pm

(defun c:Blklay (/ lDim)
(setq lDim (strcat "Layer-" (rtos (getvar "DIMSCALE"))))
(or (tblsearch "LAYER" lDim)
(vla-add (vla-get-Layers
(vlax-get-acad-object))) lDim))
(setvar "CLAYER" lDim)

David Bethel
3rd May 2009, 04:41 pm
This assumes that your DIMSCALE is a whole number

(defun SetLayer (name / ldef flag)
(cond ((or (not name)
(not (snvalid name)))
(princ "\nBad Aurgment Passed To SetLayer - ")
(prin1 name)
(command "_.LAYER")
(if (not (tblsearch "LAYER" name))
(command "_Make" name)
(setq ldef (tblsearch "LAYER" name)
flag (cdr (assoc 70 ldef)))
(and (= (logand flag 1) 1)
(command "_Thaw" name))
(and (minusp (cdr (assoc 62 ldef)))
(command "_On" name))
(and (= (logand flag 4) 4)
(command "_Unlock" name))
(and (= (logand flag 16) 16)
(princ "\nCannot Set To XRef Dependent Layer")
(command "_Set" name)))
(command "")

(SetLayer (strcat "LAYER-" (rtos (getvar "DIMSCALE") 2 0)))

3rd May 2009, 04:52 pm
That was perfect...thanks a whole bunch!!!!


Lee Mac
3rd May 2009, 04:54 pm
I suppose a reactor could be used also - hasn't got the error trapping standard of Davids though :oops:

(defun c:Ron ()
(if (not blk:react)
(setq blk:react
(vlr-command-reactor nil
'((:vlr-commandWillStart . Blklay)
(:vlr-commandEnded . Blkend)
(:vlr-commandCancelled . Blkcan)))))
(princ "\n<<-- Block Reactor ON -->>")

(defun c:Roff ()
(if blk:react
(vlr-remove blk:react)
(setq blk:react nil)
(princ "\n<<-- Block Reactor OFF -->>")))

(defun Blklay (Reac Args / lDim)
(and (eq "INSERT" (car Args))
(setq oLay (getvar "CLAYER")
lDim (strcat "Layer-" (rtos (getvar "DIMSCALE"))))
(or (tblsearch "LAYER" lDim)
(vla-add (vla-get-Layers
(vlax-get-acad-object))) lDim))
(setvar "CLAYER" lDim))

(defun Blkend (Reac Args)
(if oLay
(setvar "CLAYER" oLay))

(defun Blkcan (Reac Args)
(if oLay
(setvar "CLAYER" oLay))
Type "Ron" to turn on and "Roff" to turn off

3rd May 2009, 05:10 pm
Is there an advantage of error trapping?

Lee Mac
3rd May 2009, 05:26 pm
Well, I just meant that David's LISP accounts for Locked, Frozen and Xref Dependent layers, whereas mine does not. :)

David Bethel
3rd May 2009, 05:45 pm
Using CLAYER with a frozen layer will give you an error:

LAYER ?/Make/Set/New/ON/OFF/Color/Ltype/Freeze/Thaw/LOck/Unlock: M

New current layer <3D>: 3D
?/Make/Set/New/ON/OFF/Color/Ltype/Freeze/Thaw/LOck/Unlock: S

New current layer <3D>: 0
?/Make/Set/New/ON/OFF/Color/Ltype/Freeze/Thaw/LOck/Unlock: F

Layer name(s) to Freeze: 3D

Command: CLAYER

New value for CLAYER <"0">: 3D

Cannot set CLAYER to that value.

Lee Mac
3rd May 2009, 05:46 pm
Exactly my point :)

4th May 2009, 04:20 am
Thanks guys, I appreciate your assistance..