highlight a selection programmatically

By Xiaodong Liang

With COM API, you could setup an InwOpSelection and add the paths which is the result of the find. And call State.CurrentSelection = your InwOpSelection. While in .NET API,  current selection is exposed as a document part CurrentSelection. The highlighting  objects is to change the selected items of CurrentSelection.

The code below shows the ways to highlight in COM and .NET.

using Autodesk.Navisworks.Api;    using ComApi = Autodesk.Navisworks.Api.Interop.ComApi;    using ComApiBridge = Autodesk.Navisworks.Api.ComApi;
// highlight objects    private void highlight()    {        Document oDoc =            Autodesk.Navisworks.Api.Application.ActiveDocument;             // assume we get two model items        ModelItem oItem1 =             oDoc.Models[0].RootItem.DescendantsAndSelf.            ElementAt(1);        ModelItem oItem2 =              oDoc.Models[0].RootItem.DescendantsAndSelf.            ElementAt(2);             // create ModelItemCollection        ModelItemCollection oSel_Net =            new ModelItemCollection();        oSel_Net.Add(oItem1);        oSel_Net.Add(oItem2);              // highlight by .NET API        oDoc.CurrentSelection.SelectedItems.CopyFrom(oSel_Net);             //highlight by COM API        ComApi.InwOpState10 state =            ComApiBridge.ComApiBridge.State;        ComApi.InwOpSelection comSelectionOut =             ComApiBridge.ComApiBridge.ToInwOpSelection(oSel_Net);             state.CurrentSelection = comSelectionOut;         }

Comments

One response to “highlight a selection programmatically”

  1. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Autodesk.Navisworks.Api;
    namespace WebApplication2
    {
    public partial class WebForm1 : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    Document oActiveDoc = Autodesk.Navisworks.Api.Application.ActiveDocument;
    try
    {
    oActiveDoc.OpenFile(@”D:\test.nwd”);
    }
    catch (Autodesk.Navisworks.Api.DocumentFileException)
    {
    if (!oActiveDoc.TryOpenFile(@”D:\test.nwd”))
    {
    Response.Write(“Failed to open the file”);
    }
    }
    }
    }
    }
    提示如下错误
    未将对象引用设置到对象的实例。
    说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
    异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
    源错误:
    行 19: try
    行 20: {
    行 21: oActiveDoc.Clear();
    行 22: oActiveDoc.OpenFile(@”D:\test_2012版本.nwd”);
    行 23: }
    源文件: K:\ToQiGang\WebApplication2\WebApplication2\WebForm1.aspx.cs 行: 21
    堆栈跟踪:
    [NullReferenceException: 未将对象引用设置到对象的实例。]
    WebApplication2.WebForm1.Page_Load(Object sender, EventArgs e) in K:\ToQiGang\WebApplication2\WebApplication2\WebForm1.aspx.cs:21
    System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
    System.Web.UI.Control.OnLoad(EventArgs e) +92
    System.Web.UI.Control.LoadRecursive() +54
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

Leave a Reply to davidCancel reply

Discover more from Autodesk Developer Blog

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

Continue reading