Jump to content

Writing a script to open a bunch of drawings read-only...


Comatosis

Recommended Posts

I assume other people have needed to do this at some point or another, but I can't find any good information (or at least the info I found is for single drawings only).

 

I have a LISP routine that generates a script to run another LISP routine on all drawings in a particular folder. The routine I'm running on the drawings only gathers information from them and does not change the drawings at all, so I would prefer the drawings to be opened read-only to prevent any mishaps, and also to prevent the script from hanging up when someone else on the network has one of the drawings open.

 

Is this doable? I tried adding an extra space or "\n" to my script generator as a way to "fake" an extra hit of the return key in case AutoCAD asks if I want to open a read-only copy, but this didn't work as intended (in fact I was told it just can't open the script for some reason).

 

Thanks.

Link to comment
Share on other sites

Someone correct me if I'm wrong here, but as I understand it, the "read only" property is set by the operating system except for when the drawing is actually open. In that case, Autocad generates a lock file to warn others that the drawing is open. I don't know of a way to make your system think it's read only short of copying the files to a separate directory and setting the "read only" flag there. You could probably modify your lisp to do that for you, but you'd have to ask the lisp gurus about that. Have it create a copy of the files you are going to work on, do what ever you're going to do then delete the copies. That way you'd never have to worry about someone else, and you wouldn't clutter up the drive with lots of extra files.

Link to comment
Share on other sites

Well, I've toyed with the idea of doing something like this, at least to automatically delete the generated script file after doing its job. I'd want this process to be as automatic as possible in general, but I'm somewhat concerned about creating a procedure to copy all drawings to another folder, run the script, and then delete the folder. I would lose my job instantly if someone inadvertently managed to delete the original drawings after running this script.

Link to comment
Share on other sites

Well, I've toyed with the idea of doing something like this, at least to automatically delete the generated script file after doing its job. I'd want this process to be as automatic as possible in general, but I'm somewhat concerned about creating a procedure to copy all drawings to another folder, run the script, and then delete the folder. I would lose my job instantly if someone inadvertently managed to delete the original drawings after running this script.

 

Set the permissions level so that only certain folks can save to the drive where they are stored. Everybody else saves to a "submitted" folder, and the person with write permission for the drive (the one who checks the drawings would be my recommendation) saves them to the shared drive when the changes are approved. That way no accidents can happen and you eliminate the need to copy.

Link to comment
Share on other sites

Here's some additional information RK posted recently about *.DWL* files:

 

...

As far as the DWL* files, these are created and managed by the WHOHAS.ARX file

...

 

linky

 

I'm not sure if this will help the OP or not, but this is a simple example of how to open a drawing read-only:

 

(defun c:OPENR ()
 (princ "\nOPEN READ-ONLY ")
 (vl-load-com)
 ((lambda (acApp / f)
      (if (/= 1 (getvar 'sdi))
        (if (setq f (getfiled "Select file to open read-only" "" "dwg" 16))
          (vla-activate
            (vla-open (vla-get-documents acApp) f :vlax-true)))
        (prompt "\n** Command not available in SDI mode ** ")))
     (vlax-get-acad-object))
 (princ))

Link to comment
Share on other sites

Set the permissions level so that only certain folks can save to the drive where they are stored. Everybody else saves to a "submitted" folder' date=' and the person with write permission for the drive (the one who checks the drawings would be my recommendation) saves them to the shared drive when the changes are approved. That way no accidents can happen and you eliminate the need to copy.[/quote']

 

What I'm doing is actually quite trivial, and it's mainly there to slightly speed up our QC process (checking keynotes automatically). Going through all that trouble for what the main routine does seems somewhat of a waste of resources IMO, heh.

Link to comment
Share on other sites

Here's some additional information RK posted recently about *.DWL* files:

 

 

 

linky

 

I'm not sure if this will help the OP or not, but this is a simple example of how to open a drawing read-only:

 

(defun c:OPENR ()
(princ "\nOPEN READ-ONLY ")
(vl-load-com)
((lambda (acApp / f)
(if (/= 1 (getvar 'sdi))
(if (setq f (getfiled "Select file to open read-only" "" "dwg" 16))
(vla-activate
(vla-open (vla-get-documents acApp) f :vlax-true)))
(prompt "\n** Command not available in SDI mode ** ")))
(vlax-get-acad-object))
(princ))

 

I found a post elsewhere that showed code for opening drawings read-only by way of undefining and redefining the "open" command, but I couldn't figure out how to suppress the dialog box that pops up. Setting FILEDIA and CMDDIA to 0 didn't do anything.

 

This is what I tried out, from http://forums.augi.com/showthread.php?t=104865

 


[color=#0000ff](vl-load-com)[/color]
[color=#0000ff](command "._undefine" "open")[/color]
[color=#0000ff](defun C:open ()[/color]
[color=#0000ff] (setq fn (getfiled "Select Drawing" "" "dwg" 0))[/color]
[color=#0000ff] (if (zerop (getvar "SDI"))[/color]
[color=#0000ff]   (vlax-invoke-method[/color]
[color=#0000ff]     (vla-get-documents (vlax-get-acad-object))[/color]
[color=#0000ff]     'Open[/color]
[color=#0000ff]     fn[/color]
[color=#0000ff]     :vlax-true[/color]
[color=#0000ff]   )[/color]
[color=#0000ff] )[/color]
[color=#0000ff])[/color]


 

Would getting rid of/substituting the getfiled command fix this?

Link to comment
Share on other sites

What I'm doing is actually quite trivial, and it's mainly there to slightly speed up our QC process (checking keynotes automatically). Going through all that trouble for what the main routine does seems somewhat of a waste of resources IMO, heh.

 

One of the places I used to work for did it this way, but we had a couple hundred drafters spread out over several states. If you run a back up of the drive you save to every night, you could never lose more than a day's worth even if somebody messed up.

Link to comment
Share on other sites

While undefining OPEN will work, you will need to re-define the command, and perhaps include error checking to restore OPEN in the event of an error. IMO - It's far better to have a separate command for this.

 

With the OPENR command, you can invoke from the command line like so:

 

(defun _OPENR (f) (_OpenReadOnly f))
(defun _OpenReadOnly (f)
 ;; Example (_OpenReadOnly "C:\\file.dwg")
 (princ "\nOPEN READ-ONLY ")
 (vl-load-com)
 ((lambda (acApp)
      (if (/= 1 (getvar 'sdi))
        (if (findfile f)
          (vla-activate
            (vla-open (vla-get-documents acApp) f :vlax-true))
          (prompt "\n** File not found ** "))
        (prompt "\n** Command not available in SDI mode ** ")))
     (vlax-get-acad-object))
 (princ))

  • Like 1
Link to comment
Share on other sites

See, I knew someone with a better way would show up eventually. These lisp guys will fix you up. I know enough about lisp to see one and say "yes, that's a lisp routine!"

Link to comment
Share on other sites

While undefining OPEN will work, you will need to re-define the command, and perhaps include error checking to restore OPEN in the event of an error. IMO - It's far better to have a separate command for this.

 

With the OPENR command, you can invoke from the command line like so:

 

(defun _OPENR (f) (_OpenReadOnly f))
(defun _OpenReadOnly (f)
;; Example (_OpenReadOnly "C:\\file.dwg")
(princ "\nOPEN READ-ONLY ")
(vl-load-com)
((lambda (acApp)
(if (/= 1 (getvar 'sdi))
(if (findfile f)
(vla-activate
(vla-open (vla-get-documents acApp) f :vlax-true))
(prompt "\n** File not found ** "))
(prompt "\n** Command not available in SDI mode ** ")))
(vlax-get-acad-object))
(princ))

 

Is there something I'm doing wrong?

 

Command: (_openr "C:\dir\dir\dir\drawingfile.dwg")
OPEN READ-ONLY

 

The command only spits out the princ statement, but no file is opened.

Link to comment
Share on other sites

Is there something I'm doing wrong?

 

Command: (_openr "C:[color=red]\[/color]dir[color=red]\[/color]dir[color=red]\[/color]dir[color=red]\[/color]drawingfile.dwg")
OPEN READ-ONLY

The command only spits out the princ statement, but no file is opened.

 

To be completely honest, I have no idea. :? I wrote these functions quickly, but they each worked for me without any errors. Perhaps Lee is correct about the Open Method (it's been known to happen before! :rofl:).

 

Edit: CORRECTION. You only used single back-slashes "\", where double back-slashes "\\" (or single forward-slashes "/") are required. :wink:

Link to comment
Share on other sites

I tried double backslashes too; that didn't work. Just tried forward slashes and no dice either. I'm not getting the SDI error, and I checked the variable: set to 0.

 

CORRECTION: I'm just stupid. The double backslashes DID work, after I fixed the stupid spelling error I had in my filename (adding the double backslashes accidentally erased one of the letters in the directory name). :facepalm:

 

p.s. How do you cross out text like you just did?

Link to comment
Share on other sites

Huh *confused* ... and Lee's code works? :unsure:

 

I haven't tried Lee's yet, or at least I have no idea how to incorporate that and fudge it around to open files read-only.

 

Your code worked with the double backslashes. Now my problem is that it opens the next drawing in line, but doesn't close it or open the next one. I have no idea what it's waiting on. I commented out the main routine for now just to see if the script simply opens and closes files correctly--My script is almost literally "open, close, 'yes' to discard changes" (tried removing this too, no difference).

 

EDIT: The script generator is still wanting to run something after the second drawing opens, but nothing happens. Any attempt to close drawings, abort the evaluation, or start over produces a fatal exception in AutoCAD.:unsure:

Link to comment
Share on other sites

I know of no way thread the standard OPEN command to purposely open in Read-Only; hence the code I posted which *does* open a drawing in the desired (Read-Only) state.

 

I have no idea what *other* tasks you're attempting to perform, so I cannot suggest an applicable way of implementing this code on an entire directory of drawings. A couple come to mind, but there's no point discussing what won't work. More (complete?) information is needed, IMO.

Link to comment
Share on other sites

My script ends up looking like this (prior to trying to implement the code you gave me):

 

_.open "C:\dir\dir\dir\seconddrawing.dwg" _.kcms _.close y
.
.
.
_.open "C:\dir\dir\dir\lastdrawing.dwg" _.kcms _.close y 

 

Where "kcms" is a LISP routine I made (the one for checking keynotes I bugged you guys about in the other topic). It just cycles through all text/mtext/blocks in a drawing and extracts keynotes, then outputs the results to a .txt file. I have "seconddrawing.dwg" listed up there because I have the first drawing already open and I just run kcms on it first prior to going into the script (to prevent AutoCAD from asking me if I want to open a read-only copy of it). Anyway, this all works.

 

After your code, I changed it to this:

 

(_openr "C:\\dir\\dir\\dir\\seconddrawing.dwg") _.close y 
.
.
.
(_openr "C:\\dir\\dir\\dir\\lastdrawing.dwg") _.close y

 

I removed the _.kcms temporarily to see if I could succeed at just opening and closing drawings. I guess the problem might be that AutoCAD doesn't acknowledge the existence of _openr until after I open a drawing, but if this was the case I doubt it would have managed to open the second one in the first place. After it opens up the second one it doesn't close it, and it just stays there waiting for something else to happen. Hovering over to the VLIDE window changes my cursor to "thinking" mode, and like I said before, I get a fatal exception trying to close out of AutoCAD or abort the routine.

 

I don't think my main routine would be the issue since I know it works and it works with the script too if I open drawings "the normal way." Switching over to the read-only option is what's giving me trouble, which kinda sucks because I was looking forward to being able to do this. :(

 

p.s. Sorry for the super long posts. I've tried time and again to be less verbose, to no avail.

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