找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1553|回复: 3

[教学]:如何在3D空间根据一组3D点创建一个2D的POLYLINE对象

[复制链接]

已领礼包: 145个

财富等级: 日进斗金

发表于 2002-2-25 03:06:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
How to create a new 2D polyline in 3D space?  
ID    17257  
Applies to:    AutoCAD 2000I
AutoCAD 2002
AutoCAD 2000

Date    1/23/2002  

This document is part of    Coordinates and Transformations   Polyline   AcGe (Geometry Library)   ObjectARX     


Question
How do I create a 2d polyline with a set of 3d points that lie in a plane?
Answer
The following code sample does this, please find the whole project attached.

下面的代码,朋友们可以提取几个有用的公共函数,如根据一个点数组中的所有3D点得到他们组成平面的NORMAL向量, 添加对象到块表记录中等等.
[php]
Adesk::Boolean obtainNormal( AcGePoint3dArray &wcsPts, AcGeVector3d &normal )
{
    /* Function endeavors to form 2 non-colinear vectors from points
    in the wcsPts array.
    Once found, the vectors comprise a plane and a normal can be calculated.
    This plane normal must then be perpendicular to vectors formed from
    the remaining points in wcsPts */

    AcGeVector3d xAxis = AcGeVector3d::kIdentity;
    normal = AcGeVector3d::kIdentity;

    int nPts = wcsPts.length();
    for( int i=0; i < nPts ; i++ ) {
        AcGeVector3d vec = wcsPts[(i+1)%nPts] - wcsPts;
        if( vec.isZeroLength() )
            continue;
        else if( !normal.isZeroLength() ) {
            if( vec.isPerpendicularTo( normal ))
                continue;
            else
                return Adesk::kFalse;
        }
            
        else if( xAxis.isZeroLength() )
            xAxis = vec;
        else if( !xAxis.isParallelTo(vec ) ) {
            normal = xAxis.crossProduct( vec ).normal();
        }
    }

    if( normal.isZeroLength() ) {
        if( xAxis.isZeroLength() )
            normal = AcGeVector3d::kZAxis;
        else
            normal = xAxis.perpVector();
    }
    return Adesk::kTrue;
}

Adesk::Boolean
gatherPoints( AcGePoint3dArray &wcsPts, AcGeVector3d &normal )
{
    ads_point prevPt;
    ads_point newPt;
    while( ads_getpoint(wcsPts.length() == 0 ? NULL : prevPt, "\nEnter point
", newPt ) == RTNORM ) {
        acdbUcs2Wcs(newPt, newPt, Adesk::kFalse );

        wcsPts.append( asPnt3d( newPt ) );
        asPnt3d( prevPt ) = asPnt3d( newPt );
        int nPts = wcsPts.length();
        if( nPts >= 2 ) {
            ads_point fromPt, toPt;
            asPnt3d(fromPt) = wcsPts[nPts-2];
            acdbWcs2Ucs(fromPt,fromPt,Adesk::kFalse );
            asPnt3d(toPt) = wcsPts[nPts-1];
            acdbWcs2Ucs(toPt,toPt,Adesk::kFalse );
            ads_grdraw(fromPt, toPt, 1, 0);
        }
    }


    return obtainNormal( wcsPts, normal );
}







void
constructPolyArx( AcGePoint3dArray &wcsPts, AcGeVector3d &normal,
AcDbPolyline*&pPoly)
{
    int nPts = wcsPts.length();
    pPoly = new AcDbPolyline( nPts );
    pPoly->setNormal(normal );

    AcGeMatrix3d mat;
    AcGeVector3d xAxis = normal.perpVector();
    AcGeVector3d yAxis = normal.crossProduct(xAxis);

    // need to transform pts from WCS into UCS of plane:
    // ucsPt = ucsMat.inverse() * wcsPt
    mat.setCoordSystem(
        AcGePoint3d::kOrigin,
        xAxis, yAxis, normal );
    mat = mat.inverse();

    for( int i=0; i < nPts; i++ ) {
        AcGePoint2d pt2d;
        AcGePoint3d pt3d;
        pt3d = mat * wcsPts;
        if( i==0 )
            pPoly->setElevation(pt3d.z);
        pt2d.x = pt3d.x;
        pt2d.y = pt3d.y;


        if( i == nPts-1 && wcsPts == wcsPts[0] )
            pPoly->setClosed(Adesk::kTrue );
        else
            pPoly->addVertexAt(i, pt2d );
    }
    return;
}


Adesk::Boolean
postToMs( AcDbEntity*pEnt)
{
   AcDbBlockTable* pBT;
   if (Acad::eOk != acdbCurDwg()->getBlockTable( pBT, AcDb::kForRead ))
    return Adesk::kFalse;

   AcDbBlockTableRecord* pBTR;
   Acad::ErrorStatus es =
    pBT->getAt( ACDB_MODEL_SPACE, pBTR, AcDb::kForWrite );
   pBT->close();
   if (Acad::eOk != es)
    return Adesk::kFalse;

   es = pBTR->appendAcDbEntity( pEnt );
   pBTR->close();
   return (Acad::eOk == es);
}




void utilsmyarxpoly()
{
// TODO: add your code here

    // function to gather 3d points, and make into a 2d polyline

    AcGePoint3dArray wcsPts;
    AcGeVector3d normal;

    Adesk::Boolean planar = gatherPoints( wcsPts, normal );
    if( !planar || wcsPts.length() < 1 )
        return;

    AcDbPolyline*pPoly;

    constructPolyArx(wcsPts, normal, pPoly );

    postToMs( pPoly );
    pPoly->close();
   

// end of function
}


// TODO: add your other functions here




// END

[/php]
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
发表于 2004-3-17 09:45:15 | 显示全部楼层
第一个函数for( int i=0; i= 2 ),还有第二个函数里的for( int i=0; isetElevation(pt3d.z); pt2d和pt3d还没有定义。
这个功能对我很有用,还有生成了这样2D的POLYLINE对象后,如何取得这个对象在WCS下的三维坐标。请晓东帮帮忙
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 145个

财富等级: 日进斗金

 楼主| 发表于 2004-3-17 10:31:00 | 显示全部楼层
最初由 wufengl 发布
[B]第一个函数for( int i=0; i= 2 ),还有第二个函数里的for( int i=0; isetElevation(pt3d.z); pt2d和pt3d还没有定义。
这个功能对我很有用,还有生成了这样2D的POLYLINE对象后,如何取得这个对象在WCS下的三维坐标。... [/B]


上面的问题是论坛显示的原因,我编辑了帖子,应该都可以显示全了。

2D POLYLINE平面的NORMAL有了,它的对象坐标系也就有了,剩下的就是坐标系变换了,使用AcGeMatrix3d::alignCoordSys :


  1. static AcGeMatrix3d

  2. alignCoordSys(

  3. const AcGePoint3d& fromOrigin,

  4. const AcGeVector3d& fromXAxis,

  5. const AcGeVector3d& fromYAxis,

  6. const AcGeVector3d& fromZAxis,

  7. const AcGePoint3d& toOrigin,

  8. const AcGeVector3d& toXAxis,

  9. const AcGeVector3d& toYAxis,

  10. const AcGeVector3d& toZAxis);

  11. fromOrigin Input origin point of the from coordinate system
  12. fromXAxis Input vector defining the x axis of the from coordinate system
  13. fromYAxis Input vector defining the y axis of the from coordinate system
  14. fromZAxis Input vector defining the z axis of the from coordinate system
  15. toOrigin Input origin point of the to coordinate system
  16. toXAxis Input vector defining the x axis of the to coordinate system
  17. toYAxis Input vector defining the y axis of the to coordinate system
  18. toZAxis Input vector defining the z axis of the to coordinate system
  19. Returns matrix that maps coordinate system with the origin fromOrigin and
  20. the axes fromXAxis, fromYAxis, and fromZAxis to coordinate system with
  21. the origin toOrigin and the axes toXAxis, toYAxis, and toZAxis


复制代码


可以得到两个坐标系间的转换矩阵,然后用AcGePoint3d::transfromBy 就可以得到另一个坐标系下对应的点的座标了。


  1. AcGePoint3d::transformBy Function AcGePoint3d &

  2. transformBy(

  3. const AcGeMatrix3d& leftSide);

  4. leftSide Input transform matrix

  5. Transform this pre-multiplying it the matrix leftSide.

  6. Returns the reference to this point.

  7. Result is the same as setToProduct(leftSide, *this).

论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2004-3-17 10:59:18 | 显示全部楼层
谢谢晓东这么快给了答复
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-9-21 13:43 , Processed in 0.333624 second(s), 35 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表