Automation to Generate NWD and NWF
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello, I created a project in Visual Studio Community 2022 of the Console Application type. When executed, it starts an instance of NavisworksApplication and appends several files. At the end, it exports to NWF and NWD.
However, I have a problem with the component information. The NWF stores all the component properties and is displayed correctly in the Properties. However, the generated NWD does not have any AutoCAD properties, as if it were not being generated.
I did a test, generating the NWD manually, and it came out weighing 4.5MB. However, when generated with Autodesk.Navisworks.Api.Automation using SaveFile(), it saved the same NWD with the same files weighing 2.4MB. And when opening the document, everything is fine, but it does not have the properties of the smart components.
Does anyone know how to solve this?
My code:
List<string> filesToAppend = new List<string>();
var folders = area.Folders.Where(f => f.Gerar && Directory.Exists(f.Caminho));
//DeleteTemporaryFiles(folders.ToList());
foreach (var folder in folders)
{
foreach (var ext in folder.ExtensoesSelecionadas)
{
string pattern = ext.StartsWith("*") ? ext : $"*{ext}";
var arquivos = Directory.GetFiles(folder.Caminho, pattern, SearchOption.TopDirectoryOnly);
filesToAppend.AddRange(arquivos);
}
}
filesToAppend.AddRange(area.Files
.Where(f => f.Gerar && File.Exists(f.Caminho))
.Select(f => f.Caminho));
if (filesToAppend.Count == 0)
{
Console.WriteLine($"Nenhum arquivo válido encontrado para a área {area.Codigo}");
return;
}
Console.WriteLine($"Total de arquivos a carregar: {filesToAppend.Count}");
// Caminho de saída
string outputDir = area.Source_AreaFolder ?? Path.Combine(Path.GetTempPath(), "MaquetteOutput");
Directory.CreateDirectory(outputDir);
string outputNwcPath = Path.GetFullPath(Path.Combine(outputDir, $"{area.Codigo}.nwc"));
string outputNwfPath = Path.GetFullPath(Path.Combine(outputDir, $"{area.Codigo}.nwf"));
string outputNwdPath = Path.GetFullPath(Path.Combine(outputDir, $"{area.Codigo}.nwd"));
if (File.Exists(outputNwdPath))
{
File.Delete(outputNwdPath);
}
if (File.Exists(outputNwfPath))
{
File.Delete(outputNwfPath);
}
Console.WriteLine("Inicializando uma instância do Navisworks...");
// --- Automação via Navisworks ---
using (var nwApp = new NavisworksApplication())
{
Console.WriteLine("Navisworks Inicializado com sucesso.");
nwApp.Visible = false;
try
{
nwApp.DisableProgress();
int total = filesToAppend.Count;
int current = 0;
foreach (string filePath in filesToAppend)
{
nwApp.AppendFile(filePath);
current++;
DrawProgressBar(current, total, prefix: $"[{area.Codigo}] Carregando arquivos");
}
nwApp.SaveFile(outputNwdPath);
nwApp.SaveFile(outputNwfPath);
Console.WriteLine($"Arquivo .NWF salvo em: {outputNwfPath}");
Console.WriteLine($"Arquivo .NWD salvo em: {outputNwdPath}");
}
catch (AutomationException ex)
{
Console.WriteLine($"[Área {area.Codigo}] Erro na automação: {ex.Message}");
}
catch (AutomationDocumentFileException ex)
{
Console.WriteLine($"[Área {area.Codigo}] Erro ao salvar documento: {ex}");
}
finally
{
nwApp.EnableProgress();
}
}
Console.WriteLine($"Área {area.Codigo} concluída.");