I just received this interesting (and quick to solve) question from Henrik Ericson, this morning:
I’m looking for a new xref command (or perhaps overriding the internal xref command) that always insert the selected xref at 0,0,0 coordinates and in World coordinate system. I’m often inserting an xref and ’nothing happens’ and then I realize that I’m in a UCS.
I created a simple command called XAO – for XrefAttach[At]Origin – that does just this. As a helper function I implemented a simple extension method to both attach and insert an external reference in the current space of the active AutoCAD drawing, basing it on the online documentation.
Here’s the C# code:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.IO;
namespace XrefAttachAtZero
{
public static class Extensions
{
/// <summary>
/// Attaches the specified Xref to the current space in the current drawing.
/// </summary>
/// <param name="path">Path to the drawing file to attach as an Xref.</param>
/// <param name="pos">Position of Xref in WCS coordinates.</param>
/// <param name="name">Optional name for the Xref.</param>
/// <returns>Whether the attach operation succeeded.</returns>
public static bool XrefAttachAndInsert(
this Database db, string path, Point3d pos, string name = null
)
{
var ret = false;
if (!File.Exists(path))
return ret;
if (String.IsNullOrEmpty(name))
name = Path.GetFileNameWithoutExtension(path);
try
{
using (var tr = db.TransactionManager.StartOpenCloseTransaction())
{
var xId = db.AttachXref(path, name);
if (xId.IsValid)
{
var btr =
(BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
var br = new BlockReference(pos, xId);
btr.AppendEntity(br);
tr.AddNewlyCreatedDBObject(br, true);
ret = true;
}
tr.Commit();
}
}
catch (Autodesk.AutoCAD.Runtime.Exception)
{ }
return ret;
}
}
public class Commands
{
[CommandMethod("XAO")]
public void XrefAttachAtOrigin()
{
var doc = Application.DocumentManager.MdiActiveDocument;
if (doc == null)
return;
var db = doc.Database;
var ed = doc.Editor;
// Ask the user to specify a file to attach
var opts = new PromptOpenFileOptions("Select Reference File");
opts.Filter = "Drawing (*.dwg)|*.dwg";
var pr = ed.GetFileNameForOpen(opts);
if (pr.Status == PromptStatus.OK)
{
// Attach the specified file and insert it at the origin
var res = db.XrefAttachAndInsert(pr.StringResult, Point3d.Origin);
ed.WriteMessage(
"External reference {0}attached at the origin.",
res ? "" : "not "
);
}
}
}
}
And here’s the XAO command in action:
A simple enough command, but I can see how it would be useful for users of external references who also work with custom User Coordinate Systems. Thanks for the question, Henrik!
Update:
A big thanks to Glenn Ryan for reminding me of his RefUcsSpy tool which was published as an ADN Plugin of the Month, way back when. The good news is that it’s still available on the AutoCAD App Store. It takes a slightly different approach to the problem, and warns you if a custom UCS is active when you launch reference-adding commands such as XATTACH. A task dialog allows you to carry on and use the custom UCS or to switch to WCS for the duration of the attachment. Check it out!