Hello Forum,
if I open a drawing with DocumentManager.Open("drawing.dwg", false) and it is already open (or the user has no rights to write to it), the drawing is opened in READONLY mode without warning.
Is there way to check if DocumentManager.Open("drawing.dwg", false) will open the drawing normal or READONLY?
Thank you.
See if this helps
<CommandMethod("rdo", CommandFlags.Session)> _ Public Sub testReadOnly() Dim filename As String = "C:\Test\WorkingDrawing.dwg" Dim dm As DocumentCollection = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager Dim curdoc As Document = dm.MdiActiveDocument Using doclock As DocumentLock = curdoc.LockDocument() Dim doc As Document = dm.Open(filename, True) Dim ed As Editor = doc.Editor dm.MdiActiveDocument = doc ed.WriteMessage(vbLf & "Is Read-Only? {0}", doc.IsReadOnly.ToString()) Dim foo As String = ed.GetString(vbLf & "Type any key: ").StringResult dm.MdiActiveDocument = curdoc doc.CloseAndDiscard() End Using End Sub
~'J'~
Thank you for the response.
Doesn't your code open the drawing readonly and then tell me that the drawing is opened in readonly mode? I want to know if DocumentManager.Open("drawing.dwg", false) will open the drawing readonly or not, before I execute it. That way I can inform the user that the drawing can only be opened in readonly mode, and ask the user if he/she wants that.
Your problem is on you couldn't read this property,
so you can try this code, I saved mode as boolean
before of documents iteration, if you use it in class
declare this variable as public,
'Imports System.IO 'Imports System.Linq Public Function GetDirectoryDocs(dirpath As String, subdirs As Boolean) As List(Of FileInfo) Dim dir As New DirectoryInfo(dirpath) If Not dir.Exists Then Throw New DirectoryNotFoundException() Return Nothing End If Dim opt As SearchOption = SearchOption.AllDirectories If subdirs = False Then opt = SearchOption.TopDirectoryOnly Return dir.GetFiles("*dwg", opt).AsEnumerable().OrderBy(Function(x) x.FullName).ToList() End Function <CommandMethod("rdo", CommandFlags.Session)> _ Public Sub testReadOnly() ' set your folder path here: Dim drawings As List(Of FileInfo) = GetDirectoryDocs("C:\Test\BATM\Brad", False) Dim sb As New StringBuilder 'save variable to check mode Dim checkreadonly = True Dim dm As DocumentCollection = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager Dim curdoc As Document = dm.MdiActiveDocument Dim i As Integer = 0 For Each dwgpath As FileInfo In drawings Dim mode As Boolean = IIf(i Mod 2 = 0, True, False) Dim doc As Document = dm.Open(dwgpath.FullName, mode) checkreadonly = doc.IsReadOnly sb.AppendLine(dwgpath.FullName + " : " + checkreadonly.ToString()) ' increment to change open mode i += 1 Next For Each doc As Document In dm If doc.Name <> curdoc.Name Then doc.CloseAndDiscard() End If Next Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(sb.ToString()) End Sub
~'J'~
Hi, ty for the response.
Still looks like you open a drawing first, and then check if it is read-only or not. I want to check before I open it if it is possible to open the drawing not read-only. Maybe there is no functionality in the CAD libraries to do this.
I''ve tried the following and it seemed to work.
...
if (File.Exists(strPath)) { objStreamWriter = new StreamWriter(strPath, true); if (objStreamWriter != null) { return false; } } return true;
...
I'm not entirely certain there could be a method to do that - its a chicken-and-egg situation. That determination isn't made until the drawing is actually opened.
You could take a more roundabout route, duplicating some of the checking that would be done e.g. checking for DWL/DWL2 files, checking on the file and/or folder rights levels for the active users, etc.
Ty for the response.
The OPEN command of AutoCAD has the same check. If you try to open a drawing not-readonly and you can't, it asks
for confirmation. Maybe it does the kind of checks that you suggest.