admin 发表于 2017-4-17 21:57:04

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


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


#include "CAARCCreatePoint.h"
#include "CATIndicationAgent.h"
#include "CATMathPlane.h"

#include "CATCreateExternalObject.h"

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

#include "CATIGSMPoint.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"
#include "CATIGSMLinePtPt.h"

#include "iostream.h"

CATCreateClass( CAARCCreatePoint);


//-------------------------------------------------------------------------
// Constructor
//-------------------------------------------------------------------------
CAARCCreatePoint::CAARCCreatePoint() :
CATStateCommand ("CAARCCreatePoint", CATDlgEngOneShot, CATCommandModeExclusive)
//Valid states are CATDlgEngOneShot and CATDlgEngRepeat
,_Indication(NULL)
{
}

//-------------------------------------------------------------------------
// Destructor
//-------------------------------------------------------------------------
CAARCCreatePoint::~CAARCCreatePoint()
{
   if (_Indication != NULL)
      _Indication->RequestDelayedDestruction();
}


//-------------------------------------------------------------------------
// BuildGraph()
//-------------------------------------------------------------------------
void CAARCCreatePoint::BuildGraph()
{
// TODO: Define the StateChart
// ---------------------------
_Indication = new CATIndicationAgent ("Indication");
_Indication->SetBehavior(CATDlgEngAcceptOnPrevaluate | CATDlgEngWithPrevaluation | CATDlgEngWithUndo );

AddCSOClient(_Indication);
//设置点所在的平面
CATMathPlane PlaneXY;
_Indication -> SetMathPlane (PlaneXY);

CATDialogState * initialState = GetInitialState("创建点");
initialState -> AddDialogAgent (_Indication);

AddTransition( initialState,
               NULL,
               IsOutputSetCondition (_Indication),
               Action ((ActionMethod) &CAARCCreatePoint::ActionOne));
}


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

CATMathPoint Point3D;
CATMathPlane Plane = _Indication->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 spSpecPoint= spPoint;                                       

CATIGSMProceduralView_var spSndPntObj = spSpecPoint;

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

//更新点对象
spSpecPoint->Update();

return TRUE;
}


页: [1]
查看完整版本: Catia二次开发源码分享:鼠标点击创建点