How to merge faces in open cascade so that the faces are meshed together

I have written some code to write open cascade ```TopoDS_Shape``` to a quadrilateral and triangle mesh in the fbx file format however i am experiencing some difficulty, I wish to reduce the complexity of the CAD model as to remove overly complex details like ridges, I was wondering how I can do this? I have seen that the function ```RWGltf_CafWriter::ToMergeFaces()``` exists in [How to decrease mesh number while STEP to GLB file conversion?](https://dev.opencascade.org/content/how-decrease-mesh-number-while-step-...) however i was wondering if there is some analogous function for the general ```TopoDS_Shape```

Mikhail Sazonov's picture

Try the algorithm ShapeUpgrade_UnifySameDomain.

Lyndon Alcock's picture

I'm attempting to use this Algoritm however it breaks my codebase, here is a minimal reproduceable example of how i have used it:
```
#include <iostream>
#include <fstream>
#include <ShapeUpgrade_UnifySameDomain.hxx>
#include <STEPControl_Reader.hxx>
#include <STEPControl_Writer.hxx>
#include <TopoDS.hxx>

int main()
{
const char* inputPath = "ball-valve.step";
const char* outputPath = "output.step";

// Initialize the STEP reader
STEPControl_Reader reader;
if (reader.ReadFile(inputPath) != IFSelect_RetDone)
{
std::cout << "Error: cannot read file." << std::endl;
return 1;
}

// Check the load status
reader.PrintCheckLoad(true, IFSelect_ItemsByEntity);
if (reader.TransferRoots() != reader.NbRootsForTransfer())
{
std::cout << "Error: roots transferred unsuccessfully." << std::endl;
return 1;
}

// Get the shape from the reader
TopoDS_Shape shape = reader.Shape();

// Perform "unify domains" algorithm
ShapeUpgrade_UnifySameDomain unifyDomains(shape);
unifyDomains.Build();
TopoDS_Shape unifiedShape = unifyDomains.Shape();

// Write the unified shape to STEP file
STEPControl_Writer writer;
writer.Transfer(unifiedShape, STEPControl_AsIs);
if (!writer.Write(outputPath))
{
std::cout << "Error: failed to write output file." << std::endl;
return 1;
}

return 0;
}
```
I am getting error at unifyDomains.Build()
```
Exception thrown at 0x00007FFAA0BFE468 (TKG2d.dll) in quad-remeshing-algorithm.exe: 0xC0000005: Access violation reading location 0x0000000000000000.
```
are there any examples to see how I should use the function

Mikhail Sazonov's picture

It seems right. May be there is some bug that is reproduced with your shape. You may report it in the bug tracker. But you should publish your shape to reproduce it.

Mikhail Sazonov's picture

By the way, always check IsDone status before calling unifyDomains.Shape().

Lahiru Dilshan's picture

Hi Mikhail,
In "ShapeUpgrade_UnifySameDomain", there is no " IsDone" function to check whether the operation is successfully completed.
There are no "HasErrors" or "HasWarnings" functions either.
I think we need to have exception handling for this class to capture the program crashes.

Regards,
Lahiru Dilshan.