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

VBA -> VB.NET issues with SaveAs

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
drambo61
2223 Views, 3 Replies

VBA -> VB.NET issues with SaveAs

Hi all,
I'm trying to do what I consider a simple task (at least it's simple in VBA, but I'm trying to move away from VBA since it's no longer supported): Create a new, blank DWG file, save it to a specified directory and have it become the active file inside the ACAD UI for further editing.

I've been able to cobble together several variations of code that adds a new document to the UI, and even saves it out to the proper location'/filename, but even when it accomplishes this successfully, ACAD dies when I close it down with: "FATAL ERROR: Unhandled Access Violation Reading 0x0041 Exception at 3f892986h".

For background, I'm running ACAD2011 (plain) on Windows 7, 64-bit. The code that I've used with best results is a brute-force, stuff-the-command-line setup as follows (with thanks to Kean Walmsley of "Through the Interface"):

Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Runtime

<CommandMethod("bmiFileNew", CommandFlags.Modal)> _
Public Sub bmiFileNew()
   Dim ufn As New ufFileNew
   Dim drResult As System.Windows.Forms.DialogResult
   Dim dwgActive As Document
   Dim sPrototypeDwg As String
   Dim sNewFilename As String
   Dim sLispCommand As String

   NewFile = ""
   Try
      drResult = Application.ShowModalDialog(ufn)
      If NewFile <> "" Then
         sPrototypeDwg = "C:\TEMP\Template2011"
         sNewFilename = "C:\TEMP\" & NewFile
         Dim acDocMgr As DocumentCollection = Application.DocumentManager
         dwgActive = acDocMgr.Add(sPrototypeDwg)
         sNewFilename = Replace(sNewFilename, "\", "/")
         Dim ocmd As Object = Application.GetSystemVariable("CMDECHO")
         sLispCommand = "(setvar ""CMDECHO"" 1) " & _
                   "(command ""_.SAVEAS"" ""2010"" """ & sNewFilename & """) " & _
                   "(setvar ""CMDECHO"" " & ocmd.ToString & ") " & _
                   "(princ) "
         dwgActive.SendStringToExecute(sLispCommand, _
                   True, _
                   False, _
                   True)
         acDocMgr.MdiActiveDocument = dwgActive
      End If
   Catch ex As Exception
      MsgBox(ex.Message.ToString)
   End Try
End Sub

I've also tried using Database.Saveas() and Document.SaveAs() alternatives with less success than this and still I have the fatal error when exiting ACAD. Any assistance would be much appreciated.

Thanks,
Dave
3 REPLIES 3
Message 2 of 4

Why not just use the window filesystem FILE.COPY to make the new file (copy your prototype file to the desired location and rename)? Then open the new document and do your work.

 

 

Mike Robertson
FL. Dept. of Transportation
CADD Applications Developer
Message 3 of 4

Hi Michael,

Thanks for the response.  I suppose that is one alternative.  It just grinds me when I've got two simple lines of VBA code that I have to completely revamp to put into .NET.

 

Also, after letting ACAD send the error data back to AutoDesk, it informed me that a patch was available to help with this issue.  After downloading it and applying, it seems to have fixed the problem.  So now I have to go back thru the 5 different ways I've tried to solve this to find the "best" one.

 

Thanks again,

Dave

Message 4 of 4
ken.washburn
in reply to: drambo61

I'm somewhat new to this myself, but I just finished converting one of my VBA programs.  The issue may be with the command flag settings.  Try this:

 

 

<CommandMethod("DrawingInUI")> _

 

Public Sub DrawingInUI()

 

Dim MYDocMgr As DocumentCollection = Application.DocumentManager

 

Dim MYDoc As Document

Dim testDB As New Database(False, True)

 

Dim fileName As String = "Test" 'new drawing name

 

Dim MyPath = "c:\" & fileName & ".dwg" 'Path to save new drawing to.

testDB.ReadDwgFile("N:\ACAD\DWG\ENG-DWG\TITLE\AFE-TITLE-2.dwg", FileOpenMode.OpenForReadAndAllShare, True, "")

'Path of template file to open as a new drawing - in this case a drawing with a title block.

 

 

Try

testDB.SaveAs(MyPath, DwgVersion.Current) 'Save the template drawing as a new drawing (without opening it up in the user interface)

testDB.Dispose()

 

Catch

End Try

MYDoc = MYDocMgr.Open(MyPath, False, "") ' Open the newly created drawing up into the user interface.

DocumentManager.MdiActiveDocument = MYDoc

 

 

 

End Sub

 

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