您的当前位置:首页正文

创建块参照 AcDbBlockReference

2024-11-07 来源:个人技术集锦

插入块参照,我们可以通过block指令创建一个块,然后用insert命令注入这个块,如何用代码模拟insert呢?

	static void testGroupCreateLine()
	{
		ACHAR blockName[40];
		if (acedGetString(NULL,_T("\n请输入块名"),blockName) != RTNORM) return;
		AcGePoint3d basePoint(0, 0, 0);
		if (acedGetPoint(NULL, _T("\n请插入基点<0,0,0>"), asDblArray(basePoint)) != RTNORM) return;
		double ang = 0.0;
		if (acedGetAngle(asDblArray(basePoint), _T("\n输入旋转角度<0,0>:"), &ang) != RTNORM) return;
		double sx, sy, sz;
		ACHAR strmp[10], prompt[50];
		if (acedGetDist(asDblArray(basePoint), _T("\n输入x方向比例系数<1.00>"), &sx) != RTNORM) sx = 1.00;
		acdbRToS(sx,2,2,strmp);
		
		wcscpy(prompt,_T("\n输入y方向比例系数<"));
		wcscat(prompt,strmp);
		wcscat(prompt,_T(">:"));
		if (acedGetDist(asDblArray(basePoint), _T("\n输入y方向比例系数<1.00>"), &sy) != RTNORM) sy = sx;
		
		wcscpy(prompt, _T("\n输入z方向比例系数<"));
		wcscat(prompt, strmp);
		wcscat(prompt, _T(">:"));
		if (acedGetDist(asDblArray(basePoint), _T("\n输入z方向比例系数<1.00>"), &sz) != RTNORM) sz = sy;
		
		AcGeScale3d xyzscale(sx,sy,sz);
		AcDbBlockTable* pBT;
		acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBT,AcDb::kForRead);

		//根据块名 拿到AcDbBlockTableRecord对象 重点
		AcDbBlockTableRecord* pBTRecord;
		if (Acad::eOk != pBT->getAt(blockName, pBTRecord, AcDb::kForRead)) return;
		pBT->close();

		AcDbObjectId blockId;
		blockId = pBTRecord->objectId(); //获取指定块的ID 

		pBTRecord->close();
		AcDbBlockReference* pBlkRef = new AcDbBlockReference(); 		//创建块引用对象

		pBlkRef->setBlockTableRecord(blockId);		//设置块引用的图块ID号
		pBlkRef->setPosition(basePoint);			//设置块引用的插入点
		pBlkRef->setRotation(ang);					//设置块引用的旋转角
		pBlkRef->setScaleFactors(xyzscale);			//设置块引用 x y z方向的比例系数
		
		//打开当前数据库模型控件的块表记录 将块记录添加到模型空间的块表记录中
		AcDbBlockTable* pBlockTable;
		acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable, AcDb::kForRead);
		AcDbBlockTableRecord* pBlockTableRecord;
		pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord,AcDb::kForWrite);
		pBlockTable->close();
		
		AcDbObjectId newEntId;
		pBlockTableRecord->appendAcDbEntity(newEntId,pBlkRef);
		pBlockTableRecord->close();
		pBlkRef->close();
		
	}
显示全文