Exporting a ZCAD file
Overview
A zcad file is a compact file format that can be loaded and viewed in the browser using zea-engine
- Construct a CADAsset
- Build a tree of nodes on top of this CADAsset
- Add geometries with materials to the leaf nodes in the tree.
- Export the CADAsset to a zcad file.
Generic Classes
The C++ classes mirror closely those found in the JavaScript rendering engine.
- TreeItem: The base class for all items that make up the tree. Each tree item has a name, and a transformation which is applied to its children.
- GeomItem: A tree item that owns a geometry and a material.
The Structure of a CAD file
The root node of any zcad file is a CADAsset. This Class owns containers that store materials and shared geometries. The CADAsset is a tree item, and can contain any number of children.
Note: The CADAsset can contain children of any types of TreeItem. The exporter code is responsible for building a tree that matches users expectations. The CADAsset handles exporting the subtree to a zcad file.
- CADAsset
- CADAssembly
- CADInstance
- CADPart
- CADBody
- CADBody
- CADInstance
- CADPart
- CADBody
- CADBody
- XRef
- PMI (TreeItem)
- Tolerances (TreeItem)
- PMIItem
- Text (GeomItem)
- Captures (TreeItem)
- PMIView
auto cadAsset = std::make_shared<CADAsset>("Name");
UInt32 flags = 0;
if (!streamGeoms) flags |= EXPORT_FLAG_EMBEDGEOMS;
float precision = 0.001;
cadAsset->exportBinary(tgtFilePath, precision, flags);
Handling Instances.
Parsing CADBodies
Currently, a CADBody cannot directly own a geometry. Instead it nests under it 1 or more GeomItem classes that own a single face, edge, vertex, or a merged set of faces, edges, or points.
auto cadBody = std::make_shared<CADBody>(bodyName, cadAsset);
auto cadFace = std::make_shared<GeomItem>("Faces");
cadFace->setGeometry(faceMesh);
cadFace->setMaterial(facesMaterial);
Parsing Meshes.
auto mesh = std::make_shared<Mesh>();
mesh->setNumVertices(vertexCount);
std::vector<UInt32> faceCounts(1);
faceCounts[0] = triangleCount;
mesh->setFaceCounts(faceCounts);
// For each triangle, set the face vertex indices.
for (int i = 0; i < fcount; ++i) {
std::vector<UInt32> faceVerts;
for (int j = 0; j < 3; ++j) {
faceVerts.push_back(theTS.CoordinateIndex(i, j));
}
mesh->setFaceVertexIndices(i, faceVerts);
}
// The mesh contains a positions attribute that stores all the vertex position values.
auto posAttr = mesh->getVertexAttributePtr<Attr<Vec3_32f>>("positions");
for (int i = 0; i < vcount; i++)
{
Vec3_32f pos((float)x, (float)y, (float)z);
posAttr->setValue(i, pos);
}
VertexAttr<Vec3_32f>* normalsAttr = dynamic_cast<VertexAttr<Vec3_32f>*>(mesh->addVertexAttribute("normals", "VertexAttr<Vec3_32f>"));
for (int face = 0; face < fcount; ++face) {
std::vector<UInt32> faceVerts;
for (int j = 0; j < 3; ++j) {
Vec3_32f normal((float)x, (float)y, (float)z);
normalsAttr->setFaceVertexValue(face, j, normal);
}
}
if (hasUvs) {
VertexAttr<Vec2_32f>* texCoordsAttr = dynamic_cast<VertexAttr<Vec2_32f>*>(mesh->addVertexAttribute("texCoords", "VertexAttr<Vec2_32f>"));
for (int face = 0; face < fcount; ++face) {
for (int j = 0; j < 3; ++j) {
Vec2_32f textureCoord(float)u, (float)v);
texCoordsAttr->setFaceVertexValue(setFaceVertexValue, j, textureCoord);
}
}
mesh->setFlag(GEOMFLAG_TEXTURED);
}