• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    *Carlos

    Nobody seems to have solved the insert block problem

    199 Views, 5 Replies
    08-10-2005 04:42 PM
    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
    Please use plain text.
    Active Member
    Posts: 6
    Registered: ‎07-20-2005

    Re: Nobody seems to have solved the insert block problem

    08-12-2005 09:31 AM in reply to: *Carlos
    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
    Please use plain text.
    *Carlos

    Re: Nobody seems to have solved the insert block problem

    08-12-2005 10:38 AM in reply to: *Carlos
    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
    Please use plain text.
    Distinguished Contributor
    Posts: 195
    Registered: ‎10-03-2003

    Re: Nobody seems to have solved the insert block problem

    08-12-2005 12:17 PM in reply to: *Carlos
    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.
    Please use plain text.
    Contributor
    Posts: 24
    Registered: ‎03-14-2006

    Re: Nobody seems to have solved the insert block problem

    08-24-2006 11:39 AM in reply to: *Carlos
    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!
    Please use plain text.
    Member
    Posts: 3
    Registered: ‎05-22-2008

    Re: Nobody seems to have solved the insert block problem

    05-23-2008 12:45 AM in reply to: *Carlos
    dear mabe2k .
    can you share me the method code :
    i can't enter into the wetsite you recommend.
    thanks at advance!
    Please use plain text.