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

Finding Files Within Paths

10 REPLIES 10
Reply
Message 1 of 11
dbrblg
552 Views, 10 Replies

Finding Files Within Paths

I'm looking to be able to search for files within specific paths using Lisp.  So far I have the following code which provides an output but I need help in using that output to search for a file.

What I have so far is this:

(defun c:ace ( /)
  ; Make sure AcadE is "awake"
  (if(not wd_load)(if(setq x(findfile "wd_load.lsp"))(load x)))(wd_load)
  
  (if GBL_wd_prj
    (progn 
      (setvar "ATTREQ" 0)
      (setvar "CMDECHO" 0)
	  
      (setq x (c:wd_proj_wdp_data))
      (setq dwglst (nth 4 x))
	  (setq schematiclib (nth 1 dwglst))
	  (setq panellib (nth 3 dwglst))
	  (setq combined (strcat schematiclib panellib))
	  (setq combined (vl-remove-if '(lambda (y) (= y ""))(sparser combined ";")))
	  (princ combined) ; lets 'see' what we have!!
     )
   )
  (princ)
)

(defun sparser (str delim / ptr lst)
  (while (setq ptr (vl-string-search delim str))
    (setq lst (cons (substr str 1 ptr) lst))
    (setq str (substr str (+ ptr 2)))
  )
  (reverse (cons str lst))
)

 which outputs this:

(x:\Libs\IEC x:\Libs\PNEU x:\Libs\PANEL)

 This code is based on searching the Autocad Electrical project for the support paths specific to that particular project but I will need to adapt this to other situations (paths from different sources) in the future so the important thing at the moment is how can I feed the output into something that searches for a file, called symbols.dwg, for example in those paths?

 

Can anyone assist?

 

Many thanks

10 REPLIES 10
Message 2 of 11
marko_ribar
in reply to: dbrblg

One remark...
Change :
(princ combined) ; lets 'see' what we have!!
To :
(print combined) ; lets 'see' what we have!!
That way, output will be correct - list that conains strings that represent paths...

For your specific task, consider to add following at the end of your code :

 

(defun ff ( path file / fn )
  (if (setq fn (findfile (strcat path "\\" file)))
    (prompt "\nFile : ")(print fn)(prompt " - was found...")
  )
  fn
)

(defun fflst ( / fflst )
  (foreach path combined
    (setq fflst (cons (ff path "symbols.dwg") fflst))
  )
  (reverse fflst)
)

(print (fflst))
(princ)

 

Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 3 of 11
dbrblg
in reply to: marko_ribar

Hi Marko, thanks for your reply.

 

I've added the additional code to the end as suggested but all I get when I load the lsp file is "Command: ; error: syntax error"

 

Could you post the conbined code please so I can see whether I have the same thing as you intended?

 

Many thanks

Message 4 of 11
doni49
in reply to: dbrblg


@dbrblg wrote:

Hi Marko, thanks for your reply.

 

I've added the additional code to the end as suggested but all I get when I load the lsp file is "Command: ; error: syntax error"

 

Could you post the conbined code please so I can see whether I have the same thing as you intended?

 

Many thanks


The reason for the error lies in the IF statement in the first function.  In it's current form, autocad thinks you're passing 4 arguments to the if statement.  But the if statement can only accept two or three (a TEST conditon followed by what to do if the test is TRUE and optionally what to do if the test is FALSE).

I haven't tested the rest of the code, but it at least looks like it should work using the following code (I've marked my changes in RED.  The progn statement groups the three functions together so that the if statement reads them all as the true statement.

 

(defun ff ( path file / fn )
  (if (setq fn (findfile (strcat path "\\" file)))
    (PROGN(prompt "\nFile : ")(print fn)(prompt " - was found..."))
) fn ) (defun fflst ( / fflst ) (foreach path combined (setq fflst (cons (ff path "symbols.dwg") fflst)) ) (reverse fflst) ) (print (fflst)) (princ)

 

 



Don Ireland
Engineering Design Technician




If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.


Please do not send a PM asking for assistance. That's what the forums are for. This allows everyone to benefit from the question asked and the answers given.

Message 5 of 11
doni49
in reply to: doni49

I thought I'd explain a little more about the error on the if statement.

 

Look at the following.   The first argument in the if statement is the TEST CONDITION (is a less than b?).  Then acad will see the green as the TRUE statement and the red as the FALSE statement.  But then when it sees the magenta statement, it doesn't know what that's for so it throws a syntax error.

 

(if (a < b)(Princ "A is ")(Princ "Less than")(Princ "B"))

 

So if I change that to the following, acad will see everything within the progn as one single statement -- the TRUE statement is green still.

 

(if (a < b)(Progn(Princ "A is ")(Princ "Less than")(Princ "B")))

 



Don Ireland
Engineering Design Technician




If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.


Please do not send a PM asking for assistance. That's what the forums are for. This allows everyone to benefit from the question asked and the answers given.

Message 6 of 11
marko_ribar
in reply to: doni49

I am glad, my mistake was spoted... I throwed the code from back of my head, after all nobody's perfect... But what I am not glad is that OP wasn't the person that corrected me... Too much given-taken codes without thinking... I am just wondering when will peroson who post question on www start to solve his/her problems before they are posted... I just tried to answer to OP's question and put quick code examples that were not tested and that had sensible completness to what OP's searching for... The final step is on OP; he/she has to make it all work togeather and without errors... Just thinking if the example is considered on vaild way... It was not error that was the problem IMHO, but rather will the example be used in correct way - my code should be just help info; the real code is on OP to make posted example complete and with sense important to project OP's working on... OP's code that was posted looks like it was ripped out from some bigger and more complex lisp routine seeing that variables aren't localized, so if OP just want to see it working, he/she should consider implementing my example into it making it working all togeather, or make new command function from my example and start it after first command function was executed and because variables weren't localized they will be passed as globals to next routine that should follow it after which OP should still consider nil-ling (localizing) them in order to make CAD session cleaned from other lisp interferences...

Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 7 of 11
doni49
in reply to: marko_ribar

Marko,

 

No offense was meant.  We're all here to help (or get help). 

 

While I agree it's good if the OP can figure out on his own where suggested code has gone awry, that's not always possible.  In this case, the OP came back and asked for help in figuring out why it wasn't working. 

 

And I agree 100% -- nobody is perfect.  If you (or I) anyone (including me) posts code that has a glitch in it, if someone is able to assist in finding and fixing the issue, then GREAT.

 

I'm not perfect either and I have also occasionally posted code that when tested, did not work.



Don Ireland
Engineering Design Technician




If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.


Please do not send a PM asking for assistance. That's what the forums are for. This allows everyone to benefit from the question asked and the answers given.

Message 8 of 11
dbrblg
in reply to: doni49

Firstly, thanks to all contributers who have assisted, it is much appreciated.

 

Marko - Unfortunately we, which you yourself pointed out, are not perfect and I do not know that much about lisp as for me it is very occasionally I need to use it.  The reason for asking for assistance is that I could not get the code to work.  The day where the OP solves their problems before posting is the day where help forums will cease to exist.  People come to help forums for help and not ridicule.  

It was not my intention was never to criticise what you had posted rather seek further assistance.  I have been in discussions on other forums where the OP slated those helping them for posing incomplete code or code snippets but I don't agree with this; it not for me.  I feel it is a much better forum where discussions can take place where everyone will benefit for others experience so that the OP learns as do the people helping them and also casual watchers who can perhaps see something useful or inspiring.

Message 9 of 11
hmsilva
in reply to: dbrblg


@dbrblg wrote:

...

 This code is based on searching the Autocad Electrical project for the support paths specific to that particular project but I will need to adapt this to other situations (paths from different sources) in the future so the important thing at the moment is how can I feed the output into something that searches for a file, called symbols.dwg, for example in those paths?

...


Hi dbrblg,

 

to use the return from your c:ace function in one other function I would suggest the removal of the (princ) from the c:ace function and rename the function to ace,

 

(defun ace ( / ... );; localise the variables...
  ; Make sure AcadE is "awake"
...
	  (print combined) ; lets 'see' what we have!!
     )
   )
  ;(princ)<< this princ
)

 Then create a function to call this function and get the return at the command line

 

(defun c:ace ()
  (ace)
  (princ)
)

 

to test the existence of a file at those paths, perhaps something like this

(defun find-file (dwgname / dwg flst paths)
  (setq paths (ace))
  (foreach path paths
    (if (setq dwg (findfile (strcat path "\\" dwgname)))
      (setq flst (cons dwg flst))
    )
  )
  (if flst
    (print flst)
    (prompt (strcat "\n" dwgname " was not found!"))
  )
  (princ)
) ;; call with ;;(find-file "symbols.dwg")

 

Hope that helps

Henrique

EESignature

Message 10 of 11
hmsilva
in reply to: doni49


@doni49 wrote:

...

 We're all here to help (or get help). 

...

And I agree 100% -- nobody is perfect.  If you (or I) anyone (including me) posts code that has a glitch in it, if someone is able to assist in finding and fixing the issue, then GREAT.

...


1+

 

Henrique

EESignature

Message 11 of 11
dbrblg
in reply to: hmsilva

Thanks to all who have helped Smiley Happy
If it is of any use to anyone, I've included what I have; it is designed to look in the library paths of an Autocad Electrical project for a file and return the path if found.  I have combined suggestions and tidied up what I originally had - it works for me so I hope there are no issues.  As I said before, I am far from expert but this could be useful to others.  
(defun ACADEProjectPaths ( / x dwglst schamaticlib panellib combined y)
  ; Make sure AcadE is "awake"
  (if(not wd_load)(if(setq x(findfile "wd_load.lsp"))(load x)))(wd_load)
  
  (if GBL_wd_prj
    (progn 
      (setvar "ATTREQ" 0)
      (setvar "CMDECHO" 0)
	  
      (setq x (c:wd_proj_wdp_data))
      (setq dwglst (nth 4 x))
	  (setq schematiclib (nth 1 dwglst))
	  (setq panellib (nth 3 dwglst))
	  (setq combined (strcat schematiclib panellib))
	  (setq combined (vl-remove-if '(lambda (y) (= y ""))(sparser combined ";")))
     )
   )
)

(defun FindFilePath (dwgname / dwg flst paths path)
  (setq paths (ACADEProjectPaths))
  (foreach path paths
    (if (setq dwg (findfile (strcat path "\\" dwgname)))
      (setq flst (cons dwg flst))
    )
  )
  (if flst
    flst
	(princ)
  )
)

(defun Sparser (str delim / ptr lst)
  (while (setq ptr (vl-string-search delim str))
    (setq lst (cons (substr str 1 ptr) lst))
    (setq str (substr str (+ ptr 2)))
  )
  (reverse (cons str lst))
)

(defun c:ace () ;This is a test function!!
  (print (FindFilePath "ACADE Border.dwg"))
  (princ)
)

 

 

 

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

Post to forums  

Autodesk Design & Make Report

”Boost