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

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

Anonymous
Not applicable
2,070 Views
4 Replies
Message 1 of 5

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

Anonymous
Not applicable

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...

 

 

 

 

0 Likes
2,071 Views
4 Replies
Replies (4)
Message 2 of 5

BenoitE&A
Collaborator
Collaborator

Hi,

We do duplicate Tags but used the Document.Create.NewTag() method and it works perfectly. Having a tag to copy its type is in any case usefull.

But we did not use CopyElement, though in the end it looks rather similar to me (since the arguments of the 2 methods are basically the same).


Benoit FAVRE
CEO of etudes & automates
www.etudesetautomates.com/
Message 3 of 5

Anonymous
Not applicable

Hi! there! Nice Idea , i "ll try to Explore that path in order to reduce time with 2D Elements , i just have one question , this NewTag Method works with multiple Views? or / and multiple Levels in case of that? i mean when i tried to parse using the CopyElementsMethod above with SpecificViewElements it didnt work also i tried with arrays of views and multiple 2d elements with the CopyFromViewtoView (Rhythm Dynamo Package Node) and also didnt work.

 

If a have 10 floorplans in my Detached Model document all documented and tagged (doors,floors,windows , multicategory tags ) is there a posibility to recreate them in the "Central Delivery Model" that have the same 3d elements but not the 2d drafting elements in views , but all in just one Shot? did you Implement these kind of Workflow? or just tagging elements from a single view , and then if you want to tag another elements in another view you need to run again? 

 

i think that i can make multiple filters in order to extract the categories from tagging or try with multicategory filter .Typeof() but the deal here is tag all the doors or windows / 3d elements for documentation in all the views or specific views (but multiples not just a single one) because we have also done that process in the "Detached Model" we create the Detailed Design 2d but that model is not the central model in the cloud , so we can linked multiple times and upload if changes to the central model but 2D Documentation is not possible , thats the reason or the MainProblem we have!

 

 

 

Thanks very much to your Comment! i will explore 🙂

 

Greetings!!!!!

0 Likes
Message 4 of 5

BenoitE&A
Collaborator
Collaborator

Hi,

In Revit Tags are annotations so they depend on the view where they are created. Which means that you will have to copy your annotations from a view to the other if you have multiple views.

The topic of Linked files is a really ****ty one in Revit, as it carries complexity and different ways of dealing the same stuff as in one and only one file (think about filters, finding boundaries of rooms, etc... which are different in a Linked Document from the code in a regular one). 

I am unsure of your topic but it looks like something we do in France for marketing plans (http://www.etudesetautomates.com/plans_de_vente.html). Sorry it is only in French (but the plug-in is translated into English). But maybe we could save you months of work. Contact us in private messaging if you want to know more.


Benoit FAVRE
CEO of etudes & automates
www.etudesetautomates.com/
0 Likes
Message 5 of 5

Anonymous
Not applicable

Hi there! Thanks very much for your answer and same for your ideas!

 

Finally i found the way to do my job , Working with Complex Tree Structures and multiple nested Lists its been easily for this workflow with Python & Dynamo scripts so easily i found it with NewTag() Method i collect all geometry of the group of categories i needed to tag , then get the main inputs of the NewTag() method and then just work with List Management , easily in that way you can tag multiple elements in multiple views!

 

Greetings!

0 Likes