Jump to content

Getting the savepath directory of current drawing in variable


yonderbob

Recommended Posts

Hi guys,

 

I've been trying to get the savepath directory of my current drawing into a variable so i can modify it's string to only get the numbers out of the string.

 

I have had no succes with "VL-filename-directory" keep getting a bad retrun.

Also tried the Getenv path but that returns autocad install path which is not what i'm looking for.

 

This is want i have so far

 

  	(setq Path (getenv "path"))
 	(setq LastPath (vl-string-trim "Z:\whatever" path))
 	(setq NoTroep
       (vl-list->string
 		(vl-remove-if-not
   			'(lambda (i) (< 47 i 58))
   				(vl-string->list path)
		 )
	)
 )

 

Hope any of you could point me in the right direction.

 

Thanks :)

 

Bob

Link to comment
Share on other sites

It came with some issues with reading multiple search paths so i want to make an easier code if that's possible.

 

Is it possible to get a string return from within a string that only contains a set amount of numbers.

For example, the string is : "i have 12 eggs but i need 654321 eggs so i still needs 54321 more"

I would like it to return only the string of numbers which contains 6 numbers, so it only returns the 654321 numbers in the string.

Hope you get me :P

 

Thanks,

 

Bob

Link to comment
Share on other sites

Is it possible to get a string return from within a string that only contains a set amount of numbers.

For example, the string is : "i have 12 eggs but i need 654321 eggs so i still needs 54321 more"

I would like it to return only the string of numbers which contains 6 numbers, so it only returns the 654321 numbers in the string.

 

You could use Regular Expressions, or alternatively a function similar to the following:

(defun getpattern ( str pat / tmp )
   (cond
       (   (= "" str) nil)
       (   (or (wcmatch (setq tmp (substr str 1 (1- (strlen str)))) pat)
               (wcmatch (setq tmp (substr str 2)) pat)
           )
           (getpattern tmp pat)
       )
       (   (wcmatch str pat) str)
   )
)

Example:

_$ (setq s "i have 12 eggs but i need 654321 eggs so i still needs 54321 more")
"i have 12 eggs but i need 654321 eggs so i still needs 54321 more"
_$ (getpattern s "*######*")
"654321"

Link to comment
Share on other sites

You could use Regular Expressions, or alternatively a function similar to the following:

(defun getpattern ( str pat / tmp )
   (cond
       (   (= "" str) nil)
       (   (or (wcmatch (setq tmp (substr str 1 (1- (strlen str)))) pat)
               (wcmatch (setq tmp (substr str 2)) pat)
           )
           (getpattern tmp pat)
       )
       (   (wcmatch str pat) str)
   )
)

 

Nice function, Lee!

 

I would use something like this

(defun 6numbers (str / lst num str1)
   (setq lst  (mapcar 'chr (vl-string->list str))
         str1 ""
   )
   (foreach l lst
       (if (wcmatch l "#")
           (setq str1 (strcat str1 l))
           (if (and (/= str1 "")
                    (= (strlen str1) 6)
               )
               (setq num str1)
               (setq str1 "")
           )
       )
   )
   num
)

only accept as valid result, a string with six consecutive numbers, if more, does not validate.

 

Henrique

Link to comment
Share on other sites

Wow Lee,

Your function goes way beyond my understanding, don't get it logically at all haha.

Your expression makes sense and looks nice,

So thanks for those.

 

Henrigue,

I like yours as i understand it haha :P.

 

Thanks guys for the help and lessons appreciate it alot.

 

Time to build now :)

 

Bob

Link to comment
Share on other sites

Nice function, Lee!

 

Thank you Henrique :)

 

Here's another way:

(defun get6digits ( str / foo )
   (defun foo ( lst / rtn )
       (if (setq rtn (mapcar '(lambda ( a b ) a) lst '(0 0 0 0 0 0)))
           (if (vl-every '(lambda ( x ) (< 47 x 58)) rtn)
               (vl-list->string rtn)
               (foo (cdr lst))
           )
       )
   )
   (foo (vl-string->list str))
)

Link to comment
Share on other sites

Wow Lee,

Your function goes way beyond my understanding, don't get it logically at all haha.

Your expression makes sense and looks nice,

So thanks for those.

 

Thank you Bob - the function essentially shortens the string from either end until the supplied wildcard pattern is no longer validated, leaving the section of string matching the pattern.

Link to comment
Share on other sites

Thank you Henrique :)

 

Here's another way:

(defun get6digits ( str / foo )
   (defun foo ( lst / rtn )
       (if (setq rtn (mapcar '(lambda ( a b ) a) lst '(0 0 0 0 0 0)))
           (if (vl-every '(lambda ( x ) (< 47 x 58)) rtn)
               (vl-list->string rtn)
               (foo (cdr lst))
           )
       )
   )
   (foo (vl-string->list str))
)

 

Nice one too, Lee! :)

 

Cheers

Henrique

Link to comment
Share on other sites

Nice one too, Lee! :)

 

Cheers

Henrique

 

Thanks! - Though, this will be a lot more efficient:

 

(defun get6digits2 ( str / foo )
   (defun foo ( lst / tmp )
       (if (setq lst (vl-member-if '(lambda ( x ) (< 47 x 58)) lst))
           (if (wcmatch (setq tmp (vl-list->string lst)) "######*")
               (substr tmp 1 6)
               (foo (cdddr (cdddr lst)))
           )
       )
   )
   (foo (vl-string->list str))
)

 

Benchmark:

_$ (setq s "i have 12 eggs but i need 654321 eggs so i still needs 54321 more")
"i have 12 eggs but i need 654321 eggs so i still needs 54321 more"

Benchmarking .................Elapsed milliseconds / relative speed for 16384 iteration(s):

   (GET6DIGITS2 S)................1123 / 12.27 <fastest>
   (6NUMBERS S)...................1903 / 7.24
   (GETPATTERN S "*######*")......3339 / 4.13
   (GET6DIGITS S)................13775 / 1.00 <slowest>

Link to comment
Share on other sites

There is still room for improvement!

(defun get6digits3 ( str / l n x )
   (setq l (vl-string->list str))
   (while
       (and
           (setq x (car l))
           (< (length (setq n (if (< 47 x 58) (cons x n)))) 6)
           (setq l (cdr l))
       )
   )
   (vl-list->string (reverse n))
)

_$ (benchmark '((get6digits2 s) (get6digits3 s)))
Benchmarking ..................Elapsed milliseconds / relative speed for 32768 iteration(s):

   (GET6DIGITS3 S).....1700 / 4.98 <fastest>
   (GET6DIGITS2 S).....8471 / 1.00 <slowest>

_$ (benchmark '((getpattern s "*######*") (6numbers s) (get6digits s) (get6digits2 s) (get6digits3 s)))
Benchmarking ...................Elapsed milliseconds / relative speed for 65536 iteration(s):

   (GET6DIGITS3 S)................1903 / 20.82 <fastest>
   (GET6DIGITS2 S)................3698 / 10.71
   (6NUMBERS S)...................6224 / 6.37
   (GETPATTERN S "*######*").....10358 / 3.83
   (GET6DIGITS S)................39624 / 1.00 <slowest>

Link to comment
Share on other sites

There is still room for improvement!

 

_$ (benchmark '((getpattern s "*######*") (6numbers s) (get6digits s) (get6digits2 s) (get6digits3 s)))
Benchmarking ...................Elapsed milliseconds / relative speed for 65536 iteration(s):

   (GET6DIGITS3 S)................1903 / 20.82 <fastest>
   (GET6DIGITS2 S)................3698 / 10.71
   (6NUMBERS S)...................6224 / 6.37
   (GETPATTERN S "*######*").....10358 / 3.83
   (GET6DIGITS S)................39624 / 1.00 <slowest>

 

Always in search of perfection! :)

Nicely done!

 

Henrique

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...