风树 发表于 2012-10-29 16:31:27

求助:为什么不能显示自定义实体的夹点,附源码

//-----------------------------------------------------------------------------
#include "dbmain.h"

//-----------------------------------------------------------------------------
class DLLIMPEXP dim_xyz1 : public AcDbEntity {

public:
        ACRX_DECLARE_MEMBERS(dim_xyz1) ;

protected:
        static Adesk::UInt32 kCurrentVersionNumber ;

public:
        dim_xyz1 () ;
        virtual ~dim_xyz1 () ;

        //----- AcDbObject protocols
        //- Dwg Filing protocol
        virtual Acad::ErrorStatus dwgOutFields (AcDbDwgFiler *pFiler) const ;
        virtual Acad::ErrorStatus dwgInFields (AcDbDwgFiler *pFiler) ;

        //----- AcDbEntity protocols
        //- Graphics protocol
protected:
        virtual Adesk::Boolean subWorldDraw (AcGiWorldDraw *mode) ;
        virtual Adesk::UInt32 subSetAttributes (AcGiDrawableTraits *traits) ;

        //- Osnap points protocol
public:
                virtual Acad::ErrorStatus subGetOsnapPoints (
                AcDb::OsnapMode osnapMode,
                int gsSelectionMark,
                const AcGePoint3d &pickPoint,
                const AcGePoint3d &lastPoint,
                const AcGeMatrix3d &viewXform,
                AcGePoint3dArray &snapPoints,
                AcDbIntArray &geomIds) const ;

        //- Grip points protocol
        virtual Acad::ErrorStatus subGetGripPoints (AcGePoint3dArray &gripPoints, AcDbIntArray &osnapModes, AcDbIntArray &geomIds) const ;
       
        virtual Acad::ErrorStatus subMoveGripPointsAt (const AcDbIntArray &indices, const AcGeVector3d &offset) ;
       
        virtual Acad::ErrorStatus subTransformBy(const AcGeMatrix3d & xform);

        AcGePoint3d m_line_begin;
        AcGePoint3d m_line_mid;
        AcGePoint3d m_line_end;
        CString m_Text1;
        CString m_Text2;
        AcGePoint3d m_textbase_s;
        AcGePoint3d m_textbase_x;
        double TextSize;

} ;

#ifdef My_MODULE
ACDB_REGISTER_OBJECT_ENTRY_AUTO(dim_xyz1)
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
//----- dim_xyz1.cpp : Implementation of dim_xyz1
//-----------------------------------------------------------------------------
#include "StdAfx.h"
#include "dim_xyz1.h"

//-----------------------------------------------------------------------------
Adesk::UInt32 dim_xyz1::kCurrentVersionNumber =1 ;

//-----------------------------------------------------------------------------
ACRX_DXF_DEFINE_MEMBERS (
        dim_xyz1, AcDbEntity,
        AcDb::kDHL_CURRENT, AcDb::kMReleaseCurrent,
        AcDbProxyEntity::kNoOperation, DIM_XYZ1,
XTCOWMYAPP
|Product Desc:   A description for your object
|Company:          Your company name
|WEB Address:      Your company WEB site address
)

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
dim_xyz1::dim_xyz1 () : AcDbEntity () {

}

dim_xyz1::~dim_xyz1 () {
}

//-----------------------------------------------------------------------------
//----- AcDbObject protocols
//- Dwg Filing protocol
Acad::ErrorStatus dim_xyz1::dwgOutFields (AcDbDwgFiler *pFiler) const {
        assertReadEnabled () ;
        //----- Save parent class information first.
        Acad::ErrorStatus es =AcDbEntity::dwgOutFields (pFiler) ;
        if ( es != Acad::eOk )
                return (es) ;
        //----- Object version number needs to be saved first
        if ( (es =pFiler->writeUInt32 (dim_xyz1::kCurrentVersionNumber)) != Acad::eOk )
                return (es) ;
        //----- Output params
        //.....

        return (pFiler->filerStatus ()) ;
}

Acad::ErrorStatus dim_xyz1::dwgInFields (AcDbDwgFiler *pFiler) {
        assertWriteEnabled () ;
        //----- Read parent class information first.
        Acad::ErrorStatus es =AcDbEntity::dwgInFields (pFiler) ;
        if ( es != Acad::eOk )
                return (es) ;
        //----- Object version number needs to be read first
        Adesk::UInt32 version =0 ;
        if ( (es =pFiler->readUInt32 (&version)) != Acad::eOk )
                return (es) ;
        if ( version > dim_xyz1::kCurrentVersionNumber )
                return (Acad::eMakeMeProxy) ;
        //- Uncomment the 2 following lines if your current object implementation cannot
        //- support previous version of that object.
        //if ( version < dim_xyz::kCurrentVersionNumber )
        //        return (Acad::eMakeMeProxy) ;
        //----- Read params
        //.....

        return (pFiler->filerStatus ()) ;
}

//-----------------------------------------------------------------------------
//----- AcDbEntity protocols
Adesk::Boolean dim_xyz1::subWorldDraw (AcGiWorldDraw *mode) {//AcGiWorldDraw类提供产生几何体对而不随视口改变的功能。
        assertReadEnabled () ;
        // Bounding Polyline
        AcGePoint3d pts;
        pts = m_line_begin;
        pts = m_line_mid;
        pts = m_line_end;
        AcGePoint3d text;
        text = m_textbase_s;
        text = m_textbase_x;
        // mode->subEntityTraits()提取AcGiSubEntityTraits类,此类用于控制图形元素当前的颜色、层、线型、填充类型和图形系统标记的属性值。
        mode->subEntityTraits().setSelectionMarker(1);// Mark 1
        //设置AcGiSubEntityTraits对象使所有已绘出的图形元素使用markerId作为图形标记,这个图形元素的标记一直作用至再次调用这个函数或当前的worldDraw()
        //或viewportDraw()执行结束后。
        //警告:基于AutoCAD图形系统的性能优化,在自己的worldDraw()或viewportDraw()方法中设置选择标记定制的实体类必须在任何AcGiWorldGeometry或AcGiViewprotGeometry
        //几何体创建方法(包括mesh()或shell())被调用之前通过调用AcGiSubEntityTraits::setSelectionMarker()设置第一个选择标记。
        mode->subEntityTraits().setColor(1); // Red
        mode->geometry().polyline(3,pts);//mode->geometry()调出AcGiWorldGeometry类画线
       
        // Entity's Text
        mode->subEntityTraits().setSelectionMarker(2); // Mark 2
        mode->subEntityTraits().setColor(1); // Red
        AcGiTextStyle style;//注意此类用于设置设置文本的显示属性。它作为一个参数传递给一个AcGiGeometry::text()的成员函数。
        style.setFileName(_T("txt.shx"));//设置"txt.shx"        style
        style.setBigFontFileName(_T(""));//
        style.setTextSize(TextSize);//设置文字大小
        style.loadStyleRec();//此函数加载一个字体(不是一个样式),在AcGiTextStyle::setFileName()或 AcGiTextStyle::setBigFontFileName()设置了字体文件名后运行。此函数必须在加载一个True Type字体(TTF)后运行。
        AcGePoint3d txtPt1(text.x+1,text.y+1,text.z) ;
        mode->geometry().text(txtPt1, AcGeVector3d::kZAxis,pts-pts,m_Text1,m_Text1.GetLength(),Adesk::kFalse, style);

        mode->subEntityTraits().setSelectionMarker(3); // Mark 3
        mode->subEntityTraits().setColor(1); // Red
        //AcGiTextStyle style;//注意此类用于设置设置文本的显示属性。它作为一个参数传递给一个AcGiGeometry::text()的成员函数。
        style.setFileName(_T("txt.shx"));//设置"txt.shx"        style
        style.setBigFontFileName(_T(""));//
        style.setTextSize(TextSize);//设置文字大小
        style.loadStyleRec();//此函数加载一个字体(不是一个样式),在AcGiTextStyle::setFileName()或 AcGiTextStyle::setBigFontFileName()设置了字体文件名后运行。此函数必须在加载一个True Type字体(TTF)后运行。

        AcGePoint3d txtPt2(text.x+1,text.y-1-TextSize,text.z) ;
        mode->geometry().text(txtPt2, AcGeVector3d::kZAxis,pts-pts,m_Text1,m_Text1.GetLength(),Adesk::kFalse, style);

        return (AcDbEntity::subWorldDraw (mode)) ;
}


Adesk::UInt32 dim_xyz1::subSetAttributes (AcGiDrawableTraits *traits) {
        assertReadEnabled () ;
        return (AcDbEntity::subSetAttributes (traits)) ;
}
//- Osnap points protocol
/**        获得捕捉点,这里添加一个中心点       
*        @param   -
*        @author
*   @date       
*   @retval-
*   @note    这里仅用第一个变量,虽捕捉到,但是有点问题,鼠标移开后就没有了
*            This OSNAP solution handles only simple OSNAP types like CENTER, MIDPOINT and
*            ENDPOINT. Complex OSNAP modes like INTERSECTION require other methods implementations and
*            some additional procedures
*   求解步聚:-
*/
Acad::ErrorStatus dim_xyz1::subGetOsnapPoints (
        AcDb::OsnapMode osnapMode,
        int gsSelectionMark,
        const AcGePoint3d &pickPoint,
        const AcGePoint3d &lastPoint,
        const AcGeMatrix3d &viewXform,
        AcGePoint3dArray &snapPoints,
        AcDbIntArray &geomIds) const
{
                assertReadEnabled () ;
                switch (osnapMode) {
                        case AcDb::kOsModeEnd://端点
                        snapPoints.append(m_line_begin);
                        snapPoints.append(m_line_mid);
                        break;
                }
        return (AcDbEntity::subGetOsnapPoints (osnapMode, gsSelectionMark, pickPoint, lastPoint, viewXform, snapPoints, geomIds)) ;
}
/**        获得夹点,       
*        @param   -
*        @author
*   @date       
*   @retval-
*   @note    -
*   求解步聚:-
*/
//- Grip points protocol
Acad::ErrorStatus dim_xyz1::subGetGripPoints (
        AcGePoint3dArray &gripPoints, AcDbIntArray &osnapModes, AcDbIntArray &geomIds) const {
        assertReadEnabled () ;
        //----- This method is never called unless you return eNotImplemented
        //----- from the new getGripPoints() method below (which is the default implementation)
        gripPoints.append(m_line_begin);

        return (AcDbEntity::subGetGripPoints (gripPoints, osnapModes, geomIds)) ;
}
/**        移动夹点       
*        @param   -
*        @authorCG
*   @date       11/4/2011
*   @retval-
*   @note    -
*   求解步聚:-
*/
Acad::ErrorStatus dim_xyz1::subMoveGripPointsAt (const AcDbIntArray &indices, const AcGeVector3d &offset) {
        assertWriteEnabled () ;
        //----- This method is never called unless you return eNotImplemented
        //----- from the new moveGripPointsAt() method below (which is the default implementation)
        assertWriteEnabled();
        for(int i=0; i<indices.length(); i++)
        {
                int idx = indices.at(i);
                //
                if (idx==0) m_line_begin += offset;
                //
                if (idx==1) {m_line_mid += offset;m_line_end += offset;}
                //
                //if (idx==2) m_line_end += offset;

        }
        return (AcDbEntity::subMoveGripPointsAt (indices, offset)) ;
}

/**        实体的移动、旋转、缩放等等       
*        @param   -
*        @author
*   @date       
*   @retval-
*   @note    -
*   求解步聚:-
*/
Acad::ErrorStatus dim_xyz1::subTransformBy(const AcGeMatrix3d & xform)
{
        m_line_begin.transformBy(xform);
        m_line_mid.transformBy(xform);
        m_line_end.transformBy(xform);
        m_textbase_s.transformBy(xform);
        m_textbase_x.transformBy(xform);
        return (Acad::eOk) ;
}


风树 发表于 2012-10-29 16:32:20

捕捉也不行阿,大侠们请指教

风树 发表于 2012-10-29 17:05:44

元芳,怎么样阿

风树 发表于 2012-10-29 17:14:03

跟踪了一下。以下函数没有相应
Acad::ErrorStatus dim_xyz1::subGetOsnapPoints (
        AcDb::OsnapMode osnapMode,
        int gsSelectionMark,
        const AcGePoint3d &pickPoint,
        const AcGePoint3d &lastPoint,
        const AcGeMatrix3d &viewXform,
        AcGePoint3dArray &snapPoints,
        AcDbIntArray &geomIds) const
{
                assertReadEnabled () ;
                switch (osnapMode) {
                        case AcDb::kOsModeEnd://端点
                        snapPoints.append(m_line_begin);
                        snapPoints.append(m_line_mid);
                        break;
                }
        return (AcDbEntity::subGetOsnapPoints (osnapMode, gsSelectionMark, pickPoint, lastPoint, viewXform, snapPoints, geomIds)) ;
}

风树 发表于 2012-10-29 18:34:22

知道了:原来的subGetOsnapPoints的参数输错了
应该为:
Acad::ErrorStatusdim_xyz1::subGetOsnapPoints(
                                    AcDb::OsnapMode   osnapMode,
                                 Adesk::GsMarker   gsSelectionMark,
                                    const AcGePoint3d&pickPoint,
                                    const AcGePoint3d&lastPoint,
                                    const AcGeMatrix3d& viewXform,
                                    AcGePoint3dArray&   snapPoints,
                                    AcDbIntArray &   geomIds) const

wykdy 发表于 2014-2-5 18:12:15

也遇到类似问题,感谢分享经验

开箱剁手 发表于 2019-8-12 19:36:35

subGetOsnapPoints返回值改下,改为return Acad::eOK;
页: [1]
查看完整版本: 求助:为什么不能显示自定义实体的夹点,附源码