Reputation: 1
I am trying to export revit model to NWC model and I have successfully done it. But only the thing now I am trying it further to convert nwc model into nwd model. For this I am applying the logic like after exporting the nwc model, I am trying to open the nwc file in currently installed navisworks.
But here I am stucked to save as nwd model. Can anyone please help me put for this ?
Below is my WIP code:
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace XXXX
{
[Transaction(TransactionMode.Manual)]
public class NavisExporter : IExternalCommand
{
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
// Get the active document
Document document = commandData.Application.ActiveUIDocument.Document;
// Get the name of the active Revit document without the extension
string documentName = document.Title;
if (documentName.EndsWith(".rvt", StringComparison.InvariantCultureIgnoreCase))
{
documentName = documentName.Substring(0, documentName.Length - 4);
}
// Use FolderBrowserDialog to select folder
string folder = string.Empty;
using (FolderBrowserDialog folderDialog = new FolderBrowserDialog())
{
folderDialog.Description = "Select the folder to save the NWC file";
if (folderDialog.ShowDialog() == DialogResult.OK)
{
folder = folderDialog.SelectedPath;
}
else
{
message = "No folder selected for export.";
return Result.Cancelled;
}
}
// Name for the NWC file
string name = documentName;
bool exportLinks = false;
bool exportElementIds = true;
string navisCoordinates = "1"; // 0 for Internal Coordinates, 1 for Shared Coordinates
bool exportUrls = false;
View3D view3D = null;
// Initialize Navisworks export options
NavisworksExportOptions exportOptions = new NavisworksExportOptions
{
ExportLinks = exportLinks,
ConvertElementProperties = false,
FindMissingMaterials = true,
ExportRoomAsAttribute = false,
ExportElementIds = exportElementIds,
ExportUrls = exportUrls,
ExportRoomGeometry = false,
Coordinates = (NavisworksCoordinates)int.Parse(navisCoordinates)
};
// Get the ViewFamilyType for 3D views
ViewFamilyType viewFamilyType = new FilteredElementCollector(document)
.OfClass(typeof(ViewFamilyType))
.Cast<ViewFamilyType>()
.FirstOrDefault(x => x.ViewFamily == ViewFamily.ThreeDimensional);
// Start a transaction to create a 3D view
using (Transaction transaction = new Transaction(document, "Create View"))
{
transaction.Start();
View3D tempView3D = View3D.CreateIsometric(document, viewFamilyType.Id);
// Get the phase filter "None" if available
PhaseFilter nonePhaseFilter = new FilteredElementCollector(document)
.OfClass(typeof(PhaseFilter))
.Cast<PhaseFilter>()
.FirstOrDefault(pf => pf.Name == "None");
if (nonePhaseFilter != null)
{
tempView3D.get_Parameter(BuiltInParameter.VIEW_PHASE_FILTER).Set(nonePhaseFilter.Id);
}
// Set the view for export
if (view3D == null)
{
exportOptions.ViewId = tempView3D.Id;
}
else
{
exportOptions.ViewId = view3D.Id;
}
transaction.Commit();
try
{
// Export the document to NWC
document.Export(folder, name, exportOptions);
// Launch Navisworks Manage
string nwcFilePath = Path.Combine(folder, name + ".nwc");
if (LaunchNavisworks(nwcFilePath, out string errorMessage))
{
TaskDialog.Show("Export Successful", $"Exported to {nwcFilePath} and launched Navisworks.");
return Result.Succeeded;
}
else
{
message = errorMessage;
return Result.Failed;
}
}
catch (Exception ex)
{
message = ex.Message;
return Result.Failed;
}
}
}
private bool LaunchNavisworks(string nwcFilePath, out string errorMessage)
{
try
{
string[] possiblePaths = new string[]
{
@"C:\Program Files\Autodesk\Navisworks Manage 2024\Roamer.exe",
@"C:\Program Files\Autodesk\Navisworks Manage 2023\Roamer.exe",
@"C:\Program Files\Autodesk\Navisworks Manage 2022\Roamer.exe",
@"C:\Program Files\Autodesk\Navisworks Manage 2021\Roamer.exe",
@"C:\Program Files\Autodesk\Navisworks Manage 2020\Roamer.exe"
};
string navisworksPath = possiblePaths.FirstOrDefault(File.Exists);
if (navisworksPath == null)
{
errorMessage = "No valid Navisworks executable found !";
return false;
}
ProcessStartInfo start = new ProcessStartInfo
{
FileName = navisworksPath,
Arguments = $"\"{nwcFilePath}\"",
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true
};
using (Process proc = Process.Start(start))
{
proc.WaitForExit();
int exitCode = proc.ExitCode;
if (exitCode == 0)
{
errorMessage = null;
return true;
}
else
{
errorMessage = $"Navisworks exited with code {exitCode}.";
return false;
}
}
}
catch (Exception ex)
{
errorMessage = "Error: " + ex.Message;
return false;
}
}
}
}
Upvotes: 0
Views: 205