admin 发表于 2017-4-17 21:56:09

Catia二次开发源码分享: 通过鼠标点击创建直线


Catia二次开发源码分享: 通过鼠标点击创建直线



#include "CAARCCreateLine.h"
#include "CATIndicationAgent.h"
#include "CATMathPlane.h"

#include "CATCreateExternalObject.h"

#include "CATMathPoint2D.h"
#include "CATMathPoint.h"
#include "CATMathPlane.h"

#include "CATIGSMPoint.h"
#include "CATIGSMLinePtPt.h"

#include "CATFrmEditor.h"
#include "CATPathElement.h"

#include "CATIProduct.h"
#include "CATILinkableObject.h"
#include "CATDocument.h"

#include "CATIContainerOfDocument.h"

#include "CATIGSMProceduralView.h"

#include "CATIContainer.h"
#include "CATIGSMFactory.h"

#include "CATISpecObject.h"
CATCreateClass( CAARCCreateLine);


//-------------------------------------------------------------------------
// Constructor
//-------------------------------------------------------------------------
CAARCCreateLine::CAARCCreateLine() :
CATStateCommand ("CAARCCreateLine", CATDlgEngOneShot, CATCommandModeExclusive)
//Valid states are CATDlgEngOneShot and CATDlgEngRepeat
,_IndicationOnePoint(NULL),_IndicationTwoPoint(NULL)
{
}

//-------------------------------------------------------------------------
// Destructor
//-------------------------------------------------------------------------
CAARCCreateLine::~CAARCCreateLine()
{
   if (_IndicationOnePoint != NULL)
      _IndicationOnePoint->RequestDelayedDestruction();
   if (_IndicationTwoPoint != NULL)
   _IndicationTwoPoint->RequestDelayedDestruction();
}


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


// TODO: Define the StateChart
// ---------------------------
_IndicationOnePoint = new CATIndicationAgent ("IndicationOnePoint");
AddCSOClient(_IndicationOnePoint);

_IndicationTwoPoint = new CATIndicationAgent ("IndicationTwoPoint");

CATMathPlane PlaneXY;
_IndicationOnePoint -> SetMathPlane (PlaneXY);
_IndicationTwoPoint -> SetMathPlane (PlaneXY);

CATDialogState * initialStateOne = GetInitialState("选择第一个点位置");
initialStateOne -> AddDialogAgent (_IndicationOnePoint);

CATDialogState * initialStateTwo = AddDialogState("选择第二个点位置");
initialStateTwo -> AddDialogAgent (_IndicationTwoPoint);

AddTransition( initialStateOne,
               initialStateTwo,
               IsOutputSetCondition (_IndicationOnePoint),
               Action ((ActionMethod) &CAARCCreateLine::ActionOne));

AddTransition( initialStateTwo,
                NULL,
                IsOutputSetCondition (_IndicationTwoPoint),
                Action ((ActionMethod) &CAARCCreateLine::ActionTwo));
}


//-------------------------------------------------------------------------
// ActionOne ()
//-------------------------------------------------------------------------
CATBoolean CAARCCreateLine::ActionOne( void *data )
{
// TODO: Define the action associated with the transition
// ------------------------------------------------------
CATMathPoint2D point2D = _IndicationOnePoint->GetValue();//获得一个2D的点

CATMathPoint Point3D;
CATMathPlane Plane = _IndicationOnePoint->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);

spSpecPoint1= spPoint;                                       

CATIGSMProceduralView_var spSndPntObj = spSpecPoint1;

//*将点显示在屏幕上
spSndPntObj->InsertInProceduralView();

spSpecPoint1->Update();

return TRUE;
}

//-------------------------------------------------------------------------
// ActionTwo ()
//-------------------------------------------------------------------------
CATBoolean CAARCCreateLine::ActionTwo( void *data )
{
// TODO: Define the action associated with the transition
// ------------------------------------------------------
CATMathPoint2D point2D = _IndicationTwoPoint->GetValue();//获得一个2D的点

CATMathPoint Point3D;
CATMathPlane Plane = _IndicationTwoPoint->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二次开发源码分享: 通过鼠标点击创建直线