create a plugin that, based on the selection of a item, creates a set with a predefined range of 50cm, for example, for x, y and z. Is it possible?
Solved! Go to Solution.
Solved by julia_morseP8AHB. Go to Solution.
Hi @julia_morseP8AHB ,
The issue you’re describing is a bit unclear to me.
It is possible to create a SelectionSet
in Navisworks via the API.
But I’m not sure what you mean by “creates a set with a predefined range of 50cm, for example, for x, y, and z.”
Could you please provide more details?
Hi @naveen.kumar.t , yes.
For example, after selecting a piece of equipment, it would create a "volume" of size X that would encompass items around the element.
This volume would be the set itself, but I want to do all of this via API.
Where, after selecting the equipment, for example, it creates a selection range, a range predefined by me, where we would have an idea of the size and spacing for selection. Based on the selected piece of equipment.
Was I clear?
Thank you!
Hi @julia_morseP8AHB ,
It seems you are referring to the bounding box.
From my understanding, you are selecting an element from which you would like to obtain the bounding box and then increase its size.
Is that correct?
Could you please clarify if that is the case?
Additionally, it would be helpful if you could provide a screenshot or a brief video to illustrate your point.
Hi @naveen.kumar.t ,
Let me try to explain it better...
Would this bounding box have an action like the set? Well, I initially thought of the set because I need to attach this "volume" that would be the set itself to a task in the timeliner. Do you understand?
That's why I need to create a set from an item/equipment and from this selection it generates a set with a specific range from the equipment itself.
Hi @julia_morseP8AHB ,
Please note that the Navisworks API rarely supports functionality that isn't available in the UI. Therefore, it's always helpful to identify the optimal manual approach before attempting a programmatic solution.
Based on the referenced help pages, it appears that creating a set from a volume or bounding box in the Navisworks UI is not supported. As a result, the API likely does not offer this functionality either. If you could provide manual instructions for achieving this in the UI, I can further investigate the API possibilities.
https://help.autodesk.com/view/NAV/2023/ENU/?guid=GUID-8FD0DD39-EC51-4C2F-BD60-6F84A3540E52
https://help.autodesk.com/view/NAV/2023/ENU/?guid=GUID-E4C5DFB3-A6DE-4886-A115-0E22517D0768
In my opinion, it would be possible.
Let's go... for example, don't we manually create a fence in NavisWorks? It is possible to create a fence... and select several objects.
Does this fence have a specific size? What if we created this fence by selecting an item in the model?
I think we can get the idea from this point.
Is that clear?
Hi @julia_morseP8AHB ,
I’m sorry for any confusion.
I still don’t fully understand the issue.
Could you please share a short, non-confidential video explaining the problem and your goal?
I’ll review it and check with my team to help resolve it.
Define boundingBox3D and do search. Then create selectset for the search result.
https://adndevblog.typepad.com/aec/2012/05/search-model-items-within-a-volume-and-apply-transformati...
Oh, great @tetsuya_miwa, it's a plugin?
I managed to create a preview, where it creates the set from the selection of one or more items.
Next steps:
> define the name of the set from the name of the selected item;
> "select" the item from the corresponding tag in the schedule (timeliner)
See the code now.
using System;
using System.Windows.Forms;
using Autodesk.Navisworks.Api;
using Autodesk.Navisworks.Api.Plugins;
[Plugin("MyNamespace.CreateSetPlugin", "MyCompany", DisplayName = "Create Set from Selection")]
public class CreateSetPlugin : AddInPlugin
{
public override int Execute(params string[] parameters)
{
CreateSetFromSelection();
return 0;
}
private void CreateSetFromSelection()
{
Document doc = Autodesk.Navisworks.Api.Application.ActiveDocument;
if (doc.CurrentSelection.SelectedItems.Count > 0)
{
// Cria um novo conjunto de seleção
SelectionSet newSet = new SelectionSet();
newSet.DisplayName = "Meu Novo Set"; // Define o nome do conjunto
// Adiciona os itens selecionados ao novo conjunto
foreach (ModelItem item in doc.CurrentSelection.SelectedItems)
{
newSet.ExplicitModelItems.Add(item); // Adiciona itens explicitamente
}
// Tenta adicionar o novo conjunto ao documento
try
{
// Adiciona o conjunto de seleção à coleção de conjuntos de seleção
doc.SelectionSets.AddCopy(newSet);
MessageBox.Show($"Set criado com {newSet.ExplicitModelItems.Count} itens.");
}
catch (Exception ex)
{
MessageBox.Show($"Erro ao adicionar o conjunto: {ex.Message}");
}
}
else
{
MessageBox.Show("Nenhum item selecionado.");
}
}
}
Hi @naveen.kumar.t regarding my last answer. Can you help me?
I managed to close the code to create a set after the selection item and append the name of the equipment to the corresponding set.
Now I want to try to create a "fence" after this selection and save it in the set with an additional "volume". Because the final point is to obtain the selection not only of the desired equipment itself, but also of the items next to it.
Hi @julia_morseP8AHB ,
Based on my understanding, here’s what you need to do:
doc.CurrentSelection.SelectedItems.BoundingBox
.Intersects
or Contains
methods. Please take a look at these links BoundingBox3D.Contains Method (BoundingBox3D) and BoundingBox3D.Intersects MethodI’ve put together a rough code example (not entirely sure it will solve your issue, but it should give you a good starting point).
public class Class1 : AddInPlugin //Derives from AddInPlugin
{
public override int Execute(params string[] parameters)
{
MyNwCode();
return 0;
}
private void MyNwCode()
{
Document doc = Autodesk.Navisworks.Api.Application.ActiveDocument;
//Step 1: Create Set from Collection
CreateSet(doc);
//Step 2: Get BoundingBox of items in set
BoundingBox3D boundingBox3D = doc.CurrentSelection.SelectedItems.BoundingBox(true);
//Step 3: Expand BoundingBox
int expansionFactor = 20;
BoundingBox3D expandBoundingBox = ExpandBoundingBox(boundingBox3D, expansionFactor);
//Step 4: Get Required BoundingBox intersecting Elements
IEnumerable<ModelItem> requiredElements = BoundingBoxIntersectingElements(doc, expandBoundingBox);
MessageBox.Show("Intersecting Elements = " + requiredElements.Count().ToString());
}
private IEnumerable<ModelItem> BoundingBoxIntersectingElements(Document doc, BoundingBox3D expandBoundingBox)
{
doc.CurrentSelection.SelectAll();
ModelItemCollection collection = doc.CurrentSelection.SelectedItems;
Search search = new Search();
search.Selection.SelectAll();
search.SearchConditions.Add(SearchCondition.HasPropertyByDisplayName("Element ID", "Value"));
ModelItemCollection allItems = search.FindAll(doc, false);
IEnumerable<ModelItem> items1 =allItems.Where(y => y.BoundingBox(true).Intersects(expandBoundingBox));
IEnumerable<ModelItem> items2 = allItems.Where(y => expandBoundingBox.Contains(y.BoundingBox(true)));
return items2;
}
private BoundingBox3D ExpandBoundingBox(BoundingBox3D box, int expansionAmount)
{
return new BoundingBox3D(
new Point3D(box.Min.X - expansionAmount, box.Min.Y - expansionAmount, box.Min.Z - expansionAmount),
new Point3D(box.Max.X + expansionAmount, box.Max.Y + expansionAmount, box.Max.Z + expansionAmount));
}
private void CreateSet(Document doc)
{
ModelItemCollection modelItemCollection = doc.CurrentSelection.SelectedItems;
SavedItem newSelectionSet = new SelectionSet(modelItemCollection);
newSelectionSet.DisplayName = "MySelectionSet";
doc.SelectionSets.InsertCopy(0, newSelectionSet);
}
}
I hope this helps!
During yesterday and with the help of @tetsuya_miwa reference I was able to advance in my code.
I thank you for your help and would like to share with you the code as it is developed and functional.
For what I mentioned above.
Thank you!
using System;
using System.Windows.Forms;
using Autodesk.Navisworks.Api;
using Autodesk.Navisworks.Api.Plugins;
[Plugin("MyNamespace.CreateSetPlugin", "MyCompany", DisplayName = "Create Set Bouding")]
public class CreateSetPlugin : AddInPlugin
{
public override int Execute(params string[] parameters)
{
CreateSetFromSelection();
return 0;
}
private string GetTagFromModelItem(ModelItem item)
{
string tagName = item.DisplayName;
var properties = item.PropertyCategories.FindCategoryByDisplayName("SmartPlant 3D");
var tagProperty = properties?.Properties.FindPropertyByDisplayName("Equipment Name");
if (tagProperty != null)
{
tagName = tagProperty.Value.ToDisplayString();
}
System.Diagnostics.Debug.WriteLine($"Tag Name: {tagName}");
return tagName;
}
private void CreateSetFromSelection()
{
Document doc = Autodesk.Navisworks.Api.Application.ActiveDocument;
if (doc.CurrentSelection.SelectedItems.Count > 0)
{
SelectionSet newSet = new SelectionSet();
ModelItem firstItem = doc.CurrentSelection.SelectedItems[0];
newSet.DisplayName = GetTagFromModelItem(firstItem);
// Obtém a caixa delimitadora do primeiro item selecionado
var boundingBox = firstItem.BoundingBox();
// Cria uma nova caixa delimitadora expandida
BoundingBox3D expandedBox = new BoundingBox3D(
new Point3D(boundingBox.Min.X - 2, boundingBox.Min.Y - 2, boundingBox.Min.Z - 2),
new Point3D(boundingBox.Max.X + 2, boundingBox.Max.Y + 2, boundingBox.Max.Z + 2));
// Adiciona os itens selecionados ao novo conjunto
foreach (ModelItem item in doc.CurrentSelection.SelectedItems)
{
newSet.ExplicitModelItems.Add(item);
}
// Adiciona itens que estão dentro do delimitador expandido
foreach (ModelItem item in doc.Models[0].RootItem.Descendants)
{
if (expandedBox.Contains(item.BoundingBox()))
{
newSet.ExplicitModelItems.Add(item);
}
}
try
{
doc.SelectionSets.AddCopy(newSet);
MessageBox.Show($"Set criado com {newSet.ExplicitModelItems.Count} itens.");
}
catch (Exception ex)
{
MessageBox.Show($"Erro ao adicionar o conjunto: {ex.Message}");
}
}
else
{
MessageBox.Show("Nenhum item selecionado.");
}
}
}
Can't find what you're looking for? Ask the community or share your knowledge.