Search code examples
c#revit-apiautodeskrevit

revit api create sheet - docs - contain deprecated "view3d


I'm working from the docs on trying to create a sheet with a view on it using the Revit API in C#

here is the docs URL link. You can find the code at the bottom in the first C# block.

I get a red squiggly under the view3D.Id:

Viewport.Create(doc, viewSheet.Id, view3D.Id, new XYZ(location.U, location.V, 0));

I can't find that it was deprecated nor can I figure out how to resolve it. I'm a bit confused about why it's trying to grab its own elementID. Also, just getting into the revit-API. it seems like "views" in Revit are called "viewports" in the API. i need to read more on this.

here is the entire code block:

private void CreateSheetView(Autodesk.Revit.DB.Document document, View3D view3D)
{

    // Get an available title block from document
    FilteredElementCollector collector = new FilteredElementCollector(document);
    collector.OfClass(typeof(FamilySymbol));
    collector.OfCategory(BuiltInCategory.OST_TitleBlocks);

    FamilySymbol fs = collector.FirstElement() as FamilySymbol;
    if (fs != null)
    {
        using (Transaction t = new Transaction(document, "Create a new ViewSheet"))
        {
            t.Start();
            try
            {
                // Create a sheet view
                ViewSheet viewSheet = ViewSheet.Create(document, fs.Id);
                if (null == viewSheet)
                {
                    throw new Exception("Failed to create new ViewSheet.");
                }

                // Add passed in view onto the center of the sheet
                UV location = new UV((viewSheet.Outline.Max.U - viewSheet.Outline.Min.U) / 2,
                                     (viewSheet.Outline.Max.V - viewSheet.Outline.Min.V) / 2);

                //viewSheet.AddView(view3D, location);
                Viewport.Create(document, viewSheet.Id, view3D.Id, new XYZ(location.U, location.V, 0));
                ^ERROR HAPPENS IN LINE ABOVE AT view3D.Id


                // Print the sheet out
                if (viewSheet.CanBePrinted)
                {
                    TaskDialog taskDialog = new TaskDialog("Revit");
                    taskDialog.MainContent = "Print the sheet?";
                    TaskDialogCommonButtons buttons = TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No;
                    taskDialog.CommonButtons = buttons;
                    TaskDialogResult result = taskDialog.Show();

                    if (result == TaskDialogResult.Yes)
                    {
                        viewSheet.Print();
                    }
                }

                t.Commit();
            }
            catch
            {
                t.RollBack();
            }
        }
    }
}

Solution

  • The Building Coder discussion of exact viewport positioning includes some woking sample calls to ViewSheet.Create and Viewport.Create.