TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: KOWBOI on May 27, 2010, 10:33:20 AM

Title: Using a field with filename - path only
Post by: KOWBOI on May 27, 2010, 10:33:20 AM
My apologies if this is not in the proper area. Feel free to move it if necessary.

I make use of fields in my title block that populate automatically. I want to add a field using the filename - path only but I would like to show only the last two back slashes in the path.

Instead of showing this:
S:\STONE_APPS\CUSTOMERS\JIM VAUGHN HOMES\24115

I would like to show this:
\JIM VAUGHN HOMES\24115

Is that possible?

Thanks for any and all comments/help.
Title: Re: Using a field with filename - path only
Post by: alanjt on May 27, 2010, 11:29:41 AM
Use a Diesel expression Field.

eg.
Code: [Select]
$(substr,$(getvar,dwgprefix),[color=red]NUMBER[/color])NUMBER being what you want to cut off.
Title: Re: Using a field with filename - path only
Post by: LE3 on May 27, 2010, 11:39:48 AM
lost my lisp powers some moons ago... but here i go - just to pretend...

Code: [Select]
(defun string-break  (str pat / i j lst)
  (setq i 0)
  (while (setq j (vl-string-search pat str i))
    (setq lst (cons (substr str (1+ i) (- j i)) lst))
    (setq i (+ j (strlen pat))))
  (mapcar (function (lambda (x) (vl-string-trim " " x)))
  (cons (substr str (1+ i)) lst)))
usage:
Code: [Select]
(setq filename "S:\\STONE_APPS\\CUSTOMERS\\JIM VAUGHN HOMES\\24115")
(setq items (reverse (string-break filename "\\")))
;;;(setq backslashes (- (length items) 1))
;;;\JIM VAUGHN HOMES\24115
(setq lsp (strcat "\\" (nth (- (length items) 2) items) "\\" (last items)))
then from field command you can use LispVariable - by selecting your lisp variable there
Title: Re: Using a field with filename - path only
Post by: KOWBOI on May 27, 2010, 11:46:58 AM
Thanks for the input. The number of characters before the last two backslashes changes. Is there a way to count only the backslashes?

I replied before I saw the second response. How exactly would I use the lisp code to set the field to the last two backslashes?
Title: Re: Using a field with filename - path only
Post by: alanjt on May 27, 2010, 11:51:31 AM
I replied before I saw the second response. How exactly would I use the lisp code to set the field to the last two backslashes?
Lisp variable Field
Title: Re: Using a field with filename - path only
Post by: KOWBOI on May 27, 2010, 12:31:23 PM
I must be missing something. I don't see a field option for lisp variable.
Title: Re: Using a field with filename - path only
Post by: LE3 on May 27, 2010, 12:33:10 PM
I must be missing something. I don't see a field option for lisp variable.
from A2010...
Title: Re: Using a field with filename - path only
Post by: KOWBOI on May 27, 2010, 01:01:17 PM
Ok, I'm on 2006 so I don't have the lisp variable option. Guess I should have been specific about which version I was using. Given that I'm on 2006 what are my options now?
Title: Re: Using a field with filename - path only
Post by: Lee Mac on May 27, 2010, 01:09:56 PM
How about setting the string you need to one of USERS1, and using a DIESEL field to retrieve it? Can't think of much else....  :|
Title: Re: Using a field with filename - path only
Post by: alanjt on May 27, 2010, 01:29:34 PM
How about setting the string you need to one of USERS1, and using a DIESEL field to retrieve it? Can't think of much else....  :|
Here's an example I did...http://www.cadtutor.net/forum/showthread.php?t=40653
Title: Re: Using a field with filename - path only
Post by: KOWBOI on May 27, 2010, 01:59:30 PM
Th IT Nazis here at work have that site blocked as an interest/hobby so I can't view the example. I'd love to see it though, can you post it here?
Title: Re: Using a field with filename - path only
Post by: alanjt on May 27, 2010, 02:16:11 PM
Th IT Nazis here at work have that site blocked as an interest/hobby so I can't view the example. I'd love to see it though, can you post it here?

Sure thing. :)

I save the entire page so you could read my post. Both LSP files are also zipped.
Title: Re: Using a field with filename - path only
Post by: LE3 on May 27, 2010, 02:50:35 PM
also, don't remember if RTEXT it is available on A2006, you might see if could be of some help too, maybe
Title: Re: Using a field with filename - path only
Post by: alanjt on May 27, 2010, 03:18:09 PM
also, don't remember if RTEXT it is available on A2006, you might see if could be of some help too, maybe
Heck yeah, I still use RText, but it will only accept Diesel.
Title: Re: Using a field with filename - path only
Post by: Lee Mac on May 27, 2010, 03:25:22 PM
Without a reactor, I think your first post Alan is the closest we are going to get somehow...
Title: Re: Using a field with filename - path only
Post by: alanjt on May 27, 2010, 03:25:57 PM
Without a reactor, I think your first post Alan is the closest we are going to get somehow...
Me too. :|
Title: Re: Using a field with filename - path only
Post by: LE3 on May 27, 2010, 03:28:34 PM
you guys are correct...

i did very little diesel in my lisper life, if i get a chance will try to play into the few functions available in there, as soon i finish some stuff here...
Title: Re: Using a field with filename - path only
Post by: Lee Mac on May 27, 2010, 03:30:00 PM
This is what I use for DIESEL:

http://www.crlf.de/Dokumente/Diesel/DieselReference.html (http://www.crlf.de/Dokumente/Diesel/DieselReference.html)
Title: Re: Using a field with filename - path only
Post by: alanjt on May 27, 2010, 03:32:52 PM
This is what I use for DIESEL:

http://www.crlf.de/Dokumente/Diesel/DieselReference.html (http://www.crlf.de/Dokumente/Diesel/DieselReference.html)
x2
Title: Re: Using a field with filename - path only
Post by: KOWBOI on May 27, 2010, 04:28:52 PM
I hardly know lisp and I zip about diesel but I will take a look, thanks. Alan, your first example would work if all the path names were consistent in length but they vary a great deal.
Title: Re: Using a field with filename - path only
Post by: alanjt on May 27, 2010, 04:44:43 PM
I hardly know lisp and I zip about diesel but I will take a look, thanks. Alan, your first example would work if all the path names were consistent in length but they vary a great deal.
Then you'll need a reactor. You could easily modify one of the two versions I posted to work with your needs.
Title: Re: Using a field with filename - path only
Post by: KOWBOI on May 27, 2010, 04:54:57 PM
Geez, I feel like a moron compared to you guys. I know zip about reactors as well as diesel.
Title: Re: Using a field with filename - path only
Post by: alanjt on May 27, 2010, 04:59:34 PM
I've posted both of these before, but I don't feel like digging them up.

This could (and definitely should) easily be consolidated (more concise and faster), but it should give you something to play with...

Code: [Select]
;;; Convert string to list, based on separator
;;; #Str - String to convert
;;; #Sep - Separator to break string into items
;;; Ex. - (AT:Str2Lst "1,2,3" ",") -> '("1" "2" "3")
;;; Alan J. Thompson, 11.11.09 / 04.01.10
(defun AT:Str2Lst (#Str #Sep / #Inc #List #Str)
  (while (setq #Inc (vl-string-search #Sep #Str))
    (setq #List (cons (substr #Str 1 #Inc) #List))
    (setq #Str (substr #Str (+ 2 #Inc)))
  ) ;_ while
  (vl-remove "" (reverse (cons #Str #List)))
) ;_ defun




;;; Convert List to String
;;; L - List to process
;;; S - Separator
;;; Ex. (AT:Lst2Str '("A" "B" "C") ",") -> "A,B,C"
;;; Alan J. Thompson, 04.01.10
(defun AT:Lst2Str (L S)
  (if (cdr L)
    (strcat (vl-princ-to-string (car L)) S (AT:Lst2Str (cdr L) S))
    (vl-princ-to-string (car L))
  ) ;_ if
) ;_ defun


Code: [Select]
(at:lst2str (cdddr (at:str2lst (getvar 'dwgprefix) "\\")) "\\")If you need to add an additional backslash to the end:
Code: [Select]
(strcat (at:lst2str (cdddr (at:str2lst (getvar 'dwgprefix) "\\")) "\\") "\\")
 :|
This is extremely ugly, but it's time to go home and I just wanted to give you some options.
Title: Re: Using a field with filename - path only
Post by: Lee Mac on May 27, 2010, 05:21:52 PM
Perhaps more concise:

Code: [Select]
(setq DWG (getvar 'DWGPREFIX))

(and (eq "\\" (substr DWG (strlen DWG)))
     (setq DWG (substr DWG 1 (1- (strlen DWG)))))       

(substr DWG (1+ (vl-string-position 92 (substr DWG 1 (vl-string-position 92 DWG 0 t)) 0 t)))
Title: Re: Using a field with filename - path only
Post by: Lee Mac on May 27, 2010, 05:27:53 PM
Geez, I feel like a moron compared to you guys. I know zip about reactors as well as diesel.

Don't feel bad about that - reactors are a more advanced topic of Visual LISP  ;-)
Title: Re: Using a field with filename - path only
Post by: KOWBOI on May 27, 2010, 05:41:45 PM
Perhaps more concise:

Code: [Select]
(setq DWG (getvar 'DWGPREFIX))

(substr DWG (1+ (vl-string-position 92 (substr DWG 1 (vl-string-position 92 DWG 0 t)) 0 t)))



Instead of showing this:
S:\STONE_APPS\CUSTOMERS\JIM VAUGHN HOMES\24115

I would like to show this:
\JIM VAUGHN HOMES\24115

With the code above I get this:
\\24115\\


Ok something else I know nothing about is visual lisp.
Title: Re: Using a field with filename - path only
Post by: Lee Mac on May 27, 2010, 05:50:01 PM
Don't worry, "\\" is correct - it is an escape character sequence for "\"
Title: Re: Using a field with filename - path only
Post by: KOWBOI on May 27, 2010, 05:52:00 PM
Right but I also need the string just ahead of the first \.
Title: Re: Using a field with filename - path only
Post by: Lee Mac on May 27, 2010, 05:53:58 PM
Ok, give this a try...

**  Create a FIELD, DieselExpression > in the box for the DIESEL expression type:

Code: [Select]
$(getvar,users1)
** Place the FIELD - it will probably show "----"  :-)

Now load this LISP:

Code: [Select]
(defun c:FilenameReactor nil
  (vl-load-com)
  ;; ©  Lee Mac  ~  27.05.10

  (  (lambda ( data foo / react )
       (if
         (setq react
           (vl-some
             (function
               (lambda ( reactor )
                 (if (eq data (vlr-data reactor)) reactor)
               )
             )
             (cdar (vlr-reactors :vlr-command-reactor))
           )
         )
         (if (vlr-added-p react)
           (vlr-remove react)
           (vlr-add react)
         )
         (setq react
           (vlr-command-reactor data
             (list
               (cons :vlr-CommandWillStart foo)
             )
           )
         )
       )
       (princ
         (if (vlr-added-p react)
           "\n** Reactor Activated **"
           "\n** Reactor Deactivated **"
         )
       )
       react
     )
    "Filename-Reactor"
    'Filename-CallBack
  )
 
  (princ)
)

(defun Filename-CallBack ( reactor args / pos str )
  (if (wcmatch (strcase (car args)) "*REGEN*")
    (progn
      (setq dwg (getvar 'DWGPREFIX))

      (and (eq "\\" (substr dwg (strlen dwg)))
           (setq dwg (substr dwg 1 (1- (strlen dwg)))))

      (and (setq pos (vl-string-position 92 dwg 0 t))
           (setq str (substr dwg 1 pos))
           (setq pos (vl-string-position 92 str 0 t))
           (setq dwg (substr dwg (1+ pos)))
           (setvar "USERS1" dwg)
      )
    )
  )
  (princ)
)


** Type "FilenameReactor" at the command line (only type this once... it is a toggle).

** Regen the drawing and hopefully smile  8-)


Or you can load and call the LISP first, then make the FIELD, whichever takes your fancy :P

Title: Re: Using a field with filename - path only
Post by: Lee Mac on May 27, 2010, 05:54:53 PM
Right but I also need the string just ahead of the first \.

Gotcha - try the code I posted to see if it works, and I'll update that for you.  :-)
Title: Re: Using a field with filename - path only
Post by: Lee Mac on May 27, 2010, 05:58:10 PM
OK, try the updated code  :-)
Title: Re: Using a field with filename - path only
Post by: KOWBOI on May 27, 2010, 06:17:11 PM
That worked, thank you Lee! Can I make this run automatically each time I start a new drawing?
Title: Re: Using a field with filename - path only
Post by: Lee Mac on May 27, 2010, 06:24:07 PM
Excellent  :lol:

Put the code in the ACADDOC.lsp with (c:FilenameReactor) after it  :-)
Title: Re: Using a field with filename - path only
Post by: It's Alive! on May 27, 2010, 07:28:40 PM
FYI there is an ARX sample in the SDK that has this functionality.  it also has the hook for the field dialog.
Title: Re: Using a field with filename - path only
Post by: alanjt on May 27, 2010, 07:54:45 PM
Perhaps more concise:

Code: [Select]
(setq DWG (getvar 'DWGPREFIX))

(substr DWG (1+ (vl-string-position 92 (substr DWG 1 (vl-string-position 92 DWG 0 t)) 0 t)))

Thanks Lee; very nice.
I knew there was a better way to write it, but it was time to leave and I wanted to post 'something'. :) I figured someone would post a better solution.
Title: Re: Using a field with filename - path only
Post by: alanjt on May 27, 2010, 07:56:04 PM
Oh yeah, know that the reactor will only work if the person opening the file is running it.
Title: Re: Using a field with filename - path only
Post by: Lee Mac on May 27, 2010, 08:02:55 PM
Thanks Alan - and good point indeed  8-)
Title: Re: Using a field with filename - path only
Post by: LE3 on May 27, 2010, 08:45:09 PM
ok... no idea if this miserable code is of some use and how about the vars names, but well here it goes (it can be use for at least extract the strings for the output path, and what it is available in a2006? for the field command):  :-P
Code: [Select]
;;;S:\STONE_APPS\CUSTOMERS\JIM VAUGHN HOMES\24115
;;;
;;;\JIM VAUGHN HOMES\24115
;;;
(setq filename "S:\\STONE_APPS\\CUSTOMERS\\JIM VAUGHN HOMES\\24115")
(setq list_1 (fnsplitl filename)) ;;("S:\\STONE_APPS\\CUSTOMERS\\JIM VAUGHN HOMES\\" "24115" "")
(setq _filename (car list_1) _l (cadr list_1)) ;; "S:\\STONE_APPS\\CUSTOMERS\\JIM VAUGHN HOMES\\" "24115"
(setq __sub (substr _filename 1 (- (strlen _filename) 1) )) ;;"S:\\STONE_APPS\\CUSTOMERS\\JIM VAUGHN HOMES"
(setq list_2 (fnsplitl __sub)) ;;("S:\\STONE_APPS\\CUSTOMERS\\" "JIM VAUGHN HOMES" "")
(setq item1 (car list_2) item2 (cadr list_2)) ;;"S:\\STONE_APPS\\CUSTOMERS\\" "JIM VAUGHN HOMES"
(setq backs (substr item1 (strlen item1)))
(setq path (strcat backs item2 backs _l)) ;;"\\JIM VAUGHN HOMES\\24115"
Title: Re: Using a field with filename - path only
Post by: LE3 on May 27, 2010, 10:05:41 PM
hmm... the old undocumented function fnsplitl does not play nice with long file names nor if the first front folder have empty spaces...
Title: Re: Using a field with filename - path only
Post by: KOWBOI on May 28, 2010, 09:29:49 AM
Oh yeah, know that the reactor will only work if the person opening the file is running it.

What do you mean the person running the file? It has to be loaded on every machine right?
Title: Re: Using a field with filename - path only
Post by: alanjt on May 28, 2010, 09:33:46 AM
Oh yeah, know that the reactor will only work if the person opening the file is running it.

What do you mean the person running the file? It has to be loaded on every machine right?
Correct.
Title: Re: Using a field with filename - path only
Post by: KOWBOI on May 28, 2010, 09:46:29 AM
Cool, thank you all so much for the help it's greatly appreciated.

I setup the acaddoc.lsp with the code, works great! Thanks again, you guys are a huge help and a great resource of info.
Title: Re: Using a field with filename - path only
Post by: Lee Mac on May 28, 2010, 12:03:16 PM
You're welcome :-)

So bear in mind that if you send the file to clients - the lack of reactor means that the FIELD will not display correctly.
Title: Re: Using a field with filename - path only
Post by: LE3 on May 28, 2010, 09:19:03 PM
You're welcome :-)

So bear in mind that if you send the file to clients - the lack of reactor means that the FIELD will not display correctly.
<out of scope>
every time I read that it is always translated to beer in mind.... in all my 38 years I have speaking English....  :ugly:
<out of scope>
Title: Re: Using a field with filename - path only
Post by: Jeff_M on May 29, 2010, 01:33:48 AM
You're welcome :-)

So bear in mind that if you send the file to clients - the lack of reactor means that the FIELD will not display correctly.
<out of scope>
every time I read that it is always translated to beer in mind.... in all my 38 years I have speaking English....  :ugly:
<out of scope>
Makes sense to me, Luis! :-D I do the same thing and I've never spoke another language...well, except for German during 3 years in high school....
Title: Re: Using a field with filename - path only
Post by: Kerry on May 29, 2010, 01:59:44 AM

Some words make me think of bourbon ... but thats my personal problem  :wink:
Title: Re: Using a field with filename - path only
Post by: LE3 on May 29, 2010, 01:41:32 PM

Some words make me think of bourbon ... but thats my personal problem  :wink:
That appears to be whiskey, don't think I ever had tried....
Title: Re: Using a field with filename - path only
Post by: LE3 on May 29, 2010, 01:45:38 PM
You're welcome :-)

So bear in mind that if you send the file to clients - the lack of reactor means that the FIELD will not display correctly.
<out of scope>
every time I read that it is always translated to beer in mind.... in all my 38 years I have speaking English....  :ugly:
<out of scope>
Makes sense to me, Luis! :-D I do the same thing and I've never spoke another language...well, except for German during 3 years in high school....
  :lol:
Title: Re: Using a field with filename - path only
Post by: KOWBOI on June 01, 2010, 10:18:26 AM
You're welcome :-)

So bear in mind that if you send the file to clients - the lack of reactor means that the FIELD will not display correctly.

Not a problem, we don't send electronic files to anyone. Thanks again for all the help  :-)
Title: Re: Using a field with filename - path only
Post by: Lee Mac on June 01, 2010, 10:18:56 AM
You're very welcome  :-)
Title: Re: Using a field with filename - path only
Post by: KOWBOI on October 27, 2010, 05:49:34 PM
Lee, I've been using this setup with great success until recently. For some reason lately the field does not display properly much of the time unless I force a Regen. Any ideas or suggestions? Thanks for the help.
Title: Re: Using a field with filename - path only
Post by: Lee Mac on October 27, 2010, 05:51:51 PM
The code reacts upon the Regen command, yes.
Title: Re: Using a field with filename - path only
Post by: mjfarrell on October 27, 2010, 06:59:22 PM
Lee, I've been using this setup with great success until recently. For some reason lately the field does not display properly much of the time unless I force a Regen. Any ideas or suggestions? Thanks for the help.
you will want to investigate the FIELDEVAL system variable


Controls how fields are updated.

The setting is stored as a bitcode using the sum of the following values:

0
 Not updated
 
1
 Updated on open
 
2
 Updated on save
 
4
 Updated on plot
 
8
 Updated on use of ETRANSMIT
 
16
 Updated on regeneration
 

NoteThe Date field is updated by UPDATEFIELD, but it is not updated automatically based on the setting of the FIELDEVAL system variable.
Title: Re: Using a field with filename - path only
Post by: KOWBOI on October 28, 2010, 09:00:06 AM
Ok so I already had all the field update settings turned on. The code that Lee setup for me (thanks again Lee) had a Regen command in it, I also tried putting a Regen command in my Acaddoc.lsp but I still have to force an update for that particular field by manually typing Regen at the command line. Any thoughts/suggestions?
Title: Re: Using a field with filename - path only
Post by: Lee Mac on October 28, 2010, 10:43:28 AM
My code creates a field dependent upon one of the custom system variables 'USERS#' - this is updated when the command reactor callback is triggered upon a Regen being called.

Lee
Title: Re: Using a field with filename - path only
Post by: KOWBOI on October 28, 2010, 10:59:34 AM
Thanks Lee. Shouldn't the field update automatically with your code since there is a Regen command in there? Also, I put a Regen command in Acaddoc.lsp and I'm not getting a Regen. Any ideas?
Title: Re: Using a field with filename - path only
Post by: Lee Mac on October 28, 2010, 11:01:39 AM
Thanks Lee. Shouldn't the field update automatically with your code since there is a Regen command in there? Also, I put a Regen command in Acaddoc.lsp and I'm not getting a Regen. Any ideas?

The field will only update when you perform a regen, as this is when the command reactor is triggered.
Title: Re: Using a field with filename - path only
Post by: roy_043 on October 28, 2010, 12:40:26 PM
Just a thought: couldn't you use s::startup to handle the regen?
Title: Re: Using a field with filename - path only
Post by: KOWBOI on October 28, 2010, 01:50:13 PM
I tried using this in the Acaddoc.lsp but still no Regen at startup.

Code: [Select]
(defun-q S::STARTUP ( )
  (command "REGEN")
 )

Am I understanding the concept correctly?
Title: Re: Using a field with filename - path only
Post by: roy_043 on October 28, 2010, 06:59:48 PM
Could it be that you already have s::startup in some file?:
http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%202010%20User%20Documentation/files/WS73099cc142f4875513fb5cd10c4aa30d6b-7f62.htm

I don't know if s::startup can solve your problem, but your code should result in a regen.
Title: Re: Using a field with filename - path only
Post by: KOWBOI on November 01, 2010, 05:23:24 PM
The startup still didn't force a regen when opening a drawing using this code. I'm stumped.
Title: Re: Using a field with filename - path only
Post by: mjfarrell on November 01, 2010, 05:45:20 PM
The startup still didn't force a regen when opening a drawing using this code. I'm stumped.
what happens if you place the same field manually?