Exemplo n.º 1
0
Acad::ErrorStatus createBlockRecord(/*[in]*/const char* name)
{
	AcDbCircle* pFace = NULL;
	AcDbCircle* pLeftEye = NULL;
	AcDbCircle* pRightEye = NULL;
	AcDbArc* pMouth = NULL;
	AcDbBlockTable* pBlockTable = NULL;
	AcDbBlockTableRecord* pBlockTableRecord = NULL;

	try
	{
		// First, check if a block of the same name already exists
		// by verifying in the current database block table.
		//
		// Open the block table for read
		//
		ARXOK(acdbCurDwg()->getBlockTable(pBlockTable, AcDb::kForRead));

		if (pBlockTable->has(name) == Adesk::kTrue)
		{
			pBlockTable->close();
			return Acad::eDuplicateKey;
		}

		// Now we know the block does not exist, so we create it
		// using the name passed in.
		//
		pBlockTableRecord = new AcDbBlockTableRecord();
		if (!pBlockTableRecord)
			throw Acad::eOutOfMemory;

		pBlockTableRecord->setName(name);

		// To keep it simple, we use the origin for the insertion point
		//
		pBlockTableRecord->setOrigin(AcGePoint3d::kOrigin);

		// Open the block table for write
		// since we are adding a new block definition
		//
		ARXOK(pBlockTable->upgradeOpen());

		// Add the new block table record to the block table.
		// For now, the block table record is empty.
		//
		ARXOK(pBlockTable->add(pBlockTableRecord));
		pBlockTable->close();
		pBlockTable = NULL;

		// Now the block table record is in the database, but is empty
		// (has no sub-entity).
		// Note that after having been added to the database, an object or an entity
		// is implicitely opened for write.
		//
		// So we create the sub entities to append to the block
		// which will represent a "happy face":
		// the block should consist of a round yellow face (circle)
		// two blue eyes (circles) and a red mouth (arc)
		//
		pFace = new AcDbCircle(AcGePoint3d::kOrigin, AcGeVector3d::kZAxis, 1.0);
		pLeftEye = new AcDbCircle(AcGePoint3d(0.33, 0.25, 0.0), AcGeVector3d::kZAxis, 0.1);
		pRightEye = new AcDbCircle(AcGePoint3d(-0.33, 0.25, 0.0), AcGeVector3d::kZAxis, 0.1);
		pMouth = new AcDbArc(AcGePoint3d(0, 0.5, 0), 1.0, PI + (PI * 0.3), PI + (PI * 0.7));
		if (!pFace || !pLeftEye || !pRightEye || !pMouth)
		{
			delete pFace;
			delete pLeftEye;
			delete pRightEye;
			delete pMouth;
			throw Acad::eOutOfMemory;
		}

		// Set the color property.
		//
		pFace->setColorIndex(2);
		pLeftEye->setColorIndex(5);
		pRightEye->setColorIndex(5);
		pMouth->setColorIndex(1);

		ARXOK(pBlockTableRecord->appendAcDbEntity(pFace));
		ARXOK(pBlockTableRecord->appendAcDbEntity(pLeftEye));
		ARXOK(pBlockTableRecord->appendAcDbEntity(pRightEye));
		ARXOK(pBlockTableRecord->appendAcDbEntity(pMouth));

		pFace->close();
		pLeftEye->close();
		pRightEye->close();
		pMouth->close();

		pBlockTableRecord->close();
		return Acad::eOk;
	}

	catch(const Acad::ErrorStatus es)
	{

		if(pBlockTable)
			pBlockTable->close();

		if(pBlockTableRecord)
			// if the id is NULL, it means that the object
			// is not in the database and that we can sefely delete it
			if(pBlockTableRecord->objectId() == AcDbObjectId::kNull)
				delete pBlockTableRecord;
			else
			{
				pBlockTableRecord->erase();
				pBlockTableRecord->close();
			}
		
		if(pFace)
			if(pFace->objectId() == AcDbObjectId::kNull)
				delete pFace;
			else
				pFace->close();
		if(pLeftEye)
			if(pLeftEye->objectId() == AcDbObjectId::kNull)
				delete pLeftEye;
			else
				pLeftEye->close();
		if(pRightEye)
			if(pRightEye->objectId() == AcDbObjectId::kNull)
				delete pRightEye;
			else
				pRightEye->close();
		if(pMouth)
			if(pMouth->objectId() == AcDbObjectId::kNull)
				delete pMouth;
			else
				pMouth->close();

		return es;
	}

}