从另外一个窗体程序中控制naviswork中的视图

近期收到一个朋友的开发需求,要求从一个窗体程序中控制naviswork中的视图,当naviswork为打开时打开naviswork,当naviswork打开时,不再重新打开,并且要实现更换文件的功能。本以为这个需求是需要跨进程方面的技术才能完成的,就想着用sendmessage之类的方法去做,做着做着发现naviswork插件不知道怎么样接受消息。

于是我又重新翻阅了naviswork的开发文档,最终发现了可以使用的api函数,不得不佩服naviswork的强大之处!!!

下面贴一下代码:

发送器部分:::

        /// <summary>
        /// The NavisWorks Application
        /// </summary>
        Autodesk.Navisworks.Api.Automation.NavisworksApplication navisworksApplication= Autodesk.Navisworks.Api.Automation.NavisworksApplication.TryGetRunningInstance();

        /// <summary>
        /// The ID Prefix, or developer friendly name for the plugin we want to call
        /// </summary>
        const string pluginIDPrefix = "MessageSenderReceiver.MessageReceiver";

        /// <summary>
        /// The unique GUID for the plugin
        /// </summary>
        const string pluginGUID = "ADSK";


        public MessageSender()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDlg = new OpenFileDialog();
            if (openDlg.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openDlg.FileName;
                string[] msg = new string[] { "file", textBox1.Text };
                if (navisworksApplication != null)
                {
                    int retval = navisworksApplication.ExecuteAddInPlugin(pluginIDPrefix + "." + pluginGUID, msg);
                }
                else
                {
                    navisworksApplication = new NavisworksApplication();
                    //使程序可见
                    navisworksApplication.Visible = true;
                    //打开naviswork文档
                    navisworksApplication.OpenFile(textBox1.Text);


                }

            }


        }

        private void button2_Click(object sender, EventArgs e)
        {
            string[] msg = new string[] { "view", "视图" };
            if (navisworksApplication != null)
            {
                int retval = navisworksApplication.ExecuteAddInPlugin(pluginIDPrefix + "." + pluginGUID, msg);
            }
            else
            {
                MessageBox.Show("Naviswork未启动");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string[] msg = new string[] { "view", "视图 (1)" };
            if (navisworksApplication != null)
            {
                int retval = navisworksApplication.ExecuteAddInPlugin(pluginIDPrefix + "." + pluginGUID, msg);
            }
            else
            {
                MessageBox.Show("Naviswork未启动");
            }

        }

        private void MessageSender_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (navisworksApplication != null)
            {
                navisworksApplication.Dispose();
            }
        }

       
接受器部分:用于naviswork的插件中

 #region MessageSenderReceiver_MessageReceiver
   [PluginAttribute("MessageSenderReceiver.MessageReceiver",     //Plugin name
                    "ADSK")]                                     //4 character Developer ID or GUID
   [AddInPluginAttribute(AddInLocation.None)]                    //Identifies this as an Addin Plugin, that will not display in the ribbon
   public class MessageReceiver : AddInPlugin                    //Derives from AddInPlugin
   {
      public override int Execute(params string[] parameters)
      {
            //获取当前的document
            Document oDoc = Autodesk.Navisworks.Api.Application.ActiveDocument;
            if (parameters[0]== "file")
            {
               

                if (oDoc.FileName != parameters[1])
                {
                    oDoc.OpenFile(parameters[1]);
                }
            }
            else if(parameters[0]=="view")
            {
                SavedViewpoint selectedSavedViewPoint = new SavedViewpoint();
                DocumentSavedViewpoints saVP = oDoc.SavedViewpoints;
                SavedItemCollection savedviewpointCollection = saVP.ToSavedItemCollection();
                foreach(SavedItem si in savedviewpointCollection)
                {
                    SavedViewpoint savedViewPoint = si as SavedViewpoint;
                    if (savedViewPoint.DisplayName == parameters[1])
                    {
                        selectedSavedViewPoint = savedViewPoint;
                        break;
                    }
                }
                if (selectedSavedViewPoint != null)
                {
                    Viewpoint oSeclectVP = selectedSavedViewPoint.Viewpoint;
                    oDoc.CurrentViewpoint.CopyFrom(oSeclectVP);
                }
            }



            return 1;
        
      }
   }
   #endregion




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值