216 lines
5.5 KiB
C++
216 lines
5.5 KiB
C++
#include "stdafx.h"
|
|
#include "globalFunc.h"
|
|
#include "AcDbObjectPtr.h"
|
|
#include "Block.h"
|
|
|
|
#ifdef _DEBUG
|
|
#undef THIS_FILE
|
|
static char THIS_FILE[]=__FILE__;
|
|
#define new DEBUG_NEW
|
|
#endif
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
// Construction/Destruction
|
|
//////////////////////////////////////////////////////////////////////
|
|
extern AcDbObjectId InitLayer(const TCHAR *szLayer, BOOL bApplyChange = TRUE);
|
|
extern AcDbObjectId InitBlock(const TCHAR *pszBlock, AcDbDatabase *pDb = NULL);
|
|
|
|
CBlock::CBlock(const TCHAR* szBlkName, XPoint pt, double nScale, double Rotation, const TCHAR* szLayer)
|
|
{
|
|
InitBlock(szBlkName);
|
|
AcDbBlockTable *pBlockTable = NULL;
|
|
AcDbBlockTableRecord* pRecord = NULL;
|
|
acdbCurDwg()->getBlockTable(pBlockTable, AcDb::kForRead);
|
|
if(!pBlockTable)
|
|
return;
|
|
if ((pBlockTable->getAt(szBlkName, pRecord, AcDb::kForRead)) != Acad::eOk) {
|
|
pBlockTable->close();
|
|
return;
|
|
}
|
|
pBlockTable->close();
|
|
|
|
m_BlockId = pRecord->objectId();
|
|
pRecord->newIterator(m_pIterator);
|
|
pRecord->close();
|
|
m_pBlkRef = new AcDbBlockReference;
|
|
m_pBlkRef->setBlockTableRecord(m_BlockId);
|
|
m_pBlkRef->setRotation(Rotation);
|
|
m_pBlkRef->setPosition(AcGePoint3d(pt));
|
|
m_pBlkRef->setScaleFactors(nScale);
|
|
InitLayer(szLayer);
|
|
m_pBlkRef->setLayer(szLayer);
|
|
|
|
AcDbExtents extents;
|
|
m_pBlkRef->getGeomExtents(extents);
|
|
m_ptMax = extents.maxPoint();
|
|
m_ptMin = extents.minPoint();
|
|
m_nWidth = fabs(m_ptMax[0] - m_ptMin[0]);
|
|
m_nHeight = fabs(m_ptMax[1] - m_ptMin[1]);
|
|
|
|
}
|
|
|
|
CBlock::~CBlock()
|
|
{
|
|
if(m_pBlkRef)
|
|
m_pBlkRef->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetAttPos(Entity entBlk, const TCHAR* szTag, AcGePoint3d& pt)
|
|
{
|
|
CString csTag = szTag;
|
|
TCHAR szBlkName[256];
|
|
entBlk.GetData(2, szBlkName);
|
|
AcDbBlockTable *pBlockTable = NULL;
|
|
AcDbBlockTableRecord* pRecord = NULL;
|
|
acdbCurDwg()->getBlockTable(pBlockTable, AcDb::kForRead);
|
|
if ((pBlockTable->getAt(szBlkName, pRecord, AcDb::kForRead)) != Acad::eOk)
|
|
return;
|
|
pBlockTable->close();
|
|
if(!pRecord)
|
|
return;
|
|
AcDbBlockTableRecordIterator *pIterator;
|
|
pRecord->newIterator(pIterator);
|
|
AcDbEntity *pEnt;
|
|
AcDbAttributeDefinition *pAttdef;
|
|
for(pIterator->start();!pIterator->done();pIterator->step()){
|
|
pIterator->getEntity(pEnt, AcDb::kForRead);
|
|
pAttdef = AcDbAttributeDefinition::cast(pEnt);
|
|
if(pAttdef != NULL){
|
|
TCHAR* pStr = pAttdef->tag();
|
|
if(_tcscmp(pStr, csTag) == 0){
|
|
free(pStr);
|
|
pt = pAttdef->alignmentPoint();
|
|
delete pIterator;
|
|
pAttdef->close();
|
|
pRecord->close();
|
|
return;
|
|
}
|
|
free(pStr);
|
|
}
|
|
pEnt->close();
|
|
}
|
|
delete pIterator;
|
|
pRecord->close();
|
|
}
|
|
|
|
|
|
void CBlock::InitAttr()
|
|
{
|
|
AcGeScale3d scale = m_pBlkRef->scaleFactors();
|
|
|
|
AcDbBlockTableRecord* pRecord;
|
|
acdbOpenObject(pRecord, m_BlockId, AcDb::kForRead);
|
|
if(!pRecord)
|
|
return;
|
|
AcDbBlockTableRecordIterator *pIterator;
|
|
pRecord->newIterator(pIterator);
|
|
AcDbEntity *pEnt;
|
|
AcDbAttributeDefinition *pAttdef;
|
|
for(pIterator->start();!pIterator->done();pIterator->step()){
|
|
pIterator->getEntity(pEnt, AcDb::kForRead);
|
|
pAttdef = AcDbAttributeDefinition::cast(pEnt);
|
|
if(pAttdef != NULL){
|
|
AcDbAttribute *pAtt = new AcDbAttribute();
|
|
pAtt->setPropertiesFrom(pAttdef);
|
|
pAtt->setInvisible(pAttdef->isInvisible());
|
|
AcGePoint3d pt;
|
|
pt = pAttdef->position();
|
|
pt = pt.transformBy(m_pBlkRef->blockTransform());
|
|
|
|
pAtt->setTextStyle(pAttdef->textStyle());
|
|
|
|
pAtt->setWidthFactor(pAttdef->widthFactor());
|
|
pAtt->setPosition(pt);
|
|
pAtt->setAlignmentPoint(pt);
|
|
pAtt->setRotation(m_pBlkRef->rotation());
|
|
pAtt->setTag(_T("Tag"));
|
|
pAtt->setFieldLength(25);
|
|
|
|
pAtt->setHorizontalMode(pAttdef->horizontalMode());
|
|
pAtt->setVerticalMode(pAttdef->verticalMode());
|
|
|
|
pAtt->setHeight(pAttdef->height()*scale[0]);
|
|
TCHAR* pStr = pAttdef->tag();
|
|
pAtt->setTag(pStr);
|
|
|
|
free(pStr);
|
|
pAtt->setFieldLength(pAttdef->fieldLength());
|
|
pAtt->setTextString(pAttdef->textString());
|
|
AcDbObjectId attId;
|
|
m_pBlkRef->appendAttribute(attId, pAtt);
|
|
pAtt->setPosition(pt); // apppend后,ref修改了pos,所以要重新设置插入点
|
|
pAtt->setAlignmentPoint(pt);
|
|
pAtt->close();
|
|
}
|
|
pEnt->close();
|
|
}
|
|
delete pIterator;
|
|
pRecord->close();
|
|
}
|
|
|
|
void CBlock::Insert(XPoint ptIns)
|
|
{
|
|
m_pBlkRef->setPosition(AcGePoint3d(ptIns));
|
|
GlobalFunc::PostToDb(m_pBlkRef);
|
|
AcDbExtents extents;
|
|
m_pBlkRef->getGeomExtents(extents);
|
|
m_ptMax = extents.maxPoint();
|
|
m_ptMin = extents.minPoint();
|
|
}
|
|
|
|
void CBlock::Insert(BOOL bClose)
|
|
{
|
|
GlobalFunc::PostToDb(m_pBlkRef);
|
|
InitAttr();
|
|
|
|
AcDbExtents extents;
|
|
m_pBlkRef->getGeomExtents(extents);
|
|
m_ptMax = extents.maxPoint();
|
|
m_ptMin = extents.minPoint();
|
|
if(bClose){
|
|
m_pBlkRef->close();
|
|
m_pBlkRef = NULL;
|
|
}
|
|
}
|
|
|
|
void CBlock::TransformBy(const AcGeMatrix3d& xform)
|
|
{
|
|
m_pBlkRef->transformBy(xform);
|
|
AcDbExtents extents;
|
|
m_pBlkRef->getGeomExtents(extents);
|
|
m_ptMax = extents.maxPoint();
|
|
m_ptMin = extents.minPoint();
|
|
}
|
|
|
|
void CBlock::SetScaleFactors(AcGeScale3d scale)
|
|
{
|
|
m_pBlkRef->setScaleFactors(scale);
|
|
}
|
|
|
|
void CBlock::GetLinkPt(XPointList &lstPt)
|
|
{
|
|
for(m_pIterator->start(); !m_pIterator->done(); m_pIterator->step()){
|
|
AcDbEntity *pEnt;
|
|
m_pIterator->getEntity(pEnt, AcDb::kForRead);
|
|
AcDbBlockReference *pBlk = NULL;
|
|
if(pBlk = AcDbBlockReference::cast(pEnt)){
|
|
AcGePoint3d ptIns = pBlk->position();
|
|
|
|
AcDbObjectId BlockId = pBlk->blockTableRecord();
|
|
OPENOBJ_BEGIN(BlockId, AcDb::kForRead, AcDbBlockTableRecord, pRecord);
|
|
TCHAR* blkName;
|
|
pRecord->getName(blkName);
|
|
if(_tcscmp(blkName, _T("_LINKPT1")) == 0){
|
|
ptIns = ptIns.transformBy(m_pBlkRef->blockTransform());
|
|
lstPt.NewAppend(ptIns);
|
|
}
|
|
OPENOBJ_END();
|
|
}
|
|
pEnt->close();
|
|
}
|
|
}
|
|
|