xRef coming in as unreferenced

xRef coming in as unreferenced

Syslogs
Participant Participant
918 Views
3 Replies
Message 1 of 4

xRef coming in as unreferenced

Syslogs
Participant
Participant

I have the following section of code that attaches an xref but when it does it the xref says it is unreferenced.  What am I doing wrong?

 

Thanks in advance.

 

Dim myDWG As Document = DocumentManager.MdiActiveDocument
Using myTrans As Transaction = myDWG.TransactionManager.StartTransaction

**There is other code here that works just fine but removed for clarity.***

     Dim strFilePath As String = "Y:\_Projects\Active\2010\01201006.02\01201006.02.QC\" & strRedName_Layout & ".dwg"
     Dim dwgEditor = myDWG.Editor
                    strRedName_Layout = "test"
                    Try
                        Using lock As DocumentLock = myDWG.LockDocument
                            Dim xrefObject As ObjectId = DocumentManager.MdiActiveDocument.Database.AttachXref(strfilepath, strRedName_Layout)
                            Dim blkTable As BlockTable = myTrans.GetObject(myDb.BlockTableId, OpenMode.ForRead, False)
                            Dim modelSpace As BlockTableRecord = myTrans.GetObject(myDb.CurrentSpaceId, OpenMode.ForWrite)
                            Dim blkTableRec As BlockTableRecord = CType(myTrans.GetObject(xrefObject, OpenMode.ForWrite), BlockTableRecord)
                            Dim blkRef As BlockReference = New BlockReference(New Point3d(0, 0, 0), xrefObject)
                            modelSpace.AppendEntity(blkRef)
                            myTrans.AddNewlyCreatedDBObject(blkRef, True)


                        End Using
                    Catch ex As Exception
                        MsgBox("ERROR " * Err.Description)
                    End Try
     myTrans.Commit()
End Using

 

0 Likes
919 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable

I think the error may be in the code you haven't posted.

 

Lacking that -- my shot in the dark:  are you sure that myDwg.Database and myDb are the same database?  They _must_ be for this operation to succeed.

 

-drg

0 Likes
Message 3 of 4

Syslogs
Participant
Participant

Since this post I have tried a different approach by using some code that I found in these forums.  The only difference between what was posted and what I show below is that I have to use DocumentLock whereas the original posted code sis not use this.

 

Public Sub XrefInsertion(ByRef strFilePath, ByRef strRedName_Layout)

        Dim doc As Document = DocumentManager.MdiActiveDocument
        Dim NomFichierXref As String = strFilePath      '"C:\CERCLE.dwg"
        Dim NomXref As String = "CERCLE"
        Dim ed As Editor = doc.Editor()
        Dim db As Database = doc.Database
        Dim trans As Transaction = db.TransactionManager.StartTransaction()

        Using (trans)
            Try
                Using lock As DocumentLock = doc.LockDocument
                    Dim xrefObj As ObjectId = db.AttachXref(NomFichierXref, "test")
                    Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead, False)
                    Dim modelSpace As BlockTableRecord = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite)
                    Dim btr As BlockTableRecord = CType(trans.GetObject(xrefObj, OpenMode.ForWrite), BlockTableRecord)
                    Dim br As BlockReference = New BlockReference(New Point3d(0, 0, 0), xrefObj)

                    modelSpace.AppendEntity(br)
                    trans.AddNewlyCreatedDBObject(br, True)
                    trans.Commit()
                End Using
            Catch ex As Autodesk.AutoCAD.Runtime.Exception
                MsgBox(ex.Message & vbLf & ex.StackTrace)
            End Try

        End Using

    End Sub

 

0 Likes
Message 4 of 4

Anonymous
Not applicable

Hi there,

 

Here is a sample that i use:

 

public static void AtachAsXref(string fileName, double x, double y, double z, double scalefactor)
        {
            Database db = Application.DocumentManager.MdiActiveDocument.Database;

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                try
                {
                    // Attach the Xref
                    ObjectId xrefObj = db.AttachXref(fileName, Path.GetFileNameWithoutExtension(fileName));
                    BlockReference br = new BlockReference(new Point3d(x, y, z), xrefObj);
                    br.ScaleFactors = new Scale3d(Convert.ToDouble(scalefactor));

                    // Add the xref to model space
                    BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    BlockTableRecord modelSpace = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                    modelSpace.AppendEntity(br);
                    trans.AddNewlyCreatedDBObject(br, true);
                    trans.Commit();
                }
                catch
                {
                }
            }
        }

 It's in c# but there are enough convertors around.

 

Kind regards,

 

Irvin

0 Likes