.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

VB.NET SAVEAS / eFileAccessErr

25 REPLIES 25
SOLVED
Reply
Message 1 of 26
xpeter88
4444 Views, 25 Replies

VB.NET SAVEAS / eFileAccessErr

Hi,

whenever I try to save my curret drawing as I will get this error.

I was looking here what could be the issue but no success yet.

Do you now what could be the problem?

Many thanks.

 

I used standard method;

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Runtime
 
<CommandMethod("SaveActiveDrawing")> _
Public Sub SaveActiveDrawing()
  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  Dim strDWGName As String = acDoc.Name
 
  Dim obj As Object = Application.GetSystemVariable("DWGTITLED")
 
  '' Check to see if the drawing has been named
  If System.Convert.ToInt16(obj) = 0 Then
      '' If the drawing is using a default name (Drawing1, Drawing2, etc)
      '' then provide a new name
      strDWGName = "c:\MyDrawing.dwg"
  End If
 
  '' Save the active drawing
  acDoc.Database.SaveAs(strDWGName, True, DwgVersion.Current, _
                        acDoc.Database.SecurityParameters)
End Sub

 

25 REPLIES 25
Message 2 of 26
hgasty1001
in reply to: xpeter88

Hi,

 

Do you have permision to write in C:\?

 

Gaston Nunez

Message 3 of 26
xpeter88
in reply to: hgasty1001

Sure, I am on my laptop and there is no issue to manipulate with any files 😕

Message 4 of 26
xpeter88
in reply to: xpeter88

This is that error;

 

Untitled.png

 

Message 5 of 26
hgasty1001
in reply to: xpeter88

Hi,

 

Just try the same code, but this time saving to a folder, and see what happens. This may help:Disabling UAC

 

Gaston Nunez

Message 6 of 26
mzakiralam
in reply to: xpeter88

Hi,
sometimes if you do not lock the document then this type of error occurs. As you are using command method so document should be locked automatically. But you can try to lock the document in code explicitly and try the code once again.

Message 7 of 26
jon-p
in reply to: xpeter88

Peter,

Yes, it happens.  We fought with the SaveAs api in Autocad for a LONG time.  The following is what we have thus far.  It's a shame that something so basic / trivial has to be such a pain.

 

	''' <summary>
	''' saves the current drawing
	''' </summary>
	''' <param name="db">the database / drawing</param>
	''' <remarks>We only had to write this because the autocad api is retarded</remarks>
	<Extension()>
	Public Sub SaveDamnYou(db As Database)
		Dim doc As Document = Nothing
 
		Try
			doc = DocumentManager.GetDocument(db)
		Catch
			'db is in batch mode
		End Try
 
		'If we have a document, attempt to call a quick save
		If doc IsNot Nothing Then
			If doc.IsReadOnly Then
				Throw New ROCException("Save failed. Drawing is Read-Only.")
			Else
 
				Try
					doc.Save()
				Catch ex As Exception
					doDBSave(db)
				End Try
 
 
			End If
		Else
		'If no document, attempt to save the database itself
		doDBSave(db)
		End If
 
	End Sub
 
	''' <summary>
	''' Attempts to save the database to itself, or logs an error if it fails
	''' </summary>
	''' <param name="db"></param>
	''' <remarks></remarks>
	Public Sub doDBSave(db As Database)
		Try
			Dim DwgFilePath As String = db.GetRealFilename
			db.SaveAs(DwgFilePath, TrueDwgVersion.Current, db.SecurityParameters)
		Catch ex As Autodesk.AutoCAD.Runtime.Exception
 
			LogInfo("Acad Error Status: {0}".FMe(ex.ErrorStatus))
			Throw
		End Try
	End Sub
 
	''' <summary>
	''' Saves the document via COM
	''' </summary>
	''' <param name="doc">document to be saved </param>
	''' <remarks>Used by the above save routine to allow a lock document to be saved, because the API is SUPER retarded</remarks>
	<Extension()>
	Public Sub Save(doc As Document)
 
		Dim adoc As Interop.AcadDocument = CType(doc.AcadDocument, Interop.AcadDocument)
		'adoc.Save()
		adoc.SendCommand("_QSAVE ")
 
	End Sub
 
	''' <summary>
	''' gets the real filename of the database. instead of .sv$ filenames
	''' </summary>
	''' <param name="db">the database / drawing</param>
	''' <returns>returns the real filename</returns>
	''' <remarks>http://through-the-interface.typepad.com/through_the_interface/2008/03/getting-the-ful.html</remarks>
	<Extension()>
	Public Function GetRealFilename(db As DatabaseAs String
		Dim doc As Document = Nothing
		Try
			doc = DocumentManager.GetDocument(db)
		Catch
			'This error indicates that there is no document object
		End Try
		'is this open in batch mode?
		If doc Is Nothing Then
			Return db.Filename
		Else
			Return doc.Name
		End If
	End Function
Message 8 of 26
xpeter88
in reply to: hgasty1001


@hgasty1001 wrote:

Hi,

 

Just try the same code, but this time saving to a folder, and see what happens. This may help:Disabling UAC

 

Gaston Nunez


No this did not help.

Message 9 of 26
xpeter88
in reply to: mzakiralam


@mzakiralam wrote:

Hi,
sometimes if you do not lock the document then this type of error occurs. As you are using command method so document should be locked automatically. But you can try to lock the document in code explicitly and try the code once again.


No success as well 😕 I tried to lock the document already.

Message 10 of 26
xpeter88
in reply to: jon-p

jon-p: What references do I have to use? Some expressions are not recognized. I've got loaded AcCui, AcDbMgd, AcMgd.

 

Then;

Imports System
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Colors

 

Nothing else I do not need for code I have. But it seems that for your code I missed something. 

 

 

 

Message 11 of 26
jon-p
in reply to: xpeter88

The following are the references i have:

Imports System.Runtime.CompilerServices
Imports System.Reflection
Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.ApplicationServices.Application
Imports Autodesk.AutoCAD.DatabaseServices
Imports System.Threading.Tasks
Imports System.IO

Project References are:

AcCui, AcDbMgd, AcMgd, AcWindows, AdWindows, Autodesk.Autocad.Interop, and Autodesk.Autocad.interop.common

Granted, these are probably more than are required for the Save functionality, but i thought i'd include them just be safe... 🙂

 

If you're having issues with:

1. something called fme - change that to string.format()

2. Rocexception - change that to argumentexception

3. loginfo - change to console.writeline() or whatever your logging method is

 

if you have any other compile issues, post the messages and i can help you with them.

Message 12 of 26
Hallex
in reply to: xpeter88

I have reference to accoremgd.dll (using A2014)

in addition to usual dll's

then this code should work:

                <CommandMethod("SaveActiveDrawing",CommandFlags.Session)> _
                Public Sub SaveActiveDrawing()
            Dim acDocMgr as DocumentCollection=Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager
            Dim acDoc As Document = acDocMgr.MdiActiveDocument
            Dim strDWGName As String = acDoc.Name
           'Lock document
            Dim docLock As DocumentLock = acDoc.LockDocument
            Using docLock
                Dim obj As Object = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("DWGTITLED")

                '' Check to see if the drawing has been named
                If System.Convert.ToInt16(obj) = 0 Then
                    '' If the drawing is using a default name (Drawing1, Drawing2, etc)
                    '' then provide a new name
                    strDWGName = "c:\MyDrawing.dwg"
                End If
                '' save thumbnail preview
              acDoc.Database.UpdateThumbnail=15
                '' Save the active drawing
                acDoc.Database.SaveAs(strDWGName, True, DwgVersion.Current, _
                	acDoc.Database.SecurityParameters)
                'Close Database
          acDoc.Database.CloseInput(True)

            End Using 
            'Close Document
            try
            	DocumentExtension.CloseAndDiscard(acDoc)
            Catch
            	End try
  
        End Sub

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 13 of 26
xpeter88
in reply to: jon-p

Hmm no this does not work for me as well. Still the same error. I am running 2012 AutoCAD so I do not have AcCoreMgd.dll. That was present in 2013+ I think.

 

Is it really so difficult to SaveAs one drawing?

Message 14 of 26
jon-p
in reply to: xpeter88

Peter,

Did my code not work for you?  And, yes, in my experience, it really is that difficult to save a drawing... 😞

-Jon

Message 15 of 26
xpeter88
in reply to: jon-p

@Jon-P: unfortunately not, or not yet,...I was trying to fix all the error in code that I had but some of them I was not able to fix. I am still learning to VBA but I was expecting that it will be easier to "just" save the drawing.

Message 16 of 26
jon-p
in reply to: xpeter88

Do you have any specific errors? If you post them, I might be able to help you resolve them.

Message 17 of 26
xpeter88
in reply to: jon-p

yes, sure, I will share them if you want to help me with them as soon as I get home 🙂

 

Message 18 of 26
xpeter88
in reply to: xpeter88

@Jon-P: Here is what issues I have with code you posted. References and imports are exactly what you wrote.

 

2/3 issues you wrote I have fixed, that one missing is FMe if I replace FMe by string.format() it does not help. Except this one there is a few more 🙂

 

 

Error 4 'Exception' is ambiguous, imported from the namespaces or types 'Autodesk.AutoCAD.Runtime, System'. C:\Users\xxx\Documents\Visual Studio 2010\Projects\SaveAsExample\SaveAsExample\myCommands.vb 52 33 SaveAsExample

 

 

Error 6 'FMe' is not a member of 'String'. C:\Users\*\Documents\Visual Studio 2010\Projects\SaveAsExample\SaveAsExample\myCommands.vb 76 35 SaveAsExample

 

 

Error 5 'GetRealFilename' is not a member of 'Autodesk.AutoCAD.DatabaseServices.Database'. C:\Users\*\Documents\Visual Studio 2010\Projects\SaveAsExample\SaveAsExample\myCommands.vb 72 45 SaveAsExample

 

Error 3 'Save' is not a member of 'Autodesk.AutoCAD.ApplicationServices.Document'. C:\Users\*\Documents\Visual Studio 2010\Projects\SaveAsExample\SaveAsExample\myCommands.vb 51 25 SaveAsExample

 

Error 2 Extension methods can be defined only in modules. C:\Users\*\Documents\Visual Studio 2010\Projects\SaveAsExample\SaveAsExample\myCommands.vb 34 10 SaveAsExample

 

 

Error 7 Extension methods can be defined only in modules. C:\Users\*\Documents\Visual Studio 2010\Projects\SaveAsExample\SaveAsExample\myCommands.vb 86 10 SaveAsExample

 

Error 9 Extension methods can be defined only in modules. C:\Users\*\Documents\Visual Studio 2010\Projects\SaveAsExample\SaveAsExample\myCommands.vb 101 10 SaveAsExample

 

 

Error 8 Type 'Interop.AcadDocument' is not defined. C:\Users\*\Documents\Visual Studio 2010\Projects\SaveAsExample\SaveAsExample\myCommands.vb 89 25 SaveAsExample

 

I really cannot believe that to save the drawing as is so complicated 

Message 19 of 26
mzakiralam
in reply to: xpeter88

Error4 can be solved if you defined the namespace before Exception. Modified code should be "System.Exception" or "Autodesk.AutoCAD.Runtime.Exception". This is occuring as System namespace and Autodesk.AutoCAD.Runtime both contains 'Exception'. So while compiler start to compile it gets cofused.

 

For other error , you should also post your whole code. Then people can help you easily. But from your error message it is evident that you are trying to use some method or properties from a class which is not belongs to that class. As an example, Error5: 'GetRealFilename' is not a member of 'Autodesk.AutoCAD.DatabaseServices.Database'. I suppose that you are trying to get the current drawing name. If so then you can get the drawing path from document class. from below code snippet you can get the active document name. Please share your code so that one can help you.

Dim doc as Document = Application.DocumentManager.MdiActiveDocument
Dim fileName as string = Path.GetFileName(doc.Name)

 

Message 20 of 26
xpeter88
in reply to: mzakiralam

thanks I fixex error 4

the code is here (was posted by Jon-P in this topic just bit updated when I tried to fix the errors)

 

Imports System
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
Imports System.Runtime.CompilerServices
Imports System.Reflection
Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.ApplicationServices.Application
Imports System.Threading.Tasks
Imports System.IO
' This line is not mandatory, but improves loading performances
<Assembly: CommandClass(GetType(AutoCAD_VB_plug_in1.MyCommands))> 
Namespace AutoCAD_VB_plug_in1

    Public Class MyCommands
        ''' <summary>
        ''' saves the current drawing
        ''' </summary>
        ''' <param name="db">the database / drawing</param>
        ''' <remarks>We only had to write this because the autocad api is retarded</remarks>
        <Extension()>
        Public Sub SaveDamnYou(ByVal db As Database)
            Dim doc As Document = Nothing

            Try
                doc = DocumentManager.GetDocument(db)
            Catch
                'db is in batch mode
            End Try

            'If we have a document, attempt to call a quick save
            If doc IsNot Nothing Then
                If doc.IsReadOnly Then
                    Throw New ArgumentException("Save failed. Drawing is Read-Only.")
                Else

                    Try
                        doc.Save()
                    Catch ex As System.Exception
                        doDBSave(db)
                    End Try


                End If
            Else
                'If no document, attempt to save the database itself
                doDBSave(db)
            End If

        End Sub

        ''' <summary>
        ''' Attempts to save the database to itself, or logs an error if it fails
        ''' </summary>
        ''' <param name="db"></param>
        ''' <remarks></remarks>
        Public Sub doDBSave(ByVal db As Database)
            Try
                Dim DwgFilePath As String = db.GetRealFilename
                db.SaveAs(DwgFilePath, True, DwgVersion.Current, db.SecurityParameters)
            Catch ex As Autodesk.AutoCAD.Runtime.Exception

                Console.WriteLine("Acad Error Status: {0}".FMe(ex.ErrorStatus))
                Throw
            End Try
        End Sub

        ''' <summary>
        ''' Saves the document via COM
        ''' </summary>
        ''' <param name="doc">document to be saved </param>
        ''' <remarks>Used by the above save routine to allow a lock document to be saved, because the API is SUPER retarded</remarks>
        <Extension()>
        Public Sub Save(ByVal doc As Document)

            Dim adoc As Interop.AcadDocument = CType(doc.AcadDocument, Interop.AcadDocument)
            'adoc.Save()
            adoc.SendCommand("_QSAVE ")

        End Sub

        ''' <summary>
        ''' gets the real filename of the database. instead of .sv$ filenames
        ''' </summary>
        ''' <param name="db">the database / drawing</param>
        ''' <returns>returns the real filename</returns>
        ''' <remarks>http://through-the-interface.typepad.com/through_the_interface/2008/03/getting-the-ful.html</remarks...
        <Extension()>
        Public Function GetRealFilename(ByVal db As Database) As String
            Dim doc As Document = Nothing
            Try
                doc = DocumentManager.GetDocument(db)
            Catch
                'This error indicates that there is no document object
            End Try
            'is this open in batch mode?
            If doc Is Nothing Then
                Return db.Filename
            Else
                Return doc.Name
            End If
        End Function

    End Class
End Namespace

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost