highflybird 发表于 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::ErrorStatusAcDbCurveToAcGeCurve(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::ErrorStatusAcDbCurveToAcGeCurve(AcGeCurve3d * &pGe,const AcDbCircle * pDbCircle)
{
pGe =new AcGeCircArc3d(pDbCircle->center(),pDbCircle->normal(),pDbCircle->radius());
return Acad::eOk;
}

// ELLIPSE
Acad::ErrorStatusAcDbCurveToAcGeCurve(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::ErrorStatusAcGeCurveToAcDbCurve(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);
}
pDb =new AcDbSpline(degree,bIsRational,pGe->isClosed(),periodic,controlPoints,knots,weights,0.0,pGe->knots().tolerance());
}
return Acad::eOk;
}
// POLYLINE
Acad::ErrorStatusAcDbCurveToAcGeCurve(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);
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 = 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;
}
}

雪山飞狐_lzh 发表于 2010-7-29 10:38:00

<p>polyline确实没考虑,谢谢<font color="#61b713" face="Verdana"><b>highflybird</b></font>的提醒</p>
<p>spline的处理要仔细看下了,以前的处理确实有问题</p>

highflybird 发表于 2010-7-29 10:46:00

<p>实际上我仔细研究了一下AcDb3dPolyline和AcGePolyline3d的转换问题,觉得如下问题应该考虑:</p>
<p>AcGePolyline3d没有拟合类型,这样如果AcDb转成AcGe的时候,在有拟合的情况下,就无从处理,因此只能具体情况具体处理了。</p>
<p>2dpolyline也类似。</p>
<p>有点需要说明的是:Arx跟C#的处理有点不一样的是,arx在转换后, 如果你不需要那个类了,你应该delete或者free你创建的。</p>
<p>&nbsp;</p>
<p>另参见我的:&nbsp;</p>
<p><font face="Verdana">http://www.objectarx.net/forum.php?mod=viewthread&amp;tid=4935&amp;extra=page%3D1</font></p>

河伯 发表于 2010-10-28 10:31:00

本帖最后由 作者 于 2010-10-28 14:22:34 编辑

楼主的曲线转换类包括了以下三部分:
1.转换Ge2d曲线为Db曲线
2.转换Ge3d曲线为Db曲线
3.转换Db曲线为Ge3d曲线

好像缺少了下面一些内容,不知能否添加上?
1.没有转换Db曲线为Ge2d曲线的方法
2.转换Ge2d曲线为Db曲线中,没有CompositeCurve2dToPolyline

另外,对于转换LineSegment2d为Line曲线,似乎执行下面函数就可以了:

public static Line ToLine(LineSegment2d ls2d)
      {
            Plane plane = ();
            return
                new Line(
                  new Point3d(plane, ls2d.StartPoint),
                  new Point3d(plane, ls2d.EndPoint));
      }
为何还要执行这一步,TransformBy在这里起什么作用?

public static Line ToLine(LineSegment2d ls2d, Matrix3d mat)
      {
            Line l = ToLine(ls2d);
            l.TransformBy(mat);
            return l;
      }



雪山飞狐_lzh 发表于 2010-10-28 19:34:00

<p>Db曲线为Ge2d曲线的方法没有做,当时感觉没有这种需求</p>
<p>TransformBy主要考虑三维的操作</p>

河伯 发表于 2010-10-28 19:51:00

<p><font face="Verdana">能否把这个先加上:CompositeCurve2dToPolyline</font></p>
<p><font face="Verdana"><font face="Verdana"><br/>&nbsp;</p></font></font>

雪山飞狐_lzh 发表于 2010-10-29 10:00:00

<p>这个可以直接参考</p>
<p><font face="Verdana">public static Polyline ToPolyline(this CompositeCurve3d cc3d)</font></p>
<p>CompositeCurve的处理有点郁闷,在DB库没有严格对应的对象</p>

雪山飞狐_lzh 发表于 2010-10-29 10:05:00

<p>另外,TlsBasal基本类库已更新</p>
<p><font face="Verdana"><a href="http://bbs.mjtd.com/forum.php?mod=viewthread&tid=75701&extra=&page=1">http://bbs.mjtd.com/forum.php?mod=viewthread&tid=75701&extra=&page=1</a></font></p>
<p>&nbsp;</p>

河伯 发表于 2010-10-29 10:25:00

<p>刚开始用VB.NET,只是初步,通过转换C#到VB.NET来学习AutoCAD .NET API。</p>
<p>目前DLL还用不上。CompositeCurve3dToPolyline和CompositeCurve2dToPolyline应该有些差别,放到以后再做吧。</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

chpmould 发表于 2010-10-31 18:04:00

学习了,一定要顶
页: 1 [2] 3
查看完整版本: 曲线转换类