LOGO
帮助文档
旧版文档
技术资讯

每一次案例的完美合作,是对我们团队高度认可,相信未来会更好!

梦想CAD事件COM接口知识点

一、鼠标事件


_DMxDrawXEvents::MouseEvent


控件中的鼠标事件。


参数说明

LONG lType

事件类型,1鼠标移动,2是鼠标左键按下,3是鼠标右键按下,4是鼠标左键双击 5是鼠标左键释放 6是鼠标右键释放 7是鼠标中键按下 8是鼠标中键释放 9是鼠标中键双击 10是鼠标中键滚动

DOUBLE dX

事件发生时的鼠标X坐标,文档坐标

DOUBLE dY

事件发生时的鼠标Y坐标,文档坐标

LONG* lRet

lRet 返回非0,消息将不在往下传递


响应鼠标点击图片对象js代码:


var draw = document.getElementById("MxDrawXCtrl");
      draw.ImplementMouseEventFun = function MouseEvent(dX, dY, lType) { 
             if(lType == 2)
                {    
                  // 鼠标左键按下
         
                   // dTol是搜索范围,50是屏幕像素.
                    var dTol = 50;        
                    // 临时修改CursorWidth的大小,用FindEntAtPoint函数确定搜索范围.
                   var bak = draw.CursorWidth;
                    draw.CursorWidth = dTol;        
                    // 搜索过滤条件,只选择图片.
                    var filter = draw.NewResbuf();        
                    // MxImageMark是图片对象的DXF组码名称,5020在DXF组码,代表MxImageMark是对象类型.
                    filter.AddStringEx("MxImageMark",5020);        
                     // 查找鼠标点击的实体。                        
                     var ent = draw.FindEntAtPoint(dX,dY,filter);        
                      // 恢复光标拾取框的大小.
                        draw.CursorWidth = bak;                       
                         if(ent != null)
                        {
         
                            alert("点击了一个图片");
                            // 设置1,表示鼠标事件,不再往下传递.
                            mxOcx.SetEventRet(1);
                        }
         
                        //
         
         
                    };
                };


提示鼠标双击下被点击的实体C#代码:


if (e.lType == 4)
{
    // 鼠标左键双击.
    // 构建选择集,找到鼠标左建双击下的实体。
  
    MxDrawSelectionSet ss = new MxDrawSelectionSet();
    MxDrawResbuf filter = new MxDrawResbuf();
    filter.AddStringEx("LINE",5020);
    MxDrawPoint point = new MxDrawPoint();
    point.x = e.dX;
    point.y = e.dY;
    ss.SelectAtPoint(point, filter);
    if (ss.Count > 0)
    {
        MxDrawEntity ent = ss.Item(0);
        MessageBox.Show("点击了" + ent.ObjectName + "对象");
        /*
        if (ent is MxDrawLine)
        {
            MxDrawLine line = (MxDrawLine)ent;
        }
            */
    }
    e.lRet = 1;
    // 取消后面的命令。
    axMxDrawX1.SendStringToExecute("");
}


提示鼠标双击下被点击的实体js代码:


function MouseEvent(dX,dY,lType)
{    if(lType == 4)
    {
               
        var mxOcx = document.getElementById("MxDrawXCtrl");
        var ss = mxOcx.Call("Mx_NewSelectionSet","");
        var pt = mxOcx.Call("Mx_NewPoint","");
                var fil = mxOcx.Call("Mx_NewResbuf","");
        pt.x = dX;
                pt.y = dY;
                
                ss.SelectAtPoint2(pt,fil);        
            if(ss.Count > 0)
                {
                    var ent = ss.Item(0);
                    alert("点击了" + ent.ObjectName + "对象");
                }
        // 取消后面的命令。
            mxOcx.SendStringToExecute("");
        return 1;
        
        }
     
       
    return 0;
}


在指定位置弹出一个菜单:


主要用到函数说明:


_DMxDrawX::TrackPopupMenu


在指定位置弹出一个菜单。详细说明如下:


参数说明

DOUBLE dX

弹出菜单的位置X值,是文档坐标

DOUBLE dY

弹出菜单的位置Y值,是文档坐标

sMenuFile

菜单定义文件,支持http开头的网络路径


_DMxDrawX::FindEntAtPoint


在某点位置处查找实体。详细说明如下:


参数说明

DOUBLE dX

点X值,文档坐标

DOUBLE dY

点Y值,文档坐标

IDispatch* pFilter

过滤条件


响应鼠标右键事件,弹出菜单c#代码如下:


        private void axMxDrawX1_MouseEvent(object sender, AxMxDrawXLib._DMxDrawXEvents_MouseEventEvent e)
        {
            if (e.lType == 3)
            {
                // 右键按下
                string sCmd = ""  ;
                if (axMxDrawX1.IsRuningCommand(ref sCmd))
                {
                    // 在命令下运行.
                    String sMenu = axMxDrawX1.GetOcxAppPath() + "\\mxpoptest.mnu";
                    axMxDrawX1.TrackPopupMenu(e.dX, e.dY, sMenu);
                    // 设置1,表示鼠标事件,不再往下传递.
                    axMxDrawX1.SetEventRet(1);
 
                }
                else
                {
                    // 非命令下运行
                    MxDrawXLib.MxDrawResbuf filter = new MxDrawXLib.MxDrawResbuf();
                    MxDrawXLib.MxDrawEntity ent = (MxDrawXLib.MxDrawEntity)axMxDrawX1.FindEntAtPoint(e.dX, e.dY, filter);
 
                    String sPopMenu;
                    if(ent != null)
                    {
                        axMxDrawX1.ClearCurrentSelect();
                        axMxDrawX1.AddCurrentSelect(ent.ObjectID,true,true);
 
                        sPopMenu = axMxDrawX1.GetOcxAppPath() + "\\MxPopMenuEnt.mnu";
                    }
                    else
                    {
                        sPopMenu = axMxDrawX1.GetOcxAppPath() + "\\MxPopMenu.mnu";
                    }
 
 
                    axMxDrawX1.TrackPopupMenu(e.dX, e.dY, sPopMenu);
                    // 设置1,表示鼠标事件,不再往下传递.
                    axMxDrawX1.SetEventRet(1);
                }
 
            }
        }


响应命令执行事件,如下:


 private void axMxDrawX1_ImplementCommandEvent(object sender, AxMxDrawXLib._DMxDrawXEvents_ImplementCommandEventEvent e)
        {
            if(e.iCommandId == 99)
            {
                MessageBox.Show("用户定义命令执行....");
            }
             
         }


js中使用如下:


                    function MouseEvent(dX, dY, lType) {                    
                    if(lType == 3)
                    {                        
                    // 鼠标右键按下
                        var filter = mxOcx.NewResbuf();                        
                        // 5020 = RTDXF0,只选择直线实体,设置过滤条件
                        //filter.AddStringEx("LINE",5020);
 
                        var ent = mxOcx.FindEntAtPoint(dX, dY, filter);
 
                        var sPopMenu;                       
                         if(ent != null)
                        {
                            mxOcx.ClearCurrentSelect();
                            mxOcx.AddCurrentSelect(ent.ObjectID,true,true);
 
                            sPopMenu = mxOcx.GetOcxAppPath() + "\MxPopMenuEnt.mnu"
                        }                        
                        else
                        {
                            sPopMenu = mxOcx.GetOcxAppPath() + "\MxPopMenu.mnu"
                        }
 
 
                        mxOcx.TrackPopupMenu(dX, dY, sPopMenu);                        
                        // 设置1,表示鼠标事件,不再往下传递.
                        mxOcx.SetEventRet(1);
                        }
                    }


求面积


将此段js代码放入鼠标事件中:


// 得到鼠标位置
var point = mxOcx.NewPoint();
point.x = dX;
point.y = dY;
// 构造择集,得到鼠标所在闭合区域。
 
// 设置过滤条件
var filter = mxOcx.NewResbuf();
// 设置PL线为过滤条件
filter.AddStringEx("LWPOLYLINE", 5020);
 
// 选择范围左下角
var pt1 = mxOcx.NewPoint();
pt1.x = point.x - 100;
pt1.y = point.y - 100;
// 选择范围右上角
 
var pt2 = mxOcx.NewPoint();
pt2.x = point.x + 100;
pt2.y = point.y + 100;
 
// 选择pt1,pt2所在范围内的对象.
var ss =mxOcx.NewSelectionSet();
ss.Select(1, pt1, pt2, filter);
 
for (var i = 0; i < ss.Count; i++)
{
    // 遍历所有对象,看当前点是在那个闭合区域内
    var pl =ss.Item(i);
    var dArea = pl.GetArea2 ();
    alert("面积为:" + dArea.toString());
 
}


C#计算鼠标点位置的闭合pl线的面积:


  
    private void axMxDrawX1_MouseEvent(object sender, AxMxDrawXLib._DMxDrawXEvents_MouseEventEvent e)
            {                
            if (e.lType == 4)
                 
                {                    
                // 是鼠标左键双击
                     
                // 得到鼠标位置
          
                    MxDrawPoint point = new MxDrawPoint();
                    point.x = e.dX;
                    point.y = e.dY;                    
                    // 构造择集,得到鼠标所在闭合区域。
                    // 设置过滤条件
                    MxDrawResbuf filter = new MxDrawResbuf();                    
                    // 设置PL线为过滤条件
                    filter.AddStringEx("LWPOLYLINE", 5020);                    
                    // 选择范围左下角
                    MxDrawPoint pt1 = new MxDrawPoint();
                    pt1.x = point.x - 100;
                    pt1.y = point.y - 100;                    
                    // 选择范围右上角
                    MxDrawPoint pt2 = new MxDrawPoint();
                    pt2.x = point.x + 100;
                    pt2.y = point.y + 100;                    
                    // 选择pt1,pt2所在范围内的对象.
                    MxDrawSelectionSet ss = new MxDrawSelectionSet();
                    ss.Select(MCAD_McSelect.mcSelectionSetCrossing, pt1, pt2, filter);                    
                    for (int i = 0; i < ss.Count; i++)
                    {                        
                    // 遍历所有对象,看当前点是在那个闭合区域内
                        MxDrawPolyline pl = (MxDrawPolyline)ss.Item(i);                        
                        // 得到闭合区域的点坐标。
                        MxDrawPoints pts = new MxDrawPoints();                        
                        for (int j = 0; j < pl.NumVerts; j++)
                        {
                            MxDrawPoint pt = pl.GetPointAt(j);
                            pts.Add(pt.x, pt.y, pt.z);
                        }                        if (pts.Count < 3)                            
                        continue;                        
                        // 判断鼠标点,是不是在这个闭合的区域内.
 
                        MxDrawUtility utility = new MxDrawUtility();                        
                        if (utility.PointInPolyline(pts, point.x, point.y))
                        {                            
                        // 找到用户点击的闭合区域 
                            double dArea = 0.0;
                            pl.GetArea(out dArea);                            
                            // 对选择的区域填充
                            for (int k = 0; k < pts.Count; k++)
                            {
                                MxDrawPoint tmp = pts.Item(k);                                
                                if(k == 0)
                                    axMxDrawX1.PathMoveTo(tmp.x, tmp.y);                                
                                    else
                                    axMxDrawX1.PathLineTo(tmp.x, tmp.y);
                            }
 
                            axMxDrawX1.DrawCADColorIndex = 3;
                            axMxDrawX1.DrawPathToHatch(0.0);
                            axMxDrawX1.UpdateDisplay();
 
                                MessageBox.Show("面积为:" + dArea.ToString());
 
                            e.lRet = 1;                            
                            // 取消后面的命令。                            
                            axMxDrawX1.SendStringToExecute("");                            
                            break;
                        }
                    }
 
                }
            }


二、超链接事件


_DMxDrawXEvents::HyperlinkClick


有超连接的实体被点击了。


参数说明

IDispatch* pEntity

IMxDrawEntity,被点击的实体

DOUBLE dX

点击位置X,文档坐标

DOUBLE dY

点击位置Y,文档坐标

LONG* pRet

成功,返回1, *pRet = 1


js代码实现:


 document.getElementById("MxDrawXCtrl").ImpHyperlinkClickFun = DoHyperlinkClickFun;
 
                function DoHyperlinkClickFun(ent, dX, dY) {
                    mxOcx.GotoURL(ent.Hyperlinks);
                    mxOcx.SetEventRet(1);
                }


三、动态拖放时的绘制事件


_DMxDrawXEvents::DynWorldDraw


动态拖放时的绘制事件。详细说明如下:


参数说明

DOUBLE dX

当前鼠标位置X坐标

DOUBLE dY

当前鼠标位置Y坐标

IDispatch* pWorldDraw

IMxDrawWorldDraw对象,用于动态绘制

IDispatch* pData

IMxDrawCustomEntity对象,动态绘制数据


JS调用代码,动态插入一个图块:


  
    function DoInsert()
    {
        var mxOcx = document.all.item("MxDrawXCtrl1");
        var getPt = mxOcx.NewComObject("IMxDrawUiPrPoint");
        getPt.message ="点取插入点";
        if(getPt.go() != 1)
        {
            return;
        }
 
        var frstPt =getPt.value();
        if(frstPt == null)
        {
 
            return;
        }
 
 
        var  getSecondPt = mxOcx.NewComObject("IMxDrawUiPrPoint");
 
        var spDrawData =getSecondPt.InitUserDraw("TestDynDraw");
 
 
        getSecondPt.message = "点取旋转角度";
        getSecondPt.basePoint = frstPt;
        getSecondPt.setUseBasePt(true);
 
        spDrawData.SetPoint("pt1",frstPt);
 
 
        var sBlkName = "Tree";
        var sBlkFile = mxOcx.GetOcxAppPath() + "\Blk\树.dwg";
 
        mxOcx.InsertBlock(sBlkFile,"Tree");
 
        spDrawData.SetString("BlkName","Tree");
 
        if(getSecondPt.go() != 1)
        {
 
            return;
        }
 
        spDrawData.Draw();
    }
 
    function DoDynWorldDrawFun(dX,dY,pWorldDraw,pData)
    {
 
        var mxOcx = document.all.item("MxDrawXCtrl1");
        var sGuid = pData.Guid;
 
        mxOcx.SetEventRet(0);
        if(sGuid = "TestDynDraw")
        {
            // 动态插入图块。
            var firstPt =  pData.GetPoint("pt1");
            if(firstPt == null)
                return;
 
            var sBlkName =  pData.GetString("BlkName");
 
            var secondPt = mxOcx.NewPoint();
 
            secondPt.x = dX;
            secondPt.y = dY;
 
            var vec = firstPt.SumVector(secondPt);
            var dAng = vec.Angle();
 
            pWorldDraw.DrawBlockReference(firstPt.x,firstPt.y,sBlkName,1.0,dAng * 180.0 / 3.14159265 + 90.0);
 
            mxOcx.SetEventRet(1);
        }
 
    }
梦想CAD控件,DWG网页,CAD网页,CAD控件,CAD开发