Thanks to Bryco heres the code Let me know if you need a vb version
[CommandMethod("SendToXref")]
public static void SendToXref()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
List<ObjectId> objectsIdsErase = new List<ObjectId>();
PromptEntityOptions peo = new PromptEntityOptions("\nSelect Viewport");
peo.SetRejectMessage("\nNot a Viewport");
peo.AddAllowedClass(typeof(Viewport), true);
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK)
return;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Viewport vp = (Viewport)tr.GetObject(per.ObjectId, OpenMode.ForRead);
Matrix3d PsToMsTransForm = PaperToModel(vp);
BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead);
BlockTableRecord xrefBtr = (BlockTableRecord)bt["XrefTest"].GetObject(OpenMode.ForRead);
string xPath = xrefBtr.PathName;
PromptSelectionOptions pso = new PromptSelectionOptions();
PromptSelectionResult psr = ed.GetSelection();
if (psr.Status != PromptStatus.OK)
return;
ObjectIdCollection xrefOID = new ObjectIdCollection();
foreach (ObjectId id in psr.Value.GetObjectIds())
{
Entity psEnt = (Entity)tr.GetObject(id, OpenMode.ForWrite);
objectsIdsErase.Add(psEnt.ObjectId);
psEnt.TransformBy(PsToMsTransForm);
xrefOID.Add(psEnt.ObjectId);
}
Database xrefDB = new Database(false, false);
xrefDB.ReadDwgFile(xPath, System.IO.FileShare.Read, true, "");
BlockTable btx = tr.GetObject(xrefDB.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord xModelSpace = tr.GetObject
(btx[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
xrefDB.WblockCloneObjects(xrefOID, xModelSpace.ObjectId,
new IdMapping(), DuplicateRecordCloning.Replace, false);
xrefDB.CloseInput(true);
xrefDB.SaveAs(xPath, DwgVersion.Current);
db.ReloadXrefs(new ObjectIdCollection(new ObjectId[] { xrefBtr.ObjectId }));
tr.Commit();
}
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
BlockTableRecord btrPaper = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.PaperSpace], (OpenMode.ForWrite));
foreach (ObjectId id in btrPaper)
{
if (objectsIdsErase.Contains(id))
{
Entity ent = (Entity)id.GetObject(OpenMode.ForWrite);
ent.Erase();
}
}
tr.Commit();
}
}
private static Matrix3d PaperToModel(Viewport vp)
{
Matrix3d mx = ModelToPaper(vp);
return mx.Inverse();
}
private static Matrix3d ModelToPaper(Viewport vp)
{
Vector3d vd = vp.ViewDirection;
Point3d vc = new Point3d(vp.ViewCenter.X, vp.ViewCenter.Y, 0);
Point3d vt = vp.ViewTarget;
Point3d cp = vp.CenterPoint;
double ta = -vp.TwistAngle;
double vh = vp.ViewHeight;
double height = vp.Height;
double width = vp.Width;
double scale = vh / height;
double lensLength = vp.LensLength;
Vector3d zaxis = vd.GetNormal();
Vector3d xaxis = Vector3d.ZAxis.CrossProduct(vd);
Vector3d yaxis;
if (!xaxis.IsZeroLength())
{
xaxis = xaxis.GetNormal();
yaxis = zaxis.CrossProduct(xaxis);
}
else if (zaxis.Z < 0)
{
xaxis = Vector3d.XAxis * -1;
yaxis = Vector3d.YAxis;
zaxis = Vector3d.ZAxis * -1;
}
else
{
xaxis = Vector3d.XAxis;
yaxis = Vector3d.YAxis;
zaxis = Vector3d.ZAxis;
}
Matrix3d pcsToDCS = Matrix3d.Displacement(Point3d.Origin - cp);
pcsToDCS = pcsToDCS * Matrix3d.Scaling(scale, cp);
Matrix3d dcsToWcs = Matrix3d.Displacement(vc - Point3d.Origin);
Matrix3d mxCoords = Matrix3d.AlignCoordinateSystem(Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis, Point3d.Origin,
xaxis, yaxis, zaxis);
dcsToWcs = mxCoords * dcsToWcs;
dcsToWcs = Matrix3d.Displacement(vt - Point3d.Origin) * dcsToWcs;
dcsToWcs = Matrix3d.Rotation(ta, zaxis, vt) * dcsToWcs;
Matrix3d perspectiveMx = Matrix3d.Identity;
if (vp.PerspectiveOn)
{
double vSize = vh;
double aspectRatio = width / height;
double adjustFactor = 1.0 / 42.0;
double adjstLenLgth = vSize * lensLength * Math.Sqrt(1.0 + aspectRatio * aspectRatio) * adjustFactor;
double iDist = vd.Length;
double lensDist = iDist - adjstLenLgth;
double[] dataAry = new double[] {1,0,0,0,0,1,0,0,0,0,
(adjstLenLgth-lensDist)/adjstLenLgth,lensDist*(iDist-adjstLenLgth)/adjstLenLgth,
0,0,-1.0/adjstLenLgth,iDist/adjstLenLgth};
perspectiveMx = new Matrix3d(dataAry);
}
Matrix3d finalMx = pcsToDCS.Inverse() * perspectiveMx * dcsToWcs.Inverse();
return finalMx;
}