赞
踩
在对工程图中所有尺寸进行自动球标标注时,对尺寸位置的排序是一个很关键的问题。基本思路:
bool SortByView(const DimensionStruct &td1, const DimensionStruct &td2) { DimensionStruct d1, d2; d1 = td1; d2 = td2; float vcx1, vcx2, vcy1, vcy2; vcx1 = d1.GetViewCenter()->get(0); vcx2 = d2.GetViewCenter()->get(0); vcy1 = d1.GetViewCenter()->get(1); vcy2 = d2.GetViewCenter()->get(1); if (vcx1 > vcx2) { return false; //视图中心:d1在d2的右侧 } else if (IsEqual(vcx1, vcx2) && (vcy1 < vcy2)) { return false; //视图中心:d1在d2的下方 } return true; }
class DimensionStruct { private: pfcDimension2D_ptr dimension; pfcPoint3D_ptr location; pfcPoint3D_ptr symloc; double angle; double distance; pfcPoint3D_ptr viewcenter; xstring viewname; pfcView2D_ptr view2d; public: int Create(pfcDimension2D_ptr dim); pfcDimension2D_ptr GetDimension(); pfcPoint3D_ptr GetViewCenter(); pfcPoint3D_ptr GetSymLoc(); xstring GetViewName(); double GetAngle(); double GetDistance(); };
p-it>1
这一判定条件是避免 vector 只有一个元素时进行排序。这一点使用 vector进行测试的时候是没有异常的,这里有时候就会出问题。vector<DimensionStruct>::iterator it = dims.begin();
while (it < dims.end())
{
vector<DimensionStruct>::iterator p = it + 1;
while (p < dims.end())
{
if (p->GetViewName() == it->GetViewName())
p++;
else
break;
}
if (p - it > 1)
sort(it, p, SortByAngle);
it = p;
}
vector<DimensionStruct>::iterator it2 = dims.begin(); while (it2 != dims.end()) { vector<DimensionStruct>::iterator p = it2 + 1; double AngleBond = 5; double BondRadio = 5; while (p < dims.end()) { //动态扩展边界,降低相邻尺寸序号相差较大的概率 if (p->GetViewName() == it2->GetViewName() && p->GetAngle() - it2->GetAngle() < AngleBond) { p++; } else if (p->GetViewName() == it2->GetViewName() && p->GetAngle() - it2->GetAngle() < (AngleBond + BondRadio)) { AngleBond += BondRadio; } else break; } if (p - it2 > 1) { sort(it2, p, SortByDistance); //dims.begin没有前一节点,后面计算距离越界 if (it2 != dims.begin()) { double d1, d2; //计算当前片段起点和终点与片段前一点距离,如果终点距离较近,则将片段逆序 d1 = Cal_Distance(it2->GetSymLoc(), (it2 - 1)->GetSymLoc()); d2 = Cal_Distance((p - 1)->GetSymLoc(), (it2 - 1)->GetSymLoc()); if (d2 < d1) { reverse(it2, p); } } } it2 = p; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。