Author Topic: Overlaying an Xreference  (Read 2579 times)

0 Members and 1 Guest are viewing this topic.

HD

  • Guest
Overlaying an Xreference
« on: October 02, 2007, 08:33:00 AM »
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?

Code: [Select]
/// <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);
    }
}
« Last Edit: October 02, 2007, 04:07:29 PM by Nick Schuckert »

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Overlaying an Xreference
« Reply #1 on: October 02, 2007, 01:19:15 PM »
Nick,

  How about if you change this line
Code: [Select]
string savedPath = assemblyData as string;
to
Code: [Select]
string savedPath = assemblyData[i] as string;
I would think this would work since in your header you state that all items after the first one are the full file paths.  Other than that I don't see why it wouldn't work in '06 since the methods you use are in the '06 Arx docs.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Overlaying an Xreference
« Reply #2 on: October 03, 2007, 10:57:08 AM »
Not realizing that my test data directory contained a mishmash of AutoCAD drawing versions, the error was occurring because I was trying to xreference an AutoCAD 2008 drawing into an AutoCAD 2006 drawing.
Thanks for posting what the solution was Nick.  I'm glad it wasn't the code, or I would need to get my eyes checked.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.