Вспомогательные библиотеки для отрисовки геометрии из Revit в AutoCAD в виде простых объектов (отрезок, дуга, точка) посредством экспорта в xml
Библиотеки пригодятся при разработке плагинов, связанных с геометрией, для удобного визуального восприятия результатов. На мой взгляд просматривать результат в AutoCAD намного удобнее.
В данном проекте присутствует две библиотеки (одна для Revit, вторая для AutoCAD) и демо-проект для Revit.
Проект для AutoCAD собран с использованием библиотек от AutoCAD 2013. Будет работать со всеми последующими версиями AutoCAD
Проект Revit собран с использованием библиотек от Revit 2015. Должен работать со всеми последующими версиями (с 2015-2018 точно работает)
Библиотека RevitGeometryExporter собирается под net45 и net48 и распространяется как NuGet-пакет.
Решение также содержит демо-проект для Revit. Описание использования на примере этого проекта:
RevitGeometryExporter нужна только на этапе разработки и отладки. Библиотека устроена так, что в Debug она работает и копируется в выходную папку, а в Release вызовы её методов удаляются компилятором и сама dll в выходную папку не попадает. Никаких #if DEBUG и путей к dll в проекте не требуется.
- Все публичные методы
ExportGeometryToXmlпомечены атрибутом[Conditional("DEBUG")]– какDebug.Print. Если в проекте, который вызывает метод, не определена константаDEBUG, компилятор удаляет вызов целиком (включая вычисление аргументов). В итоговой сборке плагина не остаётся даже ссылки на RevitGeometryExporter. - В пакет вложен файл
RevitGeometryExporter.targets, который NuGet автоматически подключает к проекту. Если вDefineConstantsпроекта естьDEBUG, он добавляетRevitGeometryExporter.dllв список копируемых в выходную папку файлов (независимо от настроек копирования NuGet-зависимостей в проекте), если нет – удаляет её оттуда. - Решение принимается по константе
DEBUG, а не по имени конфигурации, поэтому работает и с пользовательскими конфигурациями (например, под версии Revit) – важно лишь, определён ли в нихDEBUG. - Константа должна называться именно
DEBUG– символы условной компиляции в C# чувствительны к регистру,Debugне подойдёт. SDK-проекты автоматически добавляют константу из имени конфигурации в верхнем регистре, поэтому в конфигурацииDebugконстантаDEBUGесть всегда, а в конфигурации, например,Debug2025появится толькоDEBUG2025– тамDEBUGнужно добавить явно. Если в пользовательской конфигурацииDefineConstantsзадаётся вручную, не затирайте стандартные константы:
<DefineConstants>$(DefineConstants);DEBUG;R2025</DefineConstants>Условия на PackageReference по $(Configuration) для этой задачи не подходят: restore NuGet выполняется один раз для всех конфигураций, а копирование в выходную папку решается уже на этапе сборки – там и сделано исключение.
Пакет подключается во всех конфигурациях (чтобы код компилировался):
<ItemGroup>
<PackageReference Include="RevitGeometryExporter" Version="1.3.1" PrivateAssets="all" />
</ItemGroup>PrivateAssets="all" – чтобы пакет не превращался в транзитивную зависимость проектов, которые ссылаются на ваш.
Проекты под .NET Framework (net45 – net48) – ничего дополнительно не требуется.
Проекты под net8.0 / net10.0 (Revit 2025+):
- Дополнительные свойства (
CopyLocalLockFileAssemblies,EnableDynamicLoading) не нужны. В SDK-проектах библиотек под современный .NET dll из NuGet-пакетов по умолчанию не копируются в выходную папку, но targets-файл пакета сам добавляетRevitGeometryExporter.dllв список копируемых файлов, если определена константаDEBUG. ВключатьCopyLocalLockFileAssembliesради этого пакета не стоит – тогда в выходную папку попадут все зависимости всех пакетов (например,runtimesи сборки WebView2). - Пакет содержит сборки только под .NET Framework, поэтому при restore появится предупреждение NU1701 (используется сборка
net48). Это ожидаемо, его можно отключить:
<PackageReference Include="RevitGeometryExporter" Version="1.3.1" PrivateAssets="all" NoWarn="NU1701" />- Именно поэтому в пакете есть сборка
net48: для net8.0/net10.0 NuGet умеет подставлять только сборки net461 – net481, аnet45в этот список не входит. По той же причине targets-файл лежит вbuild\net45иbuild\net48, а не в корнеbuild: если бы в пакете был TFM-независимыйbuild\*.targets, NuGet считал бы пакет совместимым с net8.0, не применял бы подстановку net48 и молча не подключал бы саму dll.
- Инициализация – папка для экспорта xml и (при необходимости) единицы вывода:
// setup export folder
ExportGeometryToXml.Init(@"C:\Temp");
// setup export folder and units (feet or millimeters)
ExportGeometryToXml.Init(@"C:\Temp", ExportUnits.Mm);
// setup export folder, units and clear folder
ExportGeometryToXml.Init(@"C:\Temp", ExportUnits.Mm, true);По умолчанию в библиотеке прописан путь C:\Temp\RevitExportXml, единицы – футы. В случае отсутствия директории она будет создана.
Важно! Свойства
ExportGeometryToXml.FolderNameиExportGeometryToXml.ExportUnitsдоступны только для чтения – задаются они только через методыInit(...). Читать их в коде плагина также не следует: атрибут[Conditional]действует только на методы, поэтому обращение к свойству останется в Release-сборке, а dll в выходной папке не будет – плагин упадёт во время работы.
- Вызвать один или несколько методов экспорта геометрии. Например:
List<Wall> wallsToExport = new List<Wall>();
foreach (Reference reference in selectionResult)
{
Wall wall = (Wall)doc.GetElement(reference);
wallsToExport.Add(wall);
}
ExportGeometryToXml.ExportWallsByFaces(wallsToExport, "walls");Или
List<FamilyInstance> familyInstances = new List<FamilyInstance>();
foreach (Reference reference in selectionResult)
{
Element el = doc.GetElement(reference);
if(el is FamilyInstance familyInstance)
familyInstances.Add(familyInstance);
}
ExportGeometryToXml.ExportFamilyInstancesByFaces(familyInstances, "families", false);Директивы #if DEBUG вокруг этих вызовов не нужны.
- Пакет создаётся автоматически при сборке проекта RevitGeometryExporter в конфигурации Release (
GeneratePackageOnBuild) и кладётся вRevitGeometryExporter\bin\nupkg. - Версия пакета задаётся свойством
<Version>вRevitGeometryExporter.csproj, версия сборки – вProperties\AssemblyInfo.cs. - Сборка самой библиотеки в Release не влияет на поведение: удаление вызовов зависит от константы
DEBUGпроекта, который использует библиотеку.
- С помощью команды NETLOAD загрузить библиотеку CadDrawGeometry.dll.
- Использовать одну из доступных команд:
DrawFromOneXml – отрисовка геометрии из одного указанного xml-файла
DrawFromSeveralXml – отрисовка геометрии из нескольких указанных xml-файлов. По аналогии с DrawFromOneXml, только в окне выбора файлов включена возможность мультивыбора (через Shift или Ctrl)
DrawXmlFromFolder – отрисовка геометрии из указанной папки, в которой должны располагаться xml-файлы
Элементы в Revit:
Результат экспорта и отрисовки геометрии в AutoCAD:
Helper libraries for drawing Revit geometry in AutoCAD as simple objects (line, arc, point) via export to xml
The libraries are useful when developing geometry-related plugins, for convenient visual inspection of the results. In my opinion, viewing the result in AutoCAD is much more convenient.
The solution contains two libraries (one for Revit, one for AutoCAD) and a demo project for Revit.
The AutoCAD project is built against AutoCAD 2013 libraries. It will work with all later versions of AutoCAD.
The Revit project is built against Revit 2015 libraries. It should work with all later versions (confirmed for 2015-2018).
The RevitGeometryExporter library targets net45 and net48 and is distributed as a NuGet package.
The solution also contains a demo project for Revit. Usage is described using this project as an example:
RevitGeometryExporter is needed only during development and debugging. The library is designed so that in Debug it works and is copied to the output folder, while in Release the calls to its methods are removed by the compiler and the dll itself is not copied to the output folder. No #if DEBUG directives or paths to the dll are required in the project.
- All public methods of
ExportGeometryToXmlare marked with the[Conditional("DEBUG")]attribute – just likeDebug.Print. If theDEBUGconstant is not defined in the project that calls the method, the compiler removes the call entirely (including argument evaluation). The resulting plugin assembly does not even keep a reference to RevitGeometryExporter. - The package contains the
RevitGeometryExporter.targetsfile, which NuGet automatically imports into the project. If the project'sDefineConstantscontainsDEBUG, it addsRevitGeometryExporter.dllto the list of files copied to the output folder (regardless of the project's NuGet dependency copy settings); otherwise it removes the dll from that list. - The decision is based on the
DEBUGconstant, not on the configuration name, so it also works with custom configurations (for example, per Revit version) – all that matters is whetherDEBUGis defined in them. - The constant must be named exactly
DEBUG– conditional compilation symbols in C# are case-sensitive,Debugwill not work. SDK-style projects automatically add a constant made from the configuration name in upper case, so theDebugconfiguration always hasDEBUG, while a configuration such asDebug2025only getsDEBUG2025– thereDEBUGmust be added explicitly. If you setDefineConstantsmanually in a custom configuration, do not overwrite the standard constants:
<DefineConstants>$(DefineConstants);DEBUG;R2025</DefineConstants>Conditions on PackageReference based on $(Configuration) are not suitable for this task: NuGet restore runs once for all configurations, while copying to the output folder is decided at build time – that is where the exclusion is done.
The package is referenced in all configurations (so the code compiles):
<ItemGroup>
<PackageReference Include="RevitGeometryExporter" Version="1.3.1" PrivateAssets="all" />
</ItemGroup>PrivateAssets="all" prevents the package from becoming a transitive dependency of projects that reference yours.
.NET Framework projects (net45 – net48) – nothing else is required.
net8.0 / net10.0 projects (Revit 2025+):
- Additional properties (
CopyLocalLockFileAssemblies,EnableDynamicLoading) are not needed. In SDK-style class libraries for modern .NET, dlls from NuGet packages are not copied to the output folder by default, but the package's targets file addsRevitGeometryExporter.dllto the list of copied files itself when theDEBUGconstant is defined. Do not enableCopyLocalLockFileAssembliesfor this package – it would copy all dependencies of all packages to the output folder (for example,runtimesand WebView2 assemblies). - The package contains only .NET Framework assemblies, so restore produces warning NU1701 (the
net48assembly is used). This is expected and can be suppressed:
<PackageReference Include="RevitGeometryExporter" Version="1.3.1" PrivateAssets="all" NoWarn="NU1701" />- That is why the package contains a
net48assembly: for net8.0/net10.0 NuGet can fall back only to net461 – net481 assemblies, andnet45is not in that list. For the same reason the targets file is placed inbuild\net45andbuild\net48rather than in the root ofbuild: with a TFM-independentbuild\*.targets, NuGet would consider the package compatible with net8.0, would not apply the net48 fallback and would silently not reference the dll itself.
- Initialization – the folder for xml export and (optionally) output units:
// setup export folder
ExportGeometryToXml.Init(@"C:\Temp");
// setup export folder and units (feet or millimeters)
ExportGeometryToXml.Init(@"C:\Temp", ExportUnits.Mm);
// setup export folder, units and clear folder
ExportGeometryToXml.Init(@"C:\Temp", ExportUnits.Mm, true);The default path is C:\Temp\RevitExportXml, the default units are feet. If the directory does not exist, it will be created.
Important! The
ExportGeometryToXml.FolderNameandExportGeometryToXml.ExportUnitsproperties are read-only – they are set only via theInit(...)methods. Do not read them in plugin code either: the[Conditional]attribute applies only to methods, so a property access stays in the Release build while the dll is not in the output folder – the plugin will crash at runtime.
- Call one or more geometry export methods. For example:
List<Wall> wallsToExport = new List<Wall>();
foreach (Reference reference in selectionResult)
{
Wall wall = (Wall)doc.GetElement(reference);
wallsToExport.Add(wall);
}
ExportGeometryToXml.ExportWallsByFaces(wallsToExport, "walls");Or
List<FamilyInstance> familyInstances = new List<FamilyInstance>();
foreach (Reference reference in selectionResult)
{
Element el = doc.GetElement(reference);
if(el is FamilyInstance familyInstance)
familyInstances.Add(familyInstance);
}
ExportGeometryToXml.ExportFamilyInstancesByFaces(familyInstances, "families", false);No #if DEBUG directives are needed around these calls.
- The package is created automatically when the RevitGeometryExporter project is built in the Release configuration (
GeneratePackageOnBuild) and is placed inRevitGeometryExporter\bin\nupkg. - The package version is set by the
<Version>property inRevitGeometryExporter.csproj, the assembly version – inProperties\AssemblyInfo.cs. - Building the library itself in Release does not affect the behavior: call removal depends on the
DEBUGconstant of the project that uses the library.
- Load CadDrawGeometry.dll using the NETLOAD command.
- Use one of the available commands:
DrawFromOneXml – draw geometry from one selected xml file
DrawFromSeveralXml – draw geometry from several selected xml files. Same as DrawFromOneXml, but multiple selection (with Shift or Ctrl) is enabled in the file dialog
DrawXmlFromFolder – draw geometry from all xml files in the selected folder
Elements in Revit:
Result of export and drawing in AutoCAD:

