#include "StdAfx.h"
#include "CreateEnt.h"
#include "Calculation.h"
CCreateEnt::CCreateEnt(void)
{
}
CCreateEnt::~CCreateEnt(void)
{
}
AcDbObjectId CCreateEnt::CreateLine(AcGePoint3d ptStart, AcGePoint3d ptEnd)
{
AcDbLine *pLine = new AcDbLine(ptStart, ptEnd);
// 将实体添加到图形数据库
AcDbObjectId lineId;
lineId = CCreateEnt::PostToModelSpace(pLine);
return lineId;
}
AcDbObjectId CCreateEnt::CreateCircle(AcGePoint3d ptCenter, AcGeVector3d vec, double radius)
{
AcDbCircle *pCircle = new AcDbCircle(ptCenter, vec, radius);
// 将实体添加到图形数据库
AcDbObjectId circleId;
circleId = CCreateEnt::PostToModelSpace(pCircle);
return circleId;
}
AcDbObjectId CCreateEnt::CreateCircle( AcGePoint3d ptCenter, double radius )
{
AcGeVector3d vec(0, 0, 1);
return CCreateEnt::CreateCircle(ptCenter, vec, radius);
}
AcDbObjectId CCreateEnt::PostToModelSpace(AcDbEntity* pEnt)
{
AcDbBlockTable *pBlockTable;
acdbHostApplicationServices()->workingDatabase()
->getBlockTable(pBlockTable, AcDb::kForRead);
AcDbBlockTableRecord *pBlockTableRecord;
pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord,
AcDb::kForWrite);
AcDbObjectId entId;
pBlockTableRecord->appendAcDbEntity(entId, pEnt);
pBlockTable->close();
pBlockTableRecord->close();
pEnt->close();
return entId;
}
AcDbObjectId CCreateEnt::CreateCircle(AcGePoint2d pt1, AcGePoint2d pt2)
{
// 计算圆心和半径
AcGePoint2d pt = CCalculation::MiddlePoint(pt1, pt2);
AcGePoint3d ptCenter(pt[X], pt[Y], 0); // 圆心
double radius = pt1.distanceTo(pt2) / 2;
// 创建圆
return CCreateEnt::CreateCircle(ptCenter, radius);
}
/*
AcDbObjectId CCreateEnt::CreateCircle(AcGePoint2d pt1, AcGePoint2d pt2,AcGePoint2d pt3)
{
// 使用数学方法
double xysm, xyse, xy;
AcGePoint3d ptCenter;
double radius;
xy = pow(pt1[X], 2) + pow(pt1[Y], 2);
xyse = xy - pow(pt3[X], 2) - pow(pt3[Y], 2);
xysm = xy - pow(pt2[X], 2) - pow(pt2[Y], 2);
xy = (pt1[X] - pt2[X]) * (pt1[Y] - pt3[Y]) - (pt1[X] - pt3[X]) * (pt1[Y] - pt2[Y]);
// 判断参数有效性
if (fabs(xy) < 0.000001)
{
AfxMessageBox("所输入的参数无法创建圆形!");
return 0;
}
// 获得圆心和半径
ptCenter[X] = (xysm * (pt1[Y] - pt3[Y]) - xyse * (pt1[Y] - pt2[Y])) / (2 * xy);
ptCenter[Y] = (xyse * (pt1[X] - pt2[X]) - xysm * (pt1[X] - pt3[X])) / (2 * xy);
ptCenter[Z] = 0;
radius = sqrt((pt1[X] - ptCenter[X]) * (pt1[X] - ptCenter[X]) +
(pt1[Y] - ptCenter[Y]) * (pt1[Y] - ptCenter[Y]));
if (radius < 0.000001)
{
AfxMessageBox("半径过小!");
return 0;
}
return CCreateEnt::CreateCircle(ptCenter, radius);
}
*/
AcDbObjectId CCreateEnt::CreateCircle(AcGePoint2d pt1, AcGePoint2d pt2, AcGePoint2d pt3)
{
// 使用几何类
AcGeCircArc2d geArc(pt1, pt2, pt3);
AcGePoint3d ptCenter(geArc.center().x, geArc.center().y, 0);
return CCreateEnt::CreateCircle(ptCenter, geArc.radius());
}
AcDbObjectId CCreateEnt::CreateArc(AcGePoint3d ptCenter, AcGeVector3d vec, double radius, double startAngle, double endAngle)
{
AcDbArc *pArc = new AcDbArc(ptCenter, vec, radius, startAngle, endAngle);
AcDbObjectId arcId;
arcId = CCreateEnt::PostToModelSpace(pArc);
return arcId;
}
AcDbObjectId CCreateEnt::CreateArc(AcGePoint2d ptCenter, double radius,
double startAngle, double endAngle)
{
AcGeVector3d vec(0, 0, 1);
return CCreateEnt::CreateArc(CCalculation::Pt2dTo3d(ptCenter),
vec, radius, startAngle, endAngle);
}
AcDbObjectId CCreateEnt::CreateArc(AcGePoint2d ptStart, AcGePoint2d ptOnArc,
AcGePoint2d ptEnd)
{
// 使用几何类获得圆心、半径
AcGeCircArc2d geArc(ptStart, ptOnArc, ptEnd);
AcGePoint2d ptCenter = geArc.center();
double radius = geArc.radius();
// 计算起始和终止角度
AcGeVector2d vecStart(ptStart.x - ptCenter.x, ptStart.y - ptCenter.y);
AcGeVector2d vecEnd(ptEnd.x - ptCenter.x, ptEnd.y - ptCenter.y);
double startAngle = vecStart.angle();
double endAngle = vecEnd.angle();
return CCreateEnt::CreateArc(ptCenter, radius, startAngle, endAngle);
}