CAD绘制自定义实体(com接口)

在cad使用过程中,用户可以绘制自定义实体。点击此处下载演示实例

调用DrawCustomEntity函数,绘制一个自定义实体对象。


下面代码绘制一个自定义实体,C#代码实现如下:


private void DrawMlineCommand()
{
    MxDrawUiPrPoint getPt = new MxDrawUiPrPoint();
    getPt.message = "点取第一点";
    if (getPt.go() != MCAD_McUiPrStatus.mcOk)
    {
        return;
    }
            
    var frstPt = getPt.value();
    if (frstPt == null)
    {
        return;
    }
 
    MxDrawUiPrPoint getSecondPt = new MxDrawUiPrPoint();
 
    getSecondPt.message = "点取第二点";
    getSecondPt.basePoint = frstPt;
 
    getSecondPt.setUseBasePt(false);
 
    MxDrawCustomEntity spDrawData = getSecondPt.InitUserDraw("DrawCustEntity");
    spDrawData.SetDouble("Width", 30);
 
    spDrawData.SetPoint("Point1", frstPt);
 
    Int32 lCount = 1;
    spDrawData.SetLong("Count", 1);
            
    while (true)
    {
        if (getSecondPt.go() != MCAD_McUiPrStatus.mcOk)
            break;
 
        var secondPt = getSecondPt.value();
        if (secondPt == null)
            break;
 
        lCount++;
 
        String sPointName = "Point" + lCount.ToString();
 
        spDrawData.SetPoint(sPointName, secondPt);
        spDrawData.SetLong("Count", lCount);
    }
    if (lCount > 1)
        axMxDrawX1.DrawEntity(spDrawData);
}

需要响应DMxDrawXEvents::CustomEntity_Explode事件。


下面例子,得到自实体的数据,C#代码实现如下:


private void axMxDrawX1_CustomEntity_Explode(object sender, AxMxDrawXLib._DMxDrawXEvents_CustomEntity_ExplodeEvent e)
        {
            MxDrawCustomEntity pCustomEntity = (MxDrawCustomEntity)e.pCustomEntity;
            var sGuid = pCustomEntity.Guid;
 
            MxDrawWorldDraw pWorldDraw = (MxDrawWorldDraw)e.pDraw;
 
            if (sGuid == "DrawCustEntity")
            {
                MyDrawMline(pWorldDraw, pCustomEntity, null);
                e.pRet = 1;
            }
        }
 
private void MyDrawMline(MxDrawWorldDraw pWorldDraw, MxDrawCustomEntity pCustomEntity, MxDrawPoint curPt)
{
    // 取自定义实体的端点数目,属性。
    if (!pCustomEntity.IsHave("Count"))
        return;
    long lCount = pCustomEntity.GetLong("Count");
    MxDrawPolyline tmpPl = new MxDrawPolyline();
    for (long i = 0; i < lCount; i++)
    {
        String sName;
        sName = "Point" + (i + 1).ToString();
        if (!pCustomEntity.IsHave(sName))
            break;
 
        // 取自定义实体的端点坐标。
        MxDrawPoint pt = pCustomEntity.GetPoint(sName);
 
        // 把端点坐标,传给pl线,用于生成双线。
        tmpPl.AddVertexAt(pt);
    }
    if (curPt != null)
        tmpPl.AddVertexAt(curPt);
 
    if (tmpPl.NumVerts < 2)
    {
        // 端点数少于2就,不构成直线,就不需要显示。
        return;
    }
    // 求pl线,开始点的导数.
    MxDrawVector3d vecFx;
    if (!tmpPl.GetFirstDeriv(tmpPl.GetStartParam(), out vecFx))
        return;
    if (vecFx.IsZeroLength())
        return;
    // 把向量旋转90度.
    vecFx.RotateByXyPlan(3.14159265 / 2.0);
    vecFx.Normalize();
    //  得到双线的宽度属性。
    double dWidth = pCustomEntity.GetDouble("Width");
    vecFx.Mult(dWidth);
    MxDrawPoint startPt = tmpPl.GetStartPoint();
 
    // 向pl线,两个方向偏移,
    MxDrawPoint offsetPt1 = new MxDrawPoint();
    offsetPt1.x = startPt.x;
    offsetPt1.y = startPt.y;
    offsetPt1.Add(vecFx);
    MxDrawPoint offsetPt2 = new MxDrawPoint();
    offsetPt2.x = startPt.x;
    offsetPt2.y = startPt.y;
    offsetPt2.Sum(vecFx);
    MxDrawText text = new MxDrawText();
    text.TextString = "Test";
    text.Height = 100;
    text.Position = startPt;
    text.AlignmentPoint = startPt;
    MxDrawPoint pt1, pt2;
    text.GetBoundingBox(out pt1, out pt2);
    MxDrawPoint pt3 = new MxDrawPoint();
    pt3.x = pt1.x;
    pt3.y = pt2.y;
    MxDrawPoint pt4 = new MxDrawPoint();
    pt4.x = pt2.x;
    pt4.y = pt1.y;
    MxDrawPoints pts = new MxDrawPoints();
    pts.Add(pt1.x, pt1.y, 0);
    pts.Add(pt3.x, pt3.y, 0);
    pts.Add(pt2.x, pt2.y, 0);
    pts.Add(pt4.x, pt4.y, 0);
 
    Int32 lDraworder = pWorldDraw.Draworder;
    pWorldDraw.Draworder = lDraworder + 1;
    pWorldDraw.DrawWipeout(pts);
    pWorldDraw.Draworder = lDraworder + 2;
    pWorldDraw.DrawEntity((MxDrawEntity)text);
    pWorldDraw.Draworder = lDraworder;
    //    pWorldDraw->
    {
        MxDrawResbuf newobj;
        if (tmpPl.OffsetCurves(dWidth, offsetPt1, out newobj))
        {
            for (Int32 j = 0; j < newobj.Count; j++)
            {
                MxDrawEntity tmpObj = (MxDrawEntity)newobj.AtObject(j);
                if (tmpObj == null)
                    continue;
 
                pWorldDraw.DrawEntity(tmpObj);
            }
            newobj.RemoveAll();
        }
    }
    {
        MxDrawResbuf newobj;
        if (tmpPl.OffsetCurves(dWidth, offsetPt2, out newobj))
        {
            for (Int32 j = 0; j < newobj.Count; j++)
            {
                MxDrawEntity tmpObj = (MxDrawEntity)newobj.AtObject(j);
                if (tmpObj == null)
                    continue;
 
                pWorldDraw.DrawEntity(tmpObj);
            }
            // 这不使用newobj,需要显示调用RemoveAll函数清楚内存。
            // 不然这个可能就会程序退出时才释放,这时它会去释放控件对象指针,有可能会出错。
            newobj.RemoveAll();
        }
    }
}

C#代码实现如下:


private void axMxDrawX1_DynWorldDraw(object sender, AxMxDrawXLib._DMxDrawXEvents_DynWorldDrawEvent e)
{
    MxDrawCustomEntity pCustomEntity = (MxDrawCustomEntity)e.pData;
    String sGuid = pCustomEntity.Guid;
    e.pRet = 0;
 
    MxDrawWorldDraw pWorldDraw = (MxDrawWorldDraw)e.pWorldDraw;
    MxDrawPoint curPt = new MxDrawPoint();
    curPt.x = e.dX;
    curPt.y = e.dY;
 
    if (sGuid == "DrawCustEntity")
    {
        MyDrawMline(pWorldDraw, pCustomEntity, curPt);
    }
}

需要响应_DMxDrawXEvents::CustomEntity_getGripPoints事件,C#代码实现如下:


private void axMxDrawX1_CustomEntity_getGripPoints(object sender, AxMxDrawXLib._DMxDrawXEvents_CustomEntity_getGripPointsEvent e)
{
    MxDrawCustomEntity pCustomEntity = (MxDrawCustomEntity)e.pCustomEntity;
    var sGuid = pCustomEntity.Guid;
    e.pOk = 0;
    if (sGuid == "DrawCustEntity")
    {
        if (!pCustomEntity.IsHave("Count"))
            return;
 
        long lCount = pCustomEntity.GetLong("Count");
 
        MxDrawResbuf ret = (MxDrawResbuf)axMxDrawX1.NewResbuf();
 
        for (long i = 0; i < lCount; i++)
        {
            String sName;
            sName = "Point" + (i + 1).ToString();
            if (!pCustomEntity.IsHave(sName))
                break;
 
            // 取自定义实体的端点坐标。
            MxDrawPoint pt = pCustomEntity.GetPoint(sName);
 
 
            ret.AddPoint(pt);
        }
        e.pOk = 1;
        axMxDrawX1.SetEventRetEx(ret);
    }
 
}

需要响应CustomEntity_moveGripPointsAt事件。


下面例子,夹点移动后,修改自定义实体的属性,C#代码实现如下:


private void axMxDrawX1_CustomEntity_moveGripPointsAt(object sender, AxMxDrawXLib._DMxDrawXEvents_CustomEntity_moveGripPointsAtEvent e)
{
    e.pRet = 1;
    MxDrawCustomEntity pCustomEntity = (MxDrawCustomEntity)e.pCustomEntity;
    var sGuid = pCustomEntity.Guid;
 
    if (sGuid == "DrawCustEntity")
    {
        if (!pCustomEntity.IsHave("Count"))
            return;
 
        long lCount = pCustomEntity.GetLong("Count");
                
        for (long i = 0; i < lCount; i++)
        {
            String sName;
            sName = "Point" + (i + 1).ToString();
            if (!pCustomEntity.IsHave(sName))
                break;
 
            // 取自定义实体的端点坐标。
            MxDrawPoint pt = pCustomEntity.GetPoint(sName);
 
            if (i == e.lGridIndex)
            {
                pt.x = pt.x + e.dOffsetX;
                pt.y = pt.y + e.dOffsetY;
 
                pCustomEntity.SetPoint(sName, pt);
            }
        }
    }
}
上一篇:Oracle中connect by 的执行结果记载


下一篇:INSERT