Jump to content

Recommended Posts

Posted

Quick one...

 

Is it possible to stick a stored VAR into a path? i.e.

 

I store

 

(setq usr (getvar "LOGINNAME"))

 

Later on i want to write a log file to a path:

 

(setq f ("C:\\%usr%.txt"))

 

Or something like this...

Posted

I think the function *strcat* would do the trick for you .

 

e.g .

(setq usr (getvar 'loginname))
(setq f (strcat "C:\\" usr ........ and so on

Posted

Thank you, I was just reading up on some other examples and "strcat" jumped out.

 

(Just writing my first lines of code.... eek!)

Posted

Just looking at how to remove the "." (period) from the username. I am outputting some text to a text file at a given location but the text files name is based on the LOGINNAME. But i dont want "monk.monk.txt"!

Pointers appreciated. When i have it working i will post it up!

Posted

Works so far! Will most likely have this as a subroutine somewhere.

 

(defun C:saveSupportPaths (/ usr files paths f)
 (vl-load-com)
 (setq files (vla-get-files (vla-get-preferences (vlax-get-acad-object))))
 (setq paths (vla-get-supportpath files))
 (setq f (open (strcat "C:\\Users\\monk.monk\\Desktop\\"(getvar "LOGINNAME")".txt")"w"))
 (write-line paths f)
 (close f)
 (princ)
)

Posted

There are many ways this could be accomplished, here is just one:

 

(vl-list->string (vl-remove 46 (vl-string->list (getvar 'LOGINNAME))))

Here is another:

 

(vl-string-translate "." " " (getvar 'LOGINNAME))

Posted

Thanks Lee. (I am using your site a lot right now!)

 

I will have a butchers at what you have suggested. My only thought on my code above is what happens if the path does not exist or is not available? Will the program hang or will it just stop? Do i need to add an error section?

Posted
My only thought on my code above is what happens if the path does not exist or is not available? Will the program hang or will it just stop? Do i need to add an error section?

 

If the path:

 

"C:\\Users\\monk.monk\\Desktop"

doesn't exist, the open function will return nil when attempting to create a text file at a path that doesn't exist.

 

Now, since the variable 'f' is nil, write-line is passed a null file descriptor argument and will consequently write the string to the command-line, rather than the file (since the file descriptor argument is optional).

 

However, the close function will error when passed a null file descriptor:

 

_$ (close nil)
; error: bad argument type: streamp nil

To avoid these errors, include a simple conditional statement:

 

(defun C:saveSupportPaths ( / files paths f )
   (setq files (vla-get-files (vla-get-preferences (vlax-get-acad-object))))
   (setq paths (vla-get-supportpath files))

   (if (setq f (open (strcat "C:\\Users\\monk.monk\\Desktop\\" (getvar "LOGINNAME") ".txt") "w"))
       (progn
           (write-line paths f)
           (close f)
       )
       (princ "\nUnable to Create File.")
   )
   (princ)
)
(vl-load-com)

Of course, this doesn't include the code to remove invalid characters from the filepath string.

Posted

That sir is perfect.

 

I am trying to read up on as much as i can. I am using your site, afralisp, visual lisp developers bible. Any other resources you can think of? Hard copies are good. I would like a reference / dictionary for all the functions etc but that would be huge!

Posted

You're very welcome.

 

Any other resources you can think of? Hard copies are good. I would like a reference / dictionary for all the functions etc but that would be huge!

 

IMO, the very best reference that you can use is the Visual LISP IDE (VLIDE) Help Documentation (and I strongly recommend that you use the VLIDE to write your code if you are not already doing so); this includes the AutoLISP Developer's Guide and a complete AutoLISP reference.

 

In versions pre AutoCAD 2011, it also included an ActiveX COM Reference for all the Properties and Methods derived from the AutoCAD Object Model, this was removed when Autodesk [stupidly] migrated to use online help, though the original acadauto.chm file can still be found under:

 

C:\Program Files\Common Files\Autodesk Shared

 

I've written a handful of short tutorials about the VLIDE on my site:

 

http://lee-mac.com/tutorials.html#vlide

 

And there is also a lot of information on AfraLISP:

 

http://www.afralisp.net/visual-lisp/tutorials/visual-lisp-editor-part-1.php

 

Hard copies are good.

 

I would very much disagree with you there - soft copies have many advantages over hard copies, (aside from saving the rainforests), Try Ctrl+F'ing a book and you'll get the picture ;)

Posted

Ta very much. Using all the above so I am hopefully on the right tracks!

 

I agree on the soft copy point, but it is nice to thumb through something when you are learning! But maybe that is just me!

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...