PDA

View Full Version : Prefix/Suffix Lisp


cadd4la
2006-03-06, 10:44 PM
Hi everyone,

I need some help getting this code to work again and also to have it do a suffix as well.

it is design to add a prefix before existing layers in a drawing.

So if I have these existing layer names:

walk
drive, Etc...

run lpfix and I get this after I enter l-

l-walk
l-drive, Etc...

But for some reason I now getting

Command: (LOAD "Z:/EDGCustomFiles/LPFIX.lsp")
Type LPFIX to run.........
Command: lpfix
Enter Layer Prefix : d4
; error: bad argument type: stringp nil
(prompt "nType LPFIX to run.........")
(defun C:LPFIX ( / acadDocument theLayers layName pre)
(vl-load-com)
(setq pre (getstring "nEnter Layer Prefix : "))
(setq acadDocument (vla-get-activedocument (vlax-get-acad-object)))
(setq theLayers (vla-get-layers acadDocument))
(vlax-map-collection theLayers 'layer-mod)
(princ)
);defun
(defun layer-mod (theLayer)
(setq layName (vlax-get-property theLayer 'Name))
(if (not (member layName '("0" "Defpoints" "MVIEW" "NONPRINT" "SCAN" "XREF" "XREF-REF")))
(vla-put-Name thelayer (strcat pre layName))
) ;if
);defun
(princ)

I now also need code that will place a suffix at the end of the existing layers.

l-walk
l-drive, Etc...

run LSUF and I get this after I enter 96

l-walk96
l-drive96, Etc...

I took a shot at writing the code but its not running.

prompt "nType LSUF to run.........")
(defun C:LSUF ( / acadDocument theLayers layName suf)
(vl-load-com)
(setq suf (getstring "nEnter Layer suffix : "))
(setq acadDocument (vla-get-activedocument (vlax-get-acad-object)))
(setq theLayers (vla-get-layers acadDocument))
(vlax-map-collection theLayers 'layer-mod)
(princ)
);defun
(defun layer-mod (theLayer)
(setq layName (vlax-get-property theLayer 'Name))
(if (not (member layName '("0" "Defpoints" "MVIEW" "NONPRINT" "SCAN" "XREF" "XREF-REF")))
(vla-put-Name thelayer (strcat layName suffix ))
) ;if
);defun
(princ)

Therefore, I was hoping that someone could combined both lisps into one and have ask if you would like the text at the prefix or suffix.

Thanks as always

Kyle C.

rkmcswain
2006-03-06, 11:09 PM
To find your error, open the code in the VLIDE, load it and run it. When you get the error, switch back to the VLIDE and choose "Error trace" from the View menu. This will give you a pretty good clue as to where it's bombing.

The specific error you are receiving (bad argument type: stringp nil) is because something is expecting a string and it's receiving nil

Here is a single file to do both.


(defun layer-mod (theLayer)
(setq layName (vlax-get-property theLayer 'Name))
(if (not (member layName
'("0" "Defpoints"
"MVIEW" "NONPRINT"
"SCAN" "XREF"
"XREF-REF"
)
)
)
(vla-put-Name thelayer (strcat pre layName suf))
)
)
(defun C:LPFIX (/ acadDocument theLayers layName pre suf)
(vl-load-com)
(setq pre (getstring "\n Enter Layer Prefix: "))
(setq suf (getstring "\n Enter Layer Suffix: "))
(setq acadDocument (vla-get-activedocument (vlax-get-acad-object)))
(setq theLayers (vla-get-layers acadDocument))
(vlax-map-collection theLayers 'layer-mod)
(princ)
(vlax-release-object theLayers)
(vlax-release-object acadDocument)
(princ)
)
(prompt "\n Type LPFIX to run.........")
(princ)

.T.
2006-03-06, 11:13 PM
Hi Kyle,

At first glance, you don't pass the variable pre to layer-mod and pre is declared local to c:lpfix, so pre is probably nil when layer-mod looks for it.


;snip
(defun layer-mod (theLayer)
(setq layName (vlax-get-property theLayer 'Name))
(if (not (member layName '("0" "Defpoints" "MVIEW" "NONPRINT" "SCAN" "XREF" "XREF-REF")))
(vla-put-Name thelayer (strcat pre layName))
) ;if
);defun
;snip



I'll try and have a closer look at it a little later, I have a meeting...

I hope this helps. Gotta run,

.T.
2006-03-06, 11:44 PM
Meeting canceled :)

R.K., because the variables pre and suff are declared local to c:lpfix, don't pre and suff need to be passed to layer-mod? Or did I miss something?

TIA

T.Willey
2006-03-06, 11:47 PM
Meeting canceled :)

R.K., because the variables pre and suff are declared local to c:lpfix, don't pre and suff need to be passed to layer-mod? Or did I miss something?

TIA
They don't have to be because Layer-Mod is being ran inside the main routine. It would be better coding practice to do it that way, but not necessary [IMHO].

rkmcswain
2006-03-06, 11:52 PM
"pre" won't be nil, unless the user doesn't enter a string at the prompt.

Even though the argument isn't passed to the (layer-mod) function, it's global to the (lpfix) function at that point and therefore available to (layer-mod).

If (layer-mod) isn't used outside of this lisp, then it could be defined within the (lpfix) defun.

T.Willey
2006-03-06, 11:54 PM
"pre" won't be nil, unless the user doesn't enter a string at the prompt.

"pre" and "suf" will never be nil, if enter is pressed when prompted, then it just returns a null string [""]. FYI...

cadd4la
2006-03-06, 11:56 PM
rkmcswain,

Thank you for the code & just want to give you a heads up (see comment in "red"). after I fix that problem it runs great.

...
(defun layer-mod (theLayer)
(setq layName (vlax-get-property theLayer 'Name))
(if (not (member layName
'("0" "Defpoints" <-If Defpoints is not capitize it will add a suffix to it
"MVIEW" "NONPRINT"
"SCAN" "XREF"
"XREF-REF"
)
)
)
(vla-put-Name thelayer (strcat pre layName suf))
)
)
(defun C:LPFIX (/ acadDocument theLayers layName pre suf)
(vl-load-com)
(setq pre (getstring "n Enter Layer Prefix: "))
(setq suf (getstring "n Enter Layer Suffix: "))
(setq acadDocument (vla-get-activedocument (vlax-get-acad-object)))
(setq theLayers (vla-get-layers acadDocument))
(vlax-map-collection theLayers 'layer-mod)
(princ)
(vlax-release-object theLayers)
(vlax-release-object acadDocument)
(princ)
)
(prompt "n Type LPFIX to run.........")
(princ)

Kyle C.

cadd4la
2006-03-06, 11:58 PM
Tim,

Thanks for your input.

Kyle C.
Hi Kyle,

At first glance, you don't pass the variable pre to layer-mod and pre is declared local to c:lpfix, so pre is probably nil when layer-mod looks for it.


;snip
(defun layer-mod (theLayer)
(setq layName (vlax-get-property theLayer 'Name))
(if (not (member layName '("0" "Defpoints" "MVIEW" "NONPRINT" "SCAN" "XREF" "XREF-REF")))
(vla-put-Name thelayer (strcat pre layName))
) ;if
);defun
;snip

...

rkmcswain
2006-03-06, 11:58 PM
"pre" and "suf" will never be nil, if enter is pressed when prompted, then it just returns a null string [""]. FYI...

Correct. My bad.

T.Willey
2006-03-06, 11:59 PM
rkmcswain,

Thank you for the code & just want to give you a heads up (see comment in "red"). after I fix that problem it runs great.


Kyle C.
A better check would be to have all the layers names capitol in the list, and then use

(if (not (member (strcase layName)
'("0" "DEFPOINTS"
"MVIEW" "NONPRINT"
"SCAN" "XREF"
"XREF-REF"
)
)
)

.T.
2006-03-07, 12:00 AM
Thanks for the clarification, guys.

File this under "You learn something new everyday...".

rkmcswain
2006-03-07, 12:01 AM
Another version.


(defun C:LPFIX (/ acadDocument theLayers layName pre suf)
(vl-load-com)
(defun layer-mod (theLayer)
(setq layName (vlax-get-property theLayer 'Name))
(if (not (member (strcase layName)
'("0" "DEFPOINTS"
"MVIEW" "NONPRINT"
"SCAN" "XREF"
"XREF-REF"
)
)
)
(vla-put-Name thelayer (strcat pre layName suf))
)
)
(setq pre (getstring "\n Enter Layer Prefix: "))
(setq suf (getstring "\n Enter Layer Suffix: "))
(setq acadDocument (vla-get-activedocument (vlax-get-acad-object)))
(setq theLayers (vla-get-layers acadDocument))
(vlax-map-collection theLayers 'layer-mod)
(vlax-release-object theLayers)
(vlax-release-object acadDocument)
(princ)
)
(prompt "\n Type LPFIX to run.........")
(princ)


Fixes the lowercase vs. capital problem also.