Catia CAA 二次开发----batch program

View Code
#include <iostream.h>
#include <math.h>

//System
#include "CATUnicodeString.h"

//ObjectModelerBase
#include "CATDocumentServices.h"
#include "CATSession.h" 
#include "CATSessionServices.h" 
#include "CATDocument.h"
#include "CATIContainer.h"

//ObjectSpecsModeler
#include "CATISpecObject.h"

//PartInterfaces
#include "CATIShaft.h"
#include "CATIPad.h"
#include "CATIPrtFactory.h"

//SketcherInterfaces
#include "CATISketch.h"
#include "CATI2DWFFactory.h"
#include "CATISketchFactory.h"

//MecModInterfaces
#include "CATIContainerOfDocument.h"

//Mathematics
#include "CATMath.h"

int main(int argc, char ** argv) 
{
    //--------------------------------------------------------------------
    // Check Argument
    //--------------------------------------------------------------------
    cout << " -- Check Argument --" << endl << flush;
    if ( argc < 2)
    {
        cout << "    ERROR  : Pass file name as argument" << endl;
        return 1;
    }
    else  cout << "    Argument Checked OK." << endl << flush;

    //--------------------------------------------------------------------
    // Variable declarations
    //--------------------------------------------------------------------
    //char *pFileName = "TSTScrewResult";
    char *pFileName     = argv[1];
    
    //------------------
    // Create a Session
    //------------------
    cout << " -- Create a Session --" << endl;
    char *sessionName = "TSTScrewSession";
    CATSession *pSession = NULL;
    HRESULT hr = ::Create_Session(sessionName, pSession);
    if ((FAILED(hr)) || (NULL == pSession))
    {
        cout << "ERROR in creating session" << endl << flush;
        return 2;
    } 
    
    //------------------
    // Create a Document
    //------------------
    CATDocument *pDocument = NULL;
    hr = CATDocumentServices::New("Part", pDocument);

    if( (FAILED(hr)) || (pDocument == NULL) )
    {
        cout << "ERROR in Create New Document" << endl;
        return 3;
    }
    else
    {
        cout << "---->OK" << endl;
    }
    //---------------------------------------------//
    //    1 - Retrieve Specification container     //
    //---------------------------------------------// 
    cout << " -- Retrieve Specification container --" << endl;
    //retrieve a CATIContainerOfDocument interface on the CATPart document
    CATIContainerOfDocument * piContainerOfDoc = NULL;
    hr = pDocument->QueryInterface(IID_CATIContainerOfDocument, (void**) &piContainerOfDoc);
    if (FAILED(hr) || (piContainerOfDoc == NULL)) 
    {
        cout<< "Cannot find interface CATIContainerOfDocument on CATPart" <<endl;
        return 4;
    }

    //with this interface, retrieve the specification container
    //Note: the specification container (CATPrtCont), contains the mechanical features 
    // of a document
    CATIContainer *piSpecContainer = NULL;
    hr = piContainerOfDoc->GetSpecContainer(piSpecContainer);
    if (FAILED(hr) || (piSpecContainer == NULL)) 
    {
        cout<< "Cannot retrieve Specification container " <<endl;
        return 5;
    }
    piContainerOfDoc->Release();piContainerOfDoc=NULL;

    //----------------------------------//
    //    2 - Retrieve Factories            //
    //----------------------------------//
    cout << " -- Retrieve Factories --" << endl;
    //  a - Retrieve the Sketcher Factory
    //        this factory allow us to create sketch feature 
    CATISketchFactory *piSketchFactory = NULL;
    hr = piSpecContainer->QueryInterface (IID_CATISketchFactory, (void **) &piSketchFactory );
    if (FAILED(hr) || (NULL == piSketchFactory)) 
    {
        cout<<"failed to get interface CATISketchFactory from spec. container"<<endl;
        return 5;
    }

    //  b - Retrieve the Part Factory
    CATIPrtFactory *piPrtFactory = NULL;
    hr = piSpecContainer->QueryInterface(IID_CATIPrtFactory, (void**) &piPrtFactory);
    if (FAILED(hr) || (NULL == piPrtFactory)) 
    {
        cout<<"failed to get interface CATIPrtFactory from spec. container"<<endl;
        return 6;
    }
    piSpecContainer->Release();piSpecContainer=NULL;

    //----------------------------------//
    //    3 - Create Sketch for Rod       //
    //----------------------------------//
    cout << " -- Create Sketch for Rod --" << endl;
    double HeadHeight = 11;
    double HeadRadius = 10;

    //  a - Create Head Sketch
    //  Define Origine and main vector
    double origin[3] = {0,0,0};
    double xAxis[3] = {1,0,0};
    double yAxis[3] = {0,1,0};
    double zAxis[3] = {0,0,1};

    CATISpecObject_var spSpecOnSketch = piSketchFactory->CreateSketch(origin, xAxis, yAxis);
    if (NULL_var == spSpecOnSketch) 
    {
        cout<<"failed to create Sketch feature"<<endl;
        return 7;
    }
    //  b - Open Edition
    CATISketch *piSketch = NULL;
    hr = spSpecOnSketch->QueryInterface(IID_CATISketch, (void **)&piSketch);
    if (FAILED(hr) || (NULL == piSketch)) 
    {
        cout <<"failed to get interface CATISketch on sketch" <<endl;
        return 8;
    }
    piSketch->OpenEdition();
    
    //  c - Retrieve 2DFactory
    CATI2DWFFactory *piFactoryOnSketch = NULL;
    hr = spSpecOnSketch->QueryInterface(IID_CATI2DWFFactory, (void **)&piFactoryOnSketch);
    if (FAILED(hr) || (NULL == piFactoryOnSketch)) 
    {
        cout <<"failed to get interface CATI2DWFFactory on sketch" <<endl;
        return 9;
    }

    //  d - Create element
    double P1[3] = {HeadRadius,        0,                        0};
    double P2[3] = {HeadRadius/2.,    HeadRadius*CATSqrt(3.)/2.,0};
    double P3[3] = {-HeadRadius/2.,    HeadRadius*CATSqrt(3.)/2.,0};
    double P4[3] = {-HeadRadius,    0,                        0};
    double P5[3] = {-HeadRadius/2.,-HeadRadius*CATSqrt(3.)/2.,0};
    double P6[3] = {HeadRadius/2., -HeadRadius*CATSqrt(3.)/2.,0};
    
    piFactoryOnSketch->CreateLine(P1, P2);
    piFactoryOnSketch->CreateLine(P2, P3);
    piFactoryOnSketch->CreateLine(P3, P4);
    piFactoryOnSketch->CreateLine(P4, P5);
    piFactoryOnSketch->CreateLine(P5, P6);
    piFactoryOnSketch->CreateLine(P6, P1);

    //  e - close Edition
    piSketch->CloseEdition();

    //----------------------------------//
    //    4 - Create a Pad                //
    //----------------------------------//
    cout << " -- Create a Pad --" << endl;
    //  a - Create a Pad
    CATISpecObject_var spSpecOnPad = piPrtFactory->CreatePad(spSpecOnSketch);
    if (NULL_var == spSpecOnPad) 
    {
        cout<<"failed to create Pad feature"<<endl;
        return 10;
    }
    //  b - Modify Parameter
    CATIPad *piPad = NULL;
    hr = spSpecOnPad->QueryInterface(IID_CATIPad, (void **)&piPad);
    if (FAILED(hr) || (NULL == piPad)) 
    {
        cout<<"failed to get interface CATIPad from spSpecOnPad"<<endl;
        return 11;
    }
    
    piPad->ModifyEndOffset(HeadHeight);
    piPad->ReverseDirection();
    //  c - Update
    spSpecOnPad->Update();
    
    
    
    //releases
    piSketch->Release();piSketch=NULL;     
    piFactoryOnSketch->Release();piFactoryOnSketch=NULL;     
    piPad->Release();piPad=NULL;


    //----------------------------------//
    //    5 - Create a Sketch for Head    //
    //----------------------------------//
    cout << " -- Create a Sketch for Head --" << endl;
    double RodRadius = 5;
    double RodLength = 45;

    //  a - Create a Sketch
    CATISpecObject_var spSpecOnSketchForShaft = piSketchFactory->CreateSketch(origin, zAxis, xAxis);
    if (NULL_var == spSpecOnSketchForShaft) 
    {
        cout<<"failed to create Sketch feature"<<endl;
        return 11;
    }
    
    //  b - Open Edition
    CATISketch_var spiSketch = spSpecOnSketchForShaft;
    if (NULL_var == spiSketch) 
    {
        cout <<"failed to get interface CATISketch on sketch" <<endl;
        return 12;
    }
    spiSketch->OpenEdition();
    
    //  c - Retrieve 2DFactory
    CATI2DWFFactory_var spiFactoryOnSketch = spSpecOnSketchForShaft;
    if (NULL_var == spiFactoryOnSketch) 
    {
        cout <<"failed to get interface CATI2DWFFactory on sketch" <<endl;
        return 13;
    }
    
    //  d - Create element
    double M1[3] = {0,            0,                0};
    double M2[3] = {0,            RodRadius,        0};
    double M3[3] = {RodLength,    RodRadius,        0};
    double M4[3] = {RodLength,    0,                0};

    spiFactoryOnSketch->CreateLine(M1,M2);
    spiFactoryOnSketch->CreateLine(M2,M3);
    spiFactoryOnSketch->CreateLine(M3,M4);
    spiFactoryOnSketch->CreateLine(M4,M1);

    spiFactoryOnSketch->CreateCenterLine(M1,M4);            //轴线

    //  e - close Edition
    spiSketch->CloseEdition();

    //----------------------------------//
    //    6 - Create a Shaft              //
    //----------------------------------//
    cout << " -- Create a Shaft --" << endl;
    CATISpecObject_var spSpecOnShaft = piPrtFactory->CreateShaft(spSpecOnSketchForShaft);
    if (NULL_var == spSpecOnShaft) 
    {
        cout<<"failed to create Shaft feature"<<endl;
        return 14;
    }
    //  b - Modify Parameter
    CATIShaft_var spiShaft = spSpecOnShaft;
    if (NULL_var == spiShaft) 
    {
        cout <<"failed to get interface CATIShaft on the shaft" <<endl;
        return 15;
    }
    spiShaft->ModifyEndAngle(360.);
    spiShaft->ModifyStartAngle(0.);
    
    //  c - Update
    spSpecOnShaft->Update();
    
    // Release pointers
    piSketchFactory->Release(); piSketchFactory=NULL;
    piPrtFactory->Release(); piPrtFactory=NULL;
    
    //------------------
    // Save a Document
    //------------------
    cout << " -- Save document --" << endl;
    // Write the document under the path specified by pFileName
    hr = CATDocumentServices::SaveAs(*pDocument, pFileName);
    if( (FAILED(hr)) || (pDocument == NULL) )
    {
        cout << "ERROR in saving Document" << endl;
        return 4;
    }
    else
    {
        cout << "---->OK" << endl;
    }

    //----------------
    // Delete Session
    //----------------
    cout << " -- Delete the Session --" << endl;
    pSession->Delete_Session(sessionName);
    return 0;
}

 

需要在Framework->Module中使用,导入相应的CAT模块。

 

代码说明:

 1.Create a Session

   use the global function :: Create_Session

 2.Create a Document

    use the static method CATDocumentServices:New

 3.Create sketches, a pad and a shaft

 4.Save the Document

 5.Delete the session

你可能感兴趣的:(Catia CAA 二次开发----batch program)