Nobody seems to have solved the insert block problem

Nobody seems to have solved the insert block problem

Anonymous
Not applicable
886 Views
5 Replies
Message 1 of 6

Nobody seems to have solved the insert block problem

Anonymous
Not applicable
Hi again (I know, I post like crazy. Sorry!),

There have been many threads about inserting a block (WBLOCKed as a .dwg
file) into a drawing, but the final post in each thread is always the same -
they result in a fatal error. Has anyone solved this problem? The method
that all of the attempts suggest is along these lines:

Document doc=Application.DocumentManager.MdiActiveDocument;
string fname = "PathAndFilenameOfBlockFile";
HostApplicationServices.Current.FindFile(fname, doc.Database,
FindFileHint.Default);

using(Database db = new Database(false, false))
{
db.ReadDwgFile(fname, System.IO.FileShare.Read, true, null);
using(Transaction t = doc.TransactionManager.StartTransaction())
{
ObjectId idBTR = doc.Database.Insert("BlockName",db,false);
BlockTable bt = (BlockTable)t.GetObject
(doc.Database.BlockTableId,
OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)t.GetObject
(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

Point3d origin = new Point3d(0, 0, 0);
using(BlockReference bref = new BlockReference(origin, idBTR))
{
btr.AppendEntity(bref);
t.TransactionManager.AddNewlyCreatedDBObject(bref, true);
}
t.Commit();
t.Dispose();
}
}

The fatal error is:

Unhandled Access Violation Reading 0xfffffff Exception
at 7ef399f9h

Thanks,
Carlos
0 Likes
887 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable
I converted your code to VB because I know and needed then got rid of error by specifying file extension.

You'll probably want to add more testing to check that the file exists or error trapping around FindFile part to prevent error.


Good luck.

_
Sub altBlkInsert()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim fname As String = "c:\MyBlockFile.dwg"
HostApplicationServices.Current.FindFile(fname, doc.Database, FindFileHint.Default)
' Using
Dim db As Database = New Database(False, False)
Try
db.ReadDwgFile(fname, System.IO.FileShare.Read, True, Nothing)
' Using
Dim t As Transaction = doc.TransactionManager.StartTransaction
Try
Dim idBTR As ObjectId = doc.Database.Insert("MyBlockFile", db, False)
Dim bt As BlockTable = CType(t.GetObject(doc.Database.BlockTableId, OpenMode.ForRead), BlockTable)
Dim btr As BlockTableRecord = CType(t.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
Dim origin As Point3d = New Point3d(0, 0, 0)
' Using
Dim bref As BlockReference = New BlockReference(origin, idBTR)
Try
btr.AppendEntity(bref)
t.TransactionManager.AddNewlyCreatedDBObject(bref, True)
Finally
CType(bref, IDisposable).Dispose()
End Try
t.Commit()
t.Dispose()
Finally
CType(t, IDisposable).Dispose()
End Try
Finally
CType(db, IDisposable).Dispose()
End Try
End Sub
0 Likes
Message 3 of 6

Anonymous
Not applicable
I've actually got the file extension in the filename, but I'll try adding
more error trapping.

I also noticed that you dispose of things like the bref and db, which I
wasn't doing. I'll add that stuff too.

Thanks very much for your help. I really appreciate that you would take the
time.

-Carlos


wrote in message news:[email protected]...
I converted your code to VB because I know and needed then got rid of error
by specifying file extension.

You'll probably want to add more testing to check that the file exists or
error trapping around FindFile part to prevent error.


Good luck.

_
Sub altBlkInsert()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim fname As String = "c:\MyBlockFile.dwg"
HostApplicationServices.Current.FindFile(fname, doc.Database,
FindFileHint.Default)
' Using
Dim db As Database = New Database(False, False)
Try
db.ReadDwgFile(fname, System.IO.FileShare.Read, True, Nothing)
' Using
Dim t As Transaction = doc.TransactionManager.StartTransaction
Try
Dim idBTR As ObjectId = doc.Database.Insert("MyBlockFile", db,
False)
Dim bt As BlockTable =
CType(t.GetObject(doc.Database.BlockTableId, OpenMode.ForRead), BlockTable)
Dim btr As BlockTableRecord =
CType(t.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite),
BlockTableRecord)
Dim origin As Point3d = New Point3d(0, 0, 0)
' Using
Dim bref As BlockReference = New BlockReference(origin, idBTR)
Try
btr.AppendEntity(bref)
t.TransactionManager.AddNewlyCreatedDBObject(bref, True)
Finally
CType(bref, IDisposable).Dispose()
End Try
t.Commit()
t.Dispose()
Finally
CType(t, IDisposable).Dispose()
End Try
Finally
CType(db, IDisposable).Dispose()
End Try
End Sub
0 Likes
Message 4 of 6

Mikko
Advocate
Advocate
The error is not from inserting a drawing into a drawing, that works fine. It happens when you try to insert a block from an unopened drawing that has several blocks in it and you only want to insert one specified block from that drawing. Anyhow that is what my experience has been. See http://discussion.autodesk.com/thread.jspa?threadID=416114 for some code that I've tried.
0 Likes
Message 5 of 6

Anonymous
Not applicable
Hi!

I also had the problem with inserting a block from one drawing to another. I solved it with an Lisp-Script, which my .net-code has called.

But then I found the following manual in the Autodesk-Blogs:
http://through-the-interface.typepad.com/through_the_interface/2006/08/import_blocks_f.html

... and it is so simple and wonderful. It works perfekt!
0 Likes
Message 6 of 6

Anonymous
Not applicable
dear mabe2k .
can you share me the method code :
i can't enter into the wetsite you recommend.
thanks at advance!
0 Likes