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

how to copy block from an unopened docment?

6 REPLIES 6
Reply
Message 1 of 7
netcai
382 Views, 6 Replies

how to copy block from an unopened docment?

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;
}
6 REPLIES 6
Message 2 of 7
Mikko
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
Message 3 of 7
netcai
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.
Message 4 of 7
Mikko
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
Message 5 of 7
Mikko
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?
Message 6 of 7
airtree
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 .
Message 7 of 7
jbooth
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#.

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