Announcements

Announcement: We’re aware of an issue affecting starting a new topic from category pages and creating new blog posts. New topics can still be started directly from the relevant board. Learn more here.

Automation to Generate NWD and NWF

Automation to Generate NWD and NWF

eduardo_salvador02
Enthusiast Enthusiast
641 Views
1 Reply
Message 1 of 2

Automation to Generate NWD and NWF

eduardo_salvador02
Enthusiast
Enthusiast

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.");
0 Likes
642 Views
1 Reply
Reply (1)
Message 2 of 2

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @eduardo_salvador02 ,

 

 

I have a question.

If you're not using the Navisworks Automation API and are instead using the Add-In Plugin or manually exporting the file via the UI, what results are you seeing?


Are you able to view the properties of the smart components correctly?


Are you encountering this issue only when using the Navisworks Automation API?

 


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes