在二次开发过程中,有时需要获取产品树中的约束关系信息,以进行部件关系分析。下列程序片段,可以获取并显示产品
树的约束信息。这里,只给出主程序。
#include "CATBaseUnknown.h"
#include "CATUnicodeString.h"
#include "CATLISTV_CATBaseUnknown.h"
#include "CATBoolean.h"
// ObjectModelerBase Framework
#include "CATDocumentServices.h" // to open and close document
#include "CATSession.h"
#include "CATSessionServices.h" // to open and close session
#include "CATIDocRoots.h" // to identify the root of a document
#include "CATDocument.h" // to work with documents
// CATAssemblyInterfaces Framework
#include "CATIProdDraftingProperties.h" // constraint creation
// ProductStructure Framework
#include "CATIProduct.h"
#include "CATAsmConstraintServices.h"
#include "CATICst.h"
#include "CATIAlias.h"
//-----------------------------------------------------------------
int main (int iArgc, char** iArgv )
{
if ( 2 != iArgc )
{
printf("wrong number of arguments\n");
printf("Correct use: DisplayConstrains InputDocument\n");
return 1; // wrong number of arguments
}
int ReturnCode = 0;
/* --------------------------------------------------------- */
/* 0. Open the CATIA session and open the input Document */
/* --------------------------------------------------------- */
CATSession * pSession = NULL;
HRESULT rc = ::Create_Session( "Sample_Session", pSession );
if ( SUCCEEDED(rc) )
{
CATDocument * pDoc = NULL;
rc = CATDocumentServices::OpenDocument( iArgv[1], pDoc, FALSE );
if ( SUCCEEDED(rc) && NULL != pDoc )
{
CATIProduct * piProductOnRoot = NULL;
CATIDocRoots * piDocRootsOnDoc = NULL;
rc = pDoc->QueryInterface( IID_CATIDocRoots, (void **) & piDocRootsOnDoc );
if ( SUCCEEDED(rc) )
{
CATListValCATBaseUnknown_var * pRootProducts = piDocRootsOnDoc->GiveDocRoots();
if ( NULL != pRootProducts )
{
if ( 0 < pRootProducts->Size() )
{
CATBaseUnknown_var spBU_Root = (*pRootProducts)[1];
if ( NULL_var != spBU_Root )
{
spBU_Root->QueryInterface( IID_CATIProduct,(void **) & piProductOnRoot );
CATListValCATICst_var cstList;
rc=CATAsmConstraintServices::ListConstraints(piProductOnRoot,cstList);
if (SUCCEEDED(rc))
{
CATICst_var cs;
CATCstType type;
printf("%-20s %-15s %-20s %-20s %-20s\n","约束类型","说明","部件1","部件2","部件3");
for (int i=1;i<=cstList.Size();i++)
{
CATListValCATBaseUnknown_var bloc1,bloc2,bloc3;
cs=cstList[i];
cs->GetBlocs(bloc1,bloc2,bloc3);//ListCstAttr();
type=cs->GetCstType();
CATUnicodeString description="",cstType="";
switch(type)
{
case CstType_Reference:cstType="CstType_Reference";description="Fix";break;
case CstType_Distance:cstType="CstType_Distance";description="Distance";break;
case CstType_On:cstType="CstType_On";description="Coincidence";break;
case CstType_Concentric:cstType=" CstType_Concentric";description="Concentricity";break;
case CstType_Tangent:cstType="CstType_Tangent";description="Tangency";break;
case CstType_Length:cstType="CstType_Length";description="Length";break;
case CstType_Angle:cstType="CstType_Angle";description="Angle";break;
case CstType_AnglePlanar:cstType="CstType_AnglePlanar";description="Angle with axix";break;
case CstType_Parallel:cstType="CstType_Parallel";description="Parallelism_2";break;
case CstType_ParallelAxis:cstType="CstType_ParallelAxis";description="Parallelism_1";break;
case CstType_Perpend:cstType="CstType_Perpend";description="Perpendicularity_2";break;
case CstType_PerpendAxis:cstType="CstType_PerpendAxis";description="Perpendicularity_1";break;
case CstType_Radius:cstType="CstType_Radius";description="Radius or Diameter";break;
case CstType_Symmetry:cstType="CstType_Symmetry";description="Symmetry";break;
case CstType_Equidistant:cstType="CstType_Equidistant";description="Equidistant point";break;
case CstType_Chamfer:cstType="CstType_Chamfer";description="Chamfer_3";break;
case CstType_ChamferPerpend:cstType="CstType_ChamferPerpend";description="Chamfer_2";break;
case CstType_DistanceDirected:cstType="CstType_DistanceDirected";description="Distanc along a direction";break;
case CstType_AngleInternal:cstType="CstType_AngleInternal";description="Angle_1";break;
case CstType_StContinuity:cstType="CstType_StContinuity";description="FreeFrom Continuity";break;
case CstType_StDistance:cstType="CstType_StDistance";description="FreeFormDistance";break;
case CstType_SdContinuity:cstType="CstType_SdContinuity";description="Styling Continuity";break;
case CstType_SdShape:cstType="CstType_SdShape";description="Styling Shape";
}
CATUnicodeString bloclElements="",bloc2Elements="",bloc3Elements="";
if (bloc1!=NULL)
for (int j=1;j<=bloc1.Size();j++)
{
CATIAlias_var as;
as=bloc1[j];
if (as!=NULL_var)
bloclElements.Append(as->GetAlias());
}
if (bloc2!=NULL)
for (int j=1;j<=bloc2.Size();j++)
{
CATIAlias_var as;
as=bloc2[j];
if (as!=NULL_var)
bloc2Elements.Append(as->GetAlias());
}
if (bloc3!=NULL)
for (int j=1;j<=bloc3.Size();j++)
{
CATIAlias_var as;
as=bloc3[j];
if (as!=NULL_var)
bloc3Elements.Append(as->GetAlias());
}
printf("%-20s %-15s %-20s %-20s %-20s\n",cstType.ConvertToChar(),description.ConvertToChar(), bloclElements.ConvertToChar(),bloc2Elements.ConvertToChar(),bloc3Elements.ConvertToChar());
}
printf("\nToal %d Constraints\n",cstList.Size());
} else printf("GetRootSet Failed %d\n",rc);
}
}
delete pRootProducts;
pRootProducts = NULL;
}
// releasing useless data
piDocRootsOnDoc->Release();
piDocRootsOnDoc = NULL;
}
else
{
printf(" -> failed\n");
ReturnCode = 3;
}
//-----------------------------------------
// 5 - End the session
// -----------------------------------------
rc = CATDocumentServices::Remove (*pDoc);
if ( FAILED(rc) )
{
printf(" -> failed\n");
ReturnCode = 7;
}
}
else
{
printf(" -> failed\n");
ReturnCode = 2;
}
// delete the session.
rc = ::Delete_Session("CAA2_Sample_Session");
if ( FAILED(rc) )
{
printf(" -> failed\n");
ReturnCode = 8;
}
}
else
{
printf(" -> failed\n");
ReturnCode = 2;
}
return ReturnCode;
}
下面是对一个Product文件的分析结果:
约束类型 说明 部件1 部件2 部件3
CstType_On Coincidence ATT-1.1 Main.1
CstType_Distance Distance ATT-1.1 Main.1
CstType_Tangent Tangency ATT-2.1 ATT-1.1
Toal 3 Constraints