Here's a look at the SetUCS method I've now built out.
It sort of works in that it does set the module-001-ucs settings (via the newUcsMatrix var) to the viewports in the loop, but the problem is doing a plan on them sets their view to whatever view was initially defined in the model space where that ucs was created (ie the top view for this arrangement).
private static void SetViewportUcs()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
using (DocumentLock docloc = doc.LockDocument())
{
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
Database db = HostApplicationServices.WorkingDatabase;
AcadDocument acadDoc = (AcadDocument)doc.GetAcadDocument();
using (Transaction trans = db.TransactionManager.StartTransaction())
{
LayoutManager layoutMgr = LayoutManager.Current;
Layout layoutObj;
DBDictionary layoutDict;
UcsTableRecord ucsTblRec;
UcsTable ucsTbl = trans.GetObject(db.UcsTableId, OpenMode.ForRead) as UcsTable;
if (ucsTbl.Has("module-001-ucs"))
{
ucsTblRec = (UcsTableRecord)trans.GetObject(ucsTbl["module-001-ucs"], OpenMode.ForWrite);
}
else
{
ucsTblRec = new UcsTableRecord();
ucsTblRec.Name = "module-001-ucs";
ucsTbl.UpgradeOpen();
ucsTbl.Add(ucsTblRec);
trans.AddNewlyCreatedDBObject(ucsTblRec, true);
}
Point3d toOrigin;
Vector3d toXAxis, toYAxis, toZAxis;
toOrigin = ucsTblRec.Origin;
toXAxis = ucsTblRec.XAxis;
toYAxis = ucsTblRec.YAxis;
toZAxis = toXAxis.CrossProduct(toYAxis);
Matrix3d newUcsMatrix = Matrix3d.AlignCoordinateSystem(
Point3d.Origin,
Vector3d.XAxis,
Vector3d.YAxis,
Vector3d.ZAxis,
toOrigin, toXAxis, toYAxis, toZAxis
);
using (layoutDict = trans.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary)
{
foreach (DictionaryEntry layoutEntry in layoutDict)
{
using (layoutObj = trans.GetObject((ObjectId)(layoutEntry.Value), OpenMode.ForRead) as Layout)
{
if (layoutObj.LayoutName != "Model")
{
acadDoc.ActiveLayout = acadDoc.Layouts.Item(layoutObj.LayoutName);
// Set the CurrentLayout to the layout if it is not Modelspace
layoutMgr.CurrentLayout = layoutObj.LayoutName;
//loop over viewports in current layout
for (int i = 1; i < acadDoc.PaperSpace.Count; i++)
{
try
{
AcadPViewport acadViewport = acadDoc.PaperSpace.Item(i) as AcadPViewport;
if (acadViewport.ObjectName == "AcDbViewport")
{
acadViewport.Display(true);
acadViewport.ViewportOn = true;
acadDoc.MSpace = true;
acadDoc.ActivePViewport = acadViewport;
acadViewport.UCSPerViewport = true;
ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
ViewTableRecord vtr = ed.GetCurrentView();
Vector3d vDirection = vtr.ViewDirection;
vDirection.TransformBy(newUcsMatrix);
ViewTableRecord newVtr = new ViewTableRecord();
newVtr.ViewDirection = vDirection;
ed.CurrentUserCoordinateSystem = newUcsMatrix;
ed.SetCurrentView(newVtr);
acadDoc.Regen(AcRegenType.acActiveViewport);
//doc.SendStringToExecute("_PLAN C ", true, false, true);
}
}
catch
{
}
}
}
}
}
}
trans.Commit();
}
}
}
The following lines are where I'm trying to do a transform of the current ViewDirection to the ucs matrix3D, which don't seem to be working as expected, but may well be doing exactly what they should realistically, with my limited knowledge.
ViewTableRecord vtr = ed.GetCurrentView();
Vector3d vDirection = vtr.ViewDirection;
vDirection.TransformBy(newUcsMatrix);
ViewTableRecord newVtr = new ViewTableRecord();
newVtr.ViewDirection = vDirection;
Once Again. Cheers to anyone who looks over this and points out any programmer error...