近期收到一个朋友的开发需求,要求从一个窗体程序中控制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