Author Topic: Database.SaveAs() Problems  (Read 15637 times)

0 Members and 1 Guest are viewing this topic.

MexicanCustard

  • Swamp Rat
  • Posts: 705
Database.SaveAs() Problems
« on: June 15, 2012, 09:49:00 AM »
Why am I getting an eFileSharingViolation with this code?
Code: [Select]
[CommandMethod("TESTSAVE")]
        public void TestSave()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = Application.DocumentManager.MdiActiveDocument.Database;
           
            db.SaveAs(doc.Name, false, DwgVersion.Current, null);
        }

I'm working on an app that needs to force a save after completing some operations and I get this error everytime I tried to SaveAs the currrent document.  So I wrote this test app to find a solution and I found that even the bare minimum code caused the error.

I'm using SendStringToExecute as a work around but the AutoCAD .NET Developers Guide shows using the SaveAs method
Revit 2019, AMEP 2019 64bit Win 10

Delegate

  • Guest
Re: Database.SaveAs() Problems
« Reply #1 on: June 15, 2012, 10:00:37 AM »
Tried using:

doc.Database.Saveas(doc.Name, false, DwgVersion.Current, doc.Database.SecurityParameters);

??
« Last Edit: June 15, 2012, 10:12:55 AM by Delegate »

owenwengerd

  • Bull Frog
  • Posts: 451
Re: Database.SaveAs() Problems
« Reply #2 on: June 15, 2012, 10:34:49 AM »
The active drawing file is already open by the AutoCAD editor, so you'll either need to use a different filename for SaveAs or ask the editor to save it (e.g. by sending the QSAVE command).

fixo

  • Guest
Re: Database.SaveAs() Problems
« Reply #3 on: June 15, 2012, 12:06:31 PM »
I've used something similar on this
Code: [Select]
    [CommandMethod("QsaveNClose","qnc", CommandFlags.Modal | CommandFlags.Session )]
        public static void QuickSave()
        {
            Document dwg = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = dwg.Database;           
                dwg.SendStringToExecute("_QSAVE ", true, false, false);
                dwg.SendStringToExecute("_CLOSE ", true, false, false);
 
        }


TheMaster

  • Guest
Re: Database.SaveAs() Problems
« Reply #4 on: June 15, 2012, 01:20:22 PM »
Why am I getting an eFileSharingViolation with this code?
Code: [Select]
[CommandMethod("TESTSAVE")]
        public void TestSave()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = Application.DocumentManager.MdiActiveDocument.Database;
           
            db.SaveAs(doc.Name, false, DwgVersion.Current, null);
        }

I'm working on an app that needs to force a save after completing some operations and I get this error everytime I tried to SaveAs the currrent document.  So I wrote this test app to find a solution and I found that even the bare minimum code caused the error.

I'm using SendStringToExecute as a work around but the AutoCAD .NET Developers Guide shows using the SaveAs method

The easiest solution is to use the AcadDocument.Save method:

Code - C#: [Select]
  1.  
  2. // Using System.Dynamic (requires .NET 4.0):
  3.  
  4. public static class MyDocExtensions
  5. {
  6.   public static void Save( this Document doc )
  7.   {
  8.      dynamic acadDoc = doc.AcadDocument;   // requires different code for 2013
  9.      acadDoc.Save();
  10.   }
  11. }
  12.  
  13. // Using Reflection (works on any framework version):
  14.  
  15. public static class MyDocExtensions
  16. {
  17.   public static void Save( this Document doc )
  18.   {
  19.     object acadDoc = doc.AcadDocument;            // change to .GetAcadDocument() for 2013
  20.     acadDoc.GetType().InvokeMember( "Save",
  21.              BindingFlags.InvokeMethod, null, acadDoc, new object[0]);
  22.   }
  23. }
  24.  
  25.  
  26. // The above adds the extension method Save() to the
  27. // Document class, allowing it to be used thusly:
  28.  
  29.     Application.DocumentManager.MdiActiveDocument.Save();
  30.  
  31.  


See  this thread for code portability with the AcadDocument property:


« Last Edit: June 18, 2012, 12:08:35 PM by TheMaster »

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Database.SaveAs() Problems
« Reply #5 on: June 18, 2012, 07:43:46 AM »
Thanks Tony, works great.  I hate depending on SendStringToExecute() since I have no way of knowing when and if it fired.

I don't know how it works since AcadDocument and its Save method are undocumented but it works and thats all I care about.
Revit 2019, AMEP 2019 64bit Win 10

Delegate

  • Guest
Re: Database.SaveAs() Problems
« Reply #6 on: June 18, 2012, 09:42:18 AM »
Thanks Tony, works great.  I hate depending on SendStringToExecute() since I have no way of knowing when and if it fired.

I don't know how it works since AcadDocument and its Save method are undocumented but it works and thats all I care about.

The save part does not work with as I'm missing some types for the dynamic expression. No idea where to look!

It's very confusing as to how it works! Seems the dynamic expression gives access to interop COM??

n.yuan

  • Bull Frog
  • Posts: 348
Re: Database.SaveAs() Problems
« Reply #7 on: June 18, 2012, 09:58:15 AM »
"dynamic" is introduced in C# 4.0 (.NET 4.0). So, if your project is targeting .NET 4, the keyword "dynamic" should be available for use. It is equivalent to VB's late binding

If you use VB.NET, if "Option Strict" is not turned on, you can simply do (whcih applies all .NET versions)

Dim acadDoc As Object=Application.DocumentManager.MdiDocument.AcadDocument
acadDoc.Save()

Delegate

  • Guest
Re: Database.SaveAs() Problems
« Reply #8 on: June 18, 2012, 10:28:50 AM »
Thanks - yeah the 'dynamic' keyword works fine - I ran it and inspected acadDoc

But in C# the 'acadDoc.save' says there is types missing for the dynamic expression possibly system.core or microsoft.csharp.  I've tried adding these but no success. System.Core should be built in now anyway it seems.

So I got it to work by adding the interop references and doing this:

Code - C#: [Select]
  1. AcadDocument acadDoc = (AcadDocument)doc.AcadDocument;
  2. acadDoc.Save();

But does not have the magic of using dynamics or probably other reasons not to use!  :ugly:

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Database.SaveAs() Problems
« Reply #9 on: June 18, 2012, 10:31:35 AM »
If its 2013 I think its doc.GetAcadDocument

Delegate

  • Guest
Re: Database.SaveAs() Problems
« Reply #10 on: June 18, 2012, 11:07:14 AM »
I am using C3D 2011 version.

Appears to be an issue with the Autocad Wizard for express edition. Maybe it changes a reference?

I started my own basic class library using framework 4 and it worked fine but get the error if trying to use it within the wizard.

So I created the DLL separately and referenced it in the Autocad Wizard project and it works fine.
But if I add  that same class to my Autocad Wizard project I get the acadDoc.Save(); underlined as an error.

I have double checked its all set to framework 4.0!

So sure it must be a reference or project setting I'm missing that is changed by the Autocad wizards?

*************EDIT******************

At least I will know better next time

Just had to add Microsoft.CSharp to the references lol  :lmao:
Confused as always - Microsoft.CSharp was available within the object browser already so not sure why I needed to reference it.  :ugly:
« Last Edit: June 18, 2012, 11:41:54 AM by Delegate »

TheMaster

  • Guest
Re: Database.SaveAs() Problems
« Reply #11 on: June 18, 2012, 12:10:12 PM »
I am using C3D 2011 version.

Sorry, the code I posted uses dynamic which requires .NET 4.0.

See my revision to that post for a version that works on any framework version.

Delegate

  • Guest
Re: Database.SaveAs() Problems
« Reply #12 on: June 18, 2012, 12:18:18 PM »
I am using C3D 2011 version.

Sorry, the code I posted uses dynamic which requires .NET 4.0.

See my revision to that post for a version that works on any framework version.

Thanks for your reply.

I have to say your code is always interesting to study as it introduces me to concepts within the C# language I did not know about.

TJK44

  • Guest
Re: Database.SaveAs() Problems
« Reply #13 on: August 31, 2012, 04:08:30 PM »
Thanks Tony for the useful code. I was messing around and decided this would be a cool way to check if a drawing is saved using Tony's MyDocExtensions Class.

Sorry for VB, trying to break the habit (so far unsuccessful).

Code: [Select]
Imports Autodesk.AutoCAD.ApplicationServices

Module MyDocExtensions
    Sub New()
    End Sub
    <System.Runtime.CompilerServices.Extension()> _
    Public Sub Save(ByVal doc As Document)
        Dim acadDoc As Object = doc.AcadDocument
        acadDoc.Save()
    End Sub

    <System.Runtime.CompilerServices.Extension()> _
    Public Function IsDocSaved(ByVal doc As Document) As Boolean
        Dim acadDoc As Object = doc.AcadDocument
        If acadDoc.Saved Then
            Return True
        Else
            Return False
        End If
    End Function
End Module

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Database.SaveAs() Problems
« Reply #14 on: August 31, 2012, 07:00:52 PM »
Quote
Code: [Select]
        If acadDoc.Saved Then
            Return True
        Else
            Return False
        End If

No need to make VB more VerBose than it is:

Code - vb.net: [Select]
  1. Return acadDoc.Saved
Speaking English as a French Frog