In the code below you I am batching through a list of drawing and changing the layer color of layers that match a wildcard string. I'm running this on one computer that is 32 Bit Windows XP with Autocad Architecture 2008 and it works fine. I also run it on a 64 bit Windows 7 machine with AutoCAD MEP 2011 and it throws errors. In the beginning, I read the file in to a DB with read only priv's for others. When i'm done, if there's changes, I do a db.SaveAs(). On the 64 bit machine, it throws an error saying the file is already in use, and I noticed that it is creating *.dwl and *.dwl2 on the server. Any help would be greatly appreciated.
Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument Using lock As DocumentLock = doc.LockDocument For Each drawingName As String In Drawings Dim changed As Boolean = False If Not System.IO.File.Exists(drawingName) Then errorFiles.Add(New ErrorFile(drawingName, FileErrorType.NotFound)) Continue For End If Dim db As New Database(False, False) Try db.ReadDwgFile(drawingName, System.IO.FileShare.Read, True, String.Empty) Catch ex As AutoCAD.Runtime.Exception db.Dispose() If ex.Message = "eFileSharingViolation" Then errorFiles.Add(New ErrorFile(drawingName, FileErrorType.InUse)) Continue For Else Throw New Exception(CType(ex.Message, ErrorStatus)) End If Finally End Try Dim tr As Transaction = db.TransactionManager.StartTransaction Using tr Dim acLyrTbl As LayerTable acLyrTbl = CType(tr.GetObject(db.LayerTableId, OpenMode.ForRead), LayerTable) For Each layId As ObjectId In acLyrTbl Dim ltr As LayerTableRecord = CType(tr.GetObject(layId, OpenMode.ForRead), LayerTableRecord) If ltr.Name Like MatchText Then Dim LayChangeInfo As New LayerChangeInfo LayChangeInfo.Drawing = drawingName LayChangeInfo.Name = ltr.Name LayChangeInfo.OldColor = ltr.Color.ToString LayChangeInfo.NewColor = ColorNumber ltr.UpgradeOpen() ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, CType(ColorNumber, Short)) LayChangeInfo.WriteToCSV(ReportPath) End If Next tr.Commit() changed = True End Using If changed Then ## db.SaveAs(drawingName, DwgVersion.Current) End If db.Dispose() pm.MeterProgress() Windows.Forms.Application.DoEvents() Next End Using pm.Stop() pm.Dispose() If errorFiles.Count > 0 Then Dim tempString As String = "" Dim notFoundString As String = "" Dim inUseString As String = "" For Each file As ErrorFile In errorFiles If file.ErrorType = FileErrorType.InUse Then inUseString = inUseString & file.FileName & vbCrLf ElseIf file.ErrorType = FileErrorType.NotFound Then notFoundString = notFoundString & file.FileName & vbCrLf End If Next If notFoundString.Length > 0 Then tempString = "The following files were not found: " & vbCrLf & notFoundString & vbCrLf End If If inUseString.Length > 0 Then tempString = tempString & "The following files were already open: " & vbCrLf & inUseString End If MsgBox(tempString) End If
I noticed that when i change the ReadDwg FileShare parameter to ReadWrite that it works fine on this machine. This isn't quite what I want though.
While i'm using this file, I don't want anyone else to open it. I thought I could accomplish by settings the FileShare permissions to read-only. However, that makes it the case for the current application too.
What's the point of the FileShare permissions if I can't restrict access to the file to only other programs? If I have to restrict myself as well, then I'll always be using ReadWrite if I plan to make any changes.
Any help in understanding this would be much appreciated.