admin 发表于 2017-5-6 14:18:38

CATIA二次开发入门教程---16 通过点击屏幕创建线

CATIA二次开发入门教程---16 通过点击屏幕创建线

和上面例子类似,这里需要手动添加一些action事件,在头文件里面新增加一个 indication,效果如下


代码如下:


//-------------------------------------------------------------------------
// Constructor
//-------------------------------------------------------------------------
CreateLineCmd::CreateLineCmd() :
CATStateCommand ("CreateLineCmd", CATDlgEngOneShot, CATCommandModeExclusive)
//Valid states are CATDlgEngOneShot and CATDlgEngRepeat
,_IndicationPoint1(NULL) ,_IndicationPoint2(NULL)
{
}
//-------------------------------------------------------------------------
// Destructor
//-------------------------------------------------------------------------
CreateLineCmd::~CreateLineCmd()
{
   if (_IndicationPoint1!= NULL)
      _IndicationPoint1->RequestDelayedDestruction();
   if (_IndicationPoint2!= NULL)
    _IndicationPoint2->RequestDelayedDestruction();

}

//-------------------------------------------------------------------------
// BuildGraph()
//-------------------------------------------------------------------------
void CreateLineCmd::BuildGraph()
{

// TODO: Define the StateChart
// ---------------------------
_IndicationPoint1 = new CATIndicationAgent ("IndicationOne");
AddCSOClient(_IndicationPoint1);
_IndicationPoint2 = new CATIndicationAgent ("IndicationTwo");
CATMathPlane PlaneXY;
_IndicationPoint1 -> SetMathPlane (PlaneXY);
_IndicationPoint2 -> SetMathPlane (PlaneXY);
CATDialogState * initialState1 = GetInitialState("select first point");
initialState1 -> AddDialogAgent (_IndicationPoint1);

CATDialogState * initialState2 = AddDialogState("select second point"); // add important
initialState2 -> AddDialogAgent (_IndicationPoint2);


AddTransition( initialState1, initialState2,
               IsOutputSetCondition (_IndicationPoint1),
               Action ((ActionMethod) &CreateLineCmd::ActionOne));

AddTransition( initialState2, NULL,
   IsOutputSetCondition (_IndicationPoint2),
   Action ((ActionMethod) &CreateLineCmd::ActionTwo));
}

//-------------------------------------------------------------------------
// ActionOne ()
//-------------------------------------------------------------------------
CATBoolean CreateLineCmd::ActionOne( void *data )
{
// TODO: Define the action associated with the transition
// ------------------------------------------------------
CATMathPoint2D point2D = _IndicationPoint1->GetValue();//获得一个2D的点
CATMathPoint Point3D;
CATMathPlane Plane = _IndicationPoint1->GetMathPlane();
Plane.EvalPoint(point2D.GetX(),point2D.GetY(),Point3D);//将2D点转换为3D点
//设置Container(非根节点)
//获得Editor
CATFrmEditor* pEditor = CATFrmEditor::GetCurrentEditor();
//得到当前对象的文档
CATDocument * pDocument = NULL ;
//取得当前活动对象
CATPathElement activePath = pEditor->GetUIActiveObject();
//取得当前活动的product
CATIProduct *pActiveProduct = (CATIProduct *)activePath.SearchObject(CATIProduct::ClassName());
//当前活动对象不存在
if (pActiveProduct == NULL)
{
    pDocument = pEditor->GetDocument();
}
else
{
    CATIProduct_var spRef = pActiveProduct->GetReferenceProduct();
    //当前对象的引用对象是否存在
    if ( NULL_var == spRef )
    {
      return FALSE;
    }
    //当前对象的链接对象
    CATILinkableObject * piLinkableObject = NULL;
    HRESULT rc = spRef->QueryInterface( IID_CATILinkableObject, (void**)& piLinkableObject );                           
    if ( FAILED(rc) )
    {
      piLinkableObject->Release();
      piLinkableObject = NULL ;
      return FALSE;
    }
    //得到当前对象的文档
    pDocument = piLinkableObject->GetDocument();
    piLinkableObject->Release();
    piLinkableObject = NULL ;
    if ( NULL == pDocument)
    {
      return FALSE;
    }
}
//得到文档容器集
CATIContainerOfDocument * pIContainerOfDocument = NULL;
HRESULT rc = pDocument->QueryInterface(IID_CATIContainerOfDocument, (void**)&pIContainerOfDocument);
if (FAILED(rc))
{
    //pIContainerOfDocument->Release();
    pIContainerOfDocument = NULL ;
    return FALSE;
}
//获得Document
CATIContainer* _pContainer = NULL;
//获得SpecContainer
HRESULT hr = pIContainerOfDocument->GetSpecContainer(_pContainer);
cout << "container ok" <<endl;

//GSM工厂
CATIGSMFactory_var spGSMFactory = NULL_var;
//设置工厂
spGSMFactory = _pContainer;         
CATIGSMPoint_var spPoint = spGSMFactory->CreatePoint(Point3D);
spSpecPoint1= spPoint;   
CATIGSMProceduralView_var spSndPntObj = spSpecPoint1;
//*将点显示在屏幕上
spSndPntObj->InsertInProceduralView();
cout << "create point ok" <<endl;
spSpecPoint1->Update();
cout << "update point ok" <<endl;
return TRUE;
}



CATBoolean CreateLineCmd::ActionTwo( void *data )
{
// TODO: Define the action associated with the transition
// ------------------------------------------------------
    CATMathPoint2D point2D = _IndicationPoint2->GetValue();//获得一个2D的点
CATMathPoint Point3D;
CATMathPlane Plane = _IndicationPoint2->GetMathPlane();
Plane.EvalPoint(point2D.GetX(),point2D.GetY(),Point3D);//将2D点转换为3D点
//设置Container(非根节点)
//获得Editor
CATFrmEditor* pEditor = CATFrmEditor::GetCurrentEditor();
//得到当前对象的文档
CATDocument * pDocument = NULL ;
//取得当前活动对象
CATPathElement activePath = pEditor->GetUIActiveObject();
//取得当前活动的product
CATIProduct *pActiveProduct = (CATIProduct *)activePath.SearchObject(CATIProduct::ClassName());
//当前活动对象不存在
if (pActiveProduct == NULL)
{
    pDocument = pEditor->GetDocument();
}
else
{
    CATIProduct_var spRef = pActiveProduct->GetReferenceProduct();
    //当前对象的引用对象是否存在
    if ( NULL_var == spRef )
    {
      return FALSE;
    }
    //当前对象的链接对象
    CATILinkableObject * piLinkableObject = NULL;
    HRESULT rc = spRef->QueryInterface( IID_CATILinkableObject, (void**)& piLinkableObject );                           
    if ( FAILED(rc) )
    {
      piLinkableObject->Release();
      piLinkableObject = NULL ;
      return FALSE;
    }
    //得到当前对象的文档
    pDocument = piLinkableObject->GetDocument();
    piLinkableObject->Release();
    piLinkableObject = NULL ;
    if ( NULL == pDocument)
    {
      return FALSE;
    }
}
//得到文档容器集
CATIContainerOfDocument * pIContainerOfDocument = NULL;
HRESULT rc = pDocument->QueryInterface(IID_CATIContainerOfDocument, (void**)&pIContainerOfDocument);
if (FAILED(rc))
{
    //pIContainerOfDocument->Release();
    pIContainerOfDocument = NULL ;
    return FALSE;
}
//获得Document
CATIContainer* _pContainer = NULL;
//获得SpecContainer
HRESULT hr = pIContainerOfDocument->GetSpecContainer(_pContainer);

//GSM工厂
CATIGSMFactory_var spGSMFactory = NULL_var;
//设置工厂
spGSMFactory = _pContainer;         
CATIGSMPoint_var spPoint = spGSMFactory->CreatePoint(Point3D);
CATISpecObject_var spSpecPoint2= spPoint;   
CATIGSMProceduralView_var spSndPntObj = spSpecPoint2;
//*将点显示在屏幕上
spSndPntObj->InsertInProceduralView();
spSpecPoint2->Update();
//生成线
CATIGSMLinePtPt_var spLine = spGSMFactory->CreateLine(spSpecPoint1, spSpecPoint2, NULL_var);
CATISpecObject_var spSpecLine= spLine;   
CATIGSMProceduralView_var spSndPntObjLine = spSpecLine;
//*将线显示在屏幕上
spSndPntObjLine->InsertInProceduralView();
spSpecLine->Update();

return TRUE;
}





页: [1]
查看完整版本: CATIA二次开发入门教程---16 通过点击屏幕创建线