objectarx area divided proportionally

Test results: this is proportionally 0.1,0.1,0.1,0.3,0.4 divided.

Plug Description: This is to select a widget mainly polyline poly, poly property disposed close to true, a poly Videos either side of a long line (line when drawn not intersect and poly), to find the vertical component of p'yXl line, this pyXl direction to go with the poly can intersect. In line with the poly find the tangent of the time. Then take each step value bujin translation, since the total area of the poly some small, so I get the total area totaArea / 2000 in prescribing, if the bujin <= 1, take 1 directly, so for the area large poly does not need translation many times. AcedTraceBoundary then is the use of the shifted function to obtain
a closed form poly tangent to polyline pl, pl find this area is larger and the proportion by area segmentation.

Local Note: When using acedTraceBoundary, the translation must be confirmed poly and tangents are close out, or can not find PL . If you do not acedTraceBoundary function, it can be:

//ads_point pt = { seedPoint.x,seedPoint.y,seedPoint.z };
    //acedCommandS(RTSTR, L"-boundary", RTPOINT, pt, RTSTR, L"", RTNONE);
    //acdbEntLast(pent);
    ////acedRedraw(pent, 3);
    //AcDbObjectId eId;

    //acdbGetObjectId(eId, pent);

    //AcDbPolyline * pEnt = NULL;

    //ErrorStatus es = acdbOpenObject(pEnt, eId, AcDb::OpenMode::kForWrite);

To obtain a closed polyline, and then find the area.

Details are given below Code:
seeking pyXl:

:: getDirection CAreaFenGe AcGeVector3d () 
{ 
    // end of the line is drawn in l1pt l2pt and poly sides 
    AcGeVector3d lineDirc = L2PT - l1Pt; 
    AcGeVector3d lineDirc2 = L2PT - l1Pt; 

    AcGePoint3d centerPt ((l2Pt.x + l1Pt.x) / 2 , (l2Pt.y l1Pt.y +) / 2 , 0 ); 
    AcGePoint3d ptCloset; 

    poly -> getClosestPointTo (centerPt, ptCloset, to false ); 

    AcDbLine * line1 :: = AcDbLine Cast (Line-> clone ()); 
    AcDbLine Cast :: = AcDbLine line2 * (Line-> clone ());
     Double len = ptCloset.distanceTo(centerPt);

    AcGeVector3d pyXl1 = lineDirc.rotateBy(-1 * CMathUtil::PI() / 2, AcGeVector3d::kZAxis);
    AcGeVector3d pyXl2 = lineDirc2.rotateBy(CMathUtil::PI() / 2, AcGeVector3d::kZAxis);

    line1->transformBy(AcGeMatrix3d::translation(pyXl1.normal()*len*1.5));
    line2->transformBy(AcGeMatrix3d::translation(pyXl2.normal()*len*1.5));
    AcGePoint3dArray p3dArr1, p3dArr2;

    poly->intersectWith(line1, AcDb::Intersect::kExtendArg, p3dArr1, 0, 0);
    poly-> IntersectWith (line2, AcDb :: :: kExtendArg the Intersect, p3dArr2, 0 , 0 ); 

    IF (p3dArr1.length ()> 0 ) {         
        pyXl = pyXl1;     
    } 
    the else  IF (p3dArr2.length ()> 0 ) {         
        pyXl = pyXl2; 
    } 
    the else { 
        pyXl = AcGeVector3d ( . 3 , - . 1 , 0 ); // this can comment 
    }
     Delete line1; 
    line1 = NULL;
     Delete  line2;
    line2= NULL;

    return pyXl;
}
View Code

Obtaining tangent:

//得到切线
AcGePoint3d CAreaFenGe::GetQieDian(AcDbLine * &line1)
{

    line1 = new AcDbLine(l1Pt, l2Pt);

    AcGePoint3dArray ptArr;

    while (ptArr.length() < 1) {

        poly->intersectWith(line1, AcDb::Intersect::kOnBothOperands, ptArr, 0, 0);

        line1->transformBy(AcGeMatrix3d::translation(pyXl.normal() * 1));

    }
    poly->close();

    if (ptArr.length() > 0)
        return ptArr[0];
    else 
        return AcGePoint3d :: kOrigin; 

}
View Code

Get area:

bool  CAreaFenGe::GetPyPolyline(AcGePoint3d seedPoint, double & a)
{

    AcDbVoidPtrArray ptrArr;
    ErrorStatus es = acedTraceBoundary(seedPoint, false, ptrArr);

    if (es != Acad::eOk) {
        acutPrintf(L"\nboundary=%d", es);
        return false;
    }

    AcDbPolyline * pl = NULL;
    pl = (AcDbPolyline*)ptrArr[0];

    pl->setColorIndex(1);

    pl->getArea(a);

    pl->erase();
    pl->close();
    pl = NULL;
    return true;

}

The main steps:

void CAreaFenGe::Command()
{

    ErrorStatus es;
    AcDbLine * lQieXian = NULL;
    AcGePoint3d ptQieDian = GetQieDian(lQieXian);

    acutPrintf(L"\nptQieDian=[%.2f,%.2f,%.2f]", ptQieDian.x, ptQieDian.y, ptQieDian.z);

    ptQieDian=ptQieDian.transformBy(AcGeMatrix3d::translation(pyXl.normal() * 5));
    acutPrintf(L"\nptQieDian2=[%.2f,%.2f,%.2f]", ptQieDian.x, ptQieDian.y, ptQieDian.z);

    if (ptQieDian.x == 0 && ptQieDian.y == 0) {
        return;
    }
    double bujin = sqrt(totalArea * 1 / 2000);

    if (bujin <= 1) {
        bujin = 1;
    }

    AcDbObjectId qxId;
    lQieXian->transformBy(AcGeMatrix3d::translation(pyXl.normal() * bujin*3));


    es = acdbOpenObject(poly, plId, AcDb::kForRead);

    AcGePoint3dArray ptJdArr;

    poly->intersectWith(lQieXian, AcDb::Intersect::kOnBothOperands, ptJdArr, 0, 0);


    if (ptJdArr.length() >= 2) {

        ptQieDian=AcGePoint3d((ptJdArr[0].x+ ptJdArr[1].x)/2, (ptJdArr[0].y + ptJdArr[1].y) / 2,0);

        ptQieDian= ptQieDian.transformBy(AcGeMatrix3d::translation(pyXl.normal() * -bujin*1.5));

    }
    poly->close();


    qxId = CDwgDataBaseUtil::PostToModelSpace(lQieXian);

    lQieXian->close();

    lQieXian = NULL;

    //AcDbPolyline * ptPy = new AcDbPolyline();

    //ptPy->setColorIndex(5);

    //AcDbObjectId ptId;

    AcGePoint3dArray ptJdArr2;


    for (int i = 0; i < (int)vecArea.size(); i++)
    {


        AcGePoint3d ptCopyQd = ptQieDian;

        double a = 0.0;
        
        while (a < vecArea[i]) {

            bool flag = GetPyPolyline(ptQieDian, a);

            if (!flag) {
                return;
            }
            es = acdbOpenObject(lQieXian, qxId, AcDb::kForWrite);
            lQieXian->transformBy(AcGeMatrix3d::translation(pyXl.normal() * bujin));

            ptJdArr2.removeAll();

            es = acdbOpenObject(poly, plId, AcDb::kForRead);
            poly->intersectWith(lQieXian, AcDb::Intersect::kExtendArg, ptJdArr2, 0, 0);

            poly->close();

            if (ptJdArr2.length() < 1) {

                lQieXian->close();
                break;
                
            }


            lQieXian->close();

        }

        acdbOpenObject(lQieXian, qxId, AcDb::kForWrite);

        es = acdbOpenObject(poly, plId, AcDb::kForRead);

         ptJdArr.removeAll();

        poly->intersectWith(lQieXian, AcDb::Intersect::kExtendArg, ptJdArr, 0, 0);

        AcGePoint3d preCenter;
        if (ptJdArr.length() >= 2) {

            AcDbLine* lFg = new AcDbLine(ptJdArr[0], ptJdArr[1]);

            preCenter = AcGePoint3d((ptJdArr[0].x + ptJdArr[1].x) / 2, (ptJdArr[0].y + ptJdArr[1].y) / 2, 0);
            lFg->setColorIndex(2);

            CDwgDataBaseUtil::PostToModelSpace(lFg);

            lFg->close();

        }
        else {

            poly->close();
            lQieXian->erase();
            lQieXian->close();
            lQieXian = NULL;
            //ptId = CDwgDataBaseUtil::PostToModelSpace(ptPy);
            return;

        }
    
        poly->close();

        AcDbPolyline *pCopyQx = (AcDbPolyline*)lQieXian->clone();

        pCopyQx->transformBy(AcGeMatrix3d::translation(pyXl.normal() *bujin));

        if (i != (int)vecArea.size() - 1) {


            es = acdbOpenObject(poly, plId, AcDb::kForRead);

            ptJdArr.removeAll();

            poly->intersectWith(pCopyQx, AcDb::Intersect::kExtendArg, ptJdArr, 0, 0);

            if (ptJdArr.length() >= 2) {
                ptQieDian = AcGePoint3d((ptJdArr[0].x + ptJdArr[1].x) / 2, (ptJdArr[0].y + ptJdArr[1].y) / 2, 0);

                ptQieDian = AcGePoint3d((preCenter.x + ptQieDian.x) / 2, (preCenter.y + ptQieDian.y) / 2, 0);

            }
            qxId = CDwgDataBaseUtil::PostToModelSpace(pCopyQx);

            pCopyQx->close();
            poly->close();
            pCopyQx = NULL;
        }
        lQieXian->ERASE (); 
        lQieXian -> Close (); 
        lQieXian = NULL; 

        // When running to the last area is not copied 
        IF (I == ( int ) vecArea.size () - . 1 ) { 

            Delete pCopyQx; 
            pCopyQx = NULL; 

            BREAK ; 
        } 
    } 

    / * IF (ptId.isNull ()) { 
        PTID = CDwgDataBaseUtil :: PostToModelSpace (ptPy); 
        ptPy-> Close (); 
    } * / 
}

Constructor:

CAreaFenGe::CAreaFenGe(ACHAR *fenGeStr, AcDbPolyline *&p, AcDbLine *&l)
{
    
    vector<CString> vec;

    CStringUtil::Split(W2T(fenGeStr), L",", vec, false);

    for (int i = 0; i < (int)vec.size(); i++)
    {
        vecFenGe.push_back(_wtof(vec.at(i)));
    }
    this->poly = p;
    this->line = l;

    l1Pt = l->startPoint();
    l2Pt = l->endPoint();

    if (poly->isClosed() == false) {
        poly->setClosed(true);
    }

    poly->getArea(totalArea);

    for (int i = 0; i < (int)vecFenGe.size(); i++)
    {
        vecArea.push_back(totalArea*vecFenGe.at(i));
        acutPrintf(L"%.2f", vecArea[i]);
    }
    GetDirection();
    plId = poly->objectId();
}
View Code

Guess you like

Origin www.cnblogs.com/HelloQLQ/p/12228308.html