/// <summary> /// Duplicate one existing sheet /// </summary> /// <param name="shtNumber">The new sheet number</param> /// <param name="shtName">The new sheet name</param> /// <param name="tbName">The title block for the new sheet</param> /// <param name="vsSourceSheet">The ViewSheet to copy</param> // public void DuplicateOneSheet(string shtNumber, string shtName, string tbName, ViewSheet vsSourceSheet) public void DuplicateOneSheet(NewSheetFormat nsf) { ViewSheet vsDestinationSheet; try { // try to create the sheet // see below about copy parameters vsDestinationSheet = CreateOneEmptySheet(nsf); } catch (Exception e) { throw new Exception(e.Message); } // at this point we have a source and destination ViewSheet // here we copy the elements on the source ViewSheet and // place them onto the destination ViewSheet // except, we don't copy the title block as this was added // when the ViewSheet was created // first, get a collection of elements that exist on the source sheet FilteredElementCollector colViewSheet = new FilteredElementCollector(_doc, nsf.FcSelViewSht.Id); // prepare for the elements to "copy" and "paste" ICollection <ElementId> copyIds = new List <ElementId>(); // this may fail, prepare for the failure and capture the failure message try { // in order to copy detail lines directly onto the sheet, we need to // setup a SketchPlane on the "blank" sheet SketchPlane sketch = CreateSketchPlane(vsDestinationSheet); List <Viewport> vpList = new List <Viewport>(10); List <Viewport> vpListNoCopy = new List <Viewport>(10); copyIds.Clear(); // the ViewSheet has been setup to copy / paste or duplicate the elements // held by the ViewSheet - with the exceptions that ViewPorts cannot be copied foreach (Element sourceElem in colViewSheet) { // ignore GuideGrids if (sourceElem.Category.Id == _cats.get_Item(BuiltInCategory.OST_GuideGrid).Id) { continue; } // is the current element a titleblock? - this is the source titleblock if (sourceElem.Category.Id == _cats.get_Item(BuiltInCategory.OST_TitleBlocks).Id) { ConfigureNewTitleBlock(sourceElem, vsDestinationSheet); continue; } // we cannot copy some viewports or GuideGrids if (sourceElem.Category.Id == _cats.get_Item(BuiltInCategory.OST_Viewports).Id) { Viewport vp = (Viewport)sourceElem; View vw = (View)_doc.GetElement(vp.ViewId); if (Viewport.CanAddViewToSheet(_doc, vsDestinationSheet.Id, vw.Id)) { vpList.Add(vp); // got a viewport to copy } else { vpListNoCopy.Add(vp); // got a viewport I cannot copy } continue; } // process a not viewport items copyIds.Add(sourceElem.Id); } // preform the actual copy of the elements if (copyIds.Count > 0) { ElementTransformUtils.CopyElements((ViewSheet)nsf.SelectedSheet.SheetView, copyIds, vsDestinationSheet, Transform.Identity, new CopyPasteOptions()); } // processed all of the items on the view sheet except for the viewports // we have a list of viewports we can copy and place onto the sheet // here we process the list of viewports that cannot be copy / pasted and replicate // them matching their original location if (vpList.Count > 0) { foreach (Viewport vpSource in vpList) { View vw = _doc.GetElement(vpSource.ViewId) as View; PlaceViewportOnSheet(vsDestinationSheet, vpSource, vw); } } if (vpListNoCopy.Count > 0 && nsf.OperationOption == OperOpType.DupSheetAndViews) { // get all of the view names here to make sure it is current GetAllViewNames(); foreach (Viewport vpSource in vpListNoCopy) { View vwSource = _doc.GetElement(vpSource.ViewId) as View; if (!vwSource.CanViewBeDuplicated(ViewDuplicateOption.WithDetailing)) { continue; } View vwCopy; // if PrimaryViewId is not InvalidElementId, view is dependant if (vwSource.GetPrimaryViewId() == ElementId.InvalidElementId) { vwCopy = _doc.GetElement(vwSource.Duplicate(ViewDuplicateOption.WithDetailing)) as View; } else { vwCopy = _doc.GetElement(vwSource.Duplicate(ViewDuplicateOption.AsDependent)) as View; } if (vwCopy != null) { vwCopy.Name = FindNextViewName(vwSource.Name, nsf); // vwCopy.Name = FindNextViewName(vwSource.Name, nsf.newSheetNumber); PlaceViewportOnSheet(vsDestinationSheet, vpSource, vwCopy); } } } } catch (Exception e) { throw new Exception(AppStrings.R_ErrDupSheetFailDesc + nl + e.Message); } }
/// <summary> /// Duplicates the input sheet a number of times. It allows for ViewDuplicateOption for dependent views. /// </summary> /// <param name="m_doc"></param> /// <param name="sourceSheet"></param> /// <param name="copies"></param> /// <param name="dependentOption"></param> private void DuplicateSheets(Document m_doc, ViewSheet sourceSheet, int copies, ViewDuplicateOption dependentOption) { // get Title Block FamilyInstance titleBlock = new FilteredElementCollector(m_doc) .OfClass(typeof(FamilyInstance)) .OfCategory(BuiltInCategory.OST_TitleBlocks) .Cast <FamilyInstance>() .First(q => q.OwnerViewId == sourceSheet.Id); using (TransactionGroup tg = new TransactionGroup(m_doc, "Duplicate Sheet")) { tg.Start(); for (int i = 0; i < copies; i++) { // create unique sheet number string uniqueSheetNumber = Data.Helpers.CreateUniqueSheetNumber(m_doc, sourceSheet); using (Transaction t = new Transaction(m_doc, "Duplicate Sheet")) { t.Start(); // create new SHEET, new NUMBER and new NAME ViewSheet newSheet = ViewSheet.Create(m_doc, titleBlock.GetTypeId()); newSheet.SheetNumber = uniqueSheetNumber; newSheet.Name = sourceSheet.Name; #region Loop through viewports (except schedules) because GetAllPlacedViews() does not return Schedules foreach (ElementId eid in sourceSheet.GetAllPlacedViews()) { // get view element Autodesk.Revit.DB.View sourceView = m_doc.GetElement(eid) as Autodesk.Revit.DB.View; Autodesk.Revit.DB.View newView = null; // declare newView variable // DUPLICATE views // if view element is legend newView is equal to sourceView if (sourceView.ViewType == ViewType.Legend) { newView = sourceView; } else // for non-legend views { if (sourceView.GetPrimaryViewId() == ElementId.InvalidElementId) // if parent view { ElementId newViewId = sourceView.Duplicate(ViewDuplicateOption.WithDetailing); newView = m_doc.GetElement(newViewId) as Autodesk.Revit.DB.View; newView.Name = Data.Helpers.CreateUniqueViewName(m_doc, sourceView); //newView.ChangeTypeId(sourceTypeId); } else // if dependent view { ElementId newViewId = sourceView.Duplicate(dependentOption); newView = m_doc.GetElement(newViewId) as Autodesk.Revit.DB.View; newView.Name = Data.Helpers.CreateUniqueViewName(m_doc, sourceView); //newView.ChangeTypeId(sourceTypeId); } } // CREATE viewport and MOVE it foreach (Viewport vp in new FilteredElementCollector(m_doc).OfClass(typeof(Viewport))) { if (vp.SheetId == sourceSheet.Id && vp.ViewId == sourceView.Id) { XYZ sourceCenter = vp.GetBoxCenter(); ElementId sourceTypeId = vp.GetTypeId(); Viewport newViewport = Viewport.Create(m_doc, newSheet.Id, newView.Id, XYZ.Zero); newViewport.ChangeTypeId(sourceTypeId); Ana_NoOfViewports += 1; // add 1 to the viewport counter XYZ newCenter = newViewport.GetBoxCenter(); ElementTransformUtils.MoveElement(m_doc, newViewport.Id, new XYZ( sourceCenter.X - newCenter.X, sourceCenter.Y - newCenter.Y, 0)); } } } #endregion #region Loop through schedules foreach (ScheduleSheetInstance si in (new FilteredElementCollector(m_doc).OfClass(typeof(ScheduleSheetInstance)))) { if (si.OwnerViewId == sourceSheet.Id) { if (!si.IsTitleblockRevisionSchedule) { foreach (ViewSchedule viewSchedule in new FilteredElementCollector(m_doc).OfClass(typeof(ViewSchedule))) { if (si.ScheduleId == viewSchedule.Id) { XYZ sourceCenter = si.Point; ScheduleSheetInstance newSSheetInstance = ScheduleSheetInstance.Create(m_doc, newSheet.Id, viewSchedule.Id, XYZ.Zero); XYZ newCenter = newSSheetInstance.Point; ElementTransformUtils.MoveElement(m_doc, newSSheetInstance.Id, new XYZ( sourceCenter.X - newCenter.X, sourceCenter.Y - newCenter.Y, 0)); } } } } } #endregion t.Commit(); } } tg.Assimilate(); } }