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

Database.SaveAs() and Document.CloseAndSave() woes

5 REPLIES 5
Reply
Message 1 of 6
Ojhysseus
3176 Views, 5 Replies

Database.SaveAs() and Document.CloseAndSave() woes

Hello Ppl,

I am trying to programmatically apply updates to a set of .dwg drawings, one of which involves inserting a block as per the below code. However, as per the subject line, I'm having trouble going about saving the resultant drawing to the filesystem even though it's to a different folder. The error message is "Error writing/closing file" when I use Database.SaveAs()

If I use the call to Document.CloseAndSave() instead, AutoCAD crashes, even when I've got a lock on the Document. Could somebody kindly shed some light on what I am doing wrong, and the proper way to go about it?
From the C++ samples I've checked, I shouldn't be too far from getting it right.. but that's not good enough yet. Thanks.

=====================================================
Public Shared Sub InsertSymbol(ByVal doc As Document, ByVal dwgPath As String, ByVal insertPoint As Point3d, Optional ByRef attribValues As StringDictionary = Nothing, Optional ByVal prompt As Boolean = False)

' dim loc as DocumentLock = doc.LockDocument()
Using t As Transaction = doc.TransactionManager.StartTransaction(), db As Database = New Database(False, False)
'read drawing
db.ReadDwgFile(dwgPath, FileShare.Read, True, Nothing)

'insert it as a new block
Dim idBTR As ObjectId = doc.Database.Insert("Watermark", db, True)
'create a ref to the block

Using bt As BlockTable = t.GetObject(doc.Database.BlockTableId, OpenMode.ForRead), _
btr As BlockTableRecord = t.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), _
bref As BlockReference = New BlockReference(insertPoint, idBTR)

btr.AppendEntity(bref)
doc.TransactionManager.AddNewlyCreatedDBObject(bref, True)

Dim ed As Autodesk.AutoCAD.EditorInput.Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
ed.WriteMessage("DWG Added")

End Using
t.Commit()
db.SaveAs("multicomponent_dyn.dwg", DwgVersion.Current) ' or doc.CloseAndSave("multicomponent_dyn.dwg")
' loc.Dispose()
End Using
End Sub
=====================================================

Rico.
5 REPLIES 5
Message 2 of 6
Ojhysseus
in reply to: Ojhysseus

More on this issue... I have converted/ported a sample C++ usage of AcDbDatabase::saveAs() to VB.Net.
What I run into now is some mysterious eInvalidInput error status, along with a stack trace from the line where I call db.SaveAs("c:\\test1.dwg, DwgVersion.Current")

===========================================
Public Shared Sub CreateDwg()
Using db As Database = New Database(), _
tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = db.TransactionManager, _
t as Transaction = tm.StartTransaction(), _
bt As BlockTable = tm.GetObject(db.BlockTableId, OpenMode.ForWrite), _
btr As BlockTableRecord = tm.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

Dim pCir1 As Circle = New Circle(New Point3d(1,1,1), _
New Vector3d(0,0,1), _
1.0), _
pCir2 as Circle = New Circle(New Point3d(4,4,4), _
New Vector3d(0,0,1), _
2.0)

btr.AppendEntity(pCir1)

btr.AppendEntity(pCir2)

'AcDbDatabase::saveAs() does NOT automatically
' // append a DWG file extension, so it
' // must be specified.
' //
db.SaveAs("c:\\test1.dwg", DwgVersion.Current)
End Using
End Sub
===========================================

Some help and enlightenment would be appreciated.. Thanks.

Rico.

Stack Trace:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Autodesk.AutoCAD.Runtime.Exception: eInvalidInput
at Autodesk.AutoCAD.DatabaseServices.Database.SaveAs(String fileName, DwgVersion version)
at DynaBlock.DynamicBlock.ReadDwgFile.CreateDwg() in C:\Documents and Settings\Administrator\My Documents\SharpDevelop Projects\DynaBlock\ReadDwgFile.vb:line 154
at DynaBlock.DynamicBlock.DynamicBlockCommands.DynCreate() in C:\Documents and Settings\Administrator\My Documents\SharpDevelop Projects\DynaBlock\DynBlock.vb:line 165
Message 3 of 6
Anonymous
in reply to: Ojhysseus

Change:
db.SaveAs("c:\\test1.dwg", DwgVersion.Current)
with:
db.SaveAs("c:\test1.dwg", DwgVersion.Current)
Double backslash is useful in C++/C# but not in VB
Message 4 of 6
Ojhysseus
in reply to: Ojhysseus

> Re: Database.SaveAs() and Document.CloseAndSave() woes
>Change:
>db.SaveAs("c:\\test1.dwg", DwgVersion.Current)
>with:
>db.SaveAs("c:\test1.dwg", DwgVersion.Current)
>Double backslash is useful in C++/C# but not in VB

Thanks Alexander. That seems to have been the reason for the invalid input error.
However, that amendment only takes me back to where I was with the code in the first post: "error writing/closing file" popping up and an exception eFileAccessErr being thrown.

I spent a good part of the day trying to compile the original C++ sample for CreateDwg() .. on Visual C++ Express Edition.
I think it's better I try that on the standard version instead.. or can I get the file 'mfc80u.lib' somewhere? It's missing for some reason..
Thanks.

Rico.
Message 5 of 6
Anonymous
in reply to: Ojhysseus

This code generate no errors fore me (c:\test1.dwg is not opened or locked with another application)
[code]
Public Shared Sub CreateDwg()
Using db As Database = New Database(), _
tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = db.TransactionManager, _
t As Transaction = tm.StartTransaction(), _
bt As BlockTable = tm.GetObject(db.BlockTableId, OpenMode.ForWrite), _
btr As BlockTableRecord = tm.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

Dim pCir1 As Circle = New Circle(New Point3d(1, 1, 1), _
New Vector3d(0, 0, 1), _
1.0), _
pCir2 As Circle = New Circle(New Point3d(4, 4, 4), _
New Vector3d(0, 0, 1), _
2.0)

btr.AppendEntity(pCir1)
t.AddNewlyCreatedDBObject(pCir1, True)
btr.AppendEntity(pCir2)
t.AddNewlyCreatedDBObject(pCir2, True)

'AcDbDatabase::saveAs() does NOT automatically
' // append a DWG file extension, so it
' // must be specified.
' //
db.SaveAs("c:\test1.dwg", DwgVersion.Current)
End Using
End Sub
[/code]
Message 6 of 6
Ojhysseus
in reply to: Ojhysseus

All right! Sounds good. Thanks for the feedback Alexander.
Glad to know that the code is okay and that my abilities for simple inspection weren't failing me 🙂
For my part, took me a while but I've managed to find an installation of VS.Net 2005 that compiled the C++ samples to .arx (forget about the Express Edition ppl)
And that one worked too.. got to poke around my .Net config I guess.

Rico.

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