Closest Grid Point Intersection to an Object

By Xiaodong Liang

Question:

I’m trying to create a plugin to add to a specific/selected object the closest grid intersection point in a property user tab.This should basically be similar to what the clash detective "Grid Intersection" shows. 
Should I use the "GridIntersection Class" to do this? And if so, where should I start?

Solution:
Given a referenced point, you can use GridSystem.ClosestIntersection(point) to get the closest GridIntersection which tells the postion, level, line1 and line2 of the intersection.
The referenced point of "Grid Intersection" in Clash Detective may probably be ClashResult.Center.
In this case, we could use the BoundingBox center of the object we are interested in. or BoundingBox Max or Min.
The following code is based on ToolsPlugin which is introduced from Navisworks 2014. In custom tool mode, the closest grid insection point will be marked as a sphere graphics when the user selects an object.

using Autodesk.Navisworks.Api;

using Autodesk.Navisworks.Api.Plugins;

 

namespace CustomGraphics

{

    // The ToolPlugin example.

    [Plugin("ToolPluginTest", "ADSK")]

    public class ToolPluginTest : ToolPlugin

    {

        public ModelItem clickedModel = null;

 

        public override bool MouseDown(View view,

                            KeyModifiers modifiers,

                          &
#160;
ushort button,

                            int x,

                            int y,

                            double timeOffset)

        {

            // get current selection

            PickItemResult itemResult =

                view.PickItemFromPoint(x, y);

            if (itemResult != null)

            {

                clickedModel =

                    itemResult.ModelItem;

                Application.ActiveDocument.ActiveView.

                               RequestDelayedRedraw(ViewRedrawRequests.Render);

            }

<

p style=”line-height: normal;margin: 0cm 0cm 0pt” class=”MsoNormal” align=”left”>            return false;

        }

 

        public Point3D GetClosestGridIntersection()

        {         

 

            //check that selection is valid

            if (clickedModel != null)

            {

                //Get bounding box of the selection

                BoundingBox3D bb3d =

                   clickedModel.BoundingBox();

 

                GridSystem oGS = Autodesk.Navisworks.Api.Application.ActiveDocument.Grids.ActiveSystem;

 

                if (oGS != null)

                //get the closest grid intersection point

                {

                    GridIntersection oGridIntersection = oGS.ClosestIntersection(bb3d.Center);

 

                    return oGridIntersection.Position;

                }

            }

 

            return null;

        }

 

        public override void OverlayRenderModel(View view, Graphics graphics)

        {

            if (clickedModel != null)

            {

                //color for graphics

                Color yellow = Color.FromByteRGB(255, 255, 0);

                graphics.Color(yellow, 0.8);

 

                //center of the current selection

                Point3D oCenter = GetClosestGridIntersection();

 

                if (oCenter != null)

                    //draw a Sphere

                    graphics.Sphere(oCenter, 1);

            }

        }

    }

 

    [Plugin("EnableToolPluginExample", "ADSK",

         DisplayName = "EnableToolPlugin")]

    public class EnableToolPluginExample : AddInPlugin

    {

        static bool enable = false;

        public override int Execute(params string[] parameters)

        {

            if (enable)

            {

                //switch to the native tool

                Application.MainDocument.Tool.Value = Tool.Select;

                //switch to custom tool

                ToolPluginRecord toolPluginRecord =

                    (ToolPluginRecord)Application.Plugins.FindPlugin(

                                                     "ToolPluginTest.ADSK");

                ToolPluginTest oPlugin = (ToolPluginTest)toolPluginRecord.LoadedPlugin;

                oPlugin.clickedModel = null;

            }

            else

            {

                //switch to custom tool

                ToolPluginRecord toolPluginRecord =

                    (ToolPluginRecord)Application.Plugins.FindPlugin(

                                                     "ToolPluginTest.ADSK");

                Application.MainDocument.Tool.

                    SetCustomToolPlugin(toolPluginRecord.LoadPlugin());

            }

            enable = !enable;

            return 0;

        }

    }

 

}

 

image


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading