Hello,
I am having a problem with the method below. I initially created this method for AutoCAD 2008 and it works fine. Now, I need the method to work with AutoCAD 2006. The problem that I am having is with the line ObjectId xrefObjId = db.OverlayXref(savedPath, referenceName); within the "for" loop. The method crashes on this line with an eFileAccessErr error. Also, when I am debugging and hover over the xrefObjId variable it states ObjectLeftOnDisk = 'xrefObjId.ObjectLeftOnDisk' threw an exception of type 'System.AccessViolationException' Does anyone have any ideas?
/// <summary>
/// Builds a drawing assembly with one or more drawing files.
/// </summary>
///
/// <param name="assemblyData">
/// An arraylist containing the data to build a drawing
/// assembly. The first element of the arraylist is the
/// eMatrix object type, name, and revision.
///
/// For example: "PICS ACAD Assembly|12-1234-5468-7|A"
///
/// All remaining elements of the arraylist contain the drawing
/// files that make up the drawing assembly.
/// </param>
///
public static void BuildAssembly(ArrayList assemblyData)
{
// Isolate the eMatrix object type, name and revision from the
// first element of the "assemblyData" arraylist input parameter.
string mxTypeNameRevison = assemblyData[0] as string;
string[] mxTypeNameRevisionSplit =
mxTypeNameRevison.Split(new char[] { '|' });
string mxType = mxTypeNameRevisionSplit[0] as string;
string mxName = mxTypeNameRevisionSplit[1] as string;
string mxRevision = mxTypeNameRevisionSplit[2] as string;
// Isolate the directory name where the drawing assembly
// drawing will be saved. This directory location can be
// acquired from any drawing file which starts at the
// second element of the "assemblyData" arraylist input
// parameter.
string fullPathName = assemblyData[1] as string;
string directoryName = Path.GetDirectoryName(fullPathName);
// Create the drawing assemblies full path name.
string drawingAssemblyFullPathName =
directoryName + "\\" + mxName + ".dwg";
try
{
// Create a new database.
Database db = new Database(true, false);
using (db)
{
// Start a database transaction.
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
// All remaining elements of the "assemblyData"
// arraylist input parameter are the xreferences
// to be added to the new database. Add these
// xreferences now...
for (int i = 1; i < assemblyData.Count; i++)
{
string savedPath = assemblyData[i] as string;
string referenceName =
Path.GetFileNameWithoutExtension(savedPath);
// Declare and define the xreference to be added.
ObjectId xrefObjId =
db.OverlayXref(savedPath, referenceName);
// Create a new BlockReference object.
BlockReference br =
new BlockReference(new Point3d(0,0,0),
xrefObjId);
// Obtain a BlockTable reference.
BlockTable bt =
tr.GetObject(
db.BlockTableId, OpenMode.ForRead)
as BlockTable;
// Open the ModelSpace BlockTableRecord ForWrite.
BlockTableRecord modelSpace =
tr.GetObject(
bt[BlockTableRecord.ModelSpace],
OpenMode.ForWrite)
as BlockTableRecord;
// Add the xreference object to ModelSpace.
modelSpace.AppendEntity(br);
// Inform the transaction of the new xreference
// object.
tr.AddNewlyCreatedDBObject(br, true);
}
// Everything was a success, so commit the transaction.
tr.Commit();
// Save newly created database with drawing assembly
// name.
db.SaveAs(drawingAssemblyFullPathName, DwgVersion.Current);
}
}
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}