一、工具效果
支持多段线工件排版优化。
二、关键代码
代码如下:
const double & stripWid = LB_NestConfig::STRIP_WIDTH;
const double & stripHei = LB_NestConfig::STRIP_HEIGHT;
const double & itemGap = LB_NestConfig::ITEM_GAP;
const bool & enRotation = LB_NestConfig::ENABLE_ROTATION;
if(enRotation)
RotateToMinBounds();
if(itemGap != 0) {
for(int ctr = 0; ctr < polygons.size(); ++ctr) {
polygons[ctr] = polygons[ctr].Shrinking(-itemGap);
}
}
SortByAreaDecreasing();
int stripNb = 0;
QVector<LB_Polygon2D> unPlaced = polygons;
QVector<LB_Polygon2D> operate;
while(!unPlaced.isEmpty())
{
stripNb++;
emit AddStrip();
unPlaced[0].SetLocation(0,0);
unPlaced[0].SetID(stripNb-1);
emit AddItem(unPlaced[0]);
LB_Polygon2D last = unPlaced[0];
operate.clear();
for(int i=1;i<unPlaced.size();++i) {
if(doNestWait)
{
aMutex.lock();
waitCondition.wait(&aMutex);
aMutex.unlock();
}
LB_Polygon2D &orb = unPlaced[i];
QVector<LB_Polygon2D> NFPS = NoFitPolygon(last,orb,false,false);
LB_Polygon2D nfp;
if(!NFPS.isEmpty()) {
nfp = NFPS.takeFirst();
}
else {
operate.append(orb);
continue;
}
// iterate the nfp, to find the most left position to place the polygon
int leftIndex = -1;
int left = stripWid;
for(int i=0;i<nfp.size();++i) {
orb.SetPosition(nfp[i],0);
if(orb.X()<0 || orb.X()+orb.Width()>stripWid)
continue;
if(orb.Y()<0 || orb.Y()+orb.Height()>stripHei)
continue;
if(orb.X()<left)
{
left = orb.X();
leftIndex = i;
}
}
if(leftIndex != -1) {
orb.SetPosition(nfp[leftIndex],0);
orb.SetID(stripNb-1);
emit AddItem(orb);
// get the hull of the polygons which have been placed
bool ret1 = orb.IsAntiClockWise();
bool ret2 = last.IsAntiClockWise();
if(ret1 != ret2) {
std::reverse(orb.begin(),orb.end());
}
last = last.United(orb);
}
else {
operate.append(orb);
}
}
unPlaced = operate;
}
emit NestEnd();
三、代码链接
https://download.csdn.net/download/u013083044/49176476