梦想CAD是专业的CAD插件(控件),不需安装AutoCAD即可运行。
在C#和VB中使用MxCAD的COM接口很方便,使用aximp命令或者在工具栏里面添加MxDraw com组件后,会生成AxInterop.MxDrawXLib.dll和Interop.MxDrawXLib.dll,前者主要是创建MxDraw控件,控制它的界面,响应它的事件。后者主要提供MxDraw数据库接口,操作图形对象。
下面使用代码:
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.axMxDrawX1 = new AxMxDrawXLib.AxMxDrawX();
((System.ComponentModel.ISupportInitialize)(this.axMxDrawX1)).BeginInit();
this.SuspendLayout();
//
// axMxDrawX1
//
this.axMxDrawX1.Enabled = true;
this.axMxDrawX1.Location = new System.Drawing.Point(12, 2);
this.axMxDrawX1.Name = "axMxDrawX1";
this.axMxDrawX1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMxDrawX1.OcxState")));
this.axMxDrawX1.Size = new System.Drawing.Size(776, 436);
this.axMxDrawX1.TabIndex = 0;
this.axMxDrawX1.ImplementCommandEvent += new AxMxDrawXLib._DMxDrawXEvents_ImplementCommandEventEventHandler(this.axMxDrawX1_ImplementCommandEvent);
this.axMxDrawX1.InitComplete += new System.EventHandler(this.OnInitComplete);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.axMxDrawX1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.axMxDrawX1)).EndInit();
this.ResumeLayout(false);
}
private void OnInitComplete(object sender, EventArgs e)
{
// create line
long id = this.axMxDrawX1.DrawLine(0, 0, 10, 10);
// modify line
var db = this.axMxDrawX1.GetDatabase() as MxDrawXLib.MxDrawDatabase;
var obj = db.ObjectIdToObject(id);
var ent = obj as MxDrawXLib.MxDrawEntity;
ent.colorIndex = MxDrawXLib.MCAD_COLOR.mcRed;
// register command "myLine"
axMxDrawX1.RegistUserCustomCommand("myLine", 55);
}
private void axMxDrawX1_ImplementCommandEvent(object sender, AxMxDrawXLib._DMxDrawXEvents_ImplementCommandEventEvent e)
{
//实例化控件工具集合
var mxUtility = new MxDrawXLib.MxDrawUtilityClass();
// implement command 55, that is, "myLine"
if (e.iCommandId == 55)
{
//点取第一点
var point = mxUtility.GetPoint(null, "点取第一点:");
if (point == null)
{
MessageBox.Show("用户取消...");
return;
}
//点取第二点
var point2 = mxUtility.GetPoint(point, "点取第二点:");
if (point2 == null)
{
MessageBox.Show("用户取消...");
return;
}
//调用绘制直线函数
axMxDrawX1.DrawLine(point.x, point.y, point2.x, point2.y);
}
}