Sectioning in Navisworks Manage 2024 API not Available

Sectioning in Navisworks Manage 2024 API not Available

andrea_alpiniYNYBC
Participant Participant
463 Views
2 Replies
Message 1 of 3

Sectioning in Navisworks Manage 2024 API not Available

andrea_alpiniYNYBC
Participant
Participant

is it possible to create Sectioning box (similar to GUI) by C# Code PlugIn?

0 Likes
Accepted solutions (1)
464 Views
2 Replies
Replies (2)
Message 2 of 3

tetsuya_miwa
Advocate
Advocate
Accepted solution

Autodesk.Navisworks.Api.Application.ActiveDocument.ActiveView.SetClippingPlanes

Apply section box by GUI and you can see string of Json by  GetClippingPlanes.

Message 3 of 3

andrea_alpiniYNYBC
Participant
Participant

it Works!! thank you for suggestion, 

public static void ApplyNavisworksClipping(double cloudX, double cloudY, double cloudZ, double cloudXLen, double cloudYLen, double cloudZLen)
{
try
{
// Use culture-invariant formatting to ensure decimal points are always periods
System.Globalization.CultureInfo invariantCulture = System.Globalization.CultureInfo.InvariantCulture;

// Calculate the box corners
double minX = cloudX - cloudXLen / 2;
double minY = cloudY - cloudYLen / 2;
double minZ = cloudZ - cloudZLen / 2;
double maxX = cloudX + cloudXLen / 2;
double maxY = cloudY + cloudYLen / 2;
double maxZ = cloudZ + cloudZLen / 2;

// Create JSON string for OrientedBox3D format
string jsonClipPlaneSet = $@"
{{
""Type"": ""ClipPlaneSet"",
""Version"": 1,
""OrientedBox"": {{
""Type"": ""OrientedBox3D"",
""Version"": 1,
""Box"": [
[{minX.ToString(invariantCulture)}, {minY.ToString(invariantCulture)}, {minZ.ToString(invariantCulture)}],
[{maxX.ToString(invariantCulture)}, {maxY.ToString(invariantCulture)}, {maxZ.ToString(invariantCulture)}]
],
""Rotation"": [0, 0, 0]
}},
""Enabled"": true
}}";

// Remove newlines and extra spaces for a more compact JSON
jsonClipPlaneSet = jsonClipPlaneSet.Replace("\r", "").Replace("\n", "").Replace(" ", "");

// Log coordinates for debugging
System.Diagnostics.Debug.WriteLine($"Center: ({cloudX}, {cloudY}, {cloudZ})");
System.Diagnostics.Debug.WriteLine($"Dimensions: ({cloudXLen}, {cloudYLen}, {cloudZLen})");
System.Diagnostics.Debug.WriteLine($"Box min: ({minX}, {minY}, {minZ})");
System.Diagnostics.Debug.WriteLine($"Box max: ({maxX}, {maxY}, {maxZ})");

// First try with TrySetClippingPlanes to avoid exceptions
bool success = Application.ActiveDocument.ActiveView.TrySetClippingPlanes(jsonClipPlaneSet);
if (!success)
{
System.Diagnostics.Debug.WriteLine("Failed to set clipping planes with TrySetClippingPlanes");
// Fall back to SetClippingPlanes which will throw an exception with more details if it fails
Application.ActiveDocument.ActiveView.SetClippingPlanes(jsonClipPlaneSet);
}

// Request a view update
Application.ActiveDocument.ActiveView.RequestDelayedRedraw(ViewRedrawRequests.All);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error setting clipping planes: {ex.Message}");
// Uncomment for debugging in UI
// WF.MessageBox.Show($"Error setting clipping planes: {ex.Message}", "Error");
}
}

0 Likes