Create a custom SelectionSet, which selects only Objects originating from Advance Steel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello! Through the SOftware, we have the option of importing an XML file into the SelectionSet. I was trying to do the same through the API, but I did not find any method that allowed me to import this XML file through the .NET API.
I was developing the code below, I managed to create the SelectionSet, but it still does not return the selected items according to the category and property.
I'll leave my XML code below for review, and my .NET code that I was developing.
I'm waiting!
XML:
<?xml version="1.0" encoding="UTF-8" ?>
<exchange xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://download.autodesk.com/us/navisworks/schemas/nw-exchange-12.0.xsd" units="mm" filename="Lista de Material Advance Steel.nwd" filepath="A:\01-Clientes\Inpasa\2023.115 - Usina Etanol - Sidrolândia\Áreas\_Auxiliar\Lista de Material Advance Steel">
<selectionsets>
<selectionset name="Advance Steel" guid="48671e2e-028f-4fea-9d0e-2d3c9ec659da">
<findspec mode="all" disjoint="0">
<conditions>
<condition test="contains" flags="10">
<property>
<name internal="LcOaSceneBaseClassUserName">Type</name>
</property>
<value>
<data type="wstring">AST</data>
</value>
</condition>
</conditions>
<locator>/</locator>
</findspec>
</selectionset>
</selectionsets>
</exchange>
.NET CODE:
public static void AdicionarConjuntoSelecaoAdvanceSteel()
{
try
{
Document doc = Autodesk.Navisworks.Api.Application.ActiveDocument;
// Esta linha agora retorna corretamente uma ModelItemCollection
ModelItemCollection conjuntoSelecaoAdvanceSteel = create_search("LcOaSceneBaseClassUserName", "Type", "AST", "Advance Steel");
// Usa a ModelItemCollection diretamente sem casting
doc.CurrentSelection.Clear();
doc.CurrentSelection.AddRange(conjuntoSelecaoAdvanceSteel);
}
catch (Exception ex)
{
MessageBox.Show($"Erro ao criar o conjunto de seleção 'Advance Steel'.\nErro: {ex.Message}", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public static ModelItemCollection create_search(string categoryDisplayName, string propertyDisplayName, string variantData, string searchSetName)
{
// Cria uma nova busca
Search search = new Search();
// Define a condição de busca para a propriedade com o nome de exibição fornecido
// e verifica se o valor de exibição é igual ao valor de 'variantData'
// Nota: A API padrão verifica igualdade; uma abordagem "contém" pode exigir uma implementação customizada
SearchCondition condition = SearchCondition
.HasPropertyByName(categoryDisplayName, propertyDisplayName)
.EqualValue(VariantData.FromDisplayString(variantData));
// Adiciona a condição de busca ao objeto de busca
search.SearchConditions.Add(condition);
// Configura a busca para selecionar todos os descendentes e o próprio item
search.Selection.SelectAll();
search.Locations = SearchLocations.DescendantsAndSelf;
// Executa a busca e cria um conjunto de seleção com os resultados
ModelItemCollection results = search.FindAll(Autodesk.Navisworks.Api.Application.ActiveDocument, false);
// Adiciona um novo conjunto de seleção ao documento com base nos resultados
// Isso é um pouco diferente do XML, onde o conjunto de seleção é definido dentro do arquivo
SelectionSet newSelectionSet = new SelectionSet(results);
newSelectionSet.DisplayName = searchSetName;
Autodesk.Navisworks.Api.Application.ActiveDocument.SelectionSets.AddCopy(newSelectionSet);
return results;
}