126 lines
4.2 KiB
C#
126 lines
4.2 KiB
C#
using Autodesk.Revit.Attributes;
|
|
using Autodesk.Revit.DB;
|
|
using Autodesk.Revit.UI;
|
|
using iText.Kernel.Pdf;
|
|
using iText.Kernel.Pdf.Annot;
|
|
using Microsoft.Win32;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Windows;
|
|
|
|
namespace PDF3D.Addin
|
|
{
|
|
[Transaction(TransactionMode.Manual)]
|
|
public class ExportCmd : IExternalCommand
|
|
{
|
|
public static string PDFTool = Directory.GetParent(Assembly.GetExecutingAssembly().Location) + @"\\PDFGenerator.exe";
|
|
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
|
|
{
|
|
UIDocument uIDocument = commandData.Application.ActiveUIDocument;
|
|
Document doc = uIDocument.Document;
|
|
|
|
try
|
|
{
|
|
List<Element> elems = new List<Element>();
|
|
FilteredElementCollector collector = new FilteredElementCollector(doc).WhereElementIsNotElementType();
|
|
foreach (Element e in collector)
|
|
{
|
|
if (e.Category != null)
|
|
{
|
|
if (e.Category.HasMaterialQuantities)
|
|
{
|
|
elems.Add(e);
|
|
}
|
|
else
|
|
{
|
|
if (e.Category.IsCuttable)
|
|
{
|
|
elems.Add(e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
SaveFileDialog fileDialog = new SaveFileDialog()
|
|
{
|
|
Filter = "Adobe 3dPdf Files (*.pdf)|*.pdf"
|
|
};
|
|
if (fileDialog.ShowDialog() == true)
|
|
{
|
|
|
|
IDTFBuilder iDTFBuilder = new IDTFBuilder(doc);
|
|
|
|
var categorys = elems.GroupBy(x => x.Category.Name);
|
|
foreach (var category in categorys)
|
|
{
|
|
iDTFBuilder.InsertGroup(category.Key);
|
|
}
|
|
|
|
foreach (var item in elems)
|
|
{
|
|
iDTFBuilder.InsertElement(item);
|
|
}
|
|
|
|
var views = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Views).OfClass(typeof(View3D)).Cast<View3D>().ToList();
|
|
foreach (var item in views)
|
|
{
|
|
if (item.Origin != null)
|
|
{
|
|
iDTFBuilder.InsertView(item);
|
|
}
|
|
}
|
|
|
|
var idtf = Path.GetTempFileName();
|
|
var view = Path.GetTempFileName();
|
|
|
|
iDTFBuilder.Export(idtf);
|
|
iDTFBuilder.ExportView(view);
|
|
GeneratePDF(idtf, view, fileDialog.FileName);
|
|
MessageBox.Show("导出完成");
|
|
}
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message,"导出失败");
|
|
}
|
|
|
|
return Result.Succeeded;
|
|
}
|
|
|
|
public IEnumerable<Solid> GetSolids(GeometryObject go)
|
|
{
|
|
if (go is GeometryElement ge)
|
|
{
|
|
foreach (GeometryObject item in ge)
|
|
{
|
|
foreach (var g in GetSolids(item))
|
|
{
|
|
yield return g;
|
|
}
|
|
}
|
|
}
|
|
if (go is Solid solid)
|
|
{
|
|
yield return solid;
|
|
}
|
|
yield return null;
|
|
}
|
|
|
|
public void GeneratePDF(string idtf, string view, string pdf)
|
|
{
|
|
System.Diagnostics.Process exep = new System.Diagnostics.Process();
|
|
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
|
|
startInfo.FileName = PDFTool;
|
|
startInfo.Arguments = $"\"{idtf}\" \"{view}\" \"{pdf}\"";
|
|
startInfo.CreateNoWindow = true;
|
|
startInfo.UseShellExecute = false;
|
|
exep.StartInfo = startInfo;
|
|
exep.Start();
|
|
exep.WaitForExit();
|
|
}
|
|
}
|
|
}
|