Message 1 of 14
Here's how to export Clash Result images using Navisworks API
Not applicable
10-16-2019
02:47 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I couldn't find any documentation for Navisworks 2019 API, and the documentation here (https://apidocs.co/apps/navisworks/2018/87317537-2911-4c08-b492-6496c82b3ed0.htm) does not seem to have a method for exporting images of clash results (have Autodesk abandoned Navisworks API or something?). Thus, I've attempted to code it myself.
Document doc = Autodesk.Navisworks.Api.Application.ActiveDocument;
DocumentClash documentClash = doc.GetClash();
DocumentClashTests oDCT = documentClash.TestsData;
// Assuming you've already run all Clash Tests and have the results
foreach(ClashTest test in oDCT.Tests) {
if(test.Children.Count <= 0) {
continue;
}
foreach (ClashResult result in test.Children) {
Viewpoint viewpoint = doc.CurrentViewpoint.Value;
// Get the 2 clashing elements from the ClashResult
ModelItem item1 = result.Item1;
ModelItem item2 = result.Item2;
ModelItemCollection items = new ModelItemCollection();
items.Add(item1);
items.Add(item2);
// Select the 2 clashing elements
doc.CurrentSelection.Clear();
doc.CurrentSelection.CopyFrom(items);
doc.ActiveView.FocusOnCurrentSelection();
doc.Models.ResetAllHidden();
// Hide everything except for the 2 clashing elements
ModelItemCollection modelItemCollection1 = new ModelItemCollection();
ModelItemCollection modelItemCollection2 = new ModelItemCollection();
using (IEnumerator<ModelItem> enumerator = doc.CurrentSelection.SelectedItems.GetEnumerator()) {
while (((IEnumerator) enumerator).MoveNext()) {
ModelItem current = enumerator.Current;
if (current.AncestorsAndSelf != null)
modelItemCollection2.AddRange((IEnumerable<ModelItem>) current.AncestorsAndSelf);
if (current.Descendants != null)
modelItemCollection2.AddRange((IEnumerable<ModelItem>) current.Descendants);
}
}
using (IEnumerator<ModelItem> enumerator = modelItemCollection2.GetEnumerator()) {
while (((IEnumerator) enumerator).MoveNext()) {
ModelItem current = enumerator.Current;
if (!NativeHandle.ReferenceEquals((NativeHandle) current.Parent, (NativeHandle) null))
modelItemCollection1.AddRange((IEnumerable<ModelItem>) current.Parent.Children);
}
}
using (IEnumerator<ModelItem> enumerator = modelItemCollection2.GetEnumerator()) {
while (((IEnumerator) enumerator).MoveNext()) {
ModelItem current = enumerator.Current;
modelItemCollection1.Remove(current);
}
}
doc.Models.SetHidden((IEnumerable<ModelItem>) modelItemCollection1, true);
doc.Models.SetHidden(
((IEnumerable<Model>) doc.Models)
.SelectMany<Model, ModelItem>(
(Func<Model, IEnumerable<ModelItem>>) (c => (IEnumerable<ModelItem>) c.RootItem.Children))
.Except<ModelItem>((IEnumerable<ModelItem>) modelItemCollection1)
.Except<ModelItem>((IEnumerable<ModelItem>) modelItemCollection2)
, true
);
doc.CurrentSelection.Clear();
// Adjust the camera, lighting, and paint the clashing elements in Red and Green respectively
Viewpoint copy = viewpoint.CreateCopy();
copy.Lighting = (ViewpointLighting) 0;
doc.Models.ResetAllPermanentMaterials();
doc.CurrentViewpoint.CopyFrom(copy);
Autodesk.Navisworks.Api.Color RED = Autodesk.Navisworks.Api.Color.Red;
Autodesk.Navisworks.Api.Color GREEN = Autodesk.Navisworks.Api.Color.Green;
if (!NativeHandle.ReferenceEquals((NativeHandle) items.ElementAtOrDefault<ModelItem>(0), (NativeHandle) null))
doc.Models.OverridePermanentColor((IEnumerable<ModelItem>) new ModelItem[1] {items.ElementAtOrDefault<ModelItem>(0)}, RED);
if (!NativeHandle.ReferenceEquals((NativeHandle) items.ElementAtOrDefault<ModelItem>(1), (NativeHandle) null))
doc.Models.OverridePermanentColor((IEnumerable<ModelItem>) new ModelItem[1] {items.ElementAtOrDefault<ModelItem>(1)}, GREEN);
doc.ActiveView.LookFromFrontRightTop();
doc.ActiveView.RequestDelayedRedraw((ViewRedrawRequests) 3);
// Save the Clash image
Bitmap clashImage = doc.ActiveView.GenerateThumbnail(500, 500);
string clashResultImageDir = "" // wherever you need to save
string clashResultImageName = clashResultImageDir + "\\" + test.DisplayName + "\\" + result.DisplayName + ".png";
clashImage.Save(clashResultImageName, ImageFormat.Png);
}
}P.S: Unless you have a high-end PC, the exporting process can take quite a long time (especially if the number of clashes in the document is higher than 5000). You're welcome to suggest improvements.