Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

error: bad argument type: stringp nil

17 REPLIES 17
SOLVED
Reply
Message 1 of 18
rhirtleii
30046 Views, 17 Replies

error: bad argument type: stringp nil

hi, I m trying to open a folder from inside cad using lisp, otherthan current folder. But I keep getting an error and i have tried several different methods.

I could definately use some help with this one!! see code below, My first one works which is a very common one for most, but the second is the one causing an error.

 

;starts windows explorer in the current drawing directory.
(defun c:CF ()
(startapp "explorer" (strcat "/J, "(getvar "dwgprefix")))
(princ)
)

(defun N:LIB()
(startapp "explorer" (strcat "EXPLORER/N,""\\CAD\\"CAD_LIBRARY""))
(princ)
)

 

17 REPLIES 17
Message 2 of 18
p_mcknight
in reply to: rhirtleii

The strinp nil error means you are trying to pass a nil value to a function as a string.  The only place I see you passing a variable to a string function is the CAD_LIBRARY in the strcat function.  Make sure that this is a valid string value before passing it.  You can do that any number of ways.  For debug purposes you might just princ it to the command line.  If you want to trap it as an error perhaps check the type of the variable before running startapp.  If its not a string then don;t run the startapp and alert the user to the issue.

Message 3 of 18
rhirtleii
in reply to: p_mcknight

i'm a little green with all this stuff, so how would I actually trap it to find out my error?

Message 4 of 18
p_mcknight
in reply to: rhirtleii

There are a couple of ways to do that.  For debug purposes the greenest way would be to add (if (= nil CAD_LIBRARY) (alert "CAD_LIBRARY is nil")) before you call the startapp function.  This will tell you if that is in fact causing the issue.  Once known, look at your routine that sets the CAD_LIBRARY variable and see why it is either being set as nil or not set at all.  For curiosities sake, could you post the actual filepath of the folder you are trying to open?

Message 5 of 18
dmfrazier
in reply to: rhirtleii

I think you just had your quote marks a little skashod.  Each "piece" being concatenated must be surrounded by double-quotes unless it is a symbol for a string value.

 

Assuming "CAD_LIBRARY" is being set to a valid string somewhere else, try: (strcat "EXPLORER/N," "\\CAD\\" CAD_LIBRARY).

(Note that you don't need to put spaces between the "pieces" being concatenated, but I like to include them for readability.)

 

So, (setq CAD_LIBRARY "MyLibraryFolder") would return "MyLibraryFolder" (with the quotes already).

 

Then, (strcat "EXPLORER/N," "\\CAD\\" CAD_LIBRARY) would return "EXPLORER/N,\\CAD\\MyLibraryFolder" (including the quotes).

 

 

Message 6 of 18
rhirtleii
in reply to: p_mcknight

N:\CAD\CAD LIBRARY, i actually thought this would be simple, but it keeps opening me to MY DOCUMENTS C: instead. not sure why
Message 7 of 18
rhirtleii
in reply to: rhirtleii

I tried this and it returned a error: syntax error

 

;opens cad library folder

(defun N:LIB()
(setq CAD LIBRARY "Mylibraryfolder")
(strcat "EXPLORER/N," "\\CAD\\" CAD LIBRARY)
(princ)
)

 

so then i removed (setq CAD LIBRARY "Mylibraryfolder") and it launched with no errors, but when I type in the command LIB it launchs autocad LIST?

 

Message 8 of 18
dmfrazier
in reply to: rhirtleii

If the path Explorer is being told to open doesn't exist, Explorer opens (by design) to My Documents.

 

If the path is fixed, there's no need to do any concatenating.

 

In this case, you would change the syntax significantly:

(startapp "explorer" (strcat "EXPLORER/N,""\\CAD\\"CAD_LIBRARY"")) would become:

(startapp "explorer" "N:\\CAD\\CAD_LIBRARY")

 

However, if the path changes (fully or partially), as it may if your program prompts for user input or selection, then you would want to use a symbol (variable) in place of the fixed path (as shown in my previous post).

 

Message 9 of 18
dmfrazier
in reply to: rhirtleii

I apologize if this is getting "confused" (in addition to being "confusing").

 

Until your last post, it was not clear to me what your intention was with the "EXPLORER/N," part of the argument for startapp.  (I thought it might be some special command switch that I wasn't familiar with, but now I see - I think - that you were trying to indicate a drive letter.)

 

Your code is failing because it's missing a few things.

 

Assuming you want to open Explorer to a subfolder (which may vary in some way) of N:\CAD, you can still 'hardcode" as much of the path that doesn't vary in the argument to the startapp function (but you have to remember to include the startapp function in your overall function Smiley Wink).  Here is a "corrected" version of your code, assuming I understand what you are trying to do:

 

(defun C:LIB()
(setq CAD_LIBRARY "Mylibraryfolder")
(startapp "explorer" (strcat "N:\\CAD\\" CAD_LIBRARY))
(princ)
)

 

Note that it was necessary to change the function name to "C:LIB" (AutoLISP convention) and to add the underbar character into the symbol name "CAD_LIBRARY", which was missing in the code you posted, making AutoLISP interpret it as two separate strings.

 

(Note also that the folder must exist for Explorer to open it.  If it doesn't exist, Explorer will open to your "My Documents" folder.)

Message 10 of 18
rhirtleii
in reply to: dmfrazier

Tthat worked to some degree, I replaced the code with your new one, but it returns my C:\Users\myname\Documents, not the desired folder which is on my N:drive.

 

;opens cad library folder
(defun C:LIB()
(setq CAD_LIBRARY "Mylibraryfolder")
(startapp "explorer" (strcat "N:\\CAD\\" CAD_LIBRARY))
(princ)
)

Message 11 of 18
dmfrazier
in reply to: rhirtleii

Re-read my last (two) post(s) and see if you can tell me why.

 

Hint: If there isn't an actual folder named "N:\CAD\MyLibraryFolder", then what will happen? Smiley Wink

 

If that is not the folder you want Explorer to open to, then you would have to change the program so that it specifies, either in the "setq" line or in the "startapp" line, the name of the actual folder you want Explorer to open.

 

Note that the only reason for using the "setq" line is if the last part of the path is going to vary (and if this is true, there would still need to be some changes to the code to enable the variation, such as prompting for user input or selection).  If the path is always the same ("fixed"), then you can leave out that part and add the actual folder name to the startapp argument (and remove the "strcat", since there's nothing to concatenate), like so:

 

(defun C:LIB()
(startapp "explorer" "N:\\CAD\\CAD_LIBRARY")
(princ)
)

 

(Is it starting to make more sense, now?)

Message 12 of 18
rhirtleii
in reply to: dmfrazier

haha, yup. I have to be honest I just copied and ran it. Your right i dont have a "mylibraryfolder" I guess my first cup of coffee has not kicked in yet. Thanks for all your help. So I made it a (fixed) component but it still does not open the desired path. C:users\etc\etc

It just appears so simple but yet its like N:drive wont launch.

Message 13 of 18
Gary_J_Orr
in reply to: dmfrazier


@dmfrazier wrote:

Re-read my last (two) post(s) and see if you can tell me why.

 

Hint: If there isn't an actual folder named "N:\CAD\MyLibraryFolder", then what will happen? Smiley Wink

 

If that is not the folder you want Explorer to open to, then you would have to change the program so that it specifies, either in the "setq" line or in the "startapp" line, the name of the actual folder you want Explorer to open.

 

Note that the only reason for using the "setq" line is if the last part of the path is going to vary (and if this is true, there would still need to be some changes to the code to enable the variation, such as prompting for user input or selection).  If the path is always the same ("fixed"), then you can leave out that part and add the actual folder name to the startapp argument (and remove the "strcat", since there's nothing to concatenate), like so:

 

(defun C:LIB()
(startapp "explorer" "N:\\CAD\\CAD_LIBRARY")
(princ)
)

 

(Is it starting to make more sense, now?)


dmfrasier,

Reading the OP's recent post on the actual path string and this post...

 

"N:\CAD\CAD LIBRARY, i actually thought this would be simple, but it keeps opening me to MY DOCUMENTS C: instead. not sure why"

 

he/she will still experience a failure: As you have gone through your explainations and variations you have looped around a couple of times in what you are processing...

 

To the OP:

If your path will always be the same and you want to determine what path to create for Vlisp then open explorer manually, navigate to the folder, then copy the path... my guess will be that it will look like your previous post:

"N:\CAD\CAD LIBRARY"

Paste that path into dm's lisp code

 

(defun C:LIB()
(startapp "explorer" "N:\CAD\CAD LIBRARY")
(princ)
)

 

now, every place that has a backslash (\) add a second backslash to it (backslashes are "control characters" in lisp so a second one is required at each occurance:

 

(defun C:LIB()

(startapp "explorer" "N:\\CAD\\CAD LIBRARY")

(princ)

)

 

you're done.

 

-Gary

Gary J. Orr
(Your Friendly Neighborhood) CADD/BIM/VDC Applications Manager
http://www.linkedin.com/in/garyorr

aka (current and past user names):
Gary_J_Orr (GOMO Stuff 2008-Present); OrrG (Forum Studio 2005-2008); Gary J. Orr (LHB Inc 2002-2005); Orr, Gary J. (Gossen Livingston 1997-2002)
Message 14 of 18
dmfrazier
in reply to: Gary_J_Orr

Yes, I was actually just in the midst of posting a similar conclusion/explanation when your post popped up.

 

It helps to be clear on what the actual path is. Smiley Wink

Message 15 of 18
rhirtleii
in reply to: dmfrazier

sorry for the confusion, I do appreciate all your help in tyring to resolve the problem.

Message 16 of 18
dmfrazier
in reply to: rhirtleii

It was fun, despite the confusion.

 

I hope you learned something and decide to continue to learn.  Programming in AutoLISP can be very challenging, but it can also be very rewarding.

Message 17 of 18
Gary_J_Orr
in reply to: dmfrazier


@dmfrazier wrote:

It was fun, despite the confusion.

 

I hope you learned something and decide to continue to learn.  Programming in AutoLISP can be very challenging, but it can also be very rewarding.


I second that.

 

-G

Gary J. Orr
(Your Friendly Neighborhood) CADD/BIM/VDC Applications Manager
http://www.linkedin.com/in/garyorr

aka (current and past user names):
Gary_J_Orr (GOMO Stuff 2008-Present); OrrG (Forum Studio 2005-2008); Gary J. Orr (LHB Inc 2002-2005); Orr, Gary J. (Gossen Livingston 1997-2002)
Message 18 of 18
amirbSPDJQ
in reply to: rhirtleii

Hello i have been trying to run this lisp and after selecting all of my objects it shows the error: bad argument type: stringp nil

 

Im trying to pick two blocks and a line and write the blocks name into attributes of another block and write the line's length into another attribute, here is the code (i dont know much about lisp, i just took the code from the forum and try to modified it):

 

(vl-load-com)

(defun c:lenB (/ atts i obj el1 el2 pl blk)

(while (and

 

(setq el1 (car (entsel "\nSelect first element: ")))
(or (= "INSERT" (cdr (assoc 0 (entget el1))))
(prompt "\nWrong select, need block."))

(setq el2 (car (entsel "\nSelect second element: ")))
(or (= "INSERT" (cdr (assoc 0 (entget el2))))
(prompt "\nWrong select, need block."))

(setq pl (car (entsel "\nSelect line to measure <exit>: ")))
(or (wcmatch (cdr (assoc 0 (entget pl))) "ARC,CIRCLE,ELLIPSE,HATCH,*POLYLINE,REGION,SPLINE")
(prompt "\nWrong selection, need ARC,CIRCLE,ELLIPSE,HATCH,*POLYLINE,REGION,SPLINE."))

(setq blk (car (entsel "\nSelect block to rewrite: ")))
(or (= "INSERT" (cdr (assoc 0 (entget blk))))
(prompt "\nWrong select, need block."))
(setq obj (vlax-ename->vla-object blk))
(setq atts (vlax-invoke obj "GetAttributes"))


)
(foreach att atts


(cond ((= (vla-get-tagstring att) "E2")
(vla-put-textstring att (strcat
"%<\\AcObjProp Object(%<\\_ObjId "
(LM:objectid (vlax-ename->vla-object el2))
">%).EffectiveName \f "%tc1">%" ))
(vl-cmdf "_.updatefield" (vlax-vla-object->ename att) ""))

((= (vla-get-tagstring att) "E1")
(vla-put-textstring att (strcat
"%<\\AcObjProp Object(%<\\_ObjId "
(LM:objectid (vlax-ename->vla-object el1))
">%).EffectiveName \f "%tc1">%" ))
(vl-cmdf "_.updatefield" (vlax-vla-object->ename att) ""))

((= (vla-get-tagstring att) "LEN")
(vla-put-textstring att (strcat
"%<\\AcObjProp Object(%<\\_ObjId "
(LM:objectid (vlax-ename->vla-object pl))
">%).Length \\f \"" "%lu2%pr1%ct8[1.000000000000000E-00]" "\">%" ))
(vl-cmdf "_.updatefield" (vlax-vla-object->ename att) "")))))
(princ)
)


(defun LM:objectid ( obj )
(eval
(list 'defun 'LM:objectid '( obj )
(if (wcmatch (getenv "PROCESSOR_ARCHITECTURE") "*64*")
(if (vlax-method-applicable-p (vla-get-utility (LM:acdoc)) 'getobjectidstring)
(list 'vla-getobjectidstring (vla-get-utility (LM:acdoc)) 'obj ':vlax-false)
'(LM:ename->objectid (vlax-vla-object->ename obj))
)
'(itoa (vla-get-objectid obj))
)
)
)
(LM:objectid obj)
)

;; Entity Name to ObjectID - Lee Mac
;; Returns the 32-bit or 64-bit ObjectID for a supplied entity name

(defun LM:ename->objectid ( ent )
(LM:hex->decstr
(setq ent (vl-string-right-trim ">" (vl-prin1-to-string ent))
ent (substr ent (+ (vl-string-position 58 ent) 3))
)
)
)

;; Hex to Decimal String - Lee Mac
;; Returns the decimal representation of a supplied hexadecimal string

(defun LM:hex->decstr ( hex / foo bar )
(defun foo ( lst rtn )
(if lst
(foo (cdr lst) (bar (- (car lst) (if (< 57 (car lst)) 55 48)) rtn))
(apply 'strcat (mapcar 'itoa (reverse rtn)))
)
)
(defun bar ( int lst )
(if lst
(if (or (< 0 (setq int (+ (* 16 (car lst)) int))) (cdr lst))
(cons (rem int 10) (bar (/ int 10) (cdr lst)))
)
(bar int '(0))
)
)
(foo (vl-string->list (strcase hex)) nil)
)

;; Active Document - Lee Mac
;; Returns the VLA Active Document Object

(defun LM:acdoc nil
(eval (list 'defun 'LM:acdoc 'nil (vla-get-activedocument (vlax-get-acad-object))))
(LM:acdoc)
)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost