Result Flatten(
Document doc,
ElementId viewId)
{
FilteredElementCollector col
= new FilteredElementCollector( doc, viewId )
.WhereElementIsNotElementType();
Options geometryOptions = new Options();
using( Transaction tx = new Transaction( doc ) )
{
if( tx.Start( "Convert elements to DirectShapes" )
== TransactionStatus.Started )
{
foreach( Element e in col )
{
GeometryElement gelt = e.get_Geometry(
geometryOptions );
if( null != gelt )
{
string appDataGUID = e.Id.ToString();
// Currently create direct shape
// replacement element in the original
// document – no API to properly transfer
// graphic styles to a new document.
// A possible enhancement: make a copy
// of the current project and operate
// on the copy.
//DirectShape ds = DirectShape.CreateElement(
// doc, e.Category.Id,
// _direct_shape_appGUID,
// appDataGUID ); // 2016
DirectShape ds = DirectShape.CreateElement(
doc, e.Category.Id ); //2017
ds.ApplicationId = _direct_shape_appGUID; // 2017
ds.ApplicationDataId = appDataGUID; // 2017
try
{
ds.SetShape(
new List<GeometryObject>( gelt ) );
// Delete original element
doc.Delete( e.Id );
}
catch( Autodesk.Revit.Exceptions
.ArgumentException ex )
{
Debug.Print(
"Failed to replace {0}; exception {1} {2}",
Util.ElementDescription( e ),
ex.GetType().FullName,
ex.Message );
}
}
}
tx.Commit();
}
}
return Result.Succeeded;
}