DATAKIT API  V2025.1
How To Use Catia V5 Writer APIs for CATProduct writing
You MUST ALWAYS close a Product/Component context - by calling catiav5w::EndProduct() or catiav5w::EndVirtualComponent() - before opening another context.
catiav5w::InitProduct() / catiav5w::InitVirtualComponent() CAN'T be nested !!!

the Document ID Notion

The main idea of the Assemblies CATIA V5 Writing is to add instances of a previously written Document.
For this, DATAKIT APIs introduce the Document ID notion.
The first thing to do is to create Document Reference (DocID) giving a file fullpath and a reference name.
It will result to a Document ID that you can use easily to add an instance into a CATProduct.

The CATProduct writing step-by-step.

  • First create Document IDs by catiav5w::CreateCGRDocId or catiav5w::CreateV4ModelDocId:
    Dtk_string CATPartFileName = L"C:\\sample_files\\catiav5\\V5_sample.CATPart";
    Dtk_transfo TransformationMatrix;
    //First you have to initialize the CATIA V5 writer
    catiav5w::InitWrite(L"C:\\LogFile.log", NULL);
    //The ProcessPart is an user implemented function to create a part
    Dtk_UUID PartUUID = ProcessPart(inOutputFile, L"essaiPart");
    Dtk_Size_t PartID, V4ID, CgrID, ProdID,ProdID2, ProdID3;
    //We create Document Reference to the CATPart file
    catiav5w::CreatePartDocId(inOutputFile, L"PartFile", PartUUID, PartID);
    //...and to a CGR file
    catiav5w::CreateCGRDocId("../SampleFiles/cgr/Engine.cgr", "CGRFile", CgrID);
    //...and finally to a CATIA V4 '.model' file
    catiav5w::CreateV4ModelDocId("../SampleFiles/v4/EngineComponent.model", "V4File", V4ID);
    These functions create Reference link to an existing file. For CATIA V4 files, you can only reference '.model' files.
    At this time the CatiaV5 Writer can only reference CATPart written by the writer.
    The writer can't handle external CATPart references.
    So, to reference a CATPart file, you have to provide an additional parameter.
    This parameter is the CATPart UUID and it is given by the catiav5w::InitPart function. So you can call the CreatePartDocId function:
  • Once you have created the Part DocIds, you can create CATProduct file. For this you have to create a CATProduct context with the catiav5w::InitProduct.
    //we init a CATProduct context...
    catiav5w::InitProduct( L"..\\SampleFiles\\dtk\\SubProduct.CATProduct", L"SubProduct");
  • Once the CATProduct context is created, you can easily add instances into it by the catiav5w::AddInstance function.
    //...to insert a Part instance
    TransformationMatrix.setOrigin( 0.0,0.0,0.0);
    catiav5w::AddInstance(PartID, L"PartInstance", TransformationMatrix);
    //...a CGR instance
    TransformationMatrix.setOrigin( 0.0,500.0,0.0);
    catiav5w::AddInstance(CgrID, L"CGRInstance", TransformationMatrix);
    //...and a V4 instance
    TransformationMatrix.setOrigin( 0.0,-500.0,0.0);
    catiav5w::AddInstance(V4ID, L"V4Instance", TransformationMatrix);
  • Finally you have to ends the CATProduct context - to explicitely write the CATProduct file - by the catiav5w::EndProduct function. This function will give you a resulting DocID to insert the CATProduct into multi level Assemblies.
    //we close the CATProduct context and retrieve the resulting DocID

Referencing existing files from a CATProduct

You can reference existing or previously created files into a CATProduct for theses extensions:

File Extension Function
.CATPart catiav5w::CreatePartDocId (const Dtk_string &inPartFileName, const Dtk_string &inPartName, Dtk_ID &outDocId)
.CATProduct catiav5w::CreateProductDocId
.cgr catiav5w::CreateCGRDocId
.model - V4 file - catiav5w::CreateV4ModelDocId

The Virtual Component writing step-by-step.

The most important to know is that a virtual component ALWAYS belongs to a CATProduct context.
So you have to cal catiav5w::InitProduct prior calling catiav5w::InitVirtualComponent.
Moreover you can add a virtual component instance only into the same product context that his creation.
So you can only call catiav5w::AddVirtualComponentInstance prior the catiav5w::EndProduct call.
This piece of code is correct:

//we init a CATProduct context...
catiav5w::InitProduct( SubProductFileName, "SubProduct");
//we start a virtual component prototype
catiav5w::InitVirtualComponent("VirtualSubComponent1");
TransformationMatrix.setOrigin(0,0,150);
catiav5w::AddInstance(PartID, L"PartInstance", TransformationMatrix);
//we end the component prototype
//now we had the component instance into the CATProduct
catiav5w::AddVirtualComponentInstance(CompID1, L"CompInstance", TransformationMatrix);
//we close the CATProduct context and retrieve the resulting DocID
...

...whereas this piece of code is wrong:


...
//we init a CATProduct context...
catiav5w::InitProduct( SubProductFileName, "SubProduct");
//we start a virtual component prototype
catiav5w::InitVirtualComponent("VirtualSubComponent1");
TransformationMatrix.setOrigin(0,0,150);
catiav5w::AddInstance(PartID, L"PartInstance", TransformationMatrix);
//we end the component prototype
//we close the CATProduct context and retrieve the resulting DocID
catiav5w::AddVirtualComponentInstance(CompID1, L"CompInstance", TransformationMatrix); //ERROR: Component instanciation is not it the same context as its creation...
catiav5w::InitVirtualComponent("SubComponent2"); //ERROR this virtual component doesn't belong to any product
catiav5w::AddInstance(PartID, L"PartInstance", TransformationMatrix);

Full Sample.

{
//You init the Part writer
Dtk_UUID PartUUID = catiav5w::InitPart( inOutputFile, inReferenceName );
//you create a Geometrical Set
//...and you insert a point
//Finally you close the Geometrical Set...
//...and the Part writer
//you return the Part UUID
return PartUUID;
}
void ProcessAsm()
{
Dtk_string CATPartFileName = L"..\\SampleFiles\\dtk\\V5_sample.CATPart";
Dtk_string CGRFileName = L"..\\SampleFiles\\cgr\\Engine.cgr";
Dtk_string V4ModelFileName = L"..\\SampleFiles\\v4\\EngineComponent.model";
Dtk_string SubProductFileName = L"..\\SampleFiles\\dtk\\SubProduct.CATProduct";
Dtk_string RootProductFileName = L"..\\SampleFiles\\dtk\\RootProduct.CATProduct";
Dtk_transfo TransformationMatrix;
//First you have to initialize the CATIA V5 writer
catiav5w::InitWrite(L"..\\SampleFiles\\dtk\\V5W_LogFile.log", NULL);
//The ProcessPart is an user implemented function to create a part
Dtk_UUID PartUUID = ProcessPart(CATPartFileName, L"testPart");
Dtk_ID PartID, V4ID, CgrID, ProdID,ProdID2, CompID1, CompID2;
//We create Doceument References to the CATPart file
catiav5w::CreatePartDocId(CATPartFileName, L"PartFile", PartUUID, PartID);
//...and to a CGR file
catiav5w::CreateCGRDocId( CGRFileName, "CGRFile", CgrID);
//...and finally to a CATIA V4 '.model' file
catiav5w::CreateV4ModelDocId( V4ModelFileName, "V4File", V4ID);
//we init a CATProduct context...
catiav5w::InitProduct( SubProductFileName, "SubProduct");
//we start a virtual component prototype
catiav5w::InitVirtualComponent("VirtualSubComponent1");
TransformationMatrix.setOrigin(0,0,150);
catiav5w::AddInstance(PartID, L"PartInstance", TransformationMatrix);
//we end the component prototype
//we start another virtual component prototype
catiav5w::InitVirtualComponent("VirtualComponent1");
//we add the sub component instance...
TransformationMatrix.setOrigin( 0.0,0.0,0.0);
catiav5w::AddVirtualComponentInstance(CompID1, L"VirtualSubComponent1_Instance1", TransformationMatrix );
//we close the component prototype
//we insert a Part instance...
TransformationMatrix.setOrigin( 0.0,0.0,0.0);
catiav5w::AddInstance(PartID, L"PartInstance", TransformationMatrix);
//...a CGR instance
TransformationMatrix.setOrigin( 0.0,500.0,0.0);
catiav5w::AddInstance(CgrID, L"CGRInstance", TransformationMatrix);
//...and a V4 instance
TransformationMatrix.setOrigin( 0.0,-500.0,0.0);
catiav5w::AddInstance(V4ID, L"V4Instance", TransformationMatrix);
//...and the main component instance
TransformationMatrix.setOrigin( 0.0,1000.0,0.0);
catiav5w::AddVirtualComponentInstance(CompID2, L"VirtualComponent1_Instance1", TransformationMatrix);
//we close the CATProduct context and retrieve the resulting DocID
//we create another CATProduct context
catiav5w::InitProduct( RootProductFileName, "RootProduct");
//and we insert another time the a part instance - multinstancing management -
TransformationMatrix.setOrigin( 0.0,0.0,50.0);
catiav5w::AddInstance(PartID, L"PartInstance2", TransformationMatrix);
//and two instances to the first CATProduct file
TransformationMatrix.setOrigin( 1000.0,0.0,0.0);
catiav5w::AddInstance(ProdID2, L"SubProductInstance", TransformationMatrix);
TransformationMatrix.setOrigin( -1000.0,0.0,0.0);
catiav5w::AddInstance(ProdID2, L"SubProductInstance", TransformationMatrix);
//finally we close the second CATProduct...
//...and the Main writter
}

Here a snapshot of the resulting V5 Assembly (from Full Sample)

ProcessAsm
Dtk_ErrorStatus ProcessAsm(const Dtk_string &inResultDirectory)
Definition: testlibcatiav5write.cpp:350
Dtk_ID
uint32_t Dtk_ID
Definition: define.h:689
Dtk_transfo
This is the Transformation dedicated class.
Definition: dtk_transfo.hpp:19
catiav5w::InitVirtualComponent
Dtk_ErrorStatus InitVirtualComponent(const Dtk_string &inReferenceName, const catiav5w::FileDescription &inFileDescription=catiav5w::FileDescription())
Initialize a virtual component during CATProduct process.
catiav5w::AddVirtualComponentInstance
Dtk_ErrorStatus AddVirtualComponentInstance(const Dtk_ID &inDocId, const Dtk_string &inInstanceName, const Dtk_transfo &inTransfo, Dtk_ID &outInstanceId)
Add an instance to a DocID into the Current (Sub/Root) CATProduct.
Dtk_transfo::setOrigin
void setOrigin(const Dtk_pnt &O)
Set a new O center point.
catiav5w::NodeTypeGeometricSet
@ NodeTypeGeometricSet
Definition: catiav5w.hpp:428
ProcessPart
Dtk_ErrorStatus ProcessPart(const Dtk_string &inOutputFile, const Dtk_string &inReferenceName)
Definition: testlibcatiav5write.cpp:70
Dtk_string
This is a high level string class.
Definition: dtk_string.hpp:58
Dtk_Size_t
size_t Dtk_Size_t
Definition: define.h:712
catiav5w::WriteEntity
Dtk_ErrorStatus WriteEntity(const Dtk_EntityPtr &inEntity)
Write the entity provided in parameter.
Dtk_UUID
Definition: dtk_uuid.hpp:8
catiav5w::CreateNode
Dtk_ErrorStatus CreateNode(const NodeType &inNodeType, const Dtk_string &inNodeName=Dtk_string())
Create a node in the Specification Tree.
catiav5w::InitProduct
Dtk_ErrorStatus InitProduct(const Dtk_string &inFileName, const Dtk_string &inReferenceName, const catiav5w::FileDescription &inFileDescription=catiav5w::FileDescription())
Initialize a sub Product during CATProduct process.
catiav5w::CreateCGRDocId
Dtk_ErrorStatus CreateCGRDocId(const Dtk_string &inCGRFileName, const Dtk_string &inCGRName, Dtk_ID &outDocId)
Create a CGR Reference and DocID related to a given CGR File.
catiav5w::inReferenceName
const Dtk_string & inReferenceName
Definition: catiav5w.hpp:456
catiav5w::InitPart
Dtk_ErrorStatus InitPart(const Dtk_string &inOutputFile, const Dtk_string &inReferenceName, Dtk_UUID &outPartUUID, const catiav5w::FileDescription &inFileDescription=catiav5w::FileDescription())
Initialize the part
catiav5w::EndVirtualComponent
Dtk_ErrorStatus EndVirtualComponent(Dtk_ID &outDocId)
End - and write - the virtual component initialized by catiav5w::InitVirtualComponent.
Dtk_SmartPtr< Dtk_Entity >::DtkDynamicCast
static Dtk_SmartPtr< Dtk_Entity > DtkDynamicCast(const Dtk_SmartPtr< T2 > &p)
Definition: util_ptr_dtk.hpp:101
catiav5w::AddInstance
Dtk_ErrorStatus AddInstance(const Dtk_ID &inDocId, const Dtk_string &inInstanceName, const Dtk_transfo &inTransfo, Dtk_ID &outInstanceId)
Add an instance to a DocID into the Current (Sub/Root) CATProduct.
catiav5w::CreateV4ModelDocId
Dtk_ErrorStatus CreateV4ModelDocId(const Dtk_string &inV4ModelFileName, const Dtk_string &inV4ModelName, Dtk_ID &outDocId)
Create a V4 Model Reference and DocID related to a given V4 Model File.
catiav5w::EndWrite
Dtk_ErrorStatus EndWrite()
Free the Catia V5 Writer
catiav5w::CreatePartDocId
Dtk_ErrorStatus CreatePartDocId(const Dtk_string &inPartFileName, const Dtk_string &inPartName, Dtk_ID &outDocId)
Create a Part Reference and DocID related to a given CATPart.
catiav5w::EndProduct
Dtk_ErrorStatus EndProduct(Dtk_ID &outDocId)
Write effectively the Sub Product initialized by catiav5w::InitProduct.
catiav5w::CloseCurrentNode
Dtk_ErrorStatus CloseCurrentNode()
close the current node previously created by catiav5w::CreateNode.
catiav5w::EndPart
Dtk_ErrorStatus EndPart()
Free data allocated by catiav5w::InitPart
catiav5w::InitWrite
Dtk_ErrorStatus InitWrite(const Dtk_string &inLogFile, Licence_dtk inLicFct, const WriteOptions &inOptions=WriteOptions())
Initialize the Catia V5 Writer
Dtk_Point::Create
static Dtk_PointPtr Create(const Dtk_Point &inToCopy)
constructors returning Smart pointers
Dtk_Entity
Definition: util_ent_dtk.hpp:329