Author Topic: Check if drawing is Open of in Use?  (Read 5271 times)

0 Members and 1 Guest are viewing this topic.

mgreven

  • Guest
Check if drawing is Open of in Use?
« on: February 07, 2012, 04:12:40 AM »
Hello,

Does anyone have a sample which shows whats the best way to check if a drawing is already open or in use by another user?

Regards,

Marco

Brick_top

  • Guest
Re: Check if drawing is Open of in Use?
« Reply #1 on: February 07, 2012, 05:14:46 AM »
Finally I think I can help someone!

here

Code: [Select]
(defun IsRO (fileN / fileH)
;;;by RobertB http://forums.augi.com/showpost.php?p=35803&postcount=3
 (cond ((setq fileH (open fileN "a"))
        (close fileH)))
 (not fileH))

mgreven

  • Guest
Re: Check if drawing is Open of in Use?
« Reply #2 on: February 07, 2012, 05:37:04 AM »
Thanks for the quick respons...

I came up with the following code to check if the drawing is open in de autocad:

(defun testopen   (file / n doc-collection item Dwgname opened)
  ; Routine checks if file is already open in Acad
  (setq n 0)
  (setq doc-collection (vla-get-Documents (vlax-get-acad-object)))
  (vlax-for item doc-collection
    (Setq Dwgname (vla-get-FullName item))
    (If (= Dwgname file) (Setq opened T) )
  )
  (princ opened)
)

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Check if drawing is Open of in Use?
« Reply #3 on: February 07, 2012, 06:13:44 AM »
Note that your code will only check whether the drawing is open in that instance of the application on your computer - the drawing could be open on another person's computer or in another instance of the application.

mgreven

  • Guest
Re: Check if drawing is Open of in Use?
« Reply #4 on: February 07, 2012, 07:24:32 AM »
Yeah i know i am only checking on the local autocad.

How can i best check this for other users?

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Check if drawing is Open of in Use?
« Reply #5 on: February 07, 2012, 07:52:33 AM »
Two ways:

Code - Auto/Visual Lisp: [Select]
  1. ;; filename = filename of file to test
  2. ;; Returns T if specified file is open
  3.  
  4. (defun _FileOpen-p ( filename )
  5.     (not (vl-file-rename filename filename))
  6. )

Similar to above post:

Code - Auto/Visual Lisp: [Select]
  1. ;; filename = filename of file to test
  2. ;; Returns T if specified file is open
  3.  
  4. (defun _FileOpen-p ( filename / f )
  5.     (if (setq f (open filename "a"))
  6.         (close f)
  7.         t
  8.     )
  9. )

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Check if drawing is Open of in Use?
« Reply #6 on: February 07, 2012, 09:50:57 AM »
I know the attempt to open in append mode is a bit scary, but it shouldn't do anything "wrong".

Anyhow, here's another method. It either returns nil if no-one has the drawing open, or it returns a list containing ("Username" "PC Name" "Date/Time") if someone has it open:
Code: [Select]
(defun DWGOpenedBy (filename / dwl f lst s)
  (if (and (setq dwl (findfile (strcat (vl-filename-directory filename) "\\" (vl-filename-base filename) ".DWL")))
           (not (vl-file-delete dwl)))
    (if (setq f (open dwl "r"))
      (progn
        (while (setq s (read-line f)) (setq lst (cons s lst)))
        (close f)
        (reverse lst)
      )
      T
    )
  )
)
Note though it only checks if someone's got it opened in AutoCAD. It would theoretically be possible to open a DWG in NotePad, though I can't imagine why you'd want to  :ugly:
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

mgreven

  • Guest
Re: Check if drawing is Open of in Use?
« Reply #7 on: February 07, 2012, 10:02:49 AM »
Thanks for the respons... It is very Helpfull.



Ketxu

  • Newt
  • Posts: 109
Re: Check if drawing is Open of in Use?
« Reply #8 on: February 07, 2012, 10:03:29 AM »
And another way, check the same dwl file exist :
Code - Auto/Visual Lisp: [Select]
  1. (defun _FileOpen-p (f /)
  2. (or
  3.         (findfile (strcat (vl-filename-directory f) (vl-filename-base f) ".dwl"))
  4. ))

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Check if drawing is Open of in Use?
« Reply #9 on: February 07, 2012, 10:18:08 AM »
Ketxu, that's what mine does as well. Though I try to delete the DWL file - since ACad sometimes leaves these here if it crashes.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Check if drawing is Open of in Use?
« Reply #10 on: February 07, 2012, 10:26:16 AM »
Ketxu, that's what mine does as well. Though I try to delete the DWL file - since ACad sometimes leaves these here if it crashes.
Precisely why I keep this in our startup...

Code: [Select]
; cleanup drawing directory
((lambda (path)
   (if (eq (getvar 'DWGTITLED) 1)
     (foreach file (vl-directory-files path nil 1)
       (if (wcmatch (strcase file) "*.TMP,*.LOG,*.ERR,*.DWL,*.DWL2")
         (vl-file-delete (strcat path file))
       )
     )
   )
 )
  (getvar 'dwgprefix)
)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Ketxu

  • Newt
  • Posts: 109
Re: Check if drawing is Open of in Use?
« Reply #11 on: February 07, 2012, 12:06:25 PM »
Ketxu, that's what mine does as well. Though I try to delete the DWL file - since ACad sometimes leaves these here if it crashes.
Hi, I was writing it in editor when you post your code (and i saw the alert). Tks you ^^
« Last Edit: February 07, 2012, 12:22:43 PM by Ketxu »