Message 1 of 3
Not applicable
11-15-2012
08:50 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello,
I use RealDWG2013 and have some problems.
I made a migration of files and batch in C# program.
But, sometime, I have problems of AccessViolationException (Attempted to Read or write protected memory).
My program read 2 files with RealDWG for changing the cartridge recover in the first access. It's put in the 2 files.
Do you have any idea ?
Database db = new Database(false, true);
Autocad.WorkingDatabase = db;
//Read the first DWG
db.ReadDwgFile(CartridgeFullName, FileShare.ReadWrite, false, string.Empty);
ObjectIdCollection objectIdCollectionCartridge = GetCartridge(db);
//Read the target file
db = new Database(false, true);
Autocad.WorkingDatabase = db;
db.ReadDwgFile(tempFullName, FileShare.ReadWrite, false, string.Empty);
try
{
Database dbb = objectIdCollectionCartridge[0].Database; // I TRY TO ACCESS TO DATABASE OF THE CARTRIDGE. IT'S HERE THAT THE ERROR IS CATCHED
dbb.Dispose();
}
catch(){}
db.SaveAs(tempFullName, DwgVersion.AC1800);
db.CloseInput(true);
db.Dispose();
......
private ObjectIdCollection GetCartridge(Database db)
{
ObjectId msId, psId;
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
msId = bt[BlockTableRecord.ModelSpace];
psId = bt[BlockTableRecord.PaperSpace];
tr.Commit();
}
return GetCartridge(psId, db);
}
private ObjectIdCollection GetCartridge(ObjectId btrId, Database db)
{
ObjectIdCollection ObjCollection = null;
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
foreach (ObjectId entId in btr)
{
Entity ent = tr.GetObject(entId, OpenMode.ForRead) as Entity;
if (ent != null)
{
BlockReference br = ent as BlockReference;
if (br != null)
{
BlockTableRecord bd = (BlockTableRecord)tr.GetObject(br.BlockTableRecord, OpenMode.ForRead);
if (bd.Name.ToUpper() == BLOCK_NAME_CARTOUCHE || bd.Name.ToUpper() == BLOCK_NAME_CARTOUCHE_A0)
{
try
{
ObjCollection = new ObjectIdCollection();
foreach (ObjectId obj in bd)
{
Entity ento = tr.GetObject(obj, OpenMode.ForRead) as Entity;
Line line = ento as Line;
if (line != null)
{
ObjCollection.Add(line.ObjectId);
}
AttributeDefinition atr = ento as AttributeDefinition;
if (atr != null)
{
ObjCollection.Add(atr.ObjectId);
}
DBText txt = ento as DBText;
if (txt != null)
{
ObjCollection.Add(txt.ObjectId);
}
Polyline pl = ento as Polyline;
if (pl != null)
{
ObjCollection.Add(pl.ObjectId);
}
Hatch ha = ento as Hatch;
if (ha != null)
{
ObjCollection.Add(ha.ObjectId);
}
}
}
catch (Autodesk.AutoCAD.Runtime.Exception e)
{ }
catch (System.Exception ex)
{ }
}
}
}
}
tr.Commit();
}
return ObjCollection;
}
Solved! Go to Solution.