Skip to content

Instantly share code, notes, and snippets.

@johnpierson
Last active April 11, 2024 03:02
Show Gist options
  • Save johnpierson/42299123111d76041059af9d22a2c963 to your computer and use it in GitHub Desktop.
Save johnpierson/42299123111d76041059af9d22a2c963 to your computer and use it in GitHub Desktop.
Proof of concept of aligning view title on older versions of Revit with Dynamo
public static void AlignViewTitle(global::Revit.Elements.Element viewport)
{
//get the current document (built in Dynamo method)
Autodesk.Revit.DB.Document doc = DocumentManager.Instance.CurrentDBDocument;
//cast the viewport to the internal Revit DB Type
Autodesk.Revit.DB.Viewport internalViewport = viewport.InternalElement as Autodesk.Revit.DB.Viewport;
//get the original box center (for when we re-place the viewport)
var originalBoxCenter = internalViewport.GetBoxCenter();
//store the sheet id and view id (this is where you would store other parameters as well)
var sheetId = internalViewport.SheetId;
var viewId = internalViewport.ViewId;
//force close dynamo's open transaction
TransactionManager.Instance.ForceCloseTransaction();
//use a transaction group to sequence the events into one
TransactionGroup tGroup = new TransactionGroup(doc, "Aligning Viewport");
tGroup.Start();
//delete the original viewport
using (Transaction deleteOriginalViewport = new Transaction(doc, "Deleting Original"))
{
deleteOriginalViewport.Start();
doc.Delete(internalViewport.Id);
deleteOriginalViewport.Commit();
}
//place the viewport again to get an aligned viewport title
using (Transaction replaceViewport = new Transaction(doc, "Placing Viewport with Aligned View Title"))
{
replaceViewport.Start();
Autodesk.Revit.DB.Viewport.Create(doc, sheetId, viewId, originalBoxCenter);
replaceViewport.Commit();
}
//combine all transactions
tGroup.Assimilate();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment