Skip to content

Instantly share code, notes, and snippets.

@yiskang
Created December 27, 2022 09:42
Show Gist options
  • Save yiskang/547a9d5243b50091124f3759a287f3d8 to your computer and use it in GitHub Desktop.
Save yiskang/547a9d5243b50091124f3759a287f3d8 to your computer and use it in GitHub Desktop.
Dummy Revit Custom Exporter - This sample runs a dummy exporter to test if current Revit 3D view can be exported without any errors. It won't produce any files, just run the export API.
// (C) Copyright 2022 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software
// in object code form for any purpose and without fee is hereby
// granted, provided that the above copyright notice appears in
// all copies and that both that copyright notice and the limited
// warranty and restricted rights notice below appear in all
// supporting documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK,
// INC. DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL
// BE UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is
// subject to restrictions set forth in FAR 52.227-19 (Commercial
// Computer Software - Restricted Rights) and DFAR 252.227-7013(c)
// (1)(ii)(Rights in Technical Data and Computer Software), as
// applicable.
//
using Autodesk.Revit.DB;
namespace ExportTest
{
class DummyExportContext : IExportContext
{
private Document document;
public ElementId CurrentElementId { get; private set; }
public DummyExportContext(Document document)
{
this.document = document;
}
public void Finish()
{
System.Diagnostics.Trace.WriteLine("Finish");
}
public bool IsCanceled()
{
return false;
}
public RenderNodeAction OnElementBegin(ElementId elementId)
{
Element e = this.document.GetElement(elementId);
string uid = e.UniqueId;
this.CurrentElementId = e.Id;
System.Diagnostics.Trace.WriteLine(string.Format(
"OnElementBegin: id {0} category {1} name {2}",
elementId.IntegerValue, e.Category.Name, e.Name));
return RenderNodeAction.Proceed;
}
public void OnElementEnd(ElementId id)
{
Element e = this.document.GetElement(id);
string uid = e.UniqueId;
System.Diagnostics.Trace.WriteLine(string.Format(
"OnElementEnd: id {0} category {1} name {2}",
id.IntegerValue, e.Category.Name, e.Name));
this.CurrentElementId = null;
}
public RenderNodeAction OnFaceBegin(FaceNode node)
{
System.Diagnostics.Trace.WriteLine(" OnFaceBegin: " + node.NodeName);
return RenderNodeAction.Proceed;
}
public void OnFaceEnd(FaceNode node)
{
System.Diagnostics.Trace.WriteLine(" OnFaceEnd: " + node.NodeName);
}
public RenderNodeAction OnInstanceBegin(InstanceNode node)
{
System.Diagnostics.Trace.WriteLine(" OnInstanceBegin: " + node.NodeName
+ " symbol: " + node.GetSymbolId().IntegerValue);
var symId = node.GetSymbolId();
// We can either skip this instance or proceed with rendering it.
return RenderNodeAction.Proceed;
}
public void OnInstanceEnd(InstanceNode node)
{
System.Diagnostics.Trace.WriteLine(" OnInstanceEnd: " + node.NodeName);
}
public void OnLight(LightNode node)
{
System.Diagnostics.Trace.WriteLine("OnLight: " + node.NodeName);
}
public RenderNodeAction OnLinkBegin(LinkNode node)
{
System.Diagnostics.Trace.WriteLine(" OnLinkBegin: " + node.NodeName + " Document: " + node.GetDocument().Title + ": Id: " + node.GetSymbolId().IntegerValue);
return RenderNodeAction.Proceed;
}
public void OnLinkEnd(LinkNode node)
{
System.Diagnostics.Trace.WriteLine(" OnLinkEnd: " + node.NodeName);
}
public void OnMaterial(MaterialNode node)
{
System.Diagnostics.Trace.WriteLine(" OnMaterial: " + node.NodeName);
}
public void OnPolymesh(PolymeshTopology node)
{
System.Diagnostics.Trace.WriteLine(" OnPolymesh: ");
var dNormals = node.DistributionOfNormals;
var nFaces = node.NumberOfFacets;
var nNormals = node.NumberOfNormals;
var nPts = node.NumberOfPoints;
var nUV = node.NumberOfUVs;
}
public void OnRPC(RPCNode node)
{
System.Diagnostics.Trace.WriteLine("OnRPC: " + node.NodeName);
}
public RenderNodeAction OnViewBegin(ViewNode node)
{
System.Diagnostics.Trace.WriteLine("OnViewBegin: "
+ node.NodeName + "(" + node.ViewId.IntegerValue
+ "): LOD: " + node.LevelOfDetail);
return RenderNodeAction.Proceed;
}
public void OnViewEnd(ElementId elementId)
{
Element e = this.document.GetElement(elementId);
string uid = e.UniqueId;
System.Diagnostics.Trace.WriteLine(string.Format(
"OnElementBegin: id {0} category {1} name {2}",
elementId.IntegerValue, e.Category.Name, e.Name));
}
public bool Start()
{
return true;
}
}
}
// (C) Copyright 2022 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software
// in object code form for any purpose and without fee is hereby
// granted, provided that the above copyright notice appears in
// all copies and that both that copyright notice and the limited
// warranty and restricted rights notice below appear in all
// supporting documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK,
// INC. DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL
// BE UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is
// subject to restrictions set forth in FAR 52.227-19 (Commercial
// Computer Software - Restricted Rights) and DFAR 252.227-7013(c)
// (1)(ii)(Rights in Technical Data and Computer Software), as
// applicable.
//
using System;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace ExportTest
{
[Transaction(TransactionMode.Manual)]
public class ExportCommand : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
var document = commandData.Application.ActiveUIDocument.Document;
var view = commandData.Application.ActiveUIDocument.ActiveView;
var exportContext = new DummyExportContext(document);
var exporter = new CustomExporter(document, exportContext);
exporter.ShouldStopOnError = true;
try
{
exporter.Export(view);
}
catch(Exception ex)
{
var msg = string.Format("Failed to proccess the geometries of Element Id `{0}`", exportContext.CurrentElementId);
System.Diagnostics.Trace.WriteLine(msg);
TaskDialog.Show("Revit", msg);
}
return Result.Succeeded;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment