.NET

Reply
Valued Contributor
Ertqwa
Posts: 99
Registered: ‎10-03-2011
Message 1 of 7 (748 Views)

DocumentManager.Open("drawing.dwg", false) - READONLY or not

748 Views, 6 Replies
07-29-2012 08:47 AM

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.

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 7 (742 Views)

Re: DocumentManager.Open("drawing.dwg", false) - READONLY or not

07-29-2012 10:40 AM in reply to: Ertqwa

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'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Valued Contributor
Ertqwa
Posts: 99
Registered: ‎10-03-2011
Message 3 of 7 (704 Views)

Re: DocumentManager.Open("drawing.dwg", false) - READONLY or not

07-31-2012 10:31 AM in reply to: Hallex

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.

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 7 (694 Views)

Re: DocumentManager.Open("drawing.dwg", false) - READONLY or not

07-31-2012 01:52 PM in reply to: Ertqwa

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'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Valued Contributor
Ertqwa
Posts: 99
Registered: ‎10-03-2011
Message 5 of 7 (681 Views)

Re: DocumentManager.Open("drawing.dwg", false) - READONLY or not

08-01-2012 10:40 AM in reply to: Hallex

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

 

 

 

*Expert Elite*
dgorsman
Posts: 5,596
Registered: ‎10-12-2006
Message 6 of 7 (677 Views)

Re: DocumentManager.Open("drawing.dwg", false) - READONLY or not

08-01-2012 11:31 AM in reply to: Ertqwa

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.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
Adopt. Adapt. Overcome. Or be overcome.
A good question will be halfway to a good answer.


Valued Contributor
Ertqwa
Posts: 99
Registered: ‎10-03-2011
Message 7 of 7 (669 Views)

Re: DocumentManager.Open("drawing.dwg", false) - READONLY or not

08-01-2012 01:35 PM in reply to: dgorsman

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.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!