Error: bad argument type: stringp nil

Error: bad argument type: stringp nil

Anonymous
Not applicable
4,443 Views
9 Replies
Message 1 of 10

Error: bad argument type: stringp nil

Anonymous
Not applicable

Can anyone see it?? I'v been over and over it and it all looks fine to me?

 

The weird thing is it works perfectly fine on one drawing which is virtually the same, but then error on the other!

 

 

(defun c:PAGE_CONFIG ()	

(vl-load-com)
	(vlax-for y (vla-get-plotconfigurations
    (vla-get-activedocument (vlax-get-acad-object)))
		(if (/= "Model" (vla-get-name y))					
			(vla-delete y)
		)
    )

(setq Block_Col (vla-get-blocks (vla-get-ActiveDocument (vlax-get-acad-object))))
(setq BN nil)
(while (setq B (tblnext "block" (null B)))
	(setq BN (cons (cdr (assoc 2 B)) BN))
)

(foreach Name BN
	(setq blk (vl-catch-all-apply 'vla-Item (list Block_Col (strcat Name))))	;looking through all xrefs/blocks for size
	(setq blk_name (vla-get-name blk))
	(setq ps (cond ((wcmatch blk_name "*GRP-A0-*") "A0")
				((wcmatch blk_name "*GRP-A1-*") "A1")
				((wcmatch blk_name "*GRP-A2-*") "A2")
				((wcmatch blk_name "*GRP-A3-*") "A3")
				((wcmatch blk_name "*GRP-B1-*") "B1")
		  )
	)
)

(if (wcmatch (strcase (getvar "dwgname")) "*-CO-*")
 	(setq psetup (strcat ps " - Colour - Full Size"))
	(setq psetup (strcat ps " - Mono - Full Size"))
)
   
(command "_.psetupin" "C:/Apps/_Cad/2017_BU/BU/Templates/_CDD_MEP_PageSetUps.dwt" psetup) 
	
(foreach layout (layoutlist)
	(setvar "ctab" layout)
	(COMMAND "-PLOT" "" "" psetup "" "NO" "YES" "NO")
	(C:ZE)
)
(princ)   
)

 

 

0 Likes
Accepted solutions (1)
4,444 Views
9 Replies
Replies (9)
Message 2 of 10

cadffm
Consultant
Consultant

"Error: bad argument type: stringp nil" says: A function expects a string, but gets nothing (nil)

 

Without testing cour code in detail, i guess: PS is sometimes nil

1. You should force the string and pattern in WCMATCH function to UPPER- or lowerCase (STRCASE)

2. What should happen if no one of your cond-Test is true?

 

and

 

Learn to debug

old school by hand and with printouts or alerts

and realtime debugging by in your VLIDE.

 

Sebastian

Message 3 of 10

dlanorh
Advisor
Advisor

In line with what @cadffm pointed out

 

change this

 

(setq blk_name (vla-get-name blk))

to

 

(setq blk_name (strcase (vla-get-name blk)))

And all this

 

(if (wcmatch (strcase (getvar "dwgname")) "*-CO-*")
 	(setq psetup (strcat ps " - Colour - Full Size"));;IT IS PROBABLY FAILING HERE IF PS = NIL
	(setq psetup (strcat ps " - Mono - Full Size"));;IT IS PROBABLY FAILING HERE IF PS = NIL
)
   
(command "_.psetupin" "C:/Apps/_Cad/2017_BU/BU/Templates/_CDD_MEP_PageSetUps.dwt" psetup) 
	
(foreach layout (layoutlist)
	(setvar "ctab" layout)
	(COMMAND "-PLOT" "" "" psetup "" "NO" "YES" "NO")
	(C:ZE)
)

needs to go into a condition or if

 

(cond (ps
        (if (wcmatch (strcase (getvar "dwgname")) "*-CO-*")
          (setq psetup (strcat ps " - Colour - Full Size"))
          (setq psetup (strcat ps " - Mono - Full Size"))
        )
   
        (command "_.psetupin" "C:/Apps/_Cad/2017_BU/BU/Templates/_CDD_MEP_PageSetUps.dwt" psetup) 
  
        (foreach layout (layoutlist)
          (setvar "ctab" layout)
          (COMMAND "-PLOT" "" "" psetup "" "NO" "YES" "NO")
          (C:ZE)
        )
      )
      (t (alert "Variable ps is nil"))
);end_cond

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

0 Likes
Message 4 of 10

Anonymous
Not applicable

Ok. The Upper/Lowercase shouldn't be an issue as I can see the blocks in my drawing that I'm trying to have it pick up on. And they're the same in both drawings. But I understand the theory and I will try to make it more common practice. 

And yes, I am a fan of the old school methods too - printing out and marking with pen. VLIDE has also been helpful - much the VBA viewer. 

(defun c:PAGE_CONFIG ()	

(setq Block_Col (vla-get-blocks (vla-get-ActiveDocument (vlax-get-acad-object))))
(setq BN nil)
(while (setq B (tblnext "block" (null B)))
	(setq BN (cons (cdr (assoc 2 B)) BN))
)

(foreach Name BN
	(setq blk (vl-catch-all-apply 'vla-Item (list Block_Col (strcat Name))))	
	(setq blk_name (strcase (vla-get-name blk)))
;...	
)
;... 
)

I've traced the issue back to this point here. 
A list is generated of all the blocks, and then "blk_name" becomes whatever the last listed block is called. 

 

 

I guess that's the problem! In the drawing that doesn't work, the last block listed is something like "_DOT" as oppose to something with "GRP-A0" etc in it. 

0 Likes
Message 5 of 10

cadffm
Consultant
Consultant

Excuse me, but the whole code is a bit nonsensical and I have no time (desire) to start with the basics, I wish you
a helper who takes a lot of time for you. I want to impart knowledge instead of presenting a ready-made solution
but that takes too much time for me.

 

Even if it would look very different with me, it is a better basic to start. Some good idea for you contain?

 

 

(if (findfile "C:/Apps/_Cad/2017_BU/BU/Templates/_CDD_MEP_PageSetUps.dwt")
    (progn
    (vlax-for PC (vla-get-plotconfigurations(vla-get-activedocument (vlax-get-acad-object))) (vla-delete PC)) ; I would like to delete jest unused PC at the end
    (command "_.psetupin" "C:/Apps/_Cad/2017_BU/BU/Templates/_CDD_MEP_PageSetUps.dwt" "*") ; thats just a simple way for a TEST
    (foreach layout (layoutlist); (setq layout (nth 0 (layoutlist)))
       (setq psetup nil)
       (cond ; search block in the layout with name pattern "*GRP-@#-*"
	     ((not(setq ss (ssget "_X" (list'(0 . "INSERT")'(2 . "*GRP-@#-*")(cons 410 layout))))); use dxf-Filter 2, only for NON-dynamic blocks
	      (alert (princ (strcat "Frame-Block didn't found for Layout " layout))))
	     ; more hits than one?
	     ((>(sslength ss)1)
	      (alert (princ (strcat "Multiple Frame-Blocks found in Layout " layout))))
	     ; Fity the Blockname wit the "SWLIST"? ; (setq bn (nth 1 SWLIST))
	     ((not(or (foreach bn swlist (if (wcmatch (strcase(cdr(assoc 2 (entget (ssname ss 0))))) (strcase (car bn))) (setq psetup (cdr bn)))) psetup))
	      (alert (princ (strcat "No known Frame-Block found in Layout " layout))))
	     (t ; All should works at this point, let's go.
	      
		; (setvar "CTAB" layout) ; Do you need this? Fields or RText in use? Then remove my comment sign ";"
	        (setvar "CMDECHO" 1)
		(COMMAND "_.-PLOT" "" layout psetup "" "_NO" "_YES" "_NO")
		(C:ZE)
	      
	      )
        )
    )
    (alert "ERROR: Findfile C:/Apps/_Cad/2017_BU/BU/Templates/_CDD_MEP_PageSetUps.dwt")
)


  

Sebastian

0 Likes
Message 6 of 10

ronjonp
Advisor
Advisor

If anything to help out HERE is how to easily localize your variables.

 

0 Likes
Message 7 of 10

Anonymous
Not applicable
Accepted solution

I have fixed it & will leave the solution here for those who might make use of it. 

 

(defun c:PAGE_CONFIG (/ ps blockss psetup)	

(setq ps (cond ((setq blockss (ssget "_X" '((2 . "*GRP-A0*")))) "A0")
			((setq blockss (ssget "_X" '((2 . "*GRP-A1*")))) "A1")
			((setq blockss (ssget "_X" '((2 . "*GRP-A2*")))) "A2")
			((setq blockss (ssget "_X" '((2 . "*GRP-A3*")))) "A3")
			((setq blockss (ssget "_X" '((2 . "*GRP-B1*")))) "B1")
		 (princ)
		 )
)	  

(if (wcmatch (strcase (getvar "dwgname")) "*-CO-*")
 	(setq psetup (strcat ps " - Colour - Full Size"))
	(setq psetup (strcat ps " - Mono - Full Size"))
)
   
(command "_.psetupin" "C:/Apps/_Cad/2017_BU/BU/Templates/_CDD_MEP_PageSetUps.dwt" psetup "N") 
	
(foreach layout (layoutlist)
	(setvar "ctab" layout)
	(COMMAND "-PLOT" "" "" psetup "" "NO" "YES" "NO")
	(C:ZE)
)
(princ) 
)

 The issue was coming from the beginning code - searching and returning a matching block name. It has been replaced with a simpler code. Now it is working fine. 

0 Likes
Message 8 of 10

ronjonp
Advisor
Advisor

 

Would you ever have more than one size of titleblock in a drawing? The conditional statement stops as soon as it finds a match so any other sizes will not be found.

 

 

Also if you're not doing anything with 'blockss' there is no reason to set it:

(setq ps (cond ((ssget "_X" '((2 . "*GRP-A0*"))) "A0")
	       ((ssget "_X" '((2 . "*GRP-A1*"))) "A1")
	       ((ssget "_X" '((2 . "*GRP-A2*"))) "A2")
	       ((ssget "_X" '((2 . "*GRP-A3*"))) "A3")
	       ((ssget "_X" '((2 . "*GRP-B1*"))) "B1")
	 )
)

And you should check if 'ps' exists before passing to strcat or it will chuck a wobbly 🙂

(if (and ps (wcmatch (strcase (getvar "dwgname")) "*-CO-*"))
  (setq psetup (strcat ps " - Colour - Full Size"))
  (setq psetup (strcat ps " - Mono - Full Size"))
)
0 Likes
Message 9 of 10

Anonymous
Not applicable

No, there should not be any other titleblocks. And yes, that is exactly what I want it to do. 

Ok thank you for those suggestions. 

0 Likes
Message 10 of 10

ronjonp
Advisor
Advisor

Glad to help out. Here is another way to extract the paper size:

(if (and (setq s (ssget "_X" '((2 . "*GRP-@#*"))))
	 (setq bn (strcase (cdr (assoc 2 (entget (ssname s 0))))))
    )
  (alert (setq ps (substr bn (+ 5 (vl-string-search "GRP-" bn)) 2)))
)
0 Likes