您的位置:首页 > 移动开发 > Objective-C

ObjectARX_符号表---->UCS

2018-02-06 00:00 162 查看
创建 UCS 来调整图形的参考系。

创建 UCS:

void ZffCHAP4NewUCS()
{
// 获得当前图形的UCS表
AcDbUCSTable *pUcsTbl = NULL;
acdbHostApplicationServices()->workingDatabase()->getUCSTable(pUcsTbl, AcDb::kForWrite);

// 定义UCS的参数
AcGePoint3d ptOrigin(0, 0, 0);
AcGeVector3d vecXAxis(1, 1, 0);
AcGeVector3d vecYAxis(-1, 1, 0);

// 创建新的UCS表记录
AcDbUCSTableRecord *pUcsTblRcd = new AcDbUCSTableRecord();

// 设置UCS的参数
Acad::ErrorStatus es = pUcsTblRcd->setName("NewUcs");
if (es != Acad::eOk)
{
delete pUcsTblRcd;
pUcsTbl->close();
return;
}

//AcDbUCSTableRecord 类的 setOrigin、setXAxis 和setYAxis 三个函数分别用来设置原点、X 轴和 Y 轴的方向。
pUcsTblRcd->setOrigin(ptOrigin);
pUcsTblRcd->setXAxis(vecXAxis);
pUcsTblRcd->setYAxis(vecYAxis);

// 将新建的UCS表记录添加到UCS表中
es = pUcsTbl->add(pUcsTblRcd);
if (es != Acad::eOk)
{
delete pUcsTblRcd;
pUcsTbl->close();
return;
}

// 关闭对象
pUcsTblRcd->close();
pUcsTbl->close();
}

设置为当前 UCS:

void ZffCHAP4SetCurUcs()
{
// 提示用户输入UCS的名称
char ucsName[40];
if (acedGetString(NULL, "\n输入用户坐标系的名称:", ucsName) != RTNORM)
return;

// 获得指定的UCS表记录
AcDbUCSTable *pUcsTbl = NULL;
acdbHostApplicationServices()->workingDatabase()->getUCSTable(pUcsTbl, AcDb::kForRead);
if (!pUcsTbl->has(ucsName))
{
pUcsTbl->close();
return;
}

AcDbUCSTableRecord *pUcsTblRcd = NULL;
pUcsTbl->getAt(ucsName, pUcsTblRcd, AcDb::kForRead);

// 获得UCS的变换矩阵
AcGeMatrix3d mat;// UCS 到 WCS 转换
AcGeVector3d vecXAxis, vecYAxis, vecZAxis;
vecXAxis = pUcsTblRcd->xAxis();
vecYAxis = pUcsTblRcd->yAxis();
vecZAxis = vecXAxis.crossProduct(vecYAxis);//两个矢量的向量积(根据 UCS 的 X、Y 轴方向获得 Z 方向。)
mat.setCoordSystem(pUcsTblRcd->origin(), vecXAxis,vecYAxis, vecZAxis);

// 关闭UCS表和UCS表记录
pUcsTblRcd->close();
pUcsTbl->close();

// 设置当前的UCS
acedSetCurrentUCS(mat);
}

移动UCS 的原点:

void ZffCHAP4MoveUcsOrigin()
{
// 获得当前UCS的变换矩阵
AcGeMatrix3d mat;
Acad::ErrorStatus es = acedGetCurrentUCS(mat);

// 根据变换矩阵获得UCS的参数
AcGePoint3d ptOrigin;
AcGeVector3d vecXAxis, vecYAxis, vecZAxis;
mat.getCoordSystem(ptOrigin, vecXAxis, vecYAxis, vecZAxis);

// 移动UCS的原点
AcGeVector3d vec(100, 100, 0);
ptOrigin += vec;

// 更新变换矩阵
mat.setCoordSystem(ptOrigin, vecXAxis, vecYAxis, vecZAxis);

// 应用新的UCS
acedSetCurrentUCS(mat);
}

UCS 绕 Z 轴旋转60°:

void ZffCHAP4RotateUcs()
{
// 获得当前UCS的变换矩阵
AcGeMatrix3d mat;
Acad::ErrorStatus es = acedGetCurrentUCS(mat);

// 根据变换矩阵获得UCS的参数
AcGePoint3d ptOrigin;
AcGeVector3d vecXAxis, vecYAxis, vecZAxis;
mat.getCoordSystem(ptOrigin, vecXAxis, vecYAxis, vecZAxis);

// 绕Z轴旋转60度
vecXAxis.rotateBy(60 * atan(1) * 4 / 180, vecZAxis);
vecYAxis.rotateBy(60 * atan(1) * 4 / 180, vecZAxis);

// 更新变换矩阵
mat.setCoordSystem(ptOrigin, vecXAxis, vecYAxis, vecZAxis);

// 应用新的UCS
acedSetCurrentUCS(mat);
}

在UCS 中创建实体:

void ZffCHAP4AddEntInUcs()
{
// 转换坐标系的标记
struct resbuf wcs, ucs;
wcs.restype = RTSHORT;
wcs.resval.rint = 0;
ucs.restype = RTSHORT;
ucs.resval.rint = 1;

// 提示用户输入直线的起点和终点
ads_point pt1, pt2;
if (acedGetPoint(NULL, "拾取直线的起点:", pt1) != RTNORM)
return;
if (acedGetPoint(pt1, "拾取直线的终点:", pt2) != RTNORM)
return;

// 将起点和终点坐标转换到WCS
acedTrans(pt1, &ucs, &wcs, 0, pt1);
acedTrans(pt2, &ucs, &wcs, 0, pt2);

// 创建直线
AcDbLine *pLine = new AcDbLine(asPnt3d(pt1), asPnt3d(pt2));
AcDbBlockTable *pBlkTbl = NULL;
acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlkTbl, AcDb::kForRead);
AcDbBlockTableRecord *pBlkTblRcd= NULL;
pBlkTbl->getAt(ACDB_MODEL_SPACE, pBlkTblRcd,AcDb::kForWrite);
pBlkTbl->close();
pBlkTblRcd->appendAcDbEntity(pLine);
pLine->close();
pBlkTblRcd->close();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: