- UID
- 267086
- 积分
- 0
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2005-5-26
- 最后登录
- 1970-1-1
|
发表于 2005-7-31 17:19:34
|
显示全部楼层
//如何求PL线的自交点,并在自交点处把曲线打断(#include <DBPL.H>)
int PLineInterSelf()
{
//选择实体
ads_name ssName;
struct resbuf eb;
char strbuf[20];
eb.restype = 0; //实体类型
strcpy(strbuf, "LWPOLYLINE");
eb.resval.rstring = strbuf;
eb.rbnext = NULL;
ads_printf("\n 选择一条多义线<退出>:");
ads_ssget(NULL, NULL, NULL, &eb, ssName);
long ssLen = 0;
ads_sslength(ssName, &ssLen);
if(ssLen != 1)
{
ads_ssfree(ssName);
return 0;
}
//获得顶点个数
ads_name en;
AcDbPolyline *pPLine = NULL;
AcDbObjectId enId;
int nCount = 0;
ads_ssname(ssName, 0, en);
acdbGetObjectId(enId, en);
if (Acad::eOk == acdbOpenObject(pPLine, enId, AcDb::kForRead))
{
nCount = pPLine->numVerts(); //多义线顶点个数
pPLine->close(); //关闭实体
}
if (nCount < 3) //小于三个顶点的不处理
{
ads_ssfree(ssName);
return 0;
}
ads_ssfree(ssName); //释放选择集
//得到各个顶点坐标
int i;
float *x = new float[nCount];
float *y = new float[nCount];
AcGePoint2d pt;
if (Acad::eOk == acdbOpenObject(pPLine, enId, AcDb::kForRead))
{
for (i=0; i<nCount; i++)
{
pPLine->getPointAt(i, pt);
x = (float)pt.x;
y = (float)pt.y;
}
pPLine->close(); //关闭实体
}
//求交点
AcGePoint3d LinePoint1, LinePoint2, LinePoint3;
AcDbLine *pLine1 = NULL;
AcDbLine *pLine2 = NULL;
AcDbObjectIdArray LineIds;
AcGePoint3dArray interPoint;
int j;
float fErr = 1.0E-2f;
//得到顶点连成的直线
for (i=0; i<nCount-2; i++)
{
LinePoint1[X] = x;
LinePoint1[Y] = y;
LinePoint1[Z] = 0;
LinePoint2[X] = x[i+1];
LinePoint2[Y] = y[i+1];
LinePoint2[Z] = 0;
pLine1 = new AcDbLine(LinePoint1, LinePoint2);
for (j=i+1; j<nCount-1; j++)
{
LinePoint1[X] = x[j];
LinePoint1[Y] = y[j];
LinePoint1[Z] = 0;
LinePoint2[X] = x[j+1];
LinePoint2[Y] = y[j+1];
LinePoint2[Z] = 0;
pLine2 = new AcDbLine(LinePoint1, LinePoint2);
interPoint.setLogicalLength(0); //清空数组
pLine2->intersectWith(pLine1, AcDb::kOnBothOperands, interPoint);
delete[] pLine2;
pLine2 = NULL;
int nCountInter = interPoint.length();
if (nCountInter > 0)
{
//当交点是直线的顶点时,剔除
if (fabs(interPoint[0].x - x[j]) < fErr &&
fabs(interPoint[0].y - y[j]) < fErr )
{
continue;
}
else
{
ads_printf("\n****** x= %0.2f", interPoint[0].x);
ads_printf("\n****** y= %0.2f", interPoint[0].y);
}
}
}
delete[] pLine1;
pLine1 = NULL;
}
delete[] x;
x = NULL;
delete[] y;
y = NULL;
//求直线交点
int nCountLine = LineIds.length();
if (nCountLine < 2)
{
return 0;
}
return 1;
}
//编程思路:
1、选择实体
2、得到实体的各个顶点坐标
3、把各个相邻的顶点看作直线,依次求交点
4、把与顶点重合的交点剔除
//只做了求PL线的自交点,“并在自交点处把曲线打断”这句话我没理解。
//本人水平有限,调试了好半天。
//上面的代码,上传之后就把有些地方给自动改错了(不知道斑竹知道这个情况嘛,期望修改一下),所以我又加了个附件。txt格式的。 |
|