.NET
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
how to copy block from an unopened docment?
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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;
}
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;
}
Re: how to copy block from an unopened docment?
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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.Data base, 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
<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\
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
Re: how to copy block from an unopened docment?
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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.
Re: how to copy block from an unopened docment?
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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.Data base, 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
<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\
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
Re: how to copy block from an unopened docment?
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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.Data base, 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?
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\
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?
Re: how to copy block from an unopened docment?
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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 tell me if you have got it .
Re: how to copy block from an unopened docment?
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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#.
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#.
