DATAKIT API  V2025.1
NX Write Samples

Table Of Contents:


Full samples source code

0. Introduction

Before writing NX files, you need to initialise Datakit API.
Parasolid writer is required for NX writer.

See main entry point to see call of Dtk_API::StartAPI function, and Dtk_API::StopAPI. This entry point calls UgWriteSample function.

In this function, we make twice the same tests, one ine version NX5, another with version NX1980. Datakit writer supports to write these versions.

//start_of_mainugwrite
int UgWriteSample(const Dtk_string &inResultDirectory)
{
Dtk_string outputDirectory, nxDirectory;
Dtk_ErrorStatus errStatus;
std::cout << endl << "----------------------------------------------" << endl;
std::cout << "Ug Write start" << endl;
nxDirectory = inResultDirectory + L"dtk/NX-Unigraphics/";
nxDirectory.FixPathSeparator();
nxDirectory.mkdir();
// Choosing output for version NX5
outputDirectory = inResultDirectory + L"dtk/NX-Unigraphics/nx5/";
outputDirectory.FixPathSeparator();
outputDirectory.mkdir();
errStatus = UgWriteSampleVersion(outputDirectory, DTK_UGW_VERSION_NX5);
// Choosing output for version NX1980
outputDirectory = inResultDirectory + L"dtk/NX-Unigraphics/nx1980/";
outputDirectory.FixPathSeparator();
outputDirectory.mkdir();
errStatus = UgWriteSampleVersion(outputDirectory, DTK_UGW_VERSION_NX1980);
// Choosing output for version NX2212
outputDirectory = inResultDirectory + L"dtk/NX-Unigraphics/nx2212/";
outputDirectory.FixPathSeparator();
outputDirectory.mkdir();
errStatus = UgWriteSampleVersion(outputDirectory, DTK_UGW_VERSION_NX2212);
std::cout << "Ug Write end" << endl;
return errStatus;
}
//end_of_mainugwrite_dtk

The list of available tests of this sample are in function UgWriteSampleVersion


1.Parts


1.1.One Body

Let's start with a simple code that will write a NX file containing a cube.

//start_of_ugwsample11
Dtk_ErrorStatus UgwSimpleBody(const Dtk_string& outputFileName, int version)
{
// Cube Sample
//CubeBody->info()->AddAttribute("MATERIALNAME", Dtk_string("Ash Gloss"));
// First we initialize writer with name of output file (see macro CHECK_OK above)
CHECK_OK(Ugw::InitFile(outputFileName, version));
// Then we write the body constructed
// Write final file and release the writer.
std::cout << "=> " << outputFileName.c_str() << endl;
return dtkNoError;
}
//end_of_ugwsample11

In this example, we get a cube as Dtk_Body (see common exemples)

This cube has some faces colored, these colors are inside the info fields of each face.

Then we initialize the writer with Ugw::InitFile. We provide the name of the file, for example "simplebody.prt", and also the version. The supported versions are DTK_UGW_VERSION_NX5 or DTK_UGW_VERSION_NX1980 Note that version parameter is optional, an default value is DTK_UGW_VERSION_NX5.

We put the cube wth Ugw::WriteBody, then close the writer with Ugw::EndFile.

The macro CHECK_OK just check the return value (a Dtk_ErrorStatus, if an error occurs, it returns the errors aborting the operation).

This sample gives the result below.


1.2.Multiple Bodies

This example shows how to write more than one bodies, and shows some options.

//start_of_ugwsample12
Dtk_ErrorStatus UgwTwoBodies(const Dtk_string& outputFileName, int version)
{
// Cube Sample
// We construct 2 cubes, translating the second
cube1->info()->SetName("FirstCube");
cube2->info()->SetName("SecondCube");
Dtk_transfo translate(Dtk_dir(1, 0, 0), Dtk_dir(0, 1, 0), Dtk_dir(0, 0, 1), Dtk_pnt(200, 0, 0));
cube2->Transform(translate);
// First we initialize writing with name of output file (see macro CHECK_OK above)
CHECK_OK(Ugw::InitFile(outputFileName, version));
// Then we write the 2 bodies constructed
// Write final file and release the writer.
std::cout << "=> " << outputFileName.c_str() << endl;
return dtkNoError;
}
//end_of_ugwsample12

We create two cubes, we apply a transformation on the second with Dtk_transfo translate, then we use two times the function Ugw::WriteBody. You may use this function as many times you want to write more bodies.

We give a name for each cube "FirstCube" and" "SecondCube" in info field of each body. These names appear in part navigator as featurename.


1.3.Mesh

This example shows how to write a mesh.

//start_of_ugwsample13
Dtk_ErrorStatus UgwMesh(const Dtk_string& outputFileName, int version)
{
Dtk_MeshPtr CubeMesh = sampleWriter::CreateMeshCylinder(8); // get a meshcylinder (common sample)
CubeMesh->info()->SetName("MyMesh");
CHECK_OK(Ugw::InitFile(outputFileName, version));
std::cout << "=> " << outputFileName.c_str() << endl;
return dtkNoError;
}
//end_of_ugwsample13

This simple code shows the function Ugw::WriteMesh. We give the name "MyMesh" to the mesh.


1.4.Wireframe

This example shows how to write wireframe.

//start_of_ugwsample14
Dtk_ErrorStatus UgwWire(const Dtk_string& outputFileName, int version)
{
CHECK_OK(Ugw::InitFile(outputFileName, version));
std::cout << "=> " << outputFileName.c_str() << endl;
return dtkNoError;
}
//end_of_ugwsample14

To set wireframe, you need to fill a body with wire entity, then put it inside NX with Ugw::WriteBody With info field of each curve, we can give each feature a name, here "mycircle", "myline". We can also give a color.


1.5.Axis systems and references

This example shows how to write axis systems, reference planes, references axis, reference points.

//start_of_ugwsample15
{
t.addTranslate(Dtk_dir(-100, 50, 200));
axis->SetName("MyAxisSystem");
axis->info()->AddAttribute("NXNAME", Dtk_Val("ANOTHERAXISSYSTEM"));
axis->info()->SetColor(255, 255, 0);
axis->SetMatrix(t);
return axis;
}
{
body->info() = Dtk_Info::create();
body->info()->SetName("MyReferencePlane");
body->info()->AddAttribute("NXNAME", Dtk_Val("MYPLANE"));
body->info()->SetColor(0, 255, 255);
body->AddOpenShell(shell);
Dtk_PlaneSurfacePtr plane = Dtk_PlaneSurface::Create(Dtk_pnt(150, 150, 0), Dtk_dir(0, 0, 1), Dtk_dir(1, 0, 0));
face->SetGeom(Dtk_SurfacePtr::DtkDynamicCast(plane));
shell->AddFace(face, DTK_TRUE);
return body;
}
{
body->info() = Dtk_Info::create();
body->info()->SetName("MyFixedRefPlane");
body->info()->AddAttribute("NXNAME", Dtk_Val("ANOTHERFIXEDPLANENAME"));
body->info()->SetColor(0, 0, 255);
body->AddOpenShell(shell);
Dtk_PlaneSurfacePtr plane = Dtk_PlaneSurface::Create(Dtk_pnt(250, 250, 0), Dtk_dir(0, 0, 1), Dtk_dir(1, 0, 0));
double trim[] = { -10,10,-10,10 };
plane->SetTrimUVBox(trim);
face->SetGeom(Dtk_SurfacePtr::DtkDynamicCast(plane));
shell->AddFace(face, DTK_TRUE);
return body;
}
{
body->info() = Dtk_Info::create();
body->info()->SetName("MyRefAxis");
body->info()->AddAttribute("NXNAME", Dtk_Val("ANOTHERAXISNAME"));
body->info()->SetColor(0, 255, 0);
body->AddOpenShell(shell);
Dtk_LinePtr line = Dtk_Line::Create(Dtk_pnt(160, 100, 10), Dtk_pnt(260, 200, 30));
shell->AddWire(tab);
return body;
}
{
body->info() = Dtk_Info::create();
body->info()->SetName("MyRefPoint");
body->info()->SetColor(255, 100, 0);
body->AddOpenShell(shell);
Dtk_PointPtr point = Dtk_Point::Create(Dtk_pnt(0, 310, 10));
tab.push_back(point);
shell->AddWire(tab);
return body;
}
Dtk_ErrorStatus UgwReferences(const Dtk_string& outputFileName, int version)
{
CHECK_OK(Ugw::InitFile(outputFileName, version));
cube->info()->SetName("MyCube");
cube->info()->AddAttribute("NXNAME", Dtk_Val("SOLIDBODYNAME"));
std::cout << "=> " << outputFileName.c_str() << endl;
return dtkNoError;
}
//end_of_ugwsample15

Let's see the function UgReferences. We can Ugw::InitFile and Ugw::WriteBody like previous example.

  • We add an axis system with Ugw::WriteAxisSystem. In the function MakeAxisSystem we create geometry, giving "MyAxisSystem" as name that will be in part navigator, but we add custom field "NXNAME" to add another name, usually in uppercase, that we see if we ask for properties of the feature, see screenshot.
  • We add a reference plane with Ugw::WriteReferenceGeometry. In the function MakeRefPlane, we still use name and NXNAME, we make an untrimmed plane. This will appear in NX as "Datum Plane"
  • We add a fixed reference plane with Ugw::WriteReferenceGeometry. The function MakeFixedRefPlane is similar to MakeRefPlane, but we trim the plane, to define bounds. This will appear in NX as "Fixed Datum Plane"
  • We add a fixed reference axis with Ugw::WriteReferenceGeometry. The function MakeRefAxis make the geometry, name and NXNAME. This will appear in NX as "Fixed Datum Axis"
  • We finally a point Ugw::WriteReferenceGeometry. The function MakeRefPoint make the geometry, we give a name, but not NXNAME because NX does not set another name on this feature. This will appear in NX as "Point"


1.6.Extern Parasolid file

This example shows how to add extern Parasolid file

//start_of_ugwsample16
Dtk_ErrorStatus UgwExternParasolid(const Dtk_string& outputFileName, const Dtk_string& parasolidfic, int version)
{
FILE* F = parasolidfic.OpenFile(DTK_RB);
if (!F)
{
std::cout << "error : " << parasolidfic.c_str() << " does not exist";
}
fseek(F, 0, SEEK_END);
flux.resize(ftell(F));
fseek(F, 0, SEEK_SET);
if (flux.size() > 0)
fread(&flux[0], 1, flux.size(), F);
fclose(F);
inf->SetName("MyFeatureName");
inf->AddAttribute("NXNAME", Dtk_string("SolidName"));
//inf->SetBlankedStatus(1); // set as invisible
//inf->SetColor(Dtk_RGB(255, 0, 0)); // set a body color (appear on non colored faces)
CHECK_OK(Ugw::InitFile(outputFileName, version));
CHECK_OK(Ugw::WriteExternParasolidBody(&flux[0], flux.size(), inf));
std::cout << "=> " << outputFileName.c_str() << endl;
return dtkNoError;
}
//end_of_ugwsample16

This example add a Parasolid file instead of a Dtk_Body as body. We read the file in memory before giving raw pointer of Parasolid file to function Ugw::WriteExternParasolidBody


2.Properties



2.1.Layer filters

This example shows how to make layer filters.

//start_of_ugwsample21
{
const int maxlayer = 257; // 256 layers (from 1 to 256 included for NX)
visible.resize(maxlayer, 1);
selectable.resize(maxlayer, 1);
// visibles and selectables
int i;
for (i = 1; i < maxlayer; i++)
{
visible[i] = (i % 2); // one on two
selectable[i] = ((i % 3) != 0); // one on three
}
// 2 layer filters :
Dtk_LayerFilterInfosPtr filter456 = lay->CreateLayerFilterInfos("FILTER_456", "MyDescription");
lay456.push_back(4); lay456.push_back(5); lay456.push_back(6);
filter456->SelectLayers(lay456);
Dtk_LayerFilterInfosPtr filter678 = lay->CreateLayerFilterInfos("FILTER_678");
lay678.push_back(6); lay678.push_back(7); lay678.push_back(8);
filter678->SelectLayers(lay678);
// wrok layer (default)
lay->SetDefaultLayer(12);
return lay;
}
Dtk_ErrorStatus UgwLayers(const Dtk_string& outputFileName, int version)
{
Dtk_BodyPtr CubeBody = sampleWriter::CreateCube(); // get a brepcube (common sample)
Dtk_MeshPtr CylMesh = sampleWriter::CreateMeshCylinder(8); // get a meshcyl (common sample)
Dtk_transfo translatescale(Dtk_dir(20, 0, 0), Dtk_dir(0, 20, 0), Dtk_dir(0, 0, 20), Dtk_pnt(150, 0, 0));
CylMesh->Transform(translatescale);
CHECK_OK(Ugw::InitFile(outputFileName, version));
Dtk_tab<Dtk_Int32> visible, selectable;
Dtk_LayerInfosSetPtr layerinfoset = CreateLayerInfosSet(visible, selectable);
CubeBody->info()->SetLayer(5);
CylMesh->info()->SetLayer(6);
CHECK_OK(Ugw::SetLayerData(layerinfoset, visible, selectable));
// Then we write the 2 bodies constructed
// Write final file and release the writer.
std::cout << "=> " << outputFileName.c_str() << endl;
return dtkNoError;
}
//end_of_ugwsample21

In this sample we use function Ugw::SetLayerData to fill layer datas. NX manage 256 layers (from 1 to 256 included), each of this layer can be invisible, visible only, or visible and selectable. We fill a Dtk_tab of size 257 to define each layer.

The Dtk_LayerInfoSet also contain default layer, and layer filters. In our sample, we make a filter "FILTER_456" contain layers 4,5,6, and "FILTER_678" contain layers 6,7,8

We define default layer as layer 12. We define CubeBody in layer 5, CylMesh in layer 6. Layer 6 is invisible here, so part navigator shows CylMesh as invisible.

We retrieve all these informations on the screenshot below.


2.2.Groups

This example shows how to make groups.

//start_of_ugwsample22
Dtk_ErrorStatus UgwGroups(const Dtk_string& outputFileName, int version)
{
cube1->info()->SetName("FirstCube");
cube2->info()->SetName("SecondCube");
Dtk_transfo translate(Dtk_dir(1, 0, 0), Dtk_dir(0, 1, 0), Dtk_dir(0, 0, 1), Dtk_pnt(200, 0, 0));
cube2->Transform(translate);
Dtk_MeshPtr CylMesh = sampleWriter::CreateMeshCylinder(8); // get a meshcyl (common sample)
CylMesh->info()->SetName("OneMesh");
Dtk_transfo translatescale(Dtk_dir(10, 0, 0), Dtk_dir(0, 10, 0), Dtk_dir(0, 0, 10), Dtk_pnt(-100, 0, 0));
CylMesh->Transform(translatescale);
WireBody->Transform(translatescale);
CHECK_OK(Ugw::InitFile(outputFileName, version));
// put cube1 in group GroupRoot/Sub1
cube1->info()->AddAttribute("NXGROUPS", Dtk_Val("GroupRoot\\Sub1\\"));
// put cube1 in group GroupRoot/Sub1 and also GroupRoot/Sub2
cube2->info()->AddAttribute("NXGROUPS", Dtk_Val("GroupRoot\\Sub1\\\\GroupRoot\\Sub2\\"));
// put Mesh in group GroupRoot/Sub2
CylMesh->info()->AddAttribute("NXGROUPS", Dtk_Val("GroupRoot\\Sub2\\"));
// put Wire in group GroupRoot/Sub2
WireBody->info()->AddAttribute("NXGROUPS", Dtk_Val("GroupRoot\\Sub2\\"));
// set property to group Sub2
infsub2->SetBlankedStatus(1);
infsub2->SetLayer(23);
CHECK_OK(Ugw::SetGroupDatas("Sub2", infsub2)); // Sub2 become invisible, and on layer 23
// make an empty group
infsub3->AddAttribute("NXGROUPS", Dtk_Val("GroupRoot\\Sub3\\"));
infsub3->SetLayer(19);
CHECK_OK(Ugw::SetGroupDatas("Sub3", infsub3)); // Sub3 become an empty group on layer 19
// Then we write the 2 bodies constructed
// Write final file and release the writer.
std::cout << "=> " << outputFileName.c_str() << endl;
return dtkNoError;
}
//end_of_ugwsample22

In this example, we make 2 cubes. Filling attribute "NXGROUPS" into info field of each cube, we define cube1 is in group GroupRoot/Sub1, and cube2 is in group GroupRoot/Sub1 ans GroupRoot/Sub2 The groups are a tree, it is possible to make a more complex hierarchy.

We set that group "Sub2" is invisible, and in layer 23 in this sample with Ugw::SetGoupDatas. We also make an empty group, layer 19, still with function Ugw::SetGoupDatas.


2.3.Reference Sets

This example shows how to make reference sets.

//start_of_ugwsample23
Dtk_ErrorStatus UgwRefSets(const Dtk_string& outputFileName, int version)
{
cube1->info()->SetName("FirstCube");
cube2->info()->SetName("SecondCube");
Dtk_transfo translate(Dtk_dir(1, 0, 0), Dtk_dir(0, 1, 0), Dtk_dir(0, 0, 1), Dtk_pnt(200, 0, 0));
cube2->Transform(translate);
// reference set
// Cube1 into reference set "MyRef1","MyRef2","MyRef3"
cube1->info()->AddAttribute("NXREFERENCESET", Dtk_Val("MyRef1\\MyRef2\\MyRef3\\"));
// Cube2 into reference set "MyRef1","MyRef3"
cube2->info()->AddAttribute("NXREFERENCESET", Dtk_Val("MyRef1\\MyRef3\\"));
CHECK_OK(Ugw::InitFile(outputFileName, version));
// Then we write the 2 bodies constructed
// Write final file and release the writer.
std::cout << "=> " << outputFileName.c_str() << endl;
return dtkNoError;
}
//end_of_ugwsample23

We use custom attribute "NXREFERENCESET" in a body (or a mesh, or other feature) to include it in some reference sets. Here, Cube1 into reference set "MyRef1","MyRef2","MyRef3", Cube2 into reference set "MyRef1","MyRef3".


2.4.Preferences and metadatas

This example shows how to set default units, and modeling preferences and metadatas

//start_of_ugwsample24
Dtk_MetaDataPtr MakeMetaData(const Dtk_string& propname, const Dtk_string& value, const Dtk_string& category = "")
{
meta->SetCategory(category);
return meta;
}
Dtk_ErrorStatus UgwMeta(const Dtk_string& outputFileName, int version)
{
// lastparameter 0 indicate to set INCH. (default value = 1 = millimeters)
CHECK_OK(Ugw::InitFile(outputFileName, version, 0));
// custom metadata
CHECK_OK(Ugw::AddMetaData(MakeMetaData("MyPropTitle", "MyPropValue", "CategoryNX1980")));
CHECK_OK(Ugw::AddMetaData(MakeMetaData("MyPropTitle2", "MyPropValue2", "CategoryNX1980")));
// modeling preference
CHECK_OK(Ugw::AddMetaData(MakeMetaData("Distance Tolerance", "0.015", "Modeling Preferences")));
CHECK_OK(Ugw::AddMetaData(MakeMetaData("Angle Tolerance", "0.1", "Modeling Preferences")));
CHECK_OK(Ugw::AddMetaData(MakeMetaData("Density", "6500", "Modeling Preferences")));
// for Density Units, supported values are "lb/in3", "lb/ft3", "g/m3", "kg/m3"
CHECK_OK(Ugw::AddMetaData(MakeMetaData("Density Units", "lb/ft3", "Modeling Preferences")));
std::cout << "=> " << outputFileName.c_str() << endl;
return dtkNoError;
}
//end_of_ugwsample24

In this example, we use a last parameter of Ugw::InitFile as 0 (=inch). If not set, default value = 1 (millimeters). We see on screenshot the file, in inches.

Custom metadatas are "MyPropTitle", we retrieve it in display part properties. Note that category is not supported in NX_5 output, but it is in NX_1980 output.

Specific metadatas, with catergory name as "Modeling Preferences" define some modeling preferences we see in the screeshot below. For Density Units, supported values are "lb/in3", "lb/ft3", "g/m3", "kg/m3"


2.5.Colors

This example shows how to customize colors

//start_of_ugwsample25
Dtk_ErrorStatus UgwColors(const Dtk_string& outputFileName, int version)
{
CHECK_OK(Ugw::InitFile(outputFileName, version));
for (int i = 0; i < 217; i++)
{
Dtk_string name = "Datakit_";
name.add_int(i);
Dtk_RGB rgb = Dtk_RGB(i + 30, 0, 0);
}
std::cout << "=> " << outputFileName.c_str() << endl;
return dtkNoError;
}
//end_of_ugwsample25

NX stores a palette color. (A palette color per part). There is 217 colors available. Each color can be redefined with RGB value, and a name. Function Ugw::SetColorInTable can set custom colors. Function Ugw::GetColorInTable exists too to retrieve colors.

Note : in default mode, our writer try to find the closer color in existing palette, that's why the cube appears red.


3.Assemblies



3.1.Simple assembly

This example shows how to make an simple assembly

//start_of_ugwsample31
Dtk_InfoPtr MakeInstanceData(const Dtk_string& instancename, const Dtk_RGB& col, bool visible, int layer)
{
infos->SetName(instancename);
infos->SetColor(col);
if (!visible)
infos->SetBlankedStatus(1);
if (layer != -1)
infos->SetLayer(layer);
return infos;
}
Dtk_ErrorStatus UgwAsm(const Dtk_string& outputFileName, int version)
{
Dtk_BodyPtr CubeBody = sampleWriter::CreateCube(); // get a brepcube (common sample)
Dtk_MeshPtr CubeMesh = sampleWriter::CreateMeshCylinder(8); // get a meshcube (common sample)
Dtk_BodyPtr WireBody = sampleWriter::CreateCurves(); // get wire curve into a body.
CubeMesh->Transform(Dtk_transfo(Dtk_dir(30, 0, 0), Dtk_dir(0, 30, 0), Dtk_dir(0, 0, 30), Dtk_pnt())); // rescale mesh too small
Dtk_transfo cubetransfo; // identity
CHECK_OK(Ugw::InitFile(outputFileName, version)); // create root prt file
CHECK_OK(Ugw::OpenInstance("sub1", cubetransfo, MakeInstanceData("SubAssembly", Dtk_RGB(), true, -1))); // sub1 subassembly
CHECK_OK(Ugw::WriteBody(CubeBody)); // put a cube on the subassembly
cubetransfo.addTranslate(Dtk_dir(350, 0, 0)); // shift matrix.
CHECK_OK(Ugw::OpenInstance("leaf1", cubetransfo, MakeInstanceData("RedCube_SpecificLayer12", Dtk_RGB(255, 0, 0), true, 12)));
cubetransfo.addTranslate(Dtk_dir(150, 0, 0)); // shift matrix.
CHECK_OK(Ugw::OpenInstance("leaf2", cubetransfo, MakeInstanceData("GreenCube_Transparent", Dtk_RGB(0, 255, 0, 128), true, -1)));
// create a second instance of "leaf2.prt", reusing "leaf2.prt" automatic retrieve instance
cubetransfo.addTranslate(Dtk_dir(150, 0, 0)); // shift matrix.
CHECK_OK(Ugw::OpenInstance("leaf2", cubetransfo, MakeInstanceData("ReinstanciedBlueCube", Dtk_RGB(0, 0, 255), true, -1)));
CHECK_OK(Ugw::CloseLastInstance()); // for reinstance, close immediatly
cubetransfo.addTranslate(Dtk_dir(150, 0, 0)); // shift matrix again
CHECK_OK(Ugw::OpenInstance("leaf3", cubetransfo, MakeInstanceData("TealCube_Invisible", Dtk_RGB(0, 255, 255), false, -1)));
CHECK_OK(Ugw::CloseLastInstance()); // for reinstance, close immediatly
cubetransfo.addTranslate(Dtk_dir(150, 0, 0)); // shift matrix.
CHECK_OK(Ugw::OpenInstance("leaf4", cubetransfo, MakeInstanceData("MeshCyl", Dtk_RGB(), true, -1)));
cubetransfo.addTranslate(Dtk_dir(150, 0, 0)); // shift matrix.
CHECK_OK(Ugw::OpenInstance("leaf5", cubetransfo, MakeInstanceData("Wire", Dtk_RGB(), true, -1)));
CHECK_OK(Ugw::EndFile()); // close root prt file
std::cout << "=> " << outputFileName.c_str() << endl;
return dtkNoError;
}
//end_of_ugwsample31

To create open a subassembly, use function Ugw::OpenInstance. We give a name, that will be the file name (without .prt), then a placement matrix (cubetransfo here) And some properties as Dtk_InfoPtr given by MakeInstanceData.

  • Instance name using info::SetName()
  • Instance color (may contain transparency, like in "leaf2"
  • Visibility ("leaf3" is invisible)
  • layer : "leaf1" is into layer 12

Note that we reinstanciate leaf2 twice, with different matrix, colors, and visibility. To reinstanciate an existing subassembly, call Ugw::OpenInstance with a part name already used previously, the call Ugw::CloseLastInstance immediatly.

Note that not only leaves can have bodies, see the line "put a cube on the subassembly".


3.2.Extern assembly

This example shows how to make an assembly from existing files.

//start_of_ugwsample32
//#define USE_NX_READER
#ifdef USE_NX_READER
extern void nxreader_GetData(const Dtk_string&, void*&);
#define NX_RETRIEVE_ASSEMBLYDATA nxreader_GetData
#else
#define NX_RETRIEVE_ASSEMBLYDATA NULL
#endif
Dtk_ErrorStatus UgwExternAsm(const Dtk_string& outputFileName, int version)
{
// This example create an assembly from extern prt files
// First we initialize writing with name of output file (see macro CHECK_OK above)
CHECK_OK(Ugw::InitFile(outputFileName, version));
// We add extern twobodies (.prt) written by this sample previously
// The last parameter is a pointer on a function from datakit NXreader to retrieve subassembly datas : (instance names, colors, layer, visbility).
// This is optional, if NULL is given, the current file doesn't know these datas from subassemblies.
Dtk_transfo trans;
trans.addTranslate(Dtk_dir(0, 0, 200));
// We add extern asm (.prt) written by this sample previously
// We transmit function nxreader_GetData from NXReader, to retrieve cube colors set in subassembly sub1.prt
CHECK_OK(Ugw::AddExternInstance("asm", trans, MakeInstanceData("FirstInstance", Dtk_RGB(), true, 5), NX_RETRIEVE_ASSEMBLYDATA));
trans.addTranslate(Dtk_dir(0, 200, 0));
// We add a second time extern asm (.prt) written by this sample previously
CHECK_OK(Ugw::AddExternInstance("asm", trans, MakeInstanceData("SecondInstance", Dtk_RGB(), true, 15), NX_RETRIEVE_ASSEMBLYDATA));
// Write final file and release the writer.
std::cout << "=> " << outputFileName.c_str() << endl;
return dtkNoError;
}
//end_of_ugwsample32

The function Ugw::AddExternInstance adds an existing part or assembly. As a new instance the Dtk_InfoPtr works like the Dtk_InfoPtr of function Ugw::OpenInstance.

About the last parameter, in a normal NX assembly, the root files knows all its hierarchy, not only its children, but grand children, all tree, all leaves, even deep leaves.

Including an assembly with Ugw::AddExternInstance, and NULL at last parameter make the root file (here externasm.prt) knowing only its child (asm.prt) but not the leaves. If we want externasm.prt to know all its leaves, we need to link the Datakit NX Reader, then activate USE_NX_READER, it will link the function pointer nxreader_GetData that will retrieve all datas needed from external assembly.


4.PMI



4.1.Annotation

This example shows how to make an annotation

//start_of_ugwsample41
{
// text informations
Dtk_TextStyle textstyle;
// All datas below match NX menu Note Settings/ lettering
textinfos->AddAttribute("NX_AlignmentPosition", 4); // optional (1=TL,2=TC,3=TR,4=ML(def),5=MC,6=MR,7=BL,8=BC,9=BR)
textstyle.Justification() = Dtk_TextStyle::JustificationLeft; // Text Justification
textinfos->SetCurveThickNessInMM(0.1); // optional, assume <0.5 = thin, >1.0 = thick else normal
textstyle.CharHeight() = 11; // mandatory height
textinfos->AddAttribute("NX_FontGapFactor", 0.0); // optional (def 0.0)
textstyle.Ratio() = 1.0; // optional Text Aspect Ratio
textinfos->AddAttribute("NX_LineGapFactor", 1.0);// optional (def 1.0)
textinfos->AddAttribute("NX_LetteringAngle", 0.0);// optional (def 0.0)
textinfos->AddAttribute("NX_HeightFactor", 2.0); // optional (def 2.0)
textinfos->SetColor(Dtk_RGB(255, 0, 0)); // optional, a red text
//textstyle.Font().Name() = "Arial"; // optional, defaut blockfont (strokefont).
// NX5 supports only "blockfont", "Arial" and "NX ANSI Symbols"
// NX1980, append font name in table
Dtk_pnt textorigin2D = Dtk_pnt(100, 20, 0); // all location are defined a 2D Coordinate system where Z = 0.
Dtk_Oriented2dBBox textbbox(textorigin2D, -1, -1); // -1 for width if unknown, height is given by textstyle.CharHeight()
Dtk_Text text("SampleText\nsub1\nsub2", textbbox, textbbox, DTK_PI / 2.0, 0, dtk_text_type_value, textinfos, textstyle);
Dtk_CompositeText compotext;
compotext.AddText(text);
symb->Texts() = compotext;
// arrow, see menu in NX : Note Settings / ArrowHead
Dtk_pnt arrowlocation = Dtk_pnt(50, 0, 0); // all location are defined a 2D Coordinate system where Z = 0.
double arrowlength = 5.5; // length of arrow througt line
Dtk_LeaderPtr leader = Dtk_Leader::Create(arrowlength, 0.0, arrowlocation, arrowtype, Dtk_tab<Dtk_pnt>());
// Note Settings / ArrowLine
leader->GetInfo()->SetColor(Dtk_RGB(0, 0, 255)); // optional, a blue leader
leader->GetInfo()->SetCurveThickNessInMM(0.7); // optional, assume <0.5 = thin, >1.0 = thick else normal
leader->GetInfo()->SetCurveLineType(DTK_PHANTOM); // optional, see enum enum Dtk_FontLineType
leader->GetInfo()->AddAttribute("NX_TextToLineGap", 10.0);// optional (def = 10.0)
leader->GetInfo()->AddAttribute("NX_TextOverStubFactor", 0.1);// optional (def = 0.1)
// Note Settings
leader->GetInfo()->AddAttribute("NX_StubLength", 20.0); // optional (def = 20.0)
leader->GetInfo()->AddAttribute("NX_StubSide", 1); // optional 1 for left (default), 0 for right
leader->GetInfo()->AddAttribute("NX_TextAlignment", 1); // optional 1 for top (default), 2 for middle, 3 for bottom
// Note Settings / ArrowHead
leader->GetArrowHead().GetInfo()->AddAttribute("NX_ArrowAngle", 30.0); // optional length given above (def = 30)
leader->GetArrowHead().GetInfo()->AddAttribute("NX_DotDiameter", 1.5); // optional (def = 1.5)
leader->GetArrowHead().GetInfo()->SetColor(Dtk_RGB(255, 0, 255)); // optional, a purple arrow
leader->GetArrowHead().GetInfo()->SetCurveThickNessInMM(2.0); // optional, assume <0.5 = thin, >1.0 = thick else normal
leader->GetArrowHead().GetInfo()->SetCurveLineType(DTK_SOLIDLINE); // optional, see enum enum Dtk_FontLineType
symb->AddLeader(leader);
// Final Placement :
Dtk_dir X(0, 0, 1);
Dtk_dir Y(-1, 0, 0);
Dtk_pnt O(0, 0, 0);
Dtk_transfo trans = Dtk_transfo(X, Y, X^Y, O);
pmi->info() = Dtk_Info::create();
pmi->info()->SetName("MyAnnot");
return pmi;
}
Dtk_ErrorStatus UgwAnnot(const Dtk_string& outputFileName, int version)
{
// Cube Sample
// First we initialize writing with name of output file (see macro CHECK_OK above)
CHECK_OK(Ugw::InitFile(outputFileName, version));
// Then we write the 2 bodies constructed
// Write final file and release the writer.
std::cout << "=> " << outputFileName.c_str() << endl;
return dtkNoError;
}
//end_of_ugwsample41

In this example, we create a simple Annotation. The function UGW_Sample_MakePMI shows differents options you retrieve on the screenshot.

Actually, annotation creation is limited as :

  • one text
  • one arrow with one stub


UgwGroups
Dtk_ErrorStatus UgwGroups(const Dtk_string &outputFileName, int version)
Definition: testlibugwrite.cpp:332
Dtk_TextStyle::Ratio
Dtk_Double64 & Ratio()
Retrieves the text ratio - get/set -.
MakeInstanceData
Dtk_InfoPtr MakeInstanceData(const Dtk_string &instancename, const Dtk_RGB &col, bool visible, int layer)
Definition: testlibugwrite.cpp:483
UgwRefSets
Dtk_ErrorStatus UgwRefSets(const Dtk_string &outputFileName, int version)
Definition: testlibugwrite.cpp:391
Dtk_InfoPtr
Dtk_SmartPtr< Dtk_Info > Dtk_InfoPtr
Handles a Dtk_Info object.
Definition: util_ptr_dtk.hpp:310
UgwExternParasolid
Dtk_ErrorStatus UgwExternParasolid(const Dtk_string &outputFileName, const Dtk_string &parasolidfic, int version)
Definition: testlibugwrite.cpp:205
UgwExternAsm
Dtk_ErrorStatus UgwExternAsm(const Dtk_string &outputFileName, int version)
Definition: testlibugwrite.cpp:560
Dtk_transfo
This is the Transformation dedicated class.
Definition: dtk_transfo.hpp:19
Ugw::WriteAxisSystem
Dtk_ErrorStatus WriteAxisSystem(const Dtk_AxisSystemPtr &inAxisSystem)
Add an axis system into the current node.
MakeRefAxis
Dtk_BodyPtr MakeRefAxis()
Definition: testlibugwrite.cpp:148
UgwColors
Dtk_ErrorStatus UgwColors(const Dtk_string &outputFileName, int version)
Definition: testlibugwrite.cpp:458
Dtk_TextStyle::Justification
TextJustificationEnum & Justification()
Retrieves the text justification - get/set -.
Dtk_Info::AddAttribute
Dtk_ErrorStatus AddAttribute(Dtk_string name, Dtk_Val val)
Dtk_TextStyle::JustificationLeft
@ JustificationLeft
Definition: util_draw_dtk.hpp:299
Dtk_PlaneSurface::Create
static Dtk_PlaneSurfacePtr Create(const Dtk_pnt &inOrigin, const Dtk_dir &inNormal, const Dtk_dir &inUDirection, const Dtk_dir &inVDirection=Dtk_dir())
Create an infinite plane surface.
Dtk_Info::SetName
Dtk_ErrorStatus SetName(Dtk_string inName)
UgwLayers
Dtk_ErrorStatus UgwLayers(const Dtk_string &outputFileName, int version)
Definition: testlibugwrite.cpp:301
Ugw::SetColorInTable
Dtk_ErrorStatus SetColorInTable(size_t inIth, const Dtk_RGB &inColor, const Dtk_string &inColorname)
Replace color value and color name on the palette.
Dtk_TextStyle
This is the text_style. This class gathers several informations about text style.
Definition: util_draw_dtk.hpp:260
dtkErrorFileNotExist
@ dtkErrorFileNotExist
Definition: error_dtk.hpp:95
MakeRefPlane
Dtk_BodyPtr MakeRefPlane()
Definition: testlibugwrite.cpp:114
UgwMeta
Dtk_ErrorStatus UgwMeta(const Dtk_string &outputFileName, int version)
Definition: testlibugwrite.cpp:430
Ugw::AddExternInstance
Dtk_ErrorStatus AddExternInstance(const Dtk_string &inProtoFileName, const Dtk_transfo &inTrans, Dtk_InfoPtr inInfos, void(*innxread_getdata)(const Dtk_string &, void *&))
Open an extern file to add as instance in current assembly, inProtoFileName must be in the output dir...
UgWriteSample
int UgWriteSample(const Dtk_string &inResultDirectory)
Definition: testlibugwrite.cpp:756
DTK_TRUE
#define DTK_TRUE
Definition: define.h:727
DTK_UGW_VERSION_NX5
#define DTK_UGW_VERSION_NX5
Definition: ugw.hpp:7
dtk_text_type_value
@ dtk_text_type_value
Definition: str_def.h:14
Dtk_string
This is a high level string class.
Definition: dtk_string.hpp:58
Ugw::WriteReferenceGeometry
Dtk_ErrorStatus WriteReferenceGeometry(const Dtk_BodyPtr &inRefgeom)
Add a reference geometry into the current node.
UgwAnnot
Dtk_ErrorStatus UgwAnnot(const Dtk_string &outputFileName, int version)
Definition: testlibugwrite.cpp:660
Dtk_Text
This is the base text class. It's part of Dtk_CompositeText. It's used into a lot of 2D Entities It c...
Definition: util_draw_dtk.hpp:1126
Dtk_AxisSystem::create
static Dtk_SmartPtr< Dtk_AxisSystem > create()
Dtk_Body::Create
static Dtk_BodyPtr Create()
Create a body.
Ugw::WriteBody
Dtk_ErrorStatus WriteBody(const Dtk_BodyPtr &inBody)
Add a body inside the file.
Dtk_string::OpenFile
FILE * OpenFile(const Dtk_string &inRights) const
File Utility : Open a file with the given rights.
MakeFixedRefPlane
Dtk_BodyPtr MakeFixedRefPlane()
Definition: testlibugwrite.cpp:130
UgwSimpleBody
Dtk_ErrorStatus UgwSimpleBody(const Dtk_string &outputFileName, int version)
Definition: testlibugwrite.cpp:17
DTK_SOLIDLINE
@ DTK_SOLIDLINE
Definition: util_ent_dtk.hpp:46
Dtk_Symbol::Create
static Dtk_SymbolPtr Create()
Base constructor.
Ugw::SetLayerData
Dtk_ErrorStatus SetLayerData(const Dtk_LayerInfosSetPtr &inLayerDataSet, const Dtk_tab< Dtk_Int32 > &inVisibles=Dtk_tab< Dtk_Int32 >(), const Dtk_tab< Dtk_Int32 > &inSelectables=Dtk_tab< Dtk_Int32 >())
define Data for layers
Dtk_Info::SetBlankedStatus
Dtk_ErrorStatus SetBlankedStatus(const Dtk_Int32 &inBlankedStatus)
Dtk_string::add_int
void add_int(const int integer, int force_unsigned_int=0)
concat an int to the Dtk_string (convert the int to Dtk_string)
Dtk_Val
Definition: dtk_val.hpp:67
UgwMesh
Dtk_ErrorStatus UgwMesh(const Dtk_string &outputFileName, int version)
Definition: testlibugwrite.cpp:71
Dtk_Face::Create
static Dtk_FacePtr Create(const Dtk_BodyPtr &inParentBody)
Create a face in a body.
UGW_Sample_MakePMI
Dtk_FdtPtr UGW_Sample_MakePMI()
Definition: testlibugwrite.cpp:594
MakeAxisSystem
Dtk_AxisSystemPtr MakeAxisSystem()
Definition: testlibugwrite.cpp:102
Ugw::InitFile
Dtk_ErrorStatus InitFile(const Dtk_string &inFilename, int inVersiontowrite=45, int inUnitasmm=1, int inMultithread=0)
Initialize UG Writer.
Dtk_Line::Create
static Dtk_LinePtr Create(const Dtk_Line &inLineToCopy)
constructors returning Smart pointers
Dtk_tab::resize
void resize(Dtk_Size_t n, const T &t)
Resizes the array.
Definition: util_stl_dtk.hpp:603
Ugw::WritePMI
Dtk_ErrorStatus WritePMI(const Dtk_FdtPtr &inPmi)
Add a pmi inside the file.
Ugw::WriteExternParasolidBody
Dtk_ErrorStatus WriteExternParasolidBody(const char *inFlux, size_t inSize, const Dtk_InfoPtr &inInf)
Add a Parasolid file as body inside the file.
Dtk_MetaData::TypeProperty
@ TypeProperty
Definition: dtk_metadata.hpp:28
Ugw::CloseLastInstance
Dtk_ErrorStatus CloseLastInstance()
Close the last instance.
DTK_PHANTOM
@ DTK_PHANTOM
Definition: util_ent_dtk.hpp:50
Ugw::SetGroupDatas
Dtk_ErrorStatus SetGroupDatas(const Dtk_string &inGroupName, Dtk_InfoPtr inInfos)
define Visibility for a specific group
Dtk_SmartPtr::DtkDynamicCast
static Dtk_SmartPtr< T > DtkDynamicCast(const Dtk_SmartPtr< T2 > &p)
Definition: util_ptr_dtk.hpp:101
Dtk_Info::SetCurveThickNessInMM
Dtk_ErrorStatus SetCurveThickNessInMM(const Dtk_Double64 inCurveThickNessInMM)
Dtk_ErrorStatus
Dtk_ErrorStatus
Definition: error_dtk.hpp:6
Dtk_Fdt::Create
static Dtk_FdtPtr Create()
Base constructor.
CHECK_OK
#define CHECK_OK(X)
Definition: testwriters.h:9
Dtk_SmartPtr
Definition: util_ptr_dtk.hpp:37
Dtk_string::c_str
const char * c_str() const
Retrieve the ASCII conversion string.
sampleWriter::CreateMeshCylinder
Dtk_MeshPtr CreateMeshCylinder(int nbpoints)
Mesh Cylinder sample.
Definition: testcreatemesh.cpp:337
UgwTwoBodies
Dtk_ErrorStatus UgwTwoBodies(const Dtk_string &outputFileName, int version)
Definition: testlibugwrite.cpp:39
Dtk_string::mkdir
int mkdir() const
File Utility : Create a Directory.
Dtk_Info::SetLayer
Dtk_ErrorStatus SetLayer(const Dtk_Int32 &inLayer)
Dtk_pnt
This is a mathematical point class.
Definition: dtk_pnt.hpp:22
UgWriteSampleVersion
Dtk_ErrorStatus UgWriteSampleVersion(const Dtk_string &outputDirectory, int version)
Definition: testlibugwrite.cpp:684
UgwAsm
Dtk_ErrorStatus UgwAsm(const Dtk_string &outputFileName, int version)
Definition: testlibugwrite.cpp:495
Dtk_string::FixPathSeparator
void FixPathSeparator()
File Utility : Fixes path separator consistency. It lets you replace the '\' or '/' by the OS needed ...
Dtk_CompositeText
This is the composite text class. It's basically a Dtk_Text Container. This class represents a group ...
Definition: util_draw_dtk.hpp:1557
Dtk_Leader::Create
static Dtk_LeaderPtr Create()
Base constructor.
MakeMetaData
Dtk_MetaDataPtr MakeMetaData(const Dtk_string &propname, const Dtk_string &value, const Dtk_string &category="")
Definition: testlibugwrite.cpp:423
Dtk_TextStyle::CharHeight
Dtk_Double64 & CharHeight()
Retrieves the char height - get/set -.
NX_RETRIEVE_ASSEMBLYDATA
#define NX_RETRIEVE_ASSEMBLYDATA
Definition: testlibugwrite.cpp:556
Dtk_tab
This is a high level array class.
Definition: util_stl_dtk.hpp:85
CreateLayerInfosSet
Dtk_LayerInfosSetPtr CreateLayerInfosSet(Dtk_tab< Dtk_Int32 > &visible, Dtk_tab< Dtk_Int32 > &selectable)
Definition: testlibugwrite.cpp:272
Ugw::WriteMesh
Dtk_ErrorStatus WriteMesh(const Dtk_MeshPtr &inMesh)
Add a mesh inside the file.
Dtk_transfo::addTranslate
void addTranslate(const Dtk_dir &V)
Translate the Dtk_transfo.
sampleWriter::CreateCube
Dtk_BodyPtr CreateCube()
Definition: testcreatecube.cpp:1249
Dtk_tab::size
Dtk_Size_t size() const
Returns the size of the array.
Definition: util_stl_dtk.hpp:502
Ugw::AddMetaData
Dtk_ErrorStatus AddMetaData(const Dtk_MetaDataPtr &inMeta)
Add a MetaData in the current component.
DTK_UGW_VERSION_NX1980
#define DTK_UGW_VERSION_NX1980
Definition: ugw.hpp:8
UgwWire
Dtk_ErrorStatus UgwWire(const Dtk_string &outputFileName, int version)
Definition: testlibugwrite.cpp:86
UgwReferences
Dtk_ErrorStatus UgwReferences(const Dtk_string &outputFileName, int version)
Definition: testlibugwrite.cpp:179
Ugw::OpenInstance
Dtk_ErrorStatus OpenInstance(const Dtk_string &inInstanceName, const Dtk_string &inProtoFileName, Dtk_bool &outWasAlreadyInstancied, const Dtk_transfo &inTrans, const Dtk_RGB &inRgb)
deprecated
DTK_RB
#define DTK_RB
Definition: dtk_string.hpp:40
Dtk_Leader::LeaderTerminatorTypeEnum
LeaderTerminatorTypeEnum
Internal leader terminator type.
Definition: util_draw_dtk.hpp:1999
sampleWriter::CreateCurves
Dtk_BodyPtr CreateCurves()
Definition: testcreatecube.cpp:1292
Dtk_LayerInfosSet::Create
static Dtk_LayerInfosSetPtr Create(const Dtk_Size_t inNumLayers)
Base constructor.
dtkNoError
@ dtkNoError
Definition: error_dtk.hpp:140
Dtk_tab::push_back
void push_back(const T &x)
Inserts an element at the end of the array.
Definition: util_stl_dtk.hpp:415
Dtk_Shell::Create
static Dtk_ShellPtr Create(const Dtk_BodyPtr &inParentBody)
Create a shell in a body.
MakeRefPoint
Dtk_BodyPtr MakeRefPoint()
Definition: testlibugwrite.cpp:164
Dtk_Point::Create
static Dtk_PointPtr Create(const Dtk_Point &inToCopy)
constructors returning Smart pointers
Dtk_CompositeText::AddText
void AddText(Dtk_Text inText)
Adds a Dtk_Text to the Dtk_CompositeText.
Dtk_RGB
Definition: dtk_rgb.hpp:7
Dtk_Info::SetColor
Dtk_ErrorStatus SetColor(const int &R, const int &G, const int &B)
Dtk_Info::create
static Dtk_SmartPtr< Dtk_Info > create()
Dtk_Oriented2dBBox
This is the base bounding box class. It's used into a lot of 2D Entities This class represents the ba...
Definition: util_draw_dtk.hpp:479
Dtk_dir
This is a mathematical direction class.
Definition: dtk_dir.hpp:15
DTK_UGW_VERSION_NX2212
#define DTK_UGW_VERSION_NX2212
Definition: ugw.hpp:9
Dtk_Leader::TerminatorTypeFillArrow
@ TerminatorTypeFillArrow
Definition: util_draw_dtk.hpp:2007
Ugw::EndFile
Dtk_ErrorStatus EndFile()
Close the UG file.
Dtk_MetaData::CreateMetaData
static Dtk_MetaDataPtr CreateMetaData(const MetaDataTypeEnum &inEnumType, Dtk_string inTitle, Dtk_string inValue, Dtk_string inValueType=Dtk_string(L"STRING"))
Create a Dtk_MetaDataPtr .
DTK_PI
#define DTK_PI
Definition: str_def.h:8