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

Nobody seems to have solved the insert block problem

5 REPLIES 5
Reply
Message 1 of 6
Anonymous
416 Views, 5 Replies

Nobody seems to have solved the insert block problem

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
5 REPLIES 5
Message 2 of 6
omelo
in reply to: Anonymous

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
Message 3 of 6
Anonymous
in reply to: Anonymous

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:4927502@discussion.autodesk.com...
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
Message 4 of 6
Mikko
in reply to: Anonymous

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.
Message 5 of 6
mabe2k11
in reply to: Anonymous

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!
Message 6 of 6
airtree
in reply to: Anonymous

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

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