Author Topic: XRef drawing  (Read 1615 times)

0 Members and 1 Guest are viewing this topic.


  • Newt
  • Posts: 196
XRef drawing
« on: January 04, 2012, 01:41:21 PM »
I am trying to xref a drawing a user selects into a template we have and then perform a saveas to another directory. The file shows up in the xref manager in the newly created drawing file but says it is unresolved. I have tried using db.ResolveXRefs but this does not seem to do anything. The created file is never opened so I am trying to do this by reading the database, not sure if that is possible. Here is my code if anyone could take a look and maybe I am missing something easy.



Code: [Select]
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Dim document As Document = Application.DocumentManager.MdiActiveDocument
        Dim dir As String = Path.GetDirectoryName(document.Name)
        Dim dirConst As String = Path.GetDirectoryName(document.Name) & "\" & Path.GetFileNameWithoutExtension(document.Name) & " - Construction"

            For Each obj As Object In New System.Collections.ArrayList(lstConstruction.Items)

                Dim fileName As String = Path.GetDirectoryName(document.Name) & "\" & Path.GetFileNameWithoutExtension(document.Name) & " - Parts" & "\" & obj.ToString

                Dim db As Database = New Database(False, True)
                db.ReadDwgFile("Y:\CAD STANDARDS\Blocks\Templates\CONSTRUCTION 2012 EPICOR.dwt", FileShare.ReadWrite, False, "")

                Using trx As Transaction = db.TransactionManager.StartTransaction

                    Dim bt2 As BlockTable = trx.GetObject(db.BlockTableId, OpenMode.ForWrite)
                    Dim btrMs2 As BlockTableRecord = trx.GetObject(bt2(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

                    Dim xRid As ObjectId = db.AttachXref(fileName, Path.GetFileNameWithoutExtension(fileName))

                    Dim bref As New BlockReference(Point3d.Origin, xRid)


                    Dim blname As String = "XRef - " & Path.GetFileNameWithoutExtension(fileName)

                    'Get the layer table first...
                    Dim lt As LayerTable = trx.GetObject(db.LayerTableId, OpenMode.ForRead)
                    Dim layerId As ObjectId = ObjectId.Null
                    'Check if EmployeeLayer exists...
                    If lt.Has(blname) Then
                        layerId = lt.Item(blname)
                        'If not, create the layer here.
                        Dim ltr As LayerTableRecord = New LayerTableRecord()
                        ltr.Name = blname ' Set the layer name
                        ltr.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 7)
                        layerId = lt.Add(ltr)
                        trx.AddNewlyCreatedDBObject(ltr, True)
                    End If

                    bref.LayerId = layerId

                    trx.AddNewlyCreatedDBObject(bref, True)

                    db.ResolveXrefs(True, True)


                    db.SaveAs(Path.GetDirectoryName(document.Name) & "\" & Path.GetFileNameWithoutExtension(document.Name) & " - Construction\" & obj.ToString, DwgVersion.Current)

                End Using


        Catch ex As System.Exception
        End Try
    End Sub


  • Guest
Re: XRef drawing
« Reply #1 on: January 04, 2012, 03:30:20 PM »
Lets see if I am on the right track:
1. Select a file for XRef
2. Open a template in AutoCAD
3. Xref the file
4. Save ThisDrawing as ....


  • Guest
Re: XRef drawing
« Reply #2 on: January 04, 2012, 03:59:56 PM »
I am using 2004 but it looks like something like this SHOULD work unless AutoDesk did something screwy.
Code: [Select]
Dim strTemplateFullName As String = "C:\Documents and Settings\bhardin\Local Settings\Application Data\Autodesk\AutoCAD 2004\R16.0\enu\Template\JIS A4 (portrait) -Named Plot Styles.dwt"
Dim strPath As String = "C:\MyDrawing.dwg"
Dim strMyXref As String = "C:\MyXRef.dwg"
Dim insPoint As Object

        objDWG = AcadApp.Documents.Open(strTemplateFullName)
        objDWG.Utility.CreateTypedArray(insPoint, vbDouble, 0, 0, 0)
        objDWG.ModelSpace.AttachExternalReference(strMyXref, "MyXRef", insPoint, 1, 1, 1, 0, False)
        'Add Xref
        objDWG.SaveAs(strPath, AutoCAD.AcSaveAsType.acNative)


  • Newt
  • Posts: 196
Re: XRef drawing
« Reply #3 on: January 04, 2012, 04:03:28 PM »
Yes, the user selects the file they want to xref. I then want to read the dwt file and add an external reference and saveas to new location and filename. It creates the new file with the xref, the xref just shows as unresolved.


  • Guest
Re: XRef drawing
« Reply #4 on: January 04, 2012, 04:15:26 PM »
Does the code I wrote work with your paths and some minor changes to the variables?
Such as AcadApp = Application & objDWG = document
Try hard coding your paths first.


  • Newt
  • Posts: 196
Re: XRef drawing
« Reply #5 on: January 04, 2012, 07:26:21 PM »

What does Utility refer to?


  • Swamp Rat
  • Posts: 572
Re: XRef drawing
« Reply #6 on: January 04, 2012, 11:25:06 PM »
What does Utility refer to?

Supposedly to a property of AcadDocumentClass in VBA/Interop, accessing a  "series of methods provided for utility purposes". In .Net there's no such thing; most of that methods can be found in the EditorInput namespace.

Ted, I think you need to
1) call Dispose on your self-created Database objects or wrap them in a Using statement,
2) call ResolveXrefs after committing the Transaction, and
3) disregard people who do not contribute to the discussion.



  • Newt
  • Posts: 196
Re: XRef drawing
« Reply #7 on: January 05, 2012, 11:01:38 AM »
Thanks kaefer, I will get around to trying this sometime today.


  • Guest
Re: XRef drawing
« Reply #8 on: January 05, 2012, 12:44:17 PM »
Yeah thanks kaefer guess I will keep my code to myself till work upgrades my AutoCAD.