关于objectArx /CAD二次开发中“绘制点、线、圆、圆弧、多段线、矩形、文字等”

  • Post author:
  • Post category:其他


使用CAD软件绘图时,所有的图元均可由点、线、圆、圆弧、多段线、文字等构成,使用objectArx绘制图元时可调用相应的接口函数,如AcDbLine绘制直线。

1.绘制点

参数:pt(点的位置)

AcDbPoint* CreatePoint(const AcGePoint3d& pt)
{
	AcDbPoint* pPt = new AcDbPoint(pt);
	return pPt;
}

2.绘制直线

参数:ptStart(直线起点),ptEnd(直线终点)

AcDbLine* yEntity::CreateLine(const AcGePoint3d& ptStart, const AcGePoint3d& ptEnd)
{
	AcDbLine* pLine = new AcDbLine(ptStart, ptEnd);
	return pLine;
}

3.绘制圆

参数:ptCenter(圆心位置),dRadius(半径)

AcDbCircle* CreateCircle(const AcGePoint3d& ptCenter, double dRadius)
{
	AcDbCircle* pCircle = new AcDbCircle(ptCenter, AcGeVector3d(0, 0, 1), dRadius);
	return pCircle;
}

4.圆弧

三点画弧

参数:ptStart(圆弧起点),ptOnArc(圆弧上一点),ptEnd(圆弧终点)

AcDbArc* CreateArc(const AcGePoint3d& ptStart, const AcGePoint3d& ptOnArc, const AcGePoint3d& ptEnd)
{
	// 使用几何类获得圆心、半径;
	AcGeCircArc2d geArc(ptStart.convert2d(AcGePlane::kXYPlane), ptOnArc.convert2d(AcGePlane::kXYPlane), ptEnd.convert2d(AcGePlane::kXYPlane));
	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();
	AcDbArc* pArc = new AcDbArc(AcGePoint3d(ptCenter.x, ptCenter.y, 0), radius, startAngle, endAngle);

	return pArc;
}

圆心、起点,端点画弧

参数:ptStart(圆弧起点),ptCenter(圆心),ptEnd(圆弧终点)

AcDbArc* yEntity::CreateArcByPt2(const AcGePoint3d& ptCenter, const AcGePoint3d& ptStart,
	const AcGePoint3d& ptEnd)
{
	// 计算半径;
	double radius = ptCenter.distanceTo(ptStart);
	// 计算起始和终止角度;
	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();

	AcDbArc* pArc = new AcDbArc(ptCenter, radius, startAngle, endAngle);

	return pArc;
}

半径、起点,端点画弧

参数:ptStart(圆弧起点),dRadius(半径),ptEnd(圆弧终点)

AcDbArc* yEntity::CreateArc(const AcGePoint3d& ptStart, const AcGePoint3d& ptEnd, double dRadius)
{
	AcGeCircArc2d geCircle1(ptStart.convert2d(AcGePlane::kXYPlane), dRadius);
	AcGeCircArc2d geCircle2(ptEnd.convert2d(AcGePlane::kXYPlane), dRadius);
	AcGePoint3dArray arrptInter;
	int nNum = 0;
	bool bRet = yGeometry::GetIntersectPoint(geCircle1, geCircle2, nNum, arrptInter);
	if (!bRet)
		return NULL;
	AcGePoint3d ptCenter;	//圆心点
	if (1 == nNum)
	{
		AcGePoint3d pt = arrptInter[0];
		// 计算起始和终止角度;
		AcGeVector2d vecStart(ptStart.x - pt.x, ptStart.y - pt.y);
		AcGeVector2d vecEnd(ptEnd.x - pt.x, ptEnd.y - pt.y);
		double startAngle = vecStart.angle();
		double endAngle = vecEnd.angle();
		double dBulge = yEntity::GetArcBulge(startAngle, endAngle);
		if (0 > dBulge)
			return NULL;
		ptCenter = pt;
	}
	else if (2 == nNum)
	{
		AcGePoint3d pt1 = arrptInter[0];
		AcGePoint3d pt2 = arrptInter[1];
		// 计算起始和终止角度;
		AcGeVector2d vecStart(ptStart.x - pt1.x, ptStart.y - pt1.y);
		AcGeVector2d vecEnd(ptEnd.x - pt1.x, ptEnd.y - pt1.y);
		double startAngle = vecStart.angle();
		double endAngle = vecEnd.angle();
		double dBulge = yEntity::GetArcBulge(startAngle, endAngle);
		if (0 > dBulge)
		{
			vecStart = AcGeVector2d(ptStart.x - pt2.x, ptStart.y - pt2.y);
			vecEnd = AcGeVector2d(ptEnd.x - pt2.x, ptEnd.y - pt2.y);
			startAngle = vecStart.angle();
			endAngle = vecEnd.angle();
			dBulge = yEntity::GetArcBulge(startAngle, endAngle);
			if (0 > dBulge)
				return NULL;
			ptCenter = pt2;
		}
		else
		{
			ptCenter = pt1;
		}
	}
	// 计算起始和终止角度;
	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();
	AcDbArc* pArc = new AcDbArc(ptCenter, dRadius, startAngle, endAngle);

	return pArc;
}

5.多段线

参数:ptArr(点集)

AcDbPolyline* yEntity::CreatePolyline(const AcGePoint3dArray& ptArr)
{
	if (ptArr.length() < 2)
		return NULL;
	AcGePoint2dArray pt2ds;
	for (int i = 0; i < ptArr.length(); i++)
		pt2ds.append(ptArr[i].convert2d(AcGePlane::kXYPlane));
	//创建多段线
	AcDbPolyline* pPoly = new AcDbPolyline();
	for (int i = 0; i < pt2ds.length(); ++i)
		pPoly->addVertexAt(i, pt2ds[i]);
	return pPoly;
}

6.矩形

注意:矩形对角点由用户输入

AcDbObjectId yEntity::MakeRecktangle()
{
	AcDbPolyline *pPline = new AcDbPolyline(4);
	AcGePoint3d pt[5];
	acedGetPoint(NULL, _T("\n请指定区域第一点"), asDblArray(pt[0]));
	pt[2] = yEntity::RelativePt(pt[0], 10, -10);
	pt[1].x = pt[0].x;
	pt[1].y = pt[2].y;
	pt[3].x = pt[2].x;
	pt[3].y = pt[0].y;
	pt[4] = pt[0];
	for (int i = 0; i < 5; i++)
	{
		AcGePoint2d point;
		point.x = pt[i].x;
		point.y = pt[i].y;
		pPline->addVertexAt(i, point);
	}
	AcDbObjectId objId = yDataBase::addToModelSpace(pPline);

	int nRs;
	int track = 1, type;
	struct resbuf result;
	acutPrintf(_T("\n请指定区域另一点"));
	while (track > 0)
	{
		AcGePoint3d PT;
		nRs = acedGrRead(track, &type, &result);
		PT.x = result.resval.rpoint[X];
		PT.y = result.resval.rpoint[Y];
		AcDbPolyline *pPLine;
		Acad::ErrorStatus es = acdbOpenObject(pPLine, objId, AcDb::kForWrite);
		if (es != Acad::eOk)
			return objId;

		pt[1].y = PT.y;
		pt[3].x = PT.x;
		pt[2] = PT;

		for (int i = 0; i < 5; i++)
		{
			AcGePoint2d point;
			point.x = pt[i].x;
			point.y = pt[i].y;

			pPLine->setPointAt(i, point);
		}

		if (nRs != RTNORM)
		{
			pPLine->erase();
			pPLine->close();
			return objId;
		}
		if (type == 2)
		{
			track = 0;
		}
		if (type == 3)
		{
			track = 0;
		}
		pPLine->close();
	}

	return objId;
}

7.文字

实例 textId = InsertMText(AcGePoint3d(0,0,0), _T(“0”), _T(“ONEZERO”), 0.9*relHeight, AcDbMText::kBottomLeft, 1);

AcDbObjectId yEntity::InsertMText(
	const AcGePoint3d& ptInsert,//插入点
	CString Text,//文本
	CString style,//文字风格
	double height,//文字高度
	AcDbMText::AttachmentPoint Attachment,//文字对齐方式
	int colorIndex//文字颜色
)
{
	AcDbMText* pMText = new AcDbMText;
	//设置多行文字属性
	pMText->setTextHeight(height);
	pMText->setLocation(ptInsert);
	pMText->setContents(Text);
	pMText->setAttachment(Attachment);
	pMText->setColorIndex(colorIndex);
	pMText->setTextStyle(yEntity::GetTextStyleId(style));

	//添加到模型空间
	AcDbObjectId idMText = yDataBase::addToModelSpace(pMText);
	pMText->close();
	return idMText;
}



版权声明:本文为weixin_42796921原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。