diesel expression:Counting down from the end

diesel expression:Counting down from the end

Vuxvix
Advocate Advocate
1,392 Views
15 Replies
Message 1 of 16

diesel expression:Counting down from the end

Vuxvix
Advocate
Advocate

Hi!

My file name form: AAAA-BBBB-CCCC
Inside: AAAA and CCC was fixed
BBBB is the name of each drawing.
I'm using the Diesel function to insert a name (BBBB) into a drawing canvas.
$(SUBSTR,$(GETVAR,DWGNAME),No.1,$(-,$(STRLEN,$(GETVAR,DWGNAME)),No.2))
Inside:
No.1: The start letter of the string
No.2: the starting letter of the omitted part.
Problem: Because BBBB item is not fixed number of letters. Is there any way to remove the CCCC part. By way of counting down from the end?
Thank you very much

0 Likes
1,393 Views
15 Replies
Replies (15)
Message 2 of 16

pbejse
Mentor
Mentor

Try this

$(substr,$(getvar,dwgname),6,$(-,$(strlen,$(getvar,dwgname)),14))
Message 3 of 16

Vuxvix
Advocate
Advocate

Hi!

Sorry, I don't see anything different with my Diesel Line!?

0 Likes
Message 4 of 16

pbejse
Mentor
Mentor

@Vuxvix wrote:

Hi!

Sorry, I don't see anything different with my Diesel Line!?


That means i definitely misunderstood the question :D, that is also how i would do it with Diesel

What's supposed to be the result for this anyway?

  • AAAA-BBBB-CCCC.dwg
  • AAAA-BANANA-CCCC.dwg
0 Likes
Message 5 of 16

Kent1Cooper
Consultant
Consultant

@Vuxvix wrote:

.... Because BBBB item is not fixed number of letters. Is there any way to remove the CCCC part. By way of counting down from the end?....


AutoLisp can do that easily, with the (vl-string-position) function, which can find the position of the last hyphen in a string by looking for it from the end.  The Diesel functions reference does not list any function that appears capable of doing the same.  Is AutoLisp code an option for you?

Kent Cooper, AIA
0 Likes
Message 6 of 16

Vuxvix
Advocate
Advocate
Hi!
this is my example for Drawing's name: R-03-06_4FPlan (Drafting).
I just need: "4FPlan" to display as title in Drawing frame.
Currently I only know about Diesel Function and some default fields in Autocad. I think there is no restriction in using Lisp.
Also, I don't know if using Lisp will display correctly on other PC. Because I have had cases where Fields show #### results on other PC.
Thanks!
0 Likes
Message 7 of 16

Vuxvix
Advocate
Advocate

Hi funny guy!
The result I want is: BBBB.
It will display as the title in the drawing Frame. So I don't need its prefix and suffix (nor dwg format)

0 Likes
Message 8 of 16

Kent1Cooper
Consultant
Consultant

@Vuxvix wrote:
My file name form: AAAA-BBBB-CCCC ....
The result I want is: BBBB. ....
-- and --
this is my example for Drawing's name: R-03-06_4FPlan (Drafting).
I just need: "4FPlan" to display as title in Drawing frame.
....

Those are very different.  Are you looking for the part between two hyphens, or the part between an underscore character and a space?  Are there any other configurations?

Kent Cooper, AIA
0 Likes
Message 9 of 16

Vuxvix
Advocate
Advocate

Follow the way of counting the number of letters as the original way.

I have given the form in an A-B-C Form.
Part [-C]: will depend on the type of drawing. With [space], [-] or [a specified character after], can lisp  do it!? Thanks

0 Likes
Message 10 of 16

Kent1Cooper
Consultant
Consultant

@Vuxvix wrote:

Follow the way of counting the number of letters as the original way.

I have given the form in an A-B-C Form.
Part [-C]: will depend on the type of drawing. With [space], [-] or [a specified character after], can lisp  do it!? Thanks


I doubt it can do it reliably, given the examples.  Both  AAAA-BBBB-CCCC  and  R-03-06_4FPlan (Drafting)  contain two hyphens; in one of them you want what's between the hyphens, but in the other you want something else.  You're going to need some criteria that are more consistent.

Kent Cooper, AIA
0 Likes
Message 11 of 16

Vuxvix
Advocate
Advocate

I hope this makes it easier to understand

キャプチャ.JPG

0 Likes
Message 12 of 16

pbejse
Mentor
Mentor

@Vuxvix wrote:
Hi!
this is my example for Drawing's name: R-03-06_4FPlan (Drafting).
I just need: "4FPlan" to display as title in Drawing frame.

I still dont see why what you had on your diesel expression wouldnt work if the number of characters for the PREFIX and SUFFIX is constant.

 

Unless you have different cases like the one you just posted as R-03-06_4FPlan (Drafting).dwg then you may need to use a system variable for the for both No.1 and No.2 assign via lisp

 

$(substr,$(getvar,dwgname),$(getvar,useri1),$(-,$(getvar,useri2),$(getvar,useri1)))
(defun RunAtStartpp ()
  (cond
    ((zerop (getvar 'DwgTitled)))
    ((And
       (setq fname (getvar 'dwgname))
       (setq p (vl-string-position 32 fname nil t ))) ; space
       (setvar 'USeri1 9)(setvar 'USeri2 (1+ p))
     )
       
     ((setq p (vl-string-position 45 fname nil t )) ; Hyphen/dash
       (setvar 'USeri1 6)(setvar 'USeri2 (1+ p))
       )
       )
     )

 

[ One just need to add more conditions ]

 

OR just this if you're going to with the lisp route 

 

$(getvar,users1)
(defun RunAtStartpp ()
  (cond
    ((zerop (getvar 'DwgTitled)))
    ((And
       (setq fname (getvar 'dwgname))
       (setq p (vl-string-position 32 fname nil t))
     )					; space
     (setvar 'Users1 (substr (getvar 'dwgname) 9 (- (1+ p) 9)))
    )
    ((setq p (vl-string-position 45 fname nil t)) ; Hyphen/dash
     (setvar 'Users1 (substr (getvar 'dwgname) 6 (- (1+ p) 6)))
    )
  )
)

 

 


@Vuxvix wrote:
Because I have had cases where Fields show #### results on other PC.

Thats the bit that concerns me. Why would it behave that way on other PC? 

Is it because its Autocad LT?

 

 

Message 13 of 16

Kent1Cooper
Consultant
Consultant

So Message 1 should have started with:

 

My file name form: AAAA_BBBB CCCC

 

Is that correct?

Kent Cooper, AIA
0 Likes
Message 14 of 16

Kent1Cooper
Consultant
Consultant

@Kent1Cooper wrote:

So Message 1 should have started with:

My file name form: AAAA_BBBB CCCC

....


If that's the real situation, and there is only one underscore character and one space, then there's no need to find the space by counting back from the end.  That would only have been necessary in the original format containing two of the same character [hyphens] that you want to break it around.

 

This will get what you want out of a string like the above:

 

(setq str "AAAA_BBBB CCCC")

(substr str (+ (vl-string-position 95 str) 2) (- (strlen str) (vl-string-position 32 str) 1))

 

returns "BBBB".  If the string is just "A_B C", it returns just the "B".  Etc.

Kent Cooper, AIA
0 Likes
Message 15 of 16

Vuxvix
Advocate
Advocate

Sorry for not being able to reply for a while.
I tried the following function in the attachment:
I double checked my Field in the post. actually the omitted part has been counted down from the end. I noticed {.dwg} is numbered :12-9 respectively. The coincidence of the length of the string of letters in the file name confused me. Apologies for this silliness.
@pbejse:
-I tried your Diesel code. The results are returned as shown in the attached file. It doesn't seem to work for me.
-I don't know how to use your lisp code. can you show me how to use it. Sorry about that
-As you said, every other computer in my office uses Autocad Lt. People here rarely use hotkeys, let alone lisp.
@Kent1Cooper:
-I also don't know how to use your lisp code. Does that create a Field in the Field list to choose from!?

0 Likes
Message 16 of 16

Kent1Cooper
Consultant
Consultant

@Vuxvix wrote:

....
@pbejse:
....
-As you said, every other computer in my office uses Autocad Lt. People here rarely use hotkeys, let alone lisp.
@Kent1Cooper:
-I also don't know how to use your lisp code. Does that create a Field in the Field list to choose from!?


Since AutoCAD LT does not support AutoLisp at all, my code will be of no use to your other users.

 

But where you can use it, my code does nothing other than get the substring out of the longer string.  That can be used in whatever way is needed, by other code.

Kent Cooper, AIA