注册 登录
明经CAD社区 返回首页

风树的个人空间 http://www.mjtd.com/?408117 [收藏] [复制] [分享] [RSS]

日志

提取选中的填充图案边界线(源码)

已有 1099 次阅读2013-12-20 22:50 |系统分类:应用

 给想了解的同学,提供参考。
(补充:AcGeCircArc3d转AcDbArc的正确方法已经找到,以注明
 //提取选中的填充图案边界线,并加入模型空间
void Pick_Up_Hatch_Boundary(void)
{
 std::vector<AcDbHatch*> store;
 Get_Hatch(store,false);
 AcDbHatch*pHatch=0;
 for( int i=0; i<store.size(); i++)
 {
pHatch=store[i];
std::vector<AcDbCurve*> curveSegments;
pick_up_hatch_boundary(curveSegments,pHatch);
AddToModelSpace(curveSegments);
close_entity(curveSegments);
pHatch->close();
}
//获得填充图案对象指针
//这个可以用其它函数代替
void Get_Hatch(std::vector<AcDbHatch*> &store,bool ForWrite)
{
        // create a resbuf which is filled with filter conditions.
 struct resbuf *filter = acutBuildList(RTDXF0, _T("HATCH"), RTNONE);
 ads_name ss;
 // ask users to select hatch entities.
 int res = acedSSGet(NULL, NULL, NULL, filter, ss);
 // release filter resbuf.
 acutRelRb (filter);
 if( res != RTNORM )
 {
 return;
 }

 long length=0l;
 acedSSLength (ss, &length);
 AcDbVoidPtrArray curveSegments, regions;
 for(long indexSS=0l; indexSS<length; indexSS++)
 {
ads_name eName;
res = acedSSName(ss, indexSS, eName);
if( res != RTNORM )
continue;

AcDbObjectId id;  
acdbGetObjectId(id, eName );
AcDbHatch*pHatch=NULL;   
if(ForWrite==1)
acdbOpenObject(pHatch, id, AcDb::kForWrite );
else
acdbOpenObject(pHatch, id, AcDb::kForRead );
if( pHatch == NULL ) 
continue;
store.push_back(pHatch);
 }
acedSSFree (ss);
return;

}
//复制AcDbHatch边界线
void Pick_Up_Hatch_Boundary(std::vector<AcDbCurve*> &curveSegments,AcDbHatch* pHatch)
{
assert(pHatch);
           if( AcDbHatch::cast(pHatch)==NULL ) 
return;
int nLoops = pHatch->numLoops();
for( int i=0;i<nLoops; i++ ) 
{
long loopType;
if( pHatch->loopTypeAt( i ) & AcDbHatch::kPolyline ) //处理多段线的
{             
AcGePoint2dArray vertices;
AcGeDoubleArray bulges;
pHatch->getLoopAt( i, loopType, vertices, bulges );    
int nVertices = vertices.length();
AcDbPolyline *pPoly = new AcDbPolyline(nVertices); 
for( int vx=0;vx<nVertices;vx++ ) 
{
double bulge = bulges.length() < nVertices ? 0.0: bulges[vx];
pPoly->addVertexAt( vx, vertices[vx], bulge );
}   
pPoly->setColorIndex(1);//随便给个颜色
curveSegments.push_back(pPoly);
}      
else 
{         
AcGePoint2dArray vertices;
AcGeDoubleArray bulges;
AcGeVoidPointerArray edgePtrs; 
AcGeIntArray edgeTypes;  
pHatch->getLoopAt(i, loopType,edgePtrs, edgeTypes ); 
for(int j=0; j<edgePtrs.length(); j++)
{
switch (edgeTypes[j]){
case AcDbHatch::kLine:
{
AcGeLineSeg3d *pGeLine3d = (AcGeLineSeg3d*)edgePtrs[j];
AcGePoint3d geP1, geP2;
geP1 = pGeLine3d->startPoint();
geP2 = pGeLine3d->endPoint();
AcDbLine *pLine = new AcDbLine(geP1, geP2);
pLine->setColorIndex(1);
curveSegments.push_back(pLine);
}
break;
case AcDbHatch::kCirArc:
{
AcGeCircArc3d *pGeArc = (AcGeCircArc3d*)edgePtrs[j];
if (pGeArc->isClosed())
{
AcDbCircle *pDb=0;
pDb = new AcDbCircle(pGeArc->center(),pGeArc->normal(),pGeArc->radius());
curveSegments.push_back(pDb);
}
else
{
AcDbArc *pDb=0;
pDb = new AcDbArc(pGeArc->center(),pGeArc->normal(),pGeArc->radius(),pGeArc->startAng(),pGeArc->endAng());
curveSegments.push_back(pDb);
pDb = new AcDbArc(pGeArc->center(),-pGeArc->normal(),pGeArc->radius(),-pGeArc->endAng(),-pGeArc->startAng());
curveSegments.push_back(pDb);
}
}
break;
case AcDbHatch::kEllArc:
{
//////////////////////////////////////////////////////////////////////////////////////////////////////////以下部分需要改动,见另一帖子
   AcGePoint3d geCenter;
AcGeVector3d geNorm,dMajorAxis, dMinorAxis;
double dMajorRadius, dMinorRadius;
double dStartAng, dEndAng;
AcGeEllipArc3d *pGeEllip = (AcGeEllipArc3d*)edgePtrs[j];
geCenter = pGeEllip->center();
dStartAng = pGeEllip->startAng();
dEndAng = pGeEllip->endAng();
geNorm = pGeEllip->normal();
dMajorAxis = pGeEllip->majorAxis();
dMinorAxis = pGeEllip->minorAxis();
dMajorRadius = pGeEllip->majorRadius();
dMinorRadius = pGeEllip->minorRadius();
AcDbEllipse *pEllip = new AcDbEllipse();
pEllip->set(geCenter,geNorm,dMajorAxis*dMajorRadius,dMinorRadius/dMajorRadius);
pEllip->setStartParam(dStartAng);
pEllip->setEndParam(dEndAng);
///////////////////////////////////////////////////////////////////////////////////////////////////////////以上部分需要改动,见另一帖子
if( pEllip->isNull() == Adesk::kTrue)
{
break;
}
else
{
pEllip->setColorIndex(1);
curveSegments.push_back(pEllip);
}
}
break;
case AcDbHatch::kSpline:
{
Adesk::Boolean bIsFixSpline;
AcGePoint3dArray fitPoints;
AcGeTol fitTol;
Adesk::Boolean bTangentsExist;
AcGeVector3d startTangent, endTangent;
int deg;
AcGeNurbCurve3d  *pGeSpline=(AcGeNurbCurve3d *)edgePtrs[j];
assert(pGeSpline);
deg = pGeSpline->degree();
bIsFixSpline = pGeSpline->getFitData(fitPoints,fitTol,bTangentsExist,startTangent,endTangent);
if( bIsFixSpline == Adesk::kTrue )
{
AcDbSpline *pSpline=new AcDbSpline();
pSpline->setFitData(fitPoints,deg,fitTol.equalVector(),startTangent,endTangent);
if( pSpline->isNull() == Adesk::kTrue)
{
//acutPrintf("\nFailed to create a spline.");
break;
}
else
{
pSpline->setColorIndex(1);
curveSegments.push_back(pSpline);
}
}
else
{
Adesk::Boolean rational,closed,periodic;
AcGePoint3dArray gePoints;
AcGeDoubleArray geWeights;
AcGePoint3d gePoint;
AcGeKnotVector geKnots;        
AcGeDoubleArray dKnots;

rational = pGeSpline->isRational();
closed = pGeSpline->isClosed();
periodic = Adesk::kFalse;
for(int k=0; k<pGeSpline->numControlPoints(); k++)
{
gePoints.append(pGeSpline->controlPointAt(k));
}
for(int k=0; k<pGeSpline->numWeights(); k++)
{
geWeights.append(pGeSpline->weightAt(k));
}
geKnots = pGeSpline->knots();                                                        
for(int k=0; k<geKnots.length(); k++)
{
dKnots.append(geKnots[k]);
}

AcDbSpline *pSpline=new AcDbSpline(deg,
rational,
closed,
periodic,
gePoints,
dKnots,
geWeights);
if( pSpline->isNull() == Adesk::kTrue)
{
//acutPrintf("\nFailed to create a spline.");
break;
}
else
{
pSpline->setColorIndex(1);
curveSegments.push_back(pSpline);
}
}
}
break;
default:
break;
}
}
}
}
 
                   
if (curveSegments.size()>1)
{
std::set< AcDbCurve* > linshi_;
linshi_.insert(curveSegments.begin(),curveSegments.end());
std::set< AcDbCurve* > object_pt_vector;
//删除悬挂线
Cad_Topo::delete_fly_line(object_pt_vector,linshi_,0);

curveSegments.clear();
curveSegments.insert(curveSegments.end(),object_pt_vector.begin(),object_pt_vector.end());
//排序:以任意一根线开始,将其排列成首尾相接的形式
//某些情况下不是必须的
//std::vector<AcDbCurve *> temp_store;
//Cad_Topo::find_connected_line(temp_store,curveSegments[0],curveSegments);
//curveSegments.swap(temp_store);
}

路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-10 06:10 , Processed in 0.087006 second(s), 15 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部