今天发现Revit中并没有提供导出导出设置
的按键,查阅API发现这块属于DWGExportSetting
类型,如果导出就只能自己写接口进行导出。大部分导出这个位置的应该关注的是图层修改器,这里因为他的加载标准,图层修改器是无法读取,如果需要传递图层需要传递项目标准或自己写接口进行传递,这个也算是一个很鸡肋的开发了。
这里面Revit标准采用的编码并不是UTF-8而是ANSI需要注意一下,不然导入的话全部都是乱码。
整个步骤并不是很麻烦,下面是全部的代码:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using GetGeometry;
using System.Windows;
using System.Net.Mail;
using System.Windows.Controls;
using Autodesk.Revit.DB.Architecture;
using BIMTools;
using Microsoft.Win32;
namespace UnitTest
{
[Transaction(TransactionMode.Manual)]
public class Commad : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;
var names = ExportDWGSettings.ListNames(doc);
FilteredElementCollector collector = new FilteredElementCollector(doc);
var allElements = collector.WhereElementIsNotElementType().ToElements();
var query = from e in allElements
where e.GetType() == typeof(ExportDWGSettings)
select e;
var settings = query.FirstOrDefault(x => x.Name == "导出设置_中文") as ExportDWGSettings;
var options = settings.GetDWGExportOptions();
var table = options.GetExportLayerTable();
var builder = new StringBuilder("# Revit Export Layers");
builder.AppendLine("# Maps Categories and Subcategories to layer names and color numbers");
builder.AppendLine("# Category <tab> Subcategory <tab> Layer name <tab> Color number <tab>");
builder.AppendLine("# Cut layer name <tab> Cut color number");
builder.AppendLine("# Do not remove the colon (:) after certain category names.");
builder.AppendLine("# -----------------------------------------------------");
foreach (var pair in table)
{
var key = pair.Key;
var categoryName = key.CategoryName;
if (!categoryName.Contains("dwg"))
{
var subCategoryName = key.SubCategoryName;
var value = pair.Value;
var layerName = value.LayerName;
var layerColor = value.ColorNumber;
var cutLayerName = value.CutLayerName;
var cutLayerColor = value.CutColorNumber;
if (layerColor==-1)
{
// builder.AppendLine(categoryName + " " + subCategoryName );
}
else if (cutLayerColor == -1 || cutLayerName == "-1")
{
builder.AppendLine(categoryName + "\t" + subCategoryName + "\t" + layerName + "\t" +
layerColor);
}
else
{
builder.AppendLine(categoryName + "\t" + subCategoryName + "\t" + layerName + "\t" + layerColor + "\t" +
cutLayerName + "\t" + cutLayerColor);
}
}
else
{
continue;
}
}
SaveFileDialog dialog = new SaveFileDialog()
{
FileName = settings.Name,
DefaultExt = ".txt"
};
var dialogResult = dialog.ShowDialog();
if (dialogResult == true)
{
var path = dialog.FileName;
using (StreamWriter writer = new StreamWriter(path,true,Encoding.Default))
{
writer.Write(builder);
}
}
return Result.Succeeded;
}
}
}