MarcoW Posted March 24, 2010 Posted March 24, 2010 Hello everybody, I am trying to get this done: - prompt user to browse for a specific folder (eg. shared on server) - if escape is pressed, or command cancelled somehow then do nothing, only alert for "Nothing changed". - if a folder is selected then use the result to make it the new PrinterStyleSheerDir. - Copy all files from the current PrinterStyleSheetDir to the new one and set the new one active. This is the code that I have been able to make with a lot of help here and there on the internet. (defun browsefolder (title / shlobj folder fldobj); [color=red]outval[/color] ; If outval is localize it is not working therefore after; (vl-load-com) (setq shlobj (vla-getinterfaceobject (vlax-get-acad-object) "Shell.Application" ) folder (vlax-invoke-method shlobj 'browseforfolder 0 title 0) ) (vlax-release-object shlobj) (if folder (progn (setq fldobj (vlax-get-property folder 'self) outval (vlax-get-property fldobj 'path) ) (vlax-release-object folder) (vlax-release-object fldobj) outval ) ) ) (browsefolder "Select shared folder") [color=red]; until here I have no trouble[/color] (if (not (= outval nil)) ( (setq cPrinterStyleSheetDir (strcat (getenv "PrinterStyleSheetDir") "\\") ) (foreach file (vl-directory-files cPrinterStyleSheetDir "*.*" 1) (vl-file-copy (strcat cPrinterStyleSheetDir file) (strcat outval file) ) ) (princ) (setenv "PrinterStyleSheetDir" outval) ); end if not nil (Alert "No folder selected - nothing changed") ); end if (princ) It has got to do with the if conditional... If I choose no folder (escape or cancel) then there is nothing wrong. If I do select a folder I keep on getting 2 errors allthough the folder is used. Can someone help me make this code much cleaner? I mean, it works but it has some nasty sideeffects. Tnx allready. Quote
MarcoW Posted March 24, 2010 Author Posted March 24, 2010 Ow yeah, there are the errors I get: Number 1: ; error: bad argument type: stringp nil Number 2: Huh... can't get it ... I am shure if I erase the if statement with all parenthesis it is working better. Only I need to make the if statement in case no folder is sleceted. Pff... Quote
Lee Mac Posted March 24, 2010 Posted March 24, 2010 Perhaps something like this: (defun browsefolder (title / shlobj folder fldobj outval) (vl-load-com) (setq shlobj (vla-getinterfaceobject (vlax-get-acad-object) "Shell.Application") folder (vlax-invoke-method shlobj 'browseforfolder 0 title 0)) (vlax-release-object shlobj) (if folder (progn (setq fldobj (vlax-get-property folder 'self) outval (vlax-get-property fldobj 'path)) (vlax-release-object folder) (vlax-release-object fldobj) outval))) (if (setq folder (browsefolder "Select shared folder")) (progn (setq cPrinterStyleSheetDir (strcat (getenv "PrinterStyleSheetDir") "\\")) (foreach file (vl-directory-files cPrinterStyleSheetDir "*.*" 1) (vl-file-copy (strcat cPrinterStyleSheetDir file) (strcat outval file))) (setenv "PrinterStyleSheetDir" outval)) (Alert "No folder selected - nothing changed")) (princ) The return of the browsefolder function was outval, so you can just test for this in the IF statement. I would recommend you give credit to the author of the browsefolder function (if it isn't you). Lee Quote
Lee Mac Posted March 24, 2010 Posted March 24, 2010 Marco, I'd recommend you read these threads: http://www.cadtutor.net/forum/showpost.php?p=173196&postcount=10 http://www.cadtutor.net/forum/showpost.php?p=240943&postcount=2 http://www.cadtutor.net/forum/showpost.php?p=273108&postcount=12 Quote
MSasu Posted March 24, 2010 Posted March 24, 2010 @MarcoW, also take care that should use PROGN statement in order to group if / else actions. Should be: (if (not (= outval nil)) (progn (setq cPrinterStyleSheetDir Instead of: (if (not (= outval nil)) ( (setq cPrinterStyleSheetDir Regards, Quote
Lee Mac Posted March 24, 2010 Posted March 24, 2010 Msasu, I like your avatar, you might like this thread Quote
alanjt Posted March 24, 2010 Posted March 24, 2010 @MarcoW, also take care that should use PROGN statement in order to group if / else actions. Should be: (if (not (= outval nil)) (progn (setq cPrinterStyleSheetDir Instead of: (if (not (= outval nil)) ( (setq cPrinterStyleSheetDir Regards, The (not (=... check isn't required. (if outval (setq cPrinterStyleSheetDir Quote
MarcoW Posted March 24, 2010 Author Posted March 24, 2010 Ah, I have Got the error back (I had not made outval local): ; error: bad function: "C:\\" (if selected C:\ drive) @Lee: Thanks for the reply, I will try your code soon. I would credit the author if I knew who he / she is. I usually tend to do so if there is a need for it. But okay, I have searched my IE Browse History and here is the original thread where I got the info from: http://forums.augi.com/archive/index.php/t-53632.html Quote
Lee Mac Posted March 24, 2010 Posted March 24, 2010 Ah, I have Got the error back (I had not made outval local): As, I say, Outval needn't be global, you can just test the return of the subfunction. Quote
MarcoW Posted March 24, 2010 Author Posted March 24, 2010 @ Lee I knew it was not okay not to localize outval. But I couldn't find my solution so I tried anything... Your comment was not entire clear to me. But the next commend was... I get the message now. Thanks for the links: I knew them and I read them every now and then. But due to "not programming enough" I keep loosing those things. But I should have known the progn statement, that was a mistake I must have seen myself. @ MSASU @MarcoW, also take care that should use PROGN statement in order to group if / else actions. Should be: (if (not (= outval nil)) (progn (setq cPrinterStyleSheetDir Instead of: (if (not (= outval nil)) ( (setq cPrinterStyleSheetDir Regards, As said to Lee: yes the progn... of course! That I should have known but I totally missed that one. @ Alanjt The (not (=... check isn't required True, thanks. Quote
MarcoW Posted March 24, 2010 Author Posted March 24, 2010 Lat issue solved: (foreach file (vl-directory-files cPrinterStyleSheetDir "*.ctb" 1) (vl-file-copy (strcat cPrinterStyleSheetDir file) (strcat outval [color=red]"\\"[/color] file) ) ) The red part I have added in order to make it work. Update: code did not work in new drawing: if outval is localized, it works. What am I to do with this? Can that be left like this? -> May I hereby thank everyone for the input given ! Quote
MarcoW Posted March 24, 2010 Author Posted March 24, 2010 The end: (defun browsefolder (title / shlobj folder fldobj outval) (vl-load-com) (setq shlobj (vla-getinterfaceobject (vlax-get-acad-object) "Shell.Application") folder (vlax-invoke-method shlobj 'browseforfolder 0 title 0)) (vlax-release-object shlobj) (if folder (progn (setq fldobj (vlax-get-property folder 'self) outval (vlax-get-property fldobj 'path)) (vlax-release-object folder) (vlax-release-object fldobj) outval))) (defun SetPlotterFiles ( / sFolder) (if (setq sFolder (browsefolder "Select shared folder")) (progn (setq cPrinterStyleSheetDir (strcat (getenv "PrinterStyleSheetDir") "\\")) (foreach file (vl-directory-files cPrinterStyleSheetDir "*.ctb" 1) (vl-file-copy (strcat cPrinterStyleSheetDir file) (strcat sFolder "\\" file))) (setenv "PrinterStyleSheetDir" (strcat sFolder "\\")) ) (Alert "No folder selected - nothing changed")) (princ) ) (SetPlotterFiles) (princ) I'm cool now... (I hope) 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.