Diesel Expression Simplification

Diesel Expression Simplification

wispoxy
Advisor Advisor
2,718 Views
10 Replies
Message 1 of 11

Diesel Expression Simplification

wispoxy
Advisor
Advisor

Just got into diesel expressions and I need help simplifying.

 

Please simplify:

$(substr,$(getvar,ctab),$(-,$(strlen,$(getvar,ctab)),4),3,2)$(substr,$(getvar,ctab),$(-,$(strlen,$(getvar,ctab)),1),2)

 

I'm extracting paperspace tab names and including them in my template, it's working exactly the way I want it to, taking the last five characters from the tab name. I believe it can be simplified. Let me know if I'm wrong. Feel free to point out anything that doesn't make sense or isn't needed in the expression, thanks.

0 Likes
Accepted solutions (2)
2,719 Views
10 Replies
Replies (10)
Message 2 of 11

pbejse
Mentor
Mentor
Accepted solution

@Anonymous wrote:

Just got into diesel expressions and I need help simplifying.

 

Please simplify:

$(substr,$(getvar,ctab),$(-,$(strlen,$(getvar,ctab)),4),3,2)$(substr,$(getvar,ctab),$(-,$(strlen,$(getvar,ctab)),1),2)

 

I'm extracting paperspace tab names and including them in my template, it's working exactly the way I want it to, taking the last five characters from the tab name. I believe it can be simplified. Let me know if I'm wrong. Feel free to point out anything that doesn't make sense or isn't needed in the expression, thanks.


Not sure  , equivalent to (substr (getvar 'ctab) (- (strlen (getvar 'ctab)) 4)) on diesel maybe...

 

$(substr,$(getvar,ctab),$(-,$(strlen,$(getvar,ctab)),4)) ........ tested on RTEXT

Message 3 of 11

wispoxy
Advisor
Advisor

@pbejse Worked great, thank you. A learning experience also.

 

$(substr,$(getvar,ctab),$(-,$(strlen,$(getvar,ctab)),4))

0 Likes
Message 4 of 11

pbejse
Mentor
Mentor

@Anonymous wrote:

@pbejse Worked great, thank you. A learning experience also.

 

$(substr,$(getvar,ctab),$(-,$(strlen,$(getvar,ctab)),4))


Good for you, I'm happy to help 🙂

0 Likes
Message 5 of 11

dbroad
Mentor
Mentor
Accepted solution

I kind of thought your first post was a joke or test because it couldn't possibly be "working fine".  There were mistakes in the diesel.  The first substr function had too many arguments and there were 2 substr functions.  The initial diesel was equivalent in lisp to:

 

(substr (getvar 'ctab) (- (strlen (getvar 'ctab)) 4) 3 2) ;this is an error
(substr (getvar 'ctab) (- (strlen (getvar 'ctab)) 1) )

The one marked as a solution will also error out or not get the desired value when the length of ctab is less than 5 characters.  So you really need an if statement as in the following (as lisp).

 

 

(if (< 4 (strlen (getvar 'ctab)))
  (substr(getvar 'ctab)(- (strlen(getvar 'ctab)) 4))
  (getvar 'ctab))

or this (in $diesel)

 

$(if,$(<,4,$(strlen,$(getvar,ctab)))
,$(substr,$(getvar,ctab),$(-,$(strlen,$(getvar,ctab)),4))
,$(getvar,ctab))

Rtext is probably buggier than just using fields where fields work.

Architect, Registered NC, VA, SC, & GA.
Message 6 of 11

pbejse
Mentor
Mentor

 

 

You are right dbroad, should've taken that into account

 

 

Good catch 🙂

 

 

0 Likes
Message 7 of 11

wispoxy
Advisor
Advisor

@dbroad Not sure what you mean by 'error out'. It's working fine. To give you an idea I'm doing it through a field in MText. If this is not the proper way please guide me, I'm all new to diesel expressions. See image below.

 

@dbroad I'm going to try your code and reply back. I marked @pbejse as a solution because my overall concern before was simplification. 

 

I'm all into learning more now that I have AutoCAD 2017 🙂

 

wisp_ss12k.png

0 Likes
Message 8 of 11

wispoxy
Advisor
Advisor

Wow, cool. You made it so others can no longer Edit Field, only option is Insert Field.

 

This is even better if you can confirm the statement @dbroad.

0 Likes
Message 9 of 11

dbroad
Mentor
Mentor

Glad it's working for you.  As far as errors, diesel has no error reporting. It just won't work as intended or will skip the code with the error.  Your first post had 2 separate diesel expressions, one after the other.  The first one was an error.  There were too many arguments in the $substr function. At most, it takes string, start, and length.  You had 4  arguments.  Looks like it might have tried to execute by automatically managing the error and threw the last part out.  Anyway, if you had had a layout name with less than 5 characters, it would have also errored out. The results would have been indeterminate due to the error.

 

I didn't do anything to prevent anyone from editing the diesel code.  If you are using mtext, then double clicking on the field code in the mtext while editing the mtext will open the field editor.  The field editor looks different from the rtext editor.

 

Simplification is a good goal, but correctness is of prime concern.

Architect, Registered NC, VA, SC, & GA.
0 Likes
Message 10 of 11

pbejse
Mentor
Mentor

@Anonymous wrote:

 

 

Simplification is a good goal, but correctness is of prime concern.


True


@Anonymous wrote:

...I didn't do anything to prevent anyone from editing the diesel code....


I'm wondering about that statement as well.. 

 

Indeed we learn new stuff everyday wisp . 🙂 

0 Likes
Message 11 of 11

wispoxy
Advisor
Advisor

Thanks for all the help and time. @pbejse and @dbroad

0 Likes