.NET

Reply
Active Member
Ojhysseus
Posts: 6
Registered: ‎08-04-2006
Message 1 of 6 (1,267 Views)

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

1267 Views, 5 Replies
08-05-2006 09:03 PM
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.
Active Member
Ojhysseus
Posts: 6
Registered: ‎08-04-2006
Message 2 of 6 (1,267 Views)

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

08-06-2006 02:03 PM 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
Distinguished Contributor
Alexander Rivilis
Posts: 303
Registered: ‎09-26-2004
Message 3 of 6 (1,267 Views)

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

08-07-2006 02:46 AM 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
Active Member
Ojhysseus
Posts: 6
Registered: ‎08-04-2006
Message 4 of 6 (1,267 Views)

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

08-07-2006 03:10 AM 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.
Distinguished Contributor
Alexander Rivilis
Posts: 303
Registered: ‎09-26-2004
Message 5 of 6 (1,267 Views)

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

08-07-2006 03:18 AM 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]
Active Member
Ojhysseus
Posts: 6
Registered: ‎08-04-2006
Message 6 of 6 (1,267 Views)

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

08-07-2006 11:45 AM 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 :smileyhappy:
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.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.