copy 2d elements from view(source document) to another view(target document)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi! There! ive been working on developing one revit Adiin / Dynamo Script to be able to copy the 2D Documentation elements from One view(eg: floor plans , to another document(floor plan)) the Source Document is the Detached document of the Target Document that is the Document to deliver.
Know ive been trying with the CopyElements Method
(View, ICollection(ElementId), View, Transform, CopyPasteOptions)
but i have no result suceeded;
THE CODE OF MY EXTERNAL COMMAND IS THIS:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace Copy2DElementsFromViews
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
class Duplicate2DElementsCommand : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication app = commandData.Application;
Document doc = app.ActiveUIDocument.Document;
Document toDocument = null;
FilteredElementCollector collector = new FilteredElementCollector(doc);
FilteredElementCollector Tocollector = new FilteredElementCollector(toDocument);
List<Type> viewTypes = new List<Type>();
viewTypes.Add(typeof(ViewPlan));
ElementMulticlassFilter Fromfilter = new ElementMulticlassFilter(viewTypes);
ElementMulticlassFilter ToFilter = new ElementMulticlassFilter(viewTypes);
collector.WherePasses(Fromfilter);
Tocollector.WherePasses(ToFilter);
collector.WhereElementIsViewIndependent();
Tocollector.WhereElementIsViewIndependent();
IEnumerable<ViewPlan> floorPlans = collector.OfType<ViewPlan>();
IEnumerable<ViewPlan> toFloorPlans = Tocollector.OfType<ViewPlan>();
int numDraftingPlanElements = Duplicate2DUtilities.CopyPaste2DElements(doc, floorPlans, toFloorPlans, toDocument);
int numfloorplans = toFloorPlans.Count<ViewPlan>();
return Result.Succeeded;
}
}
}
AND THE CLASS CODE IS THIS:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace Copy2DElementsFromViews
{
class Duplicate2DUtilities
{
private static ElementId ViewConvertToElementId(View view)
{
return view.Id;
}
public static int CopyPaste2DElements (Document fromDocument, IEnumerable<ViewPlan> views, IEnumerable<ViewPlan> Toviews,Document toDocument)
{
int numberOfDetailedViews = 0;
using (TransactionGroup tg = new TransactionGroup(toDocument,
"API - Duplication across documents with detailing"))
{
tg.Start();
List<ElementId> ids =
views.AsEnumerable<View>().ToList<View>().ConvertAll<ElementId>(ViewConvertToElementId);
List<ElementId> Toids =
Toviews.AsEnumerable<View>().ToList<View>().ConvertAll<ElementId>(ViewConvertToElementId);
Dictionary<ElementId, ElementId> viewMap = Maping2DElements(fromDocument, ids, toDocument, Toids, true);
foreach(ElementId viewId in viewMap.Keys)
{
View fromView = fromDocument.GetElement(viewId) as View;
View ToView = toDocument.GetElement(viewMap[viewId]) as View;
numberOfDetailedViews += DuplicateDetailingAcrossViews(fromView, ToView);
}
tg.Assimilate();
}
return numberOfDetailedViews;
}
private static Dictionary<ElementId, ElementId> Maping2DElements(Document fromDoc, ICollection<ElementId> elementsids, Document toDoc, ICollection<ElementId> Toelementsids,bool Isthere)
{
Dictionary<ElementId, ElementId> elementMap = new Dictionary<ElementId, ElementId>();
if(Isthere)
{
Dictionary<String, ElementId> nameTofromElementsMap = new Dictionary<String, ElementId>();
foreach (ElementId id in elementsids)
{
Element e = fromDoc.GetElement(id);
String name = e.Name;
if (!String.IsNullOrEmpty(name))
nameTofromElementsMap.Add(name, id);
}
Dictionary<String, ElementId> nameToToElementsMap = new Dictionary<string, ElementId>();
foreach(ElementId id in Toelementsids)
{
Element e = toDoc.GetElement(id);
string name = e.Name;
if (!String.IsNullOrEmpty(name)) nameToToElementsMap.Add(name, id);
}
foreach(String name in nameTofromElementsMap.Keys)
{
ElementId TOids;
if(nameToToElementsMap.TryGetValue(name,out TOids))
{
elementMap.Add(nameTofromElementsMap[name], TOids);
}
}
}
return elementMap;
}
private static int DuplicateDetailingAcrossViews(View fromView , View ToView)
{
FilteredElementCollector collector = new FilteredElementCollector(fromView.Document, fromView.Id);
collector.WherePasses(new ElementCategoryFilter(ElementId.InvalidElementId, true));
ICollection<ElementId> toCopy = collector.ToElementIds();
int numberOfCopiedElements = 0;
if (toCopy.Count > 0)
{
using(Transaction t2 = new Transaction(ToView.Document,"COPIANDO ELEMENTOS 2D"))
{
t2.Start();
CopyPasteOptions options = new CopyPasteOptions();
options.SetDuplicateTypeNamesHandler(new HideAndAcceptDuplicateTypeNamesHandler());
ICollection<ElementId> copied2DElements = ElementTransformUtils.CopyElements(fromView, toCopy, ToView, Transform.Identity, options);
numberOfCopiedElements = copied2DElements.Count;
FailureHandlingOptions failureOptions = t2.GetFailureHandlingOptions();
failureOptions.SetFailuresPreprocessor(new HidePasteDuplicateTypesPreprocessor());
t2.Commit(failureOptions);
}
}
return numberOfCopiedElements;
}
class HideAndAcceptDuplicateTypeNamesHandler : IDuplicateTypeNamesHandler
{
#region IDuplicateTypeNamesHandler Members
/// <summary>
/// Implementation of the IDuplicateTypeNameHandler
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
public DuplicateTypeAction OnDuplicateTypeNamesFound(DuplicateTypeNamesHandlerArgs args)
{
// Always use duplicate destination types when asked
return DuplicateTypeAction.UseDestinationTypes;
}
#endregion
}
class HidePasteDuplicateTypesPreprocessor : IFailuresPreprocessor
{
#region IFailuresPreprocessor Members
/// <summary>
/// Implementation of the IFailuresPreprocessor.
/// </summary>
/// <param name="failuresAccessor"></param>
/// <returns></returns>
public FailureProcessingResult PreprocessFailures(FailuresAccessor failuresAccessor)
{
foreach (FailureMessageAccessor failure in failuresAccessor.GetFailureMessages())
{
// Delete any "Can't paste duplicate types. Only non duplicate types will be pasted." warnings
if (failure.GetFailureDefinitionId() == BuiltInFailures.CopyPasteFailures.CannotCopyDuplicates)
{
failuresAccessor.DeleteWarning(failure);
}
}
// Handle any other errors interactively
return FailureProcessingResult.Continue;
}
#endregion
}
}
}
my main guide for doing this is the "DUPLICATE VIEWS" RSDK SAMPLES"
also ive tried with the "Rythm" package in dynamo "COPY ELEMENTS FROM VIEW TO ANOTHER VIEW" and also didnt work for me , im trying with detail groups , dimentions , tags etc...