[转] ObjectARX 创建并使用UCS

例子一

切换UCS。

How to programmatically create named UCS and set it current

https://adndevblog.typepad.com/autocad/2012/12/how-to-programmatically-create-named-ucs-and-set-it-current.html

		
		Acad::ErrorStatus es;
		AcDbUCSTableRecord *myUCS = new AcDbUCSTableRecord;
		//define your own ucs 
		AcGePoint3d  origin_point(0,0,0);
		AcGeVector3d UCSXaxis(0,1,0);
		AcGeVector3d UCSYaxis(1,0,0);
		myUCS->setOrigin(origin_point);
		myUCS->setXAxis(UCSXaxis);
		myUCS->setYAxis(UCSYaxis);
		es=myUCS->setName( _T("MyUCS"));
		if (es != Acad::eOk)
		{
			acutPrintf(_T("\nFailed to set name"));
			return;
		}
		AcDbObjectId UCSId;
		AcDbSymbolTable *pUCSTable;
		if (acdbHostApplicationServices()->workingDatabase()->
			getUCSTable(pUCSTable,AcDb::kForWrite)==Acad::eOk)
		{
			es=pUCSTable->add(UCSId,myUCS);
			es=pUCSTable->close();
			es= myUCS->close();
		}
		else
		{
			acutPrintf(_T("\nFailed to get UCS table"));
			return;
		}
		//To set the current UCS, I accessed 
		// the active AcDbViewportTableRecord
		// and used setUCS to set the UCS I created as current.
		AcDbViewportTable *pVT; 
		es = acedVports2VportTableRecords();
		if (es != Acad::eOk)
		{
			acutPrintf(_T("\nFailed to load vport info into vport table records"));
			return;
		}
		es=acdbHostApplicationServices()->
			workingDatabase()->getViewportTable(pVT,AcDb::kForRead);
		if (es != Acad::eOk)
		{
			acutPrintf(_T("\nFailed to get vport table"));
			pVT->close();
			return;
		}
		AcDbViewportTableIterator* pIter = NULL;
		es=pVT->newIterator(pIter);
		if (es != Acad::eOk)
		{
			acutPrintf(_T("\nFailed to get vport table"));
			pVT->close();
			delete pIter;
			return;
		}
		for (pIter->start();!pIter->done();pIter->step())
		{
			AcDbViewportTableRecord* pRec;
			//it should be open for write mode
			es=pIter->getRecord(pRec,AcDb::kForWrite); 
			if (es != Acad::eOk)
			{
				acutPrintf(
					_T("\nFailed to get vport table record"));
				pVT->close();
				pRec->close();
				delete pIter;
				return;
			} 
			TCHAR* name=NULL;
			es=pRec->getName(name);
			if (es != Acad::eOk)
			{
				acutPrintf(
					_T("\nFailed to get name from vport table"));
				pVT->close();
				pRec->close();
				delete pIter;
				return;
			} 
			if (_tcsicmp(name,_T("*ACTIVE"))==0)
			{
				es=pRec->setUcs(UCSId);
			}
			es=pRec->close();     
		}
		es=acedVportTableRecords2Vports(); //force update
		es=pVT->close();
		delete pIter;
		return ;

例子二

这个例子不更改ucs名称

Restore Previously Saved UCS Using ObjectARX

https://adndevblog.typepad.com/autocad/2012/07/restore-previously-saved-ucs-using-objectarx.html

ACHAR name[133];
// Get the UCS name to restore
if (RTNORM != acedGetString(0, ACRX_T("\nUCS to restore: "), name))
    return;
AcDbDatabase *pDb 
            = acdbHostApplicationServices()->workingDatabase();
AcDbUCSTable *pTable = NULL;
if (Acad::eOk != pDb->getUCSTable(pTable, AcDb::kForRead))
    return;
// Get the UCS table record knowing its name
AcDbUCSTableRecord *pUCS = NULL;
if (Acad::eOk != pTable->getAt(name, pUCS, AcDb::kForRead)) 
{
    acutPrintf(ACRX_T("\nCannot get UCS '%s'."), name); 
    pTable->close();
    return;
}
pTable->close();
// Get the UCS parameters
AcGePoint3d origin = pUCS->origin();
AcGeVector3d xDirection = pUCS->xAxis();
AcGeVector3d yDirection = pUCS->yAxis();
AcGeVector3d zDirection = xDirection.crossProduct(yDirection);
pUCS->close();
// Create the matrix for the UCS
AcGeMatrix3d matrix;
matrix.setCoordSystem(origin, xDirection, yDirection, zDirection);
// Activate the UCS
acedSetCurrentUCS(matrix);
posted @ 2022-03-21 15:14  edata  阅读(331)  评论(0编辑  收藏  举报