ObjectARX_多段線(多邊形,矩形,圓,圓弧)

運行效果:數據庫

命令的主要執行app

void ZffCHAP2AddPolyline()
{
// 建立僅包含一段直線的多段線
AcGePoint2d ptStart(0, 0), ptEnd(100, 100);
CCreateEnt::CreatePolyline(ptStart, ptEnd, 1);

//建立二維多義線
//設置頂點座標
AcGePoint3dArray ptArr;
ptArr.setLogicalLength(4);
for(int i = 0; i < 4; i++)
{
ptArr[i].set((double)(i/2),(double)(i%2),0.0);
}
CCreateEnt::Create2dPolyLine(ptArr);

// 建立三維多段線
AcGePoint3d pt1(0, 0, 0), pt2(100, 0, 0), pt3(100, 100, 0);
AcGePoint3dArray points;
points.append(pt1);
points.append(pt2);
points.append(pt3);
CCreateEnt::Create3dPolyline(points);

// 建立正多邊形
CCreateEnt::CreatePolygon(AcGePoint2d::kOrigin, 6, 30, 0, 1);

// 建立矩形
AcGePoint2d pt(60, 70);
CCreateEnt::CreateRectangle(pt, ptEnd, 1);

// 建立圓
pt.set(50, 50);
CCreateEnt::CreatePolyCircle(pt, 30, 1);

// 建立圓弧
CCreateEnt::CreatePolyArc(pt, 50, CCalculation::GtoR(45),
CCalculation::GtoR(225), 1);
}

封裝的一些函數函數

// 弧度轉化爲角度
double CCalculation::RtoG(double angle)
{
     return angle * 180 / CCalculation::PI();
}
// 角度轉化爲弧度
double CCalculation::GtoR(double angle)
{
     return angle * CCalculation::PI() / 180;
}
double CCalculation::Max(double a, double b)
{
     if (a > b)
    {
        return a;
    }
    else
    {
        return b;
    }
}
double CCalculation::Min(double a, double b)
{
    if (a < b)
    {
       return a;
    }
    else
    {
       return b;
    }
}
//旋轉
Acad::ErrorStatus CModifyEnt::Rotate(AcDbObjectId entId,
AcGePoint2d ptBase, double
rotation)
{
AcGeMatrix3d xform;
AcGeVector3d vec(0, 0, 1);
xform.setToRotation(rotation, vec, CCalculation::Pt2dTo3d(ptBase));
AcDbEntity *pEnt;
Acad::ErrorStatus es = acdbOpenObject(pEnt, entId, AcDb::kForWrite,false);
pEnt->transformBy(xform);
pEnt->close();
return es;
}
//移動
Acad::ErrorStatus CModifyEnt::Move(AcDbObjectId entId, AcGePoint3d ptBase,
AcGePoint3d ptDest)
{
// 設置變換矩陣的參數
AcGeMatrix3d xform;
AcGeVector3d vec(ptDest.x - ptBase.x, ptDest.y - ptBase.y,ptDest.z - ptBase.z);
xform.setToTranslation(vec);
AcDbEntity *pEnt;
Acad::ErrorStatus es = acdbOpenObject(pEnt, entId, AcDb::kForWrite,false);
pEnt->transformBy(xform);
pEnt->close();
return es;
}
//縮放
Acad::ErrorStatus CModifyEnt::Scale(AcDbObjectId entId,
AcGePoint3d ptBase, double scaleFactor)
{
// 設置變換矩陣的參數
AcGeMatrix3d xform;
xform.setToScaling(scaleFactor, ptBase);
AcDbEntity *pEnt;
Acad::ErrorStatus es = acdbOpenObject(pEnt, entId, AcDb::kForWrite,false);
pEnt->transformBy(xform);
pEnt->close();
return es;
}

多段線:.net

AcDbObjectId CCreateEnt::CreatePolyline(AcGePoint2d ptStart,
AcGePoint2d ptEnd, double width)
{
AcGePoint2dArray points;
points.append(ptStart);
points.append(ptEnd);
return CCreateEnt::CreatePolyline(points, width);
}

AcDbObjectId CCreateEnt::CreatePolyline(AcGePoint2dArray points, double width)
{
int numVertices = points.length(); 
AcDbPolyline *pPoly = new AcDbPolyline(numVertices);
for (int i = 0; i < numVertices; i++)
{
pPoly->addVertexAt(i, points.at(i), 0, width, width);
}
AcDbObjectId polyId;
polyId = CCreateEnt::PostToModelSpace(pPoly);
return polyId;
}

//獲得多段線頂點的座標
void GetPolyLineSelectLineIndex(AcDbPolyline *Poly,AcGePoint3d SelectPt,int Index) 
{ 
AcGePoint2d tempPt(SelectPt.x,SelectPt.y); 
double Distance=0.0;  
AcDbVoidPtrArray entitySet; 
Poly->explode(entitySet); //將曲線分解成單獨的份量。
for(int i=0;i<entitySet.length(); ++i)
{ 
    AcDbEntity *pEnt = AcDbPolyline::cast((AcRxObject*)entitySet[i]); 
    if(pEnt->isA()==AcDbLine::desc())
    { 
        AcDbLine *pLine=AcDbLine::cast(pEnt);
        AcGePoint2d StPt,EndPt; 
        StPt.x=pLine->startPoint().x; 
        StPt.y=pLine->startPoint().y;
        EndPt.x=pLine->endPoint().x; 
        EndPt.y=pLine->endPoint().y; 
        pLine->close(); 
        AcGeLine2d GeLine(StPt,EndPt); 
        double pama=0; 
        if((GeLine.isOn(tempPt,pama)==Adesk::kTrue)&&pama<=1&&pama>=0) 
        {
           Index=i; 
           break; 
        } 
        else 
        { 
            if(GeLine.distanceTo(tempPt)
            { 
                AcGeLine2d tempLine; 
                GeLine.getPerpLine(tempPt,tempLine);
                AcGePoint2d InsertPt; 
                GeLine.intersectWith(tempLine,InsertPt); 
                double pama=0.0; 
                if((GeLine.isOn(InsertPt,pama)==Adesk::kTrue)&&pama<=1&&pama>=0) 
                { 
                    Distance=GeLine.distanceTo(tempPt);
                    Index=i; 
                } 
            }
         }
     } 
     pEnt->close(); 
}

二維多義線:3d

AcDbObjectId CCreateEnt::Creat2dPolyLine(AcGePoint3dArray ptArr)
{
AcDb2dPolyLine * pPoly2d = new AcDb2dPolyLine(AcDb::k2dSimplePoly,ptArr,0.0,Adesk::kTrue);
return CCreateEnt::PostToModelSpace(pPoly2d );
}

三維多段線:code

AcDbObjectId CCreateEnt::Create3dPolyline(AcGePoint3dArray points)
{
AcDb3dPolyline *pPoly3d = new
AcDb3dPolyline(AcDb::k3dSimplePoly, points);
return CCreateEnt::PostToModelSpace(pPoly3d);
}

正多邊形:orm

AcDbObjectId CCreateEnt::CreatePolygon(AcGePoint2d ptCenter, int number,
double radius, double rotation, double width)
{
AcGePoint2dArray points;
double angle = 2 * CCalculation::PI() / (double)number;
for (int i = 0; i < number; i++)
{
AcGePoint2d pt;
pt.x = ptCenter.x + radius * cos(i * angle);
pt.y = ptCenter.y + radius * sin(i * angle);
points.append(pt);
}

AcDbObjectId polyId = CCreateEnt::CreatePolyline(points, width);

// 將其閉合
AcDbEntity *pEnt;
acdbOpenAcDbEntity(pEnt, polyId, AcDb::kForWrite);
AcDbPolyline *pPoly = AcDbPolyline::cast(pEnt);
if (pPoly != NULL)
{
pPoly->setClosed(Adesk::kTrue);
}

pEnt->close();
CModifyEnt::Rotate(polyId, ptCenter, rotation);
return polyId;
}

矩形:blog

AcDbObjectId CCreateEnt::CreateRectangle(AcGePoint2d pt1, AcGePoint2d pt2,
double width)
{
// 提取兩個角點的座標值
double x1 = pt1.x, x2 = pt2.x;
double y1 = pt1.y, y2 = pt2.y;

// 計算矩形的角點
AcGePoint2d ptLeftBottom(CCalculation::Min(x1, x2),
CCalculation::Min(y1, y2));
AcGePoint2d ptRightBottom(CCalculation::Max(x1, x2),
CCalculation::Min(y1, y2));
AcGePoint2d ptRightTop(CCalculation::Max(x1, x2),
CCalculation::Max(y1, y2));
AcGePoint2d ptLeftTop(CCalculation::Min(x1, x2),
CCalculation::Max(y1, y2));

// 建立對應的多段線
AcDbPolyline *pPoly = new AcDbPolyline(4);
pPoly->addVertexAt(0, ptLeftBottom, 0, width, width);
pPoly->addVertexAt(1, ptRightBottom, 0, width, width);
pPoly->addVertexAt(2, ptRightTop, 0, width, width);
pPoly->addVertexAt(3, ptLeftTop, 0, width, width);
pPoly->setClosed(Adesk::kTrue);

// 將多段線添加到模型空間
AcDbObjectId polyId;
polyId = CCreateEnt::PostToModelSpace(pPoly);
return polyId;
}

圓:get

AcDbObjectId CCreateEnt::CreatePolyCircle(AcGePoint2d ptCenter,
double radius, double width)
{
// 計算頂點的位置
AcGePoint2d pt1, pt2, pt3;
pt1.x = ptCenter.x + radius;
pt1.y = ptCenter.y;
pt2.x = ptCenter.x - radius;
pt2.y = ptCenter.y;
pt3.x = ptCenter.x + radius;
pt3.y = ptCenter.y;

// 建立多段線
AcDbPolyline *pPoly = new AcDbPolyline(3);
pPoly->addVertexAt(0, pt1, 1, width, width);
pPoly->addVertexAt(1, pt2, 1, width, width);
pPoly->addVertexAt(2, pt3, 1, width, width);
pPoly->setClosed(Adesk::kTrue);

// 將多段線添加到模型空間
AcDbObjectId polyId;
polyId = CCreateEnt::PostToModelSpace(pPoly);
return polyId;
}

圓弧:博客

AcDbObjectId CCreateEnt::CreatePolyArc(AcGePoint2d ptCenter, double radius,
double angleStart, double angleEnd, double width)
{
// 計算頂點的位置
AcGePoint2d pt1, pt2;
pt1.x = ptCenter.x + radius * cos(angleStart);
pt1.y = ptCenter.y + radius * sin(angleStart);
pt2.x = ptCenter.x + radius * cos(angleEnd);
pt2.y = ptCenter.y + radius * sin(angleEnd);

// 建立多段線
AcDbPolyline *pPoly = new AcDbPolyline(3);
pPoly->addVertexAt(0, pt1, tan((angleEnd - angleStart) / 4), width,width);
pPoly->addVertexAt(1, pt2, 0, width, width); 

// 將多段線添加到模型空間
AcDbObjectId polyId;
polyId = CCreateEnt::PostToModelSpace(pPoly);
return polyId;
}

CCreateEnt::PostToModelSpace:添加到圖形數據庫的模型空間參考另一篇博客:http://www.javashuo.com/article/p-wsbmywtn-ev.html

相關文章
相關標籤/搜索