PDF3D/PDF3D.Addin/ExportCmd.cs

126 lines
4.2 KiB
C#
Raw Normal View History

2023-04-24 16:20:24 +08:00
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Annot;
2023-05-05 17:08:24 +08:00
using Microsoft.Win32;
2023-04-24 16:20:24 +08:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
2023-05-12 18:26:38 +08:00
using System.Windows;
2023-04-24 16:20:24 +08:00
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;
2023-05-05 17:08:24 +08:00
try
{
2023-05-12 18:26:38 +08:00
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);
}
}
}
}
2023-04-24 16:20:24 +08:00
2023-05-05 17:08:24 +08:00
SaveFileDialog fileDialog = new SaveFileDialog()
{
Filter = "Adobe 3dPdf Files (*.pdf)|*.pdf"
};
if (fileDialog.ShowDialog() == true)
{
2023-04-24 16:20:24 +08:00
2023-05-05 17:08:24 +08:00
IDTFBuilder iDTFBuilder = new IDTFBuilder(doc);
2023-04-24 16:20:24 +08:00
2023-05-05 17:08:24 +08:00
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);
}
2023-05-11 11:46:35 +08:00
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);
}
}
2023-05-05 17:08:24 +08:00
2023-05-11 17:06:50 +08:00
var idtf = Path.GetTempFileName();
var view = Path.GetTempFileName();
2023-05-05 17:08:24 +08:00
2023-05-11 17:06:50 +08:00
iDTFBuilder.Export(idtf);
iDTFBuilder.ExportView(view);
GeneratePDF(idtf, view, fileDialog.FileName);
2023-05-12 18:26:38 +08:00
MessageBox.Show("导出完成");
2023-05-05 17:08:24 +08:00
}
2023-04-24 16:20:24 +08:00
}
2023-05-12 18:26:38 +08:00
catch(Exception ex)
2023-05-05 17:08:24 +08:00
{
2023-05-12 18:26:38 +08:00
MessageBox.Show(ex.Message,"导出失败");
2023-05-05 17:08:24 +08:00
}
2023-04-24 16:20:24 +08:00
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;
}
2023-05-11 17:06:50 +08:00
public void GeneratePDF(string idtf, string view, string pdf)
2023-04-24 16:20:24 +08:00
{
System.Diagnostics.Process exep = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = PDFTool;
2023-05-11 17:06:50 +08:00
startInfo.Arguments = $"\"{idtf}\" \"{view}\" \"{pdf}\"";
2023-04-24 16:20:24 +08:00
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
exep.StartInfo = startInfo;
exep.Start();
exep.WaitForExit();
}
}
}