Collision Detection working ;)

//Here we have the collision detection code for two part STEP assembly

// read step assembly

STEPControl_Reader reader;

reader.ReadFile("C:/Users/Desktop/assembly.STEP");

reader.TransferRoot();

Standard_Integer aCount = reader.NbRootsForTransfer();

Handle(TColStd_HSequenceOfTransient) list = reader.GiveList();

TopoDS_Shape STEPshape = reader.OneShape();

// split step part into seperate solids

TopTools_IndexedMapOfShape mapOfShapes;

TopExp::MapShapes(STEPshape, TopAbs_SOLID, mapOfShapes);

std::cout << mapOfShapes.Extent() << " solids found in STEP file." << std::endl;

// solid to shapes

TopoDS_Solid solid1 = TopoDS::Solid(mapOfShapes.FindKey(1));

TopoDS_Solid solid2 = TopoDS::Solid(mapOfShapes.FindKey(2));

// making mesh for the 2 shapes to prepare the shapes for proximity test 

const Standard_Real aLinearDeflection = 0.01;

const Standard_Real anAngularDeflection = 0.01;

BRepMesh_IncrementalMesh aMesh1(shape1, aLinearDeflection, Standard_False, anAngularDeflection);

BRepMesh_IncrementalMesh aMesh2(shape2, aLinearDeflection, Standard_False, anAngularDeflection);

TopoDS_Compound compound;

BRep_Builder aBuilder;

aBuilder.MakeCompound(compound);

aBuilder.Add(compound, shape1);

 

TopoDS_Compound compound2;

BRep_Builder aBuilder2;

aBuilder2.MakeCompound(compound2);

aBuilder2.Add(compound2, shape2);

Standard_Boolean done = 0;

 

//Perform proximity test

BRepExtrema_ShapeProximity proximity(compound, compound2, 0.001);

proximity.Perform();

// initializing compound for store collision faces

TopoDS_Builder aCompBuilder;

TopoDS_Compound CollisionFaceCompound;

aCompBuilder.MakeCompound(CollisionFaceCompound);

// if proximity test is successful 

if (proximity.IsDone())

{

 

// gives collision collision face and edges of shape1 

 

for (BRepExtrema_MapOfIntegerPackedMapOfInteger::Iterator 

anIt1(proximity.OverlapSubShapes1()); anIt1.More(); anIt1.Next())

{

const TopoDS_Face& aFace1 = proximity.GetSubShape1(anIt1.Key());

TopoDS_Shape F1 = aFace1;

 

// gives collision collision face and as well as edges of shape2, so we use BRepAlgoAPI_Common to //take common faces alone

for (BRepExtrema_MapOfIntegerPackedMapOfInteger::Iterator anIt2(proximity.OverlapSubShapes2()); anIt2.More(); anIt2.Next())

{

const TopoDS_Face& aFace2 = proximity.GetSubShape2(anIt2.Key());

/// int this step we take 1st shape's collision face is in "aFace1" and compare with all the collision ///faces of second shape

//and BRepAlgoAPI_Common return exact collision faces 

TopoDS_Shape S1 = BRepAlgoAPI_Common(aFace1, aFace2);

aCompBuilder.Add(CollisionFaceCompound, S1);

}

 

}

 

}

 

//// TopoDS_Shape S1 has exact faces of collision face of the 2 parts step assembly