COMPASSi/trunk/code/projects/OCC/OCCLib/OCCMakeSolid.cpp

221 lines
4.8 KiB
C++

#include "Stdafx.h"
#include "OCCMakeSolid.h"
//#include "BOPTools_Tools2D.hxx" // XUEFENG DELETE 202009
#include "BRepPrimAPI_MakeHalfSpace.hxx"
#include "SplitAllInsectionCurves.h"
OCCMakeSolid::OCCMakeSolid(void)
{
}
OCCMakeSolid::OCCMakeSolid( vector<TopoDS_Shape> shapeSet )
{
this->shapeSet = shapeSet;
}
OCCMakeSolid::~OCCMakeSolid(void)
{
}
TopoDS_Shape OCCMakeSolid::GetResult()
{
return this->result;
}
void OCCMakeSolid::Perform()
{
TopoDS_Compound comp;
BRep_Builder builder;
builder.MakeCompound(comp);
TopoDS_Shape ss,s;
if(shapeSet.size() == 1)
{
vector<TopoDS_Shape> faceSet;
for(TopExp_Explorer ex(shapeSet[0], TopAbs_FACE); ex.More(); ex.Next())
{
faceSet.push_back(TopoDS::Face(ex.Current()));
}
TopoDS_Shell shell = SewShell(faceSet);
BRepBuilderAPI_MakeSolid newsolid = BRepBuilderAPI_MakeSolid(shell);
newsolid.Build();
result = newsolid.Solid();
return;
}
vector<gp_Pnt> pSet;
gp_Pnt center = ComputeSolidCenter();
for(int i = 0; i < shapeSet.size(); i++)
{
TopoDS_Shape bShape = shapeSet[i];
TopoDS_Compound c;
BRep_Builder b;
b.MakeCompound(c);
for(int j = 0; j < shapeSet.size(); j++)
{
if(i == j)
{
continue;
}
b.Add(c,shapeSet[j]);
}
TopoDS_Solid solid;
if(bShape.ShapeType() ==TopAbs_FACE)
{
BRepPrimAPI_MakeHalfSpace MHS(TopoDS::Face(bShape), center);
solid=MHS.Solid();
}
else if(bShape.ShapeType() ==TopAbs_SHELL)
{
BRepPrimAPI_MakeHalfSpace MHS(TopoDS::Shell(bShape), center);
solid=MHS.Solid();
}
if(solid.IsNull())
{
continue;
}
if(!IsReversed(solid, center))
{
solid.Reverse();
}
if(i == 0)
{
ss = solid;
continue;
}
BRepAlgoAPI_Common newcut(solid ,ss);
if(newcut.IsDone())
{
//builder.Add(comp, newcut.Shape());
ss = newcut.Shape();
}
//throw comp;
}
throw ss;
TopoDS_Compound compAll;
builder.MakeCompound(compAll);
for(int i = 0; i < shapeSet.size(); i++)
{
builder.Add(compAll, shapeSet[i]);
}
//throw compAll;
TopoDS_Compound comp1;
builder.MakeCompound(comp1);
for(TopExp_Explorer ex(comp, TopAbs_FACE);ex.More(); ex.Next())
{
builder.Add(comp1, TopoDS::Face(ex.Current()));
}
comp = comp1;
BRepAlgoAPI_Cut newcut(compAll ,comp);
if(!newcut.IsDone())
{
return;
}
s = newcut.Shape();
throw s;
vector<TopoDS_Shape> faceSet;
for(TopExp_Explorer ex(s, TopAbs_FACE); ex.More(); ex.Next())
{
faceSet.push_back(TopoDS::Face(ex.Current()));
}
TopoDS_Shell shell = SewShell(faceSet);
BRepBuilderAPI_MakeSolid newsolid = BRepBuilderAPI_MakeSolid(shell);
newsolid.Build();
result = newsolid.Solid();
}
gp_Pnt OCCMakeSolid::ComputeSolidCenter()
{
vector<gp_Pnt> pSet;
for(int i = 0; i < shapeSet.size(); i++)
{
TopoDS_Shape bShape = shapeSet[i];
TopoDS_Compound c;
BRep_Builder b;
b.MakeCompound(c);
for(int j = 0; j < shapeSet.size(); j++)
{
if(i == j)
{
continue;
}
b.Add(c,shapeSet[j]);
}
BRepAlgoAPI_Section asect(bShape,c,Standard_False);
asect.ComputePCurveOn1(Standard_True);
asect.Approximation(Standard_True);
asect.Build();
TopoDS_Shape splitResult = asect.Shape();
map<int, TopoDS_Edge> mapEdge;
for (TopExp_Explorer Ex(splitResult,TopAbs_EDGE); Ex.More(); Ex.Next())
{
mapEdge.insert(make_pair(mapEdge.size(), TopoDS::Edge(Ex.Current())));
}
std::vector<TopoDS_Edge> allWires;
std::vector<std::string> allWiresName;
for(map<int, TopoDS_Edge>::iterator iter = mapEdge.begin(); iter != mapEdge.end(); iter++)
{
allWires.push_back(iter->second);
std::string linename;
linename = std::to_string(i);
allWiresName.push_back(linename);
//builder.Add(compLine,faceBorder[i]);
}
// 将原面拆分 成网格面
CSplitAllInsectionCurves m_tool;
m_tool.Init(allWires,allWiresName);
if(m_tool.GetAllWires(false))
{
map<int, TopoDS_Edge> mapEdge;
std::map<int,vector<CResultData>> closedatas= m_tool.GetClosedResultDatas();
for(std::map<int,vector<CResultData>>::iterator iter= closedatas.begin(); iter != closedatas.end(); iter++)
{
vector<CResultData> wireDate = iter->second;
for(vector<CResultData>::iterator it = wireDate.begin(); it != wireDate.end(); it++)
{
CResultData data = *it;
double df,dl;
Handle(Geom_Curve) cv = BRep_Tool::Curve(m_tool.GetIWireByID(data.m_iWireID),df,dl);
Handle(Geom_TrimmedCurve) myTrimmed = new Geom_TrimmedCurve(cv, data.m_dStartRatio,data.m_dEndRatio);
TopoDS_Edge newEdge = BRepBuilderAPI_MakeEdge(myTrimmed);
gp_Pnt p1,p2;
GetEdgeStartEndPoint(newEdge, p1,p2);
pSet.push_back(p1);
pSet.push_back(p2);
}
}
}
}
gp_Pnt center(0,0,0);
for(int i = 0; i < pSet.size(); i++)
{
center = gp_Pnt(center.X() + pSet[i].X(), center.Y() + pSet[i].Y(), center.Z() + pSet[i].Z());
}
center = gp_Pnt(center.X() / pSet.size(), center.Y() / pSet.size(), center.Z() / pSet.size());
return center;
}