此问题已解决,抄出来与大家分享一下。BlockName=“*U”即为无名块,设为其它值即为有名块。 void getUcsToWcsOriginMatrix(AcGeMatrix3d& m,const AcGePoint3d& wcsBasePt, AcDbDatabase* db,double angle) { ASSERT(db != NULL); AcGeMatrix3d tmpMat; if (acdbUcsMatrix(tmpMat, db)) { AcGePoint3d origin; AcGeVector3d xDir, yDir, zDir; tmpMat.getCoordSystem(origin, xDir, yDir, zDir); origin += wcsBasePt.asVector(); xDir.rotateBy(angle,zDir); yDir.rotateBy(angle,zDir); m.setToAlignCoordSys(origin, xDir, yDir, zDir, AcGePoint3d::kOrigin, AcGeVector3d::kXAxis, AcGeVector3d::kYAxis, AcGeVector3d::kZAxis); } else { ASSERT(0); m = AcGeMatrix3d::kIdentity; } } Acad::ErrorStatus cloneAndXformObjects(AcDbDatabase* db, AcDbObjectIdArray entsToClone,const AcDbObjectId& ownerBlockId, const AcGeMatrix3d& xformMat) { ASSERT(db != NULL); AcDbIdMapping idMap; Acad::ErrorStatus es = db->deepCloneObjects( const_cast<AcDbObjectIdArray&>(entsToClone), const_cast<AcDbObjectId&>(ownerBlockId), idMap); if (es != Acad::eOk) { return es; } AcDbEntity* clonedEnt; AcDbIdPair idPair; AcDbIdMappingIter iter(idMap); for (iter.start(); !iter.done(); iter.next()) { if (!iter.getMap(idPair)) return Acad::eInvalidInput; if (idPair.isCloned()) { es = acdbOpenAcDbEntity(clonedEnt, idPair.value(), AcDb::kForWrite); if (es == Acad::eOk) { if (idPair.isPrimary()) { clonedEnt->transformBy(xformMat); } clonedEnt->close(); } } } return Acad::eOk; } Acad::ErrorStatus defineNewAnonymousBlock(AcDbBlockTableRecord*& newBlkRec,AcDbObjectId& newBlkRecId, AcDbDatabase* db) { ASSERT(db != NULL); AcDbBlockTable* blkTbl; Acad::ErrorStatus es = db->getSymbolTable(blkTbl, AcDb::kForWrite); if (es != Acad::eOk) return es; newBlkRec = new AcDbBlockTableRecord; newBlkRec->setPathName(""); es = newBlkRec->setName(_T("*U")); if (es == Acad::eOk) es = blkTbl->add(newBlkRecId, newBlkRec); if (es != Acad::eOk) { delete newBlkRec; newBlkRec = NULL; } blkTbl->close(); return es; } AcDbObjectId fdm_MakeBlock(CString BlockName,AcDbObjectIdArray EntIdArray,const AcGePoint3d &BasePt,double angle,const AcGeScale3d &scale) { AcDbObjectId newBlkRecId; AcDbBlockTableRecord* newBlkRec; if (defineNewAnonymousBlock(newBlkRec, newBlkRecId, acdbHostApplicationServices()->workingDatabase()) == Acad::eOk) { newBlkRec->setName(BlockName); newBlkRec->setOrigin(AcGePoint3d::kOrigin); newBlkRec->close(); AcGeMatrix3d matrix; getUcsToWcsOriginMatrix(matrix,BasePt,acdbCurDwg(),angle); if (cloneAndXformObjects(acdbHostApplicationServices()->workingDatabase(), EntIdArray, newBlkRecId, matrix) == Acad::eOk) { AcDbObjectId ReferenceId=AcDbObjectId::kNull; AcDbBlockReference *pReference=new AcDbBlockReference; pReference->setBlockTableRecord(newBlkRecId); pReference->setPosition(BasePt); pReference->setRotation(angle); pReference->setScaleFactors(scale); if(fdm_addToModelSpace(ReferenceId,pReference)==Acad::eOk) { EraseEntity(EntIdArray); pReference->close(); return ReferenceId; } } } return AcDbObjectId::kNull; } |