找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 215|回复: 0

[每日一码] ARX实例代码 -- 重建填充的边界

[复制链接]

1

主题

0

回帖

33

积分

管理员

积分
33
发表于 2024-3-14 21:04:47 | 显示全部楼层 |阅读模式
  1. //-----------------------------------------------------------------------------
  2. // This is command 'RESTOREHATCHBOUNDARY, by Bill Zhang [2003-2-20], DevTech, Autodesk
  3. void BzhRestoreHatchBoundary()
  4. {
  5. // TODO: Implement the command
  6. // create a resbuf which is filled with filter conditions.
  7.   struct resbuf *filter = acutBuildList(RTDXF0, 采用T("HATCH"), RTNONE);
  8.   ads采用name ss;
  9.   // ask users to select hatch entities.
  10.   int res = acedSSGet(NULL, NULL, NULL, filter, ss);
  11.   // release filter resbuf.
  12.   acutRelRb (filter);
  13.   if( res != RTNORM )
  14.   {
  15.    acutPrintf(采用T("\nNo hatch entities selected!"));
  16.    return;
  17.   }
  18.   long length=0l;
  19.   acedSSLength (ss, &length);
  20.   for(long indexSS=0l; indexSS<length; indexSS++)
  21.   {
  22. ads采用name eName;
  23. res = acedSSName(ss, indexSS, eName);
  24.     if( res != RTNORM )
  25.   continue;
  26. AcDbObjectId id;
  27. acdbGetObjectId(id, eName );
  28. AcDbHatch*pHatch=NULL;  
  29. acdbOpenObject(pHatch, id, AcDb::kForRead );
  30. if( pHatch == NULL )
  31.         continue;
  32. // For each loop, draw the boundary.
  33. int nLoops = pHatch->numLoops();
  34. for( int i=0;i<nLoops; i++ )
  35. {
  36.   long loopType;
  37.   if( pHatch->loopTypeAt( i ) & AcDbHatch::kPolyline )
  38.   {        
  39.    AcGePoint2dArray vertices;
  40.    AcGeDoubleArray bulges;
  41.    pHatch->getLoopAt( i, loopType, vertices, bulges );   
  42.    int nVertices = vertices.length();
  43.    AcDbPolyline *pPoly = new AcDbPolyline(nVertices);
  44.    for( int vx=0;vx<nVertices;vx++ )
  45.    {
  46.     double bulge = bulges.length() < nVertices ? 0.0: bulges[vx];
  47.     pPoly->addVertexAt( vx, vertices[vx], bulge );
  48.    }  
  49.    // append it to the current space of AutoCAD database
  50.    pPoly->setColorIndex(1);
  51.    addAEntToTheCurrentSpaceAndClose(pPoly);
  52.   }     
  53.   else
  54.   {        
  55.    AcGePoint2dArray vertices;
  56.    AcGeDoubleArray bulges;
  57.    AcGeVoidPointerArray edgePtrs;
  58.    AcGeIntArray edgeTypes;
  59.    pHatch->getLoopAt(i, loopType,edgePtrs, edgeTypes );
  60.    for(int j=0; j<edgePtrs.length(); j++)
  61.    {
  62.     switch (edgeTypes[j]){
  63.     case AcDbHatch::kLine:
  64.      {
  65.       AcGeLineSeg3d *pGeLine3d = (AcGeLineSeg3d*)edgePtrs[j];
  66.       AcGePoint3d geP1, geP2;
  67.       geP1 = pGeLine3d->startPoint();
  68.       geP2 = pGeLine3d->endPoint();
  69.       AcDbLine *pLine = new AcDbLine(geP1, geP2);
  70.       pLine->setColorIndex(1);
  71.       addAEntToTheCurrentSpaceAndClose(pLine);
  72.      }
  73.      break;
  74.     case AcDbHatch::kCirArc:
  75.      {
  76.       AcGePoint3d geCenter;
  77.       double dRadius, dStartAng, dEndAng;
  78.       AcGeCircArc3d *pGeArc = (AcGeCircArc3d*)edgePtrs[j];
  79.       geCenter = pGeArc->center();
  80.       dRadius = pGeArc->radius();
  81.       dStartAng = pGeArc->startAng();
  82.       dEndAng = pGeArc->endAng();
  83.       double dAngDiff;
  84.       dAngDiff = fabs( dEndAng - dStartAng - atan(double(1))*8 );
  85.       if( dAngDiff > 1e-5 ) // It's an ARC.
  86.       {
  87.        AcDbArc *pArc = new AcDbArc(geCenter, dRadius, dStartAng, dEndAng);
  88.        pArc->setColorIndex(1);
  89.        addAEntToTheCurrentSpaceAndClose(pArc);
  90.       }
  91.       else // It's a circle.
  92.       {
  93.        AcGeVector3d geNorm = pGeArc->normal();
  94.        AcDbCircle *pCir = new AcDbCircle(geCenter, geNorm, dRadius);
  95.        pCir->setColorIndex(1);
  96.        addAEntToTheCurrentSpaceAndClose(pCir);
  97.       }
  98.      }
  99.      break;
  100.     case AcDbHatch::kEllArc:
  101.      {
  102.       AcGePoint3d geCenter;
  103.       AcGeVector3d geNorm,dMajorAxis, dMinorAxis;
  104.       double dMajorRadius, dMinorRadius;
  105.       double dStartAng, dEndAng;
  106.       AcGeEllipArc3d *pGeEllip = (AcGeEllipArc3d*)edgePtrs[j];
  107.       geCenter = pGeEllip->center();
  108.       dStartAng = pGeEllip->startAng();
  109.       dEndAng = pGeEllip->endAng();
  110.       geNorm = pGeEllip->normal();
  111.       dMajorAxis = pGeEllip->majorAxis();
  112.       dMinorAxis = pGeEllip->minorAxis();
  113.       dMajorRadius = pGeEllip->majorRadius();
  114.       dMinorRadius = pGeEllip->minorRadius();
  115.       AcDbEllipse *pEllip = new AcDbEllipse();
  116.       // Note: radiusRatio = dMinorRadius/dMajorRadius (must be within [0, 1])
  117.       pEllip->set(geCenter,geNorm,dMajorAxis*dMajorRadius,dMinorRadius/dMajorRadius);
  118.       pEllip->setStartParam(dStartAng);
  119.       pEllip->setEndParam(dEndAng);
  120.       if( pEllip->isNull() == Adesk::kTrue)
  121.       {
  122.        acutPrintf(采用T("\nFailed to create an ellipse."));
  123.        break;
  124.       }
  125.       else
  126.       {
  127.        pEllip->setColorIndex(1);
  128.        addAEntToTheCurrentSpaceAndClose(pEllip);
  129.       }
  130.      }
  131.      break;
  132.     case AcDbHatch::kSpline:
  133.      {
  134.       Adesk::Boolean bIsFixSpline;
  135.       AcGePoint3dArray fitPoints;
  136.       AcGeTol fitTol;
  137.       Adesk::Boolean bTangent**ist;
  138.       AcGeVector3d startTangent, endTangent;
  139.       int deg;
  140.       AcGeNurbCurve3d  *pGeSpline=(AcGeNurbCurve3d *)edgePtrs[j];
  141.       assert(pGeSpline);
  142.       deg = pGeSpline->degree();
  143.       bIsFixSpline = pGeSpline->getFitData(fitPoints,
  144.                fitTol,
  145.                bTangent**ist,
  146.                startTangent,
  147.                endTangent);
  148.       if( bIsFixSpline == Adesk::kTrue )
  149.       {
  150.        AcDbSpline *pSpline=new AcDbSpline();
  151.        pSpline->setFitData(fitPoints,
  152.             deg,
  153.             fitTol.equalVector(),
  154.             startTangent,
  155.             endTangent);
  156.        if( pSpline->isNull() == Adesk::kTrue)
  157.        {
  158.         acutPrintf(采用T("\nFailed to create a spline."));
  159.         break;
  160.        }
  161.        else
  162.        {
  163.         pSpline->setColorIndex(1);
  164.         addAEntToTheCurrentSpaceAndClose(pSpline);
  165.        }
  166.       }
  167.       else
  168.       {
  169.        Adesk::Boolean rational,closed,periodic;
  170.        AcGePoint3dArray gePoints;
  171.        AcGeDoubleArray geWeights;
  172.        AcGePoint3d gePoint;
  173.        AcGeKnotVector geKnots;
  174.        AcGeDoubleArray dKnots;
  175.        rational = pGeSpline->isRational();
  176.        closed = pGeSpline->isClosed();
  177.        periodic = Adesk::kFalse;
  178.        for(int k=0; k<pGeSpline->numControlPoints(); k++)
  179.        {
  180.         gePoints.append(pGeSpline->controlPointAt(k));
  181.        }
  182.        for(int k=0; k<pGeSpline->numWeights(); k++)
  183.        {
  184.         geWeights.append(pGeSpline->weightAt(k));
  185.        }
  186.        geKnots = pGeSpline->knots();      
  187.        for(int k=0; k<geKnots.length(); k++)
  188.        {
  189.         dKnots.append(geKnots[k]);
  190.        }
  191.        AcDbSpline *pSpline=new AcDbSpline(deg,
  192.            rational,
  193.            closed,
  194.            periodic,
  195.            gePoints,
  196.            dKnots,
  197.            geWeights);
  198.        if( pSpline->isNull() == Adesk::kTrue)
  199.        {
  200.         acutPrintf(采用T("\nFailed to create a spline."));
  201.         break;
  202.        }
  203.        else
  204.        {
  205.         pSpline->setColorIndex(1);
  206.         addAEntToTheCurrentSpaceAndClose(pSpline);
  207.        }
  208.       }
  209.      }
  210.      break;
  211.     default:
  212.      break;
  213.     }
  214.    }
  215.   }
  216. }
  217. pHatch->close();
  218. }
  219.   // free up the selection set
  220.   acedSSFree (ss);
  221. }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|膜结构网

GMT+8, 2024-12-29 20:35 , Processed in 0.130171 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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