目录
[Map 3D开发实战系列] Map Resource Explorer 背景介绍--Kick off
[Map 3D开发实战系列] Map Resource Explorer 之二-- 运行和调试
在前面的文章中介绍了如何在Visual Studio中建立项目开发Map 3D的应用程序,并且编写了一个非常简单的命令行命令。在Map 3D中通过netload命令加载我们的自定义程序集后,输入自定义命令即可调用相关的自定义函数还实现我们的功能。通过这种形式,我们可以写很多自定义命令来开发更多的自定义的功能来扩展Map 3D。但实际项目过程中,不光会用的自定义的命令,还经常会有界面,比如还可能会有一个对话框等等可视化的元素。这一节主要讲一下如果在AutoCAD Map 3D开发中来添加用户界面。
前文我们说过,Map 3D是基于AutoCAD基础之上的产品,AutoCAD.NET API提供的丰富的API来创建与AutoCAD风格一致的界面。在Map 3D中创建界面,主要是应用AutoCAD.NET API来完成。.Net framework 3.0的发布,让WPF技术也如火如荼的发展起来。AutoCAD 的Ribbon界面也是采用的WPF技术。为了使得我们的程序界面也和AutoCAD界面风格一致,我们也采用WPF技术来做界面。
在AutoCAD中添加一个WPF的Palette其实还是挺简单的。首先在我们的工程中添加一个基于WPF的用户控件。
你可以在这个WPF用户控件中来使用XAML来定义你的界面,也可以用Microsoft Blend等工具来设计你的用户界面,这不是我们讨论的重点,如果你对WPF界面设计感兴趣,可以参考MSDN或者其他资料。现在我们关心的是有了这样的WPF界面之后,如何在AutoCAD 中调用。
在AutoCAD中,我们可以利用AutoCAD.NET API来创建一个Palette,并把这个WPF用户控件加入到Palette中。请看下面的代码:
// (C) Copyright 2002-2009 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Windows;
namespace MyPaletteWebcast
{
public class MyPlugin : IExtensionApplication
{
public void Initialize()
{
// Initialize your plug-in application here
}
public void Terminate()
{
// Do plug-in application clean up here
}
// create a reference to the modeless paletteset window
static PaletteSet ps = null;
static UserControl1 myUserControl = null;
// Define Command "MyCommand"
[CommandMethod("MyCommand")]
static public void MyCommand() // This method can have any name
{
// check to see if the ps is created
if (ps == null)
{
// create an instance of the ps
ps = new PaletteSet("MyCommand");
myUserControl = new UserControl1();
ps.AddVisual("MyPalette", myUserControl);
}
// show the paletteset
ps.Visible = true;
}
}
}
启动AutoCAD,Map 3D或者Civil 3D,通过netload命令加载自定义程序集,然后输入自定义命令MyCommand即可调出AutoCAD 风格的自定义的Palette。
通过这样简单的代码,我们就可以定义一个自定义命令MyCommand,在这个命令执行时,创建一个Palette加载WPF用户控件,并显示出来。是不是很简单?你也可以试试看。
好了,今天先到这里,以后我们将继续记录Map 3D开发实战的进展情况。
Cheers,
Daniel