NP CAD Page | Articles | Russian

N.Poleshchuk. AutoCAD 2010 .NET API: eFileSharingViolation in Database.SaveAs

Link to J.Bergmark's blog with this theme

If you look through AutoCAD 2010 .NET help system (inside ObjectARX 2010 documentation) you will see that Autodesk.AutoCAD.DatabaseServices.Database class has SaveAs method which is represented by three overloaded functions:

  • Database.SaveAs (string, [MarshalAs(UnmanagedType.U1)] bool, DwgVersion, Autodesk.AutoCAD.DatabaseServices.SecurityParameters)

  • Database.SaveAs (string, Autodesk.AutoCAD.DatabaseServices.SecurityParameters)

  • Database.SaveAs (string, DwgVersion)

  • But use of the last one in version 2010 is problematic (it is OK in 2009 and others). It does not work when you try to save the file that was opened (not created!) in this session.

    Here is an example. Let us define a command SaveDr saving the current drawing with the same name and in the same DWG version using Database.SaveAs (string, DwgVersion) function.

    So create a new Visual Basic.NET project VbSaveYourDrawing with the help of ObjectARX Wizard for managed VB applications and change the Command.vb file to the following source text:

    Imports Autodesk.AutoCAD.ApplicationServices
    Imports Autodesk.AutoCAD.DatabaseServices
    Imports Autodesk.AutoCAD.Runtime
    Public Class Commands

    <CommandMethod("SaveDr")> _
    Public Sub SaveYourDrawing()
    If MsgBox("Hi!" & vbCrLf & "Is it time to save the drawing?", _
    MsgBoxStyle.YesNo, _
    "Database.SaveAs") = MsgBoxResult.Yes Then
    Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
    acDoc.Database.SaveAs(acDoc.Name, DwgVersion.Current)
    End If
    End Sub
    End Class

    Build the Release version of the VbSaveYourDrawing application.

    Next:
    1. Launch AutoCAD 2010 and open an existing DWG file.
    2. Type NETLOAD command and load VbSaveYourDrawing.dll file (usually it is located in the VbSaveYourDrawing\VbSaveYourDrawing\bin\Release folder).
    3. Run the SaveDr command.
    4. The program shows a MessageBox window (Pic.1).

    Picture 1

    5. Click Yes.
    6. This will cause an exception (Pic.2 with Details opened).

    Picture 2

    Thus we cannot save the current drawing under some circumstances. That's why AutoCAD .NET Developer's Guide uses more reliable acDoc.Database.SaveAs(acDoc.Name, True, DwgVersion.Current, acDoc.Database.SecurityParameters) instead.

    The sample project file can be downloaded from here.

    The same problem arises in C# too.


    NP CAD Page | Articles | Russian