admin 发表于 2024-2-26 10:28:46

[每日一码] 图块位置不变的情况下重设块的插入点

void ResetAllInstances(AcDbBlockTableRecord * pBlockTableRecord, AcGeVector3d &translation)
{

// Note that this function does not implement any error checking.
// We are assuming that pBlockTableREcord was opened for write by the calling function

// Iterate through all block references of this block table record.
AcDbBlockReferenceIdIterator * pIterator;
pBlockTableRecord->newBlockReferenceIdIterator(pIterator);
for (pIterator->start();!pIterator->done();pIterator->step())
{
AcDbBlockReference * pBlockRef;
pIterator->getBlockReference(pBlockRef, AcDb::kForWrite,true);

// Transform the translation vector from block coordinates to world coordinates.
AcGeVector3d realTranslation = (pBlockRef->blockTransform())*translation;

// Translate the block reference to counter the effect of the origin change.
pBlockRef->setPosition(pBlockRef->position()+realTranslation);


pBlockRef->close();
} // Next block reference
delete pIterator;
}


// This is command 'SGP'
void testFunc()
{

// Note that error checking is minimal.

ads采用name ename ;
ads采用point pt ;

// Ask the user to select an object
if( acedEntSel ("\nSelect the Block Reference", ename, pt) != RTNORM )
return ;

// Open it
AcDbObjectId objId;
acdbGetObjectId (objId, ename) ;
AcDbEntity * pEnt;
acdbOpenObject (pEnt, objId, AcDb::kForRead);

// Check its a block reference
AcDbBlockReference *pRef = AcDbBlockReference::cast(pEnt);
if (pRef == NULL)
{
pEnt->close();
return;
}

// Get the block reference's associated block table record.
AcDbObjectId blockId =pRef->blockTableRecord ();
pRef->close();
AcDbBlockTableRecord *pBlockTableRecord;
acdbOpenObject (pBlockTableRecord, blockId, AcDb::kForWrite);

// Store the BTR's current origin (we'll need it later).
AcGePoint3d origin = pBlockTableRecord->origin();

// And set its new origin to (0, 0, 0)
pBlockTableRecord->setOrigin(AcGePoint3d(0, 0, 0));


// Create a translation vector for the change in displacement required to offset the origin change (in the block's coordinate system)
AcGeVector3d translation;
translation.set(-origin, -origin, -origin);

// Now process all instances of this block reference to compensate for the change in the block origin
ResetAllInstances(pBlockTableRecord, translation);

pBlockTableRecord->close();
}
页: [1]
查看完整版本: [每日一码] 图块位置不变的情况下重设块的插入点