找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 659|回复: 3

[每日一码] 阻止块被PURGE

[复制链接]

已领礼包: 13个

财富等级: 恭喜发财

发表于 2016-11-23 19:42:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×

Preventing a block from being purged
By Balaji Ramamoorthy

We will look at two approaches to prevent a block from getting purged.

1) Create a block reference out of the block and assign it to a hidden layer

2) Create a custom object that stores a hard pointer to the block. An instance of the custom object can be stored within your dictionary stored under the named object dictionary.

Here is the code snippet for the first method :
static Acad::ErrorStatus CreateBlock_Method1()
{
    Acad::ErrorStatus es;
    AcDbBlockTable *pBlockTable = NULL;
 
    AcDbDatabase *pDb
            = acdbHostApplicationServices()->workingDatabase();
 
    pDb->getBlockTable(pBlockTable, AcDb::kForWrite);
    AcDbBlockTableRecord* pAnonBlock = new AcDbBlockTableRecord();
    pAnonBlock->setName(_T("*U"));
 
    AcDbObjectId idAnonBlock = AcDbObjectId::kNull;
    pBlockTable->add(idAnonBlock, pAnonBlock);
 
    AcDbCircle *pCircle = new AcDbCircle(
                                            AcGePoint3d::kOrigin,
                                            AcGeVector3d::kZAxis,
                                            5.0
                                        );
 
    pAnonBlock->appendAcDbEntity(pCircle);
 
    pCircle->close();
 
    pAnonBlock->close();
 
    // Method : 1
    // Create an anonymous layer and use it for the
    // newly created block reference.
    AcDbObjectId layerId = AcDbObjectId::kNull;
    AcDbLayerTable* lTable;
    es = pDb->getSymbolTable(lTable, AcDb::kForWrite);
 
    if(Acad::eOk == es && lTable)
    {
        AcDbLayerTableRecord* lTblRec = new AcDbLayerTableRecord;
        lTblRec->setName(ACRX_T("TEST"));
        lTblRec->setIsHidden(true);
        AcCmColor layerColor;
        layerColor.setColorIndex(2);
        lTblRec->setColor(layerColor);
 
        if (lTable->add(layerId, lTblRec) != Acad::eOk)
            AfxMessageBox(_T("ERROR Creating Layer"));
        else
            lTblRec->close();
 
        lTable->close();
    }
 
    // Create a block reference and set to the hidden layer
    AcDbBlockReference *pBlkRef
            = new AcDbBlockReference(
                                        AcGePoint3d::kOrigin,
                                        idAnonBlock
                                    );
    pBlkRef->setLayer(layerId, true, true);
 
    AcDbBlockTableRecord *pModelSpace;
    pBlockTable->getAt(
                        ACDB_MODEL_SPACE,
                        pModelSpace,
                        AcDb::kForWrite
                      );
 
    pModelSpace->appendAcDbEntity(pBlkRef);
 
    pBlkRef->close();
    pModelSpace->close();
    pBlockTable->close();
 
    return Acad::eOk;
}
Here is the code snippet for the second method :

// Step : 1
// Create a basic custom object using the ARX wizard
 
 
// Step : 2
// In your custom object header file, add an id that will
// hold the ObjectId of the block that we are trying to
// prevent getting purged.
public:
    // Id of the object to prevent purge
    AcDbObjectId _Id;
 
 
// Step : 3
// Ensure that the objectId of the block is filed.
// For this make the following changes to the
// dwgInFields and dwgOutFields methods.
Acad::ErrorStatus AdsMyCustomObj::dwgOutFields
                                    (AcDbDwgFiler *pFiler) const
{
    // ...
    //----- Output params
    pFiler->writeHardPointerId(_Id);
 
    return (pFiler->filerStatus ()) ;
}
 
Acad::ErrorStatus AdsMyCustomObj::dwgInFields
                                        (AcDbDwgFiler *pFiler)
{
    // ...
    //----- Read params
    AcDbHardPointerId hardPointerId;
    pFiler->readHardPointerId(&hardPointerId);
    _Id = hardPointerId;
 
    return (pFiler->filerStatus ()) ;
}
 
 
// Step : 4
// Here is the method that creates a block table record
// and stores its id in the custom object instance.
// The custom object instance is stored in a custom dictionary
// under the named object dictionary.
static Acad::ErrorStatus CreateBlock_Method2( )
{
    Acad::ErrorStatus es;
    AcDbBlockTable *pBlockTable = NULL;
 
    AcDbDatabase *pDb
            = acdbHostApplicationServices()->workingDatabase();
 
    pDb->getBlockTable(pBlockTable, AcDb::kForWrite);
    AcDbBlockTableRecord* pAnonBlock = new AcDbBlockTableRecord();
    pAnonBlock->setName(_T("*U"));
 
    AcDbObjectId idAnonBlock = AcDbObjectId::kNull;
    pBlockTable->add(idAnonBlock, pAnonBlock);
 
    AcDbCircle *pCircle = new AcDbCircle(
                                            AcGePoint3d::kOrigin,
                                            AcGeVector3d::kZAxis,
                                            5.0
                                        );
 
    pAnonBlock->appendAcDbEntity(pCircle);
    pCircle->close();
    pAnonBlock->close();
 
    // Method : 2
    // Custom object in the NOD to store the hard pointer
    // to the block
    AcDbObjectPointer<AcDbDictionary> pNOD(
                                            pDb->namedObjectsDictionaryId(),
                                            AcDb::kForWrite
                                          );
    AcDbDictionary *pMyDict = NULL;
 
    AcDbObject *pMyDictObject = NULL;
    if(pNOD->getAt(
                    ACRX_T("MYOBJDICT"),
                    pMyDictObject,
                    AcDb::kForWrite
                  ) == Acad::eOk)
    {
        pMyDict = AcDbDictionary::cast(pMyDictObject);
    }
    else
    {
        pMyDict = new AcDbDictionary();
        AcDbObjectId DictObjectId = AcDbObjectId::kNull;
        pNOD->setAt(ACRX_T("MYOBJDICT"), pMyDict, DictObjectId);
    }
 
    AcDbObjectId customObjectId;
    AdsMyCustomObj *pCustObj = new AdsMyCustomObj();
    pCustObj->_Id = idAnonBlock;
    pMyDict->setAt (_T("DETAILS"), pCustObj, customObjectId);
    pCustObj->close ();
 
    pMyDict->close();
    pNOD->close();
 
    pBlockTable->close();
    return Acad::eOk;
}
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 6475个

财富等级: 富甲天下

发表于 2016-11-24 00:17:18 | 显示全部楼层
谢谢提供,来学习一下
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 1304个

财富等级: 财源广进

发表于 2016-11-24 10:00:48 | 显示全部楼层
确实是难得好帖啊,顶先
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2023-12-20 17:12:46 | 显示全部楼层
好贴,后续慢慢学习
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

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

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-4-27 14:50 , Processed in 0.365805 second(s), 33 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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