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

    .NET

    Reply
    Valued Contributor
    Posts: 66
    Registered: ‎07-22-2004

    how to copy block from an unopened docment?

    198 Views, 6 Replies
    07-15-2005 06:25 AM
    I tried many times and use several method to copy a block in an unopened file to current document. com method is ok,but net arx method always failed .who can help me and post a sample of net arx.

    the following is my code of arx method
    private static ObjectId GetBlockArx(string blockFile, String name )
    {
    ObjectId block = ObjectId.Null;
    Database db = new Database();
    db.ReadDwgFile(blockFile, System.IO.FileShare.Read, false, null);
    ArrayList list = new ArrayList();
    Transaction dbTrans = db.TransactionManager.StartTransaction();
    try
    {
    BlockTable dbbt = dbTrans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
    BlockTableRecord dbBtr = dbTrans.GetObject(dbbt[name], OpenMode.ForRead) as BlockTableRecord;
    foreach (ObjectId entId in dbBtr)
    {
    Entity ent = dbTrans.GetObject(entId, OpenMode.ForRead) as Entity;
    Entity newEnt = ent.Clone() as Entity;
    list.Add(newEnt);
    }
    dbTrans.Commit();
    }
    finally
    {
    dbTrans.Dispose();
    db.Dispose();
    }

    Transaction trans = Arx.Tm.StartTransaction();
    try
    {
    BlockTable bt = trans.GetObject(Arx.Db.BlockTableId, OpenMode.ForWrite) as BlockTable;
    foreach (ObjectId idd in bt)
    {
    BlockTableRecord btrr = trans.GetObject(idd, OpenMode.ForWrite) as BlockTableRecord;
    if (btrr.Name.ToUpper() == name.ToUpper())
    {
    btrr.Erase();
    }
    }

    BlockTableRecord newBtr = new BlockTableRecord();
    newBtr.Name = name;
    block = bt.Add(newBtr);
    trans.AddNewlyCreatedDBObject(newBtr, true);
    foreach (object obj in list)
    {
    newBtr.AppendEntity(obj as Entity);
    trans.AddNewlyCreatedDBObject(obj as Entity, true);

    }
    foreach (ObjectId nentId in newBtr)
    {
    Entity nent = trans.GetObject(nentId, OpenMode.ForRead) as Entity;
    }
    trans.Commit(); // a fatal error here
    }
    finally { trans.Dispose(); }

    return block;
    }
    Please use plain text.
    Distinguished Contributor
    Posts: 195
    Registered: ‎10-03-2003

    Re: how to copy block from an unopened docment?

    07-15-2005 11:17 AM in reply to: netcai
    Here is a VB example based on what was posted by Albert Szilvasy.

    <CommandMethod("BlkTest")> _
    Public Sub blkTest()
    Dim doc As Document = Application.DocumentManager.MdiActiveDocument
    Dim ed As Editor = doc.Editor
    Dim tr As Transaction = doc.TransactionManager.StartTransaction
    Try
    Dim dwgName As String = HostApplicationServices.Current.FindFile("C:\temp\myLines.dwg", Application.DocumentManager.MdiActiveDocument.Database, FindFileHint.Default)
    Dim db As Database = New Database(False, False)
    db.ReadDwgFile(dwgName, IO.FileShare.Read, True, "")
    Dim NewBlkId As ObjectId
    NewBlkId = doc.Database.Insert(dwgName, db, False)
    Dim bt As BlockTable = tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead, True)
    Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite, True)
    Dim bref As BlockReference = New BlockReference(New Point3d(0.0, 0.0, 0.0), NewBlkId)
    btr.AppendEntity(bref)
    tr.AddNewlyCreatedDBObject(bref, True)
    tr.Commit()
    Catch ex As Exception
    ed.WriteMessage(ex.ToString)
    End Try
    End Sub
    Please use plain text.
    Valued Contributor
    Posts: 66
    Registered: ‎07-22-2004

    Re: how to copy block from an unopened docment?

    07-15-2005 11:01 PM in reply to: netcai
    what I mean is to insert a block in an unopened drawing to current drawing, not to insert an unopened drawing as a block to current drawing.
    Please use plain text.
    Distinguished Contributor
    Posts: 195
    Registered: ‎10-03-2003

    Re: how to copy block from an unopened docment?

    07-16-2005 04:37 AM in reply to: netcai
    myTest.dwg has a block called circles in it.

    <CommandMethod("blkInblk")> _
    Public Sub blkInblk()
    Dim doc As Document = Application.DocumentManager.MdiActiveDocument
    Dim ed As Editor = doc.Editor
    Dim tr As Transaction = doc.TransactionManager.StartTransaction
    Try
    Dim dwgName As String = HostApplicationServices.Current.FindFile("C:\temp\myTest.dwg", Application.DocumentManager.MdiActiveDocument.Database, FindFileHint.Default)
    Dim db As Database = New Database(False, False)
    db.ReadDwgFile(dwgName, IO.FileShare.Read, True, "")
    Dim takeBlkId As ObjectId
    Dim tbt As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForWrite)
    If (tbt.Has("Circles")) Then
    takeBlkId = tbt("Circles")
    Else
    ed.WriteMessage("This aint happening")
    Exit Sub
    End If
    Dim bt As BlockTable = tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead, True)
    Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite, True)
    Dim bref As BlockReference = New BlockReference(New Point3d(0.0, 0.0, 0.0), takeBlkId)
    btr.AppendEntity(bref)
    tr.AddNewlyCreatedDBObject(bref, True)
    tr.Commit()
    Catch ex As Exception
    ed.WriteMessage(ex.ToString)
    Finally
    tr.Dispose()
    End Try
    End Sub
    Please use plain text.
    Distinguished Contributor
    Posts: 195
    Registered: ‎10-03-2003

    Re: how to copy block from an unopened docment?

    07-16-2005 05:36 AM in reply to: netcai
    After looking at this a bit more, I find I get an fatal error when trying to reopen the drawing.
    Realised I didn't write the block definition so I revised the code somewhat but I'm still getting
    a fatal error.

    <CommandMethod("blkInblk")> _
    Public Sub blkInblk()
    Dim bibId As ObjectId
    Dim doc As Document = Application.DocumentManager.MdiActiveDocument
    Dim ed As Editor = doc.Editor
    Dim tr As Transaction = doc.TransactionManager.StartTransaction
    Try
    Dim dwgName As String = HostApplicationServices.Current.FindFile("C:\temp\myTest.dwg", Application.DocumentManager.MdiActiveDocument.Database, FindFileHint.Default)
    Dim db As Database = New Database(False, False)
    db.ReadDwgFile(dwgName, IO.FileShare.Read, True, "")
    Dim takeBlkId As ObjectId
    Dim tbt As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForWrite)
    If (tbt.Has("Circles")) Then
    takeBlkId = tbt("Circles")
    Else
    ed.WriteMessage("This aint happening")
    Exit Sub
    End If
    Dim bt As BlockTable = tr.GetObject(doc.Database.BlockTableId, OpenMode.ForWrite, True)
    Dim newBTR As BlockTableRecord = New BlockTableRecord
    newBTR.Name = "Circles"
    bibId = bt.Add(newBTR)
    tr.AddNewlyCreatedDBObject(newBTR, True)
    Dim bref As BlockReference = New BlockReference(New Point3d(0.0, 0.0, 0.0), takeBlkId)
    newBTR.AppendEntity(bref)
    tr.AddNewlyCreatedDBObject(bref, True)
    tr.Commit()
    Catch ex As Exception
    ed.WriteMessage(ex.ToString)
    Finally
    tr.Dispose()
    End Try
    End Sub

    FATAL ERROR: Unhandled Access Violation Reading 0x0019 Exception at 648fefd 1h

    Anybody see my error?
    Please use plain text.
    Member
    Posts: 3
    Registered: ‎05-22-2008

    Re: how to copy block from an unopened docment?

    05-23-2008 01:38 AM in reply to: netcai
    my friend .i have the same problem with you .have you firgured oot the solution.
    please tell me if you have got it .
    Please use plain text.
    Distinguished Contributor
    Posts: 223
    Registered: ‎02-17-2006

    Re: how to copy block from an unopened docment?

    05-23-2008 11:10 AM in reply to: netcai
    Attached is a snippet that I have had no issues with.

    What I did was use wblock to map a clone of the blocktable's entities from an external drawing database into the active database. If the external drawing didn't have a matching blocktable, the modelspace table is used instead as a default.


    ps: I wrote this function two years ago. it may not be correctly disposing the AcDb objects it uses, so be aware of that if you plan to copy/paste it into your code. Also, it's in VB.NET so if you want to use it, you will have to translate it into C#.
    Please use plain text.