Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to add views on a sheet

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
imaliasad
5989 Views, 6 Replies

How to add views on a sheet

Hello all,

I'm trying to create an add-in, where a user will be able to add all selected views on a sheet. I looked at viewports on sheets post on thebuildingcoder. But I wasn't able to find a solution to add views on sheet programmatically (via API). I would like to know, can we achieve this functionality via API? would appreciate any help. 

 

Thank you.

 

Ali Asad

6 REPLIES 6
Message 2 of 7
aignatovich
in reply to: imaliasad

Hi!

 

Look at static Viewport.Create method. Pass document, ids of viewsheet and your view and insertion point in sheet coordinates.

Message 3 of 7
imaliasad
in reply to: aignatovich

Thank you so much @aignatovich. The solution works 🙂

Message 4 of 7
imaliasad
in reply to: aignatovich

@aignatovich Is there any way to remove the views from sheets? does API provides any method for it?

Message 5 of 7
aignatovich
in reply to: imaliasad

View on sheet is Viewport (or ScheduleSheetInstance for schedule views). Find appropriate viewport on the sheet and delete it (via Document.Delete method)

Message 6 of 7
imaliasad
in reply to: aignatovich

I see. that makes sense. So, I'll need to find the appropriate viewport that holds the reference of a view ID and sheet ID, thank you again @aignatovich for making sense of this 🙂

Message 7 of 7
imaliasad
in reply to: aignatovich

@aignatovich I've used your solution to get the appropriate viewport first, delete it and then add the view to the sheet by using the following code. but the solution didn't work for me. because The CanAddViewToSheet() method in the following code returns false. 

 

Would appreciate if you look at it, and share your thoughts on how can I add views on sheet. Thank you

 

 

 

 

        public static void MoveViewOnSheet(View view, Sheet sheet, Document doc)
        {
            var revitView = doc.GetElement(view.ElementID) as ViewPlan;
            var revitSheet = doc.GetElement(sheet.ElementID) as ViewSheet;


            // Get all viewports
            foreach (Viewport vp in (new FilteredElementCollector(doc).OfClass(typeof(Viewport))))
            {
                // Get those viewport which matches the viewid
                if (vp.ViewId.ToString().Contains(revitView.Id.ToString()))
                {
                    // Delete the viewport
                    using (Transaction transaction = new Transaction(doc, "Remove viewport"))
                    {
                        
                            transaction.Start();
                             doc.Delete(vp.Id);
                            transaction.Commit();


                    }
                }
            }


            // Map the view on sheet
            using (Transaction transaction = new Transaction(doc, "Mapping views on sheet"))
            {
                
                if (Viewport.CanAddViewToSheet(doc, revitSheet.Id, revitView.Id))
                {

                    transaction.Start();
                    Viewport.Create(doc, revitSheet.Id, revitView.Id, XYZ.Zero);
                    transaction.Commit();

                }

            }
        }

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community