找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1581|回复: 0

[分享] 以一个点打断曲线

[复制链接]

已领礼包: 13个

财富等级: 恭喜发财

发表于 2013-9-1 16:28:03 | 显示全部楼层 |阅读模式

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

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

×
To break a curve at a point, use the AcDbCurve::getSplitCurves method. All you need to know are some points or parameters where you want to break the curve. Here is a sample that lets you select a AcDbCurve (Line, Spline, Arc, ...) and a point near the curve. The point that is on the curve and closest to the selected point is chosen. It then splits the curve and adds the new curve segments to the model space.


static void ADSProjectBreak(void)
{
    AcDbObjectId id;  
    AcGePoint3d pick;  
    AcDbEntity* pEnt;  

    // Let the user select a curve  
    pEnt = selectEntity(
                            _T("\nSelect curve : "),
                            id,
                            pick,
                            AcDb::kForRead
                       );
    if(! pEnt)  
    {
        acutPrintf(_T("\nSelection failed "));   
        return;  
    }

    // Check that it is really a curve  
    AcDbCurve* pCurve = AcDbCurve::cast(pEnt);  
    if(! pCurve)  
    {   
        acutPrintf(L"\nYou didn't select a curve");   
        pEnt->close();   
        return;  
    }  

    // Get the break position and convert to WC coordinates  
    AcGePoint3d breakPnt;  
    if(acedGetPoint (     0,
                        L"\nPick a point on the curve: ",
                        asDblArray(breakPnt)
                    ) != RTNORM)  
    {
        pCurve->close();   
        return;  
    }  

    acdbUcs2Wcs (     asDblArray(breakPnt),
                    asDblArray(breakPnt),
                    Adesk::kFalse
                );

    // Check that the point is on the curve or
    // calculate the closest point tothe curve
    Acad::ErrorStatus es;
    double dist;  
    es = pCurve->getDistAtPoint(breakPnt, dist);  
    if(es != Acad::eOk)  
    {
        es = pCurve->getClosestPointTo(breakPnt, breakPnt);   
        if(es != Acad::eOk)   
        {      
            acutPrintf(
                        L"\ngetClosestPointTo failed : es =%s",
                        acadErrorStatusText(es)
                      );      
            pCurve->close();      
            return;   
        }  
    }  


    AcGePoint3dArray breakPoints;
    AcDbVoidPtrArray newCurves;
    // Get the segments according to the trim points  
    breakPoints.append(breakPnt);  
    es = pCurve->getSplitCurves(breakPoints, newCurves);  
    if(es != Acad::eOk)  
    {
        acutPrintf  (
                        L"\ngetSplitCurves failed : es = %s",
                        acadErrorStatusText(es)
                    );   
        pCurve->close();   
        return;  
    }  

    pCurve->close();  

    // Here we add the segements to the database with different colors  
    for(int i=0; i < newCurves.length(); i++)  
    {   
        pEnt = (AcDbEntity*)newCurves;   
        pEnt->setColorIndex(i+1);   
        appendToBlock(    pEnt,
                        id,
                        ACDB_MODEL_SPACE,
                        acdbHostApplicationServices()->workingDatabase());   
        pEnt->close();  
    }
}

static AcDbEntity* selectEntity(    const ACHAR* prompt,
                                    AcDbObjectId& id,
                                    AcGePoint3d& pick,
                                    AcDb::OpenMode openMode
                                )
{  
    AcDbEntity* ent = NULL;
    Acad::ErrorStatus es;  
    ads_name ename;
    if(acedEntSel(prompt, ename, asDblArray(pick)) != RTNORM)
        return ent;     

    if(acdbGetObjectId(id,ename) != Acad::eOk)
        return ent;

    if((es = acdbOpenAcDbEntity(ent,id,openMode)) != Acad::eOk)
    {
        acutPrintf(
                    _T("\nselectEntity : acdbOpenEntity failed es =%s"),
                    acadErrorStatusText(es)
                  );   
        return NULL;
    }  
    return ent;
}


static Acad::ErrorStatus appendToBlock(    AcDbEntity* pEnt,
                                        AcDbObjectId& idEnt,
                                        const ACHAR* pcBlockName,
                                        AcDbDatabase * pDb
                                      )
{
    // Default to current database  
    if(!pDb)   
        pDb= acdbHostApplicationServices()->workingDatabase();  

    Acad::ErrorStatus es;
    AcDbBlockTable      * pBlockTable = NULL;  
    // Get block table  
    es = pDb->getBlockTable( pBlockTable, AcDb::kForRead );
    if(es != Acad::eOk)
        return es;  

    // Find block record
    AcDbBlockTableRecord  * pBlockTableRecord = NULL;  
    es = pBlockTable->getAt (
                                pcBlockName,
                                pBlockTableRecord,
                                AcDb::kForWrite
                            );
    if(es != Acad::eOk)
    {
        pBlockTable->close();   
        return es;  
    }

    // append new entity to block:
    es = pBlockTableRecord->appendAcDbEntity( idEnt, pEnt );
    pBlockTableRecord->close();  
    pBlockTable->close();

    return es;
}




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

本版积分规则

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

GMT+8, 2024-4-26 22:24 , Processed in 0.245512 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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