Make field with part of drawing file name

Make field with part of drawing file name

LispResearch
Enthusiast Enthusiast
462 Views
5 Replies
Message 1 of 6

Make field with part of drawing file name

LispResearch
Enthusiast
Enthusiast

Could somebody help how to make two fields:

1) Takes the part of name until the first space 

123.A - hello_3.dwg -> "123.A"

2) Takes part of name after the second space until the first dot after second space

123.A - hello_3.dwg -> "hello_3"

 

I found how to take name

%<\AcVar Filename \f "%fn7">%

but dont understand how to use LISP or DIESEL to filter the parts.

 

Signature:
Interested in Lisp
0 Likes
463 Views
5 Replies
Replies (5)
Message 2 of 6

vladimir_michl
Advisor
Advisor

The first or last dot? The second one is quite easy:

%<\AcDiesel $(substr,$(getvar,dwgname),1,$(-,$(strlen,$(getvar,dwgname)),4))>%

or even

%<\AcVar Filename \f "%fn2">%

 

You probably won't be able to split strings directly in the field evaluator. But you can use an additional LISP function (and run it on an event, e.g. on Open, Regen and Save), e.g.:

(defun SplitStr ( s d / p )
(if (setq p (vl-string-search d s))
(cons (substr s 1 p) (SplitStr (substr s (+ p 1 (strlen d))) d)) (list s)))

(setq DWGNAMEspa (car (SplitStr (getvar "DWGNAME") " ")))

 

and then refer the auxiliar LISP variable with:

%<\AcVar.17.0 Lisp.dwgnamespa>%

 

And similarly the dot split if you need the first dot.

 

Vladimir Michl, www.arkance.world  -  www.cadforum.cz

 

Message 3 of 6

Sea-Haven
Mentor
Mentor

A bit of goggling. "The only "coding" you can embed into fields is Diesel codes." So what @vladimir_michl has suggetsed can be added in a field but you will have a problem with say "A12. XYZ " etc as the start does not have 5 characters, like "A123.".

 

 

 

Message 4 of 6

LispResearch
Enthusiast
Enthusiast

You are smiling too much... (joke, sorry)

Thank you for reply, but yes it is a nice way to remove extension, but then I receive a string that should be split between " - ". I meant the first dot after the second space, but this is solved by reducing the extension. 

So the first step is to remove extension ok:

123.A - hello_3.dwg -> "123.A - hello_3"

 

I often do the Regen but it is ok to process when opening too. 

1) Takes the part of name until the first space 

123.A - hello_3 -> "123.A"

123.AB - hello_3 -> "123.AB"

123.AB.01 - hello_3 -> "123.AB.01"

here the part "123.A" can vary in length "it can also be "123.AB" or "123.AB.01"

 

Right in the moment I am trying to recall how should I add the LISP function, so it will take some time for answer:

Let us suppose I would create a variable "Number" in parameters, then I open Visual LISP Editor and type:

(defun SplitStr ( s d / p )
(if (setq p (vl-string-search d s))
(cons (substr s 1 p) (SplitStr (substr s (+ p 1 (strlen d))) d)) (list s)))

(setq DWGNAMEspa (car (SplitStr (getvar "DWGNAME") " ")))

 

Then save it as application "DWGNumber.VLX"

 

And in parameters for Number variable I should in Expression use "DWGNumber.VLX"?

 

And then make a field where insert the "Number" variable as parameter.

Signature:
Interested in Lisp
0 Likes
Message 5 of 6

vladimir_michl
Advisor
Advisor

OK, understood. So you will definitely need the LISP coding. The code just sets the LISP variable DWGNAMEspa and you can then refer its value in the Field (as proposed). Similarly, for the "dot-split", add  a line:

(setq DWGNAMEdot (car (SplitStr (getvar "DWGNAME") ".")))

and then refer the DWGNAMEdot variable in your field.

 

You can auto-load your LSP or VLX file and let it execute so on any load of a drawing. Or you can add a reactor to run it on Regen or Save (this is more complicated).

 

Vladimir Michl, www.arkance.world  -  www.cadforum.cz

 

Message 6 of 6

Sea-Haven
Mentor
Mentor

This is what I use for splitting strings.

; tab 9 space 32 comma 44 semicolum 59 slash / 47

; thanks to Lee-mac for this defun
(defun csv->lst ( str / pos )
(if (setq pos (vl-string-position 32 str))
    (cons (substr str 1 pos) (csv->lst (substr str (+ pos 2))))
    (list str)
    )
)

(csv->lst "123.AB.01 - hello_3 ")
("123.AB.01" "-" "hello_3" "")