Hi,
I am a novice with more little experience with VB.net and AutoCAD.
I am trying to, after opening autocad, to have my routine open file A, attach file B, save file A then close file A.
I've got the below bit working to the point of opening file A, and then attaching file B.
My problem is that after the the code opens file A, it is not the active file ?? and so when the xref is attached, it does not get attached to file A, but rather whatever drawing that was opened when I started the routine.
Here is what I have:
<CommandMethod("OpenAttach")> Public Sub OpenThenAttachXref() Dim HostSheet As String = "C:\STTemp\Host.dwg" Dim XrefFilePathName As String = "C:\STTemp\Xref.dwg" Dim XrefFileName As String = "Xref" Dim doc As Document = DocumentManager.MdiActiveDocument Dim ed As Editor = doc.Editor() Dim db As Database = doc.Database Dim trans As Transaction = db.TransactionManager.StartTransaction() Dim acDocMgr As DocumentCollection = Application.DocumentManager Dim acDoc As Document = acDocMgr.MdiActiveDocument If File.Exists(HostSheet) Then acDocMgr.Open(HostSheet, False) Else acDocMgr.MdiActiveDocument.Editor.WriteMessage("File " & HostSheet & " does not exist.") End If Using (trans) Try Dim xrefObj As ObjectId = db.AttachXref(XrefFilePathName, XrefFileName) 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() Catch ex As Autodesk.AutoCAD.Runtime.Exception MsgBox(ex.Message & vbLf & ex.StackTrace) End Try End Using End Sub
Any assistance in this would be greatly appriciated! This is the last push to setting up over a one thousard files with x-refs. I have already built a pure VB.net app that builds a job file that contains all host files names and the xref that will get attached to them.
Regards,
Marcus
Solved! Go to Solution.
Solved by _gile. Go to Solution.
Solved by norman.yuan. Go to Solution.
You need to make your command running in Application Context, not Document Context as it currently does.
That is, in your CommandMethodAttribute, add CommandFlags.Session:
[CommandMethod("OpenAttach", CommandFlags.Session)]
Public Shared Sub OpenThenAttach()
...
End Sub
Norman Yuan
Hi,
Another way, without openig the host file in the Editor, is using a "side Databse" with the Database.ReadDwgFile method.
[CommandMethod("ReadAttach", CommandFlags.Modal)] public void ReadAttach() { Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; string host = @"C:\Temp\A.dwg"; string xref = @"C:\Temp\B.dwg"; if (!File.Exists(host)) ed.WriteMessage("\nFile '{0}' does not exist.", host); if (!File.Exists(xref)) ed.WriteMessage("\nFile '{0}' does not exist.", xref); // create a new Database using (Database sideDb = new Database()) { // open the host file in the Database sideDb.ReadDwgFile(host, FileShare.ReadWrite, false, ""); // start a Transaction in the 'side' Database using (Transaction sideTr = sideDb.TransactionManager.StartTransaction()) { // attach the xref ObjectId id = sideDb.AttachXref(xref, Path.GetFileNameWithoutExtension(xref)); BlockTableRecord modelSpace = (BlockTableRecord)SymbolUtilityServices .GetBlockModelSpaceId(sideDb) .GetObject(OpenMode.ForWrite); BlockReference br = new BlockReference(Point3d.Origin, id); modelSpace.AppendEntity(br); sideTr.AddNewlyCreatedDBObject(br, true); sideTr.Commit(); } // save the Database sideDb.SaveAs(host, DwgVersion.Current); } }
VB
<CommandMethod("ReadAttach", CommandFlags.Modal)> _ Public Sub ReadAttach() Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor Dim host As String = "C:\Temp\A.dwg" Dim xref As String = "C:\Temp\B.dwg" If Not File.Exists(host) Then ed.WriteMessage(vbLf & "File '{0}' does not exist.", host) End If If Not File.Exists(xref) Then ed.WriteMessage(vbLf & "File '{0}' does not exist.", xref) End If ' create a new Database Using sideDb As New Database() ' open the host file in the Database sideDb.ReadDwgFile(host, FileShare.ReadWrite, False, "") ' start a Transaction in the 'side' Database Using sideTr As Transaction = sideDb.TransactionManager.StartTransaction() ' attach the xref Dim id As ObjectId = sideDb.AttachXref(xref, Path.GetFileNameWithoutExtension(xref)) Dim modelSpace As BlockTableRecord = _ DirectCast(SymbolUtilityServices _ .GetBlockModelSpaceId(sideDb) _ .GetObject(OpenMode.ForWrite), BlockTableRecord) Dim br As New BlockReference(Point3d.Origin, id) modelSpace.AppendEntity(br) sideTr.AddNewlyCreatedDBObject(br, True) sideTr.Commit() End Using ' save the Database sideDb.SaveAs(host, DwgVersion.Current) End Using End Sub
Thanks so much Guys! I had to take a day from codeing to take care of other pressing items, but tonight I will apply your solutions.
Thanks Again Gentleman, your are life savers!
Marcus
Gentleman,
Both solutions worked perfectly. Thanks for the help.. I am attaching files like a madman now!
Regards,
Marcus
Never mind!! I figured it out... just had to look!
Dim id As ObjectId = sideDb.OverlayXref(Xref, Path.GetFileNameWithoutExtension(Xref))