- 积分
- 36616
- 明经币
- 个
- 注册时间
- 2010-7-10
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
发表于 2010-7-29 10:26:00
|
显示全部楼层
我也来发一个arx版本的,关于spline和3dpolyline,以及2dpolyline的处理比较复杂。
另外感觉楼主的两个地方考虑欠妥。
1、是对于polyline 在3d空间状态下的转换如果normal不是(0,0,1),会出问题。
2、对于spline的处理,用Nurb得到的如果转成AcDbSpline,在封闭情况下,会跟原来的不一致。
特别感谢楼主的代码,我转化为arx的时候从中受益颇多。-
- #include "Conversion.h"
- // LINE
- Acad::ErrorStatus AcDbCurveToAcGeCurve(AcGeCurve3d * &pGe,const AcDbLine * pLine)
- {
- pGe = new AcGeLineSeg3d(pLine->startPoint(), pLine->endPoint());
- return Acad::eOk;
- }
- Acad::ErrorStatus AcGeCurveToAcDbCurve(AcDbCurve * &pDb,const AcGeLineSeg3d * pGe)
- {
- pDb= new AcDbLine(pGe->startPoint(),pGe->endPoint());
- return Acad::eOk;
- }
- // ARC
- Acad::ErrorStatus AcDbCurveToAcGeCurve(AcGeCurve3d * &pGe,const AcDbArc * pDbArc)
- {
- pGe = new AcGeCircArc3d(
- pDbArc->center(),
- pDbArc->normal(),
- pDbArc->normal().perpVector(),
- pDbArc->radius(),
- pDbArc->startAngle(),
- pDbArc->endAngle());
- return Acad::eOk;
- }
- Acad::ErrorStatus AcGeCurveToAcDbCurve(AcDbCurve * &pDb,const AcGeCircArc3d * pGe)
- {
- if (pGe->isClosed())
- {
- pDb = new AcDbCircle(pGe->center(),pGe->normal(),pGe->radius());
- }
- else
- {
- pDb = new AcDbArc(pGe->center(),pGe->normal(),pGe->radius(),pGe->startAng(),pGe->endAng());
- }
- return Acad::eOk;
- }
- // CIRCLE
- Acad::ErrorStatus AcDbCurveToAcGeCurve(AcGeCurve3d * &pGe,const AcDbCircle * pDbCircle)
- {
- pGe = new AcGeCircArc3d(pDbCircle->center(),pDbCircle->normal(),pDbCircle->radius());
- return Acad::eOk;
- }
- // ELLIPSE
- Acad::ErrorStatus AcDbCurveToAcGeCurve(AcGeCurve3d * &pGe,const AcDbEllipse * pDb)
- {
- pGe = new AcGeEllipArc3d(
- pDb->center(),
- pDb->majorAxis(),
- pDb->minorAxis(),
- pDb->majorAxis().length(),
- pDb->minorAxis().length(),
- pDb->startAngle(),
- pDb->endAngle());
- return Acad::eOk;
- }
- Acad::ErrorStatus AcGeCurveToAcDbCurve(AcDbCurve * &pDb,const AcGeEllipArc3d * pGe)
- {
- pDb = new AcDbEllipse(
- pGe->center(),
- pGe->normal(),
- pGe->majorAxis()*pGe->majorRadius(),
- pGe->minorRadius()/pGe->majorRadius(),
- pGe->startAng(),
- pGe->endAng());
- return Acad::eOk;
- }
- // SPLINE
- Acad::ErrorStatus AcDbCurveToAcGeCurve(AcGeCurve3d * &pGe,const AcDbSpline * pSpline,bool isFit)
- {
- Acad::ErrorStatus es;
- int degree;
- double fitTolerance,controlPtTol,knotTol;
- Adesk::Boolean tangentsExist,tangentStartDef,tangentEndDef,bIsRational,bIsPeriodic,bIsClosed;
- AcGeVector3d startTangent,endTangent;
- AcGePoint3dArray controlPoints,fitPoints;
- AcGeDoubleArray knots,weights;
- bIsClosed = pSpline->isClosed();
- AcGeNurbCurve3d *pNurb = NULL;
- if (pSpline->hasFitData() && isFit)
- {
- AcGeTol tol;
- es = pSpline->getFitData(fitPoints,degree,fitTolerance,tangentsExist,startTangent,endTangent);
- if (es == Acad::eOk)
- {
- tangentStartDef = tangentsExist;
- tangentEndDef = tangentsExist;
- AcGeTol fitTol;
- pSpline->fitTolerance();
- fitTol.setEqualPoint(fitTolerance);
- if (tangentsExist)
- {
- pNurb = new AcGeNurbCurve3d(fitPoints,startTangent,endTangent,tangentStartDef,tangentEndDef,fitTol);
- }
- else
- {
- pNurb = new AcGeNurbCurve3d(fitPoints,fitTol);
- }
- }
- else
- {
- return Acad::eNotImplementedYet;
- }
- }
- else
- {
- es = pSpline->getNurbsData(degree,bIsRational,bIsClosed,bIsPeriodic,controlPoints,knots,weights,controlPtTol,knotTol);
- if (es == Acad::eOk)
- {
- if (bIsRational)
- {
- pNurb = new AcGeNurbCurve3d(degree,knots,controlPoints,weights,bIsPeriodic);
- }
- else
- {
- pNurb = new AcGeNurbCurve3d(degree,knots,controlPoints,bIsPeriodic);
- }
- }
- else
- {
- return Acad::eNotImplementedYet;
- }
- }
- bIsClosed?pNurb->makeClosed():pNurb->makeOpen();
- pGe = pNurb;
- return es;
- }
- Acad::ErrorStatus AcGeCurveToAcDbCurve(AcDbCurve * &pDb,const AcGeNurbCurve3d * pGe)
- {
- if (pGe->hasFitData())
- {
- AcGePoint3dArray fitPoints;
- AcGeTol fitTolerance;
- Adesk::Boolean tangentsExist;
- AcGeVector3d startTangent;
- AcGeVector3d endTangent;
- double tol;
- pGe->getFitData(fitPoints,fitTolerance,tangentsExist,startTangent,endTangent);
- pDb = new AcDbSpline(fitPoints,startTangent,endTangent,pGe->order(),fitTolerance.equalPoint());
- }
- else
- {
- int degree;
- Adesk::Boolean bIsRational,periodic;
- AcGePoint3dArray controlPoints;
- AcGeKnotVector knots1;
- AcGeDoubleArray weights;
- pGe->getDefinitionData(degree,bIsRational,periodic,knots1,controlPoints,weights);
- AcGeDoubleArray knots;
- for (int i = 0;i<knots1.length();i++)
- {
- knots.append(knots1[i]);
- }
- pDb =new AcDbSpline(degree,bIsRational,pGe->isClosed(),periodic,controlPoints,knots,weights,0.0,pGe->knots().tolerance());
- }
- return Acad::eOk;
- }
- // POLYLINE
- Acad::ErrorStatus AcDbCurveToAcGeCurve(AcGeCurve3d * &pGe,const AcDbPolyline * pPoly)
- {
- AcGeLineSeg3d *pLine = NULL;
- AcGeCircArc3d *pArc = NULL;
- AcGeVoidPointerArray GeCurves;
- for( int i = 0; i < pPoly->numVerts(); i++ )
- {
- if( pPoly->segType(i) == AcDbPolyline::kLine )
- {
- pLine = new AcGeLineSeg3d;
- pPoly->getLineSegAt(i, *pLine);
- GeCurves.append(pLine);
- }
- else if( pPoly->segType(i) == AcDbPolyline::kArc )
- {
- pArc = new AcGeCircArc3d;
- pPoly->getArcSegAt(i, *pArc);
- GeCurves.append(pArc);
- }
- }
- pGe = new AcGeCompositeCurve3d(GeCurves);
- return Acad::eOk;
- }
- Acad::ErrorStatus AcGeCurveToAcDbCurve(AcDbCurve * &pDb,const AcGeCompositeCurve3d * pGe)
- {
- AcGePoint3d startPnt,endPnt;
- if( pGe->hasEndPoint(endPnt) == Adesk ::kFalse ||
- pGe->hasStartPoint(startPnt) == Adesk::kFalse)
- {
- return Acad::eNotImplementedYet;
- }
- //get the plane of Curve3d
- AcGePlane plane;
- AcGeLine3d line;
- AcGePoint3d p1,p2,p3;
- if(pGe->isPlanar(plane))
- {
- if(pGe->isLinear(line)) //Oh,it's a little tricky!
- {
- line.getPerpPlane(startPnt,plane);
- plane.get(p1,p2,p3);
- plane.set(p2,p3-p2);
- }
- plane.get(p1,p2,p3);
- }
- else
- {
- return Acad::eNotImplementedYet;
- }
- //Creat a polyline
- AcDbPolyline *pPoly = new AcDbPolyline();
- AcGeVoidPointerArray curveList;
- pGe->getCurveList(curveList); //get all the segments
- AcGeCurve3d *pCurve = NULL;
- AcGeCircArc3d *pArc = NULL;
- int i;
- double b;
- AcGePoint2d pt;
- for(i = 0;i < curveList.length();i++)
- {
- pCurve = (AcGeCurve3d *) (curveList[i]);
- pCurve->hasStartPoint(startPnt);
- pt = startPnt.convert2d(plane);
- if (pCurve->isKindOf(AcGe::kCircArc3d))
- {
- pArc = (AcGeCircArc3d *)(pCurve);
- b = tan(0.25 * pArc->endAng());
- if (pArc->normal()!=plane.normal())
- {
- pPoly->addVertexAt(i,pt,-b);
- }
- else
- {
- pPoly->addVertexAt(i,pt,b);
- }
- }
- else
- {
- pPoly->addVertexAt(i,pt);
- }
- }
- if(!pGe->isClosed())
- {
- pt = endPnt.convert2d(plane);
- pPoly->addVertexAt(i,pt);
- }
- else
- {
- pPoly->setClosed(Adesk::kTrue);
- }
- //the most important step;
- AcGeMatrix3d xform;
- AcGeVector3d XAxis = p1-p2;
- AcGeVector3d YAxis = p3-p2;
- AcGeVector3d ZAxis = XAxis.crossProduct(YAxis);
- xform.setCoordSystem(p2,XAxis,YAxis,ZAxis);
- pPoly->transformBy(xform);
- pDb = pPoly;
- return Acad::eOk;
- }
- // POLYLINE3D
- Acad::ErrorStatus AcDbCurveToAcGeCurve(AcGeCurve3d * &pGe,const AcDb3dPolyline * pPoly3d,bool isFit)
- {
- AcGeVoidPointerArray GeCurves;
- AcGePoint3d pt1;
- AcGePoint3d pt2;
- double Param;
- pPoly3d->getEndParam(Param);
- GeCurves.setLogicalLength((int)Param);
- for (int i= 0; i < (int)Param;i++)
- {
- pPoly3d->getPointAtParam(i,pt1);
- pPoly3d->getPointAtParam(i+1,pt2);
- GeCurves[i] = new AcGeLineSeg3d(pt1,pt2);
- }
- if (!isFit)
- {
- pGe = new AcGeCompositeCurve3d(GeCurves);
- return Acad::eOk;
- }
- AcDbSpline *pSpline= NULL;
- pPoly3d->getSpline(pSpline);
- Acad::ErrorStatus es = AcDbCurveToAcGeCurve(pGe,pSpline);
- delete pSpline;
- pSpline = NULL;
- return es;
- }
- Acad::ErrorStatus AcGeCurveToAcDbCurve(AcDbCurve * &pDb,const AcGePolyline3d *pGe)
- {
- AcGePoint3dArray pts;
- for (int i = 0;i < pGe->numControlPoints();i++)
- {
- pts.append(pGe->controlPointAt(i));
- }
- pDb = new AcDb3dPolyline((AcDb::Poly3dType)pGe->type(),pts,pGe->isClosed());
- return Acad::eOk;
- }
- // POLYLINE2D
- Acad::ErrorStatus AcDbCurveToAcGeCurve(AcGeCurve3d * &pGe,const AcDb2dPolyline *pPoly2d,bool isFit)
- {
- AcDb::Poly2dType type;
- type=pPoly2d->polyType();
- AcDbPolyline * pLwpoly = NULL;
- Acad::ErrorStatus es;
- if ((type==AcDb::k2dSimplePoly)||(type==AcDb::k2dFitCurvePoly))
- {
- pLwpoly=new AcDbPolyline;
- es = pLwpoly->convertFrom((AcDbEntity *&)pPoly2d,Adesk::kFalse);
- if (es!=Acad::eOk)
- {
- delete pLwpoly;
- pLwpoly=NULL;
- return es;
- }
- es = AcDbCurveToAcGeCurve(pGe,pLwpoly);
- pLwpoly->close();
- return es;
- }
- else
- {
- AcGeVoidPointerArray GeCurves;
- AcGePoint3d pt1;
- AcGePoint3d pt2;
- double Param;
- pPoly2d->getEndParam(Param);
- AcGeLineSeg3d *pLine = NULL;
- for (int i= 0; i < (int)Param;i++)
- {
- pPoly2d->getPointAtParam(i,pt1);
- pPoly2d->getPointAtParam(i+1,pt2);
- pLine = new AcGeLineSeg3d(pt1,pt2);
- GeCurves.append(pLine);
- }
- pGe = new AcGeCompositeCurve3d(GeCurves);
- return Acad::eOk;
- }
- }
- // catch all for all other entity types.
- Acad::ErrorStatus AcDbCurveToAcGeCurve(AcGeCurve3d * &pGe,const AcDbCurve *pDbCurve)
- {
- if (pDbCurve->isKindOf(AcDbLine::desc()))
- {
- return AcDbCurveToAcGeCurve(pGe,(AcDbLine *)pDbCurve);
- }
- if (pDbCurve->isKindOf(AcDbArc::desc()))
- {
- return AcDbCurveToAcGeCurve(pGe,(AcDbArc *)pDbCurve);
- }
- if (pDbCurve->isKindOf(AcDbCircle::desc()))
- {
- return AcDbCurveToAcGeCurve(pGe,(AcDbCircle *)pDbCurve);
- }
- if (pDbCurve->isKindOf(AcDbEllipse::desc()))
- {
- return AcDbCurveToAcGeCurve(pGe,(AcDbEllipse *)pDbCurve);
- }
- if (pDbCurve->isKindOf(AcDbSpline::desc()))
- {
- return AcDbCurveToAcGeCurve(pGe,(AcDbSpline *)pDbCurve);
- }
- if (pDbCurve->isKindOf(AcDbPolyline::desc()))
- {
- return AcDbCurveToAcGeCurve(pGe,(AcDbPolyline *)pDbCurve);
- }
- if (pDbCurve->isKindOf(AcDb3dPolyline::desc()))
- {
- return AcDbCurveToAcGeCurve(pGe,(AcDb3dPolyline *)pDbCurve);
- }
- if (pDbCurve->isKindOf(AcDb2dPolyline::desc()))
- {
- return AcDbCurveToAcGeCurve(pGe,(AcDb2dPolyline *)pDbCurve);
- }
- return Acad::eNotImplementedYet;
- }
- Acad::ErrorStatus AcDbCurveToAcGeCurve(AcGeCurve3d * &pGe,const AcDbEntity *pEnt)
- {
- if (pEnt->isKindOf(AcDbCurve::desc()))
- {
- return AcDbCurveToAcGeCurve(pGe,(AcDbCurve *)pEnt);
- }
- return Acad::eNotImplementedYet;
- }
- Acad::ErrorStatus AcGeCurveToAcDbCurve(AcDbCurve * &pDb,const AcGeCurve3d * pGe)
- {
- AcGe::EntityId type = pGe->type();
- switch (type)
- {
- case AcGe::kLineSeg3d:
- return AcGeCurveToAcDbCurve(pDb,(AcGeLineSeg3d *) pGe);
- case AcGe::kCircArc3d:
- return AcGeCurveToAcDbCurve(pDb,(AcGeCircArc3d *) pGe);
- case AcGe::kEllipArc3d:
- return AcGeCurveToAcDbCurve(pDb,(AcGeEllipArc3d *) pGe);
- case AcGe::kNurbCurve3d:
- return AcGeCurveToAcDbCurve(pDb,(AcGeNurbCurve3d *) pGe);
- case AcGe::kCompositeCrv3d:
- return AcGeCurveToAcDbCurve(pDb,(AcGeCompositeCurve3d*) pGe);
- case AcGe::kPolyline3d:
- return AcGeCurveToAcDbCurve(pDb,(AcGePolyline3d *) pGe);
- default:
- return Acad::eNotImplementedYet;
- }
- }
复制代码 |
评分
-
参与人数 1 | 威望 +1 |
明经币 +2 |
金钱 +10 |
贡献 +10 |
激情 +10 |
收起
理由
|
雪山飞狐_lzh
| + 1 |
+ 2 |
+ 10 |
+ 10 |
+ 10 |
【好评】好程序 受教了! 找个时间把Bug修 |
查看全部评分
|