天天看點

ObjectARX(VC)-符号表之視口-建立4個等大的視口“MustSwitchTo_acedCommandC_or_acedCommandS”: 未聲明的辨別符

(1)注冊一個指令AAAMyGroupCreate4VPorts()

(2)獲得視口表

AcDbViewportTable *pVPortTbl = NULL;
acdbHostApplicationServices()->workingDatabase()
	->getViewportTable(pVPortTbl, AcDb::kForWrite);//使用“寫”的模式打開資料庫的Viewport table
           

(3)分别建立四個視口

AcGePoint2d pt1, pt2;
		AcDbViewportTableRecord *pVPortTblRcd1 = new AcDbViewportTableRecord;
		pt1.set(0, 0);
		pt2.set(0.5, 0.5);
		pVPortTblRcd1->setLowerLeftCorner(pt1);//設定左下角的點
		pVPortTblRcd1->setUpperRightCorner(pt2);//設定右下角的點
		pVPortTblRcd1->setName(TEXT("4VPorts"));

		AcDbViewportTableRecord *pVPortTblRcd2 = new AcDbViewportTableRecord;
		pt1.set(0.5, 0);
		pt2.set(1, 0.5);
		pVPortTblRcd2->setLowerLeftCorner(pt1);
		pVPortTblRcd2->setUpperRightCorner(pt2);
		pVPortTblRcd2->setName(TEXT("4VPorts"));

		AcDbViewportTableRecord *pVPortTblRcd3 = new AcDbViewportTableRecord;
		pt1.set(0, 0.5);
		pt2.set(0.5, 1);
		pVPortTblRcd3->setLowerLeftCorner(pt1);
		pVPortTblRcd3->setUpperRightCorner(pt2);
		pVPortTblRcd3->setName(TEXT("4VPorts"));


		AcDbViewportTableRecord *pVPortTblRcd4 = new AcDbViewportTableRecord;
		pt1.set(0.5, 0.5);
		pt2.set(1, 1);
		pVPortTblRcd4->setLowerLeftCorner(pt1);
		pVPortTblRcd4->setUpperRightCorner(pt2);
		pVPortTblRcd4->setName(TEXT("4VPorts"));
           

(4)将新建立的視口添加到視口表pVPortTbl

pVPortTbl->add(pVPortTblRcd1);
		pVPortTbl->add(pVPortTblRcd2);
		pVPortTbl->add(pVPortTblRcd3);
		pVPortTbl->add(pVPortTblRcd4);
           

(5)關閉視口表

pVPortTbl->close();
           

(6)關閉視口表記錄

pVPortTblRcd1->close();
		pVPortTblRcd2->close();
		pVPortTblRcd3->close();
		pVPortTblRcd4->close();
           

(7)判斷目前的空間

struct resbuf rb;
		acedGetVar(TEXT("TILEMODE"), &rb);//指定TILEMODE為AutoCAD為系統變量
		//TILEMODE:Accesses the current TILEMODE value for the database. The value of false is 0. The value of true is 1. 
		if (rb.resval.rint == 1)//目前工作空間是模型空間
		{
			acedCommandS(RTSTR, TEXT(".-VPORTS"), 
				RTSTR, TEXT("R"),
				RTSTR, TEXT("4VPorts"),
				RTNONE);
			
		}
		else//目前工作空間是圖紙空間
		{
			acedCommandS(RTSTR, TEXT(".-VPORTS"),
				RTSTR, TEXT("R"),
				RTSTR, TEXT("4VPorts"),
				RTSTR, TEXT(""), RTNONE);
		}
           

注意:arx 2015之後已經将acedCommand函數更新為acedCommandS函數,使用該函數需要添加頭檔案”acedCmdNF.h”

如果使用acedCommand函數會報錯:

“MustSwitchTo_acedCommandC_or_acedCommandS”: 未聲明的辨別符

(8)效果

ObjectARX(VC)-符号表之視口-建立4個等大的視口“MustSwitchTo_acedCommandC_or_acedCommandS”: 未聲明的辨別符

(9)項目源代碼:

static void AAAMyGroupCreate4VPorts() {
		// Put your command code here
		// 獲得視口表
		AcDbViewportTable *pVPortTbl = NULL;
		acdbHostApplicationServices()->workingDatabase()
			->getViewportTable(pVPortTbl, AcDb::kForWrite);//使用“寫”的模式打開資料庫的Viewport table

		//分别建立四個視口
		AcGePoint2d pt1, pt2;
		AcDbViewportTableRecord *pVPortTblRcd1 = new AcDbViewportTableRecord;
		pt1.set(0, 0);
		pt2.set(0.5, 0.5);
		pVPortTblRcd1->setLowerLeftCorner(pt1);//設定左下角的點
		pVPortTblRcd1->setUpperRightCorner(pt2);//設定右下角的點
		pVPortTblRcd1->setName(TEXT("4VPorts"));

		AcDbViewportTableRecord *pVPortTblRcd2 = new AcDbViewportTableRecord;
		pt1.set(0.5, 0);
		pt2.set(1, 0.5);
		pVPortTblRcd2->setLowerLeftCorner(pt1);
		pVPortTblRcd2->setUpperRightCorner(pt2);
		pVPortTblRcd2->setName(TEXT("4VPorts"));

		AcDbViewportTableRecord *pVPortTblRcd3 = new AcDbViewportTableRecord;
		pt1.set(0, 0.5);
		pt2.set(0.5, 1);
		pVPortTblRcd3->setLowerLeftCorner(pt1);
		pVPortTblRcd3->setUpperRightCorner(pt2);
		pVPortTblRcd3->setName(TEXT("4VPorts"));


		AcDbViewportTableRecord *pVPortTblRcd4 = new AcDbViewportTableRecord;
		pt1.set(0.5, 0.5);
		pt2.set(1, 1);
		pVPortTblRcd4->setLowerLeftCorner(pt1);
		pVPortTblRcd4->setUpperRightCorner(pt2);
		pVPortTblRcd4->setName(TEXT("4VPorts"));

		pVPortTbl->add(pVPortTblRcd1);//将新建立的視口添加到視口表pVPortTbl中
		pVPortTbl->add(pVPortTblRcd2);
		pVPortTbl->add(pVPortTblRcd3);
		pVPortTbl->add(pVPortTblRcd4);

		//關閉視口表
		pVPortTbl->close();

		//關閉視口表記錄
		pVPortTblRcd1->close();
		pVPortTblRcd2->close();
		pVPortTblRcd3->close();
		pVPortTblRcd4->close();

		//判斷目前的空間
		struct resbuf rb;
		acedGetVar(TEXT("TILEMODE"), &rb);//Retrieves the current value of the specified AutoCAD system variable. 
		//TILEMODE:Accesses the current TILEMODE value for the database. The value of false is 0. The value of true is 1. 
		if (rb.resval.rint == 1)//目前工作空間是模型空間
		{
			acedCommandS(RTSTR, TEXT(".-VPORTS"), 
				RTSTR, TEXT("R"),
				RTSTR, TEXT("4VPorts"),
				RTNONE);
			
		}
		else//目前工作空間是圖紙空間
		{
			acedCommandS(RTSTR, TEXT(".-VPORTS"),
				RTSTR, TEXT("R"),
				RTSTR, TEXT("4VPorts"),
				RTSTR, TEXT(""), RTNONE);
		}


	}
           

參考資料:

《AutoCAD ObjectARX(VC)開發基礎與執行個體教程》