Kerry, I got it to work with a few tips, I digged from Autodesk discussion forum. Thanks to javenkat!
I'm posting the code, so that it may be of help to others who is just into C#.
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Specialized;
using System.Windows.Forms;
namespace XU2
{
public class XU : IExtensionApplication
{
public static Matrix3d ucs;
public static ObjectId curLayer;
private Autodesk.AutoCAD.ApplicationServices.DocumentCollection docs = null;
public void Initialize()
{
DocumentCollection dm = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
dm.DocumentLockModeChanged += new DocumentLockModeChangedEventHandler(Change);
//Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
//doc.CommandEnded += new CommandEventHandler(doc_CommandEnded);
docs = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
foreach (Document doc in docs)
{
Attach(doc);
}
docs.DocumentCreated += new Autodesk.AutoCAD.ApplicationServices.DocumentCollectionEventHandler(r1);
docs.DocumentToBeDestroyed += new Autodesk.AutoCAD.ApplicationServices.DocumentCollectionEventHandler(r2);
}
public void Terminate()
{
docs.DocumentCreated -= new Autodesk.AutoCAD.ApplicationServices.DocumentCollectionEventHandler(r1);
docs.DocumentToBeDestroyed -= new Autodesk.AutoCAD.ApplicationServices.DocumentCollectionEventHandler(r2);
}
private void Change(object sender, DocumentLockModeChangedEventArgs e)
{
if (e.GlobalCommandName == "XATTACH")
{
ModifyLayers();
CreateLayer();
ucs = UCS2WCS();
}
}
private static ObjectId CreateLayer()
{
ObjectId layerId = ObjectId.Null;//the return value for this function
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
curLayer = db.Clayer;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
//Get the layer table first...
LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForRead);
//Check if 0-XREF exists;;;
if (lt.Has("0-XREF"))
{
layerId = lt["0-XREF"];
db.Clayer = layerId;
}
else
{
//if not, create the layer here.
LayerTableRecord ltr = new LayerTableRecord();
ltr.Name = "0-XREF";
ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, 1);
ltr.IsFrozen = false;
ltr.IsLocked = false;
ltr.IsOff = false;
//upgrade the open from read to write
lt.UpgradeOpen();
layerId = lt.Add(ltr);
trans.AddNewlyCreatedDBObject(ltr, true);
db.Clayer = layerId;
}
trans.Commit();
}
return layerId;
}
public void ModifyLayers()
{
Database db = HostApplicationServices.WorkingDatabase;
try
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
foreach (ObjectId objId in lt)
{
LayerTableRecord ltr = (LayerTableRecord)tr.GetObject(objId, OpenMode.ForWrite);
if (ltr.Name == "0" || ltr.Name == "Defpoints")
{
if (ltr.IsLocked) { ltr.IsLocked = false; }
if (ltr.IsFrozen) { ltr.IsFrozen = false; }
if (ltr.IsOff) { ltr.IsOff = false; }
}
}
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public static Matrix3d UCS2WCS()
{
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
Matrix3d original = ed.CurrentUserCoordinateSystem;
ed.CurrentUserCoordinateSystem = Matrix3d.Identity;
return original;
}
void doc_CommandEnded(object sender, CommandEventArgs e)
{
try
{
if (e.GlobalCommandName == "XATTACH")
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("\n The command is " + e.GlobalCommandName);
ed.CurrentUserCoordinateSystem = ucs;
ed.WriteMessage("\n" + ucs);
db.Clayer = curLayer;
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void doc_CommandCancelled(object sender, CommandEventArgs e)
{
try
{
if (e.GlobalCommandName == "XATTACH" )
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
ed.CurrentUserCoordinateSystem = ucs;
db.Clayer = curLayer;
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void r1(object sender, Autodesk.AutoCAD.ApplicationServices.DocumentCollectionEventArgs e)
{
Attach(e.Document);
}
void r2(object sender, Autodesk.AutoCAD.ApplicationServices.DocumentCollectionEventArgs e)
{
Detach(e.Document);
}
private void Attach(Document doc)
{
doc.CommandEnded +=new CommandEventHandler(doc_CommandEnded);
doc.CommandCancelled +=new CommandEventHandler(doc_CommandCancelled);
}
private void Detach(Document doc)
{
doc.CommandEnded -= new CommandEventHandler(doc_CommandEnded);
doc.CommandCancelled -= new CommandEventHandler(doc_CommandCancelled);
}
}
}
But if I run XATTACH through the XREF dialog box, the CommandEnded and CommandCancelled Event is not fired. Hence the layer and ucs doesn't go back to the original setting.
Thanks