明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2071|回复: 2

求助 :如何制作无名块?

[复制链接]
发表于 2007-3-27 17:10:00 | 显示全部楼层 |阅读模式

如何制作无名块?块中的实体要保留其属性,如赋有材质的话要保留。

哪位知道的话,望能提供一点原代码,谢谢!!

 楼主| 发表于 2007-3-28 16:18:00 | 显示全部楼层

此问题已解决,抄出来与大家分享一下。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;
}

 楼主| 发表于 2007-3-28 16:21:00 | 显示全部楼层

此问题已解决,抄出来与大家一起分享。 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;
}

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 21:20 , Processed in 0.165973 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表