• 将外部dwg图纸中指定带属性的块插入到当前图纸中


    static void InsertBlock()
    {
    //获取要插入的块名
    TCHAR str[40];
    acedGetString(Adesk::kFalse, _T(" 请输入要插入的块名称:"), str);
    CString blockName ;
    blockName.Format(_T("%s"), str);

    //判断在当前图纸中是否已经被定义
    AcDbDatabase *pCurDb = acdbHostApplicationServices()->workingDatabase();
    AcDbBlockTable *pBlkTab;
    AcDbObjectId blockId;
    pCurDb->getBlockTable(pBlkTab, AcDb::kForRead);

    //当前图纸中未定义该块
    if (!pBlkTab->has(blockName))
    {
    pBlkTab->close();

    //获取指定的外部图纸中的数据库
    TCHAR *filePath = _T("D:\BlockTest.dwg");
    CString strName;
    strName.Format(_T("%s"), filePath);
    AcDbDatabase *pDwg = getDatabaseByDwg(strName);

    //获取外部数据库中该块中所有实体的id
    AcDbObjectIdArray objIds;
    getIdsByDwgBlkName(pDwg, blockName, objIds);//该函数属于自己编写,将在后面给出

    //将外部数据库中对应ID的实体写入到pDb数据库中,该操作的作用是选择出我们所需要的所有实体

    AcDbDatabase *pDb= NULL;
    pDwg->wblock(pDb, objIds, AcGePoint3d(0, 0, 0));

    //将pDb中的所有实体插入到当前图形中
    Acad::ErrorStatus es = pCurDb->insert(blockId, blockName, pDb, Adesk::kFalse);
    if (Acad::eOk != es)
    {
    acutPrintf(_T(" 插入块错误!"));
    return;
    }
    delete pDb;
    }

    //当前图纸中已经定义了该块
    else
    {
    pBlkTab->getAt(blockName, blockId);
    }

    //获取块参照的插入点
    ads_point pt;
    acedGetPoint(NULL, _T("请选择插入点:"), pt);
    AcGePoint3d ptInsert = asPnt3d(pt);

    //创建块参照
    AcDbBlockReference *pBlkRef = new AcDbBlockReference(ptInsert, blockId);

    //获得模型空间块表记录,并插入块参照
    AcDbBlockTable *pBlockTab;
    acdbHostApplicationServices()->workingDatabase()
    ->getBlockTable(pBlockTab, AcDb::kForRead);
    AcDbBlockTableRecord *pBlockTabRcd;
    pBlockTab->getAt(ACDB_MODEL_SPACE, pBlockTabRcd, AcDb::kForWrite);
    pBlockTab->close();
    pBlockTabRcd->appendZcDbEntity(pBlkRef);

    //获得属性
    AcDbBlockTableRecord *pBlkDefRcd;
    acdbOpenObject(pBlkDefRcd, blockId, AcDb::kForRead);
    if (pBlkDefRcd->hasAttributeDefinitions())
    {

    //遍历块表记录
    AcDbBlockTableRecordIterator *pItr;
    pBlkDefRcd->newIterator(pItr);
    for (pItr->start(); !pItr->done(); pItr->step())
    {

    //如果是属性,则加入到要插入的属性对象中
    AcDbEntity *pEnt;
    pItr->getEntity(pEnt, AcDb::kForRead);
    AcDbAttributeDefinition *pAttDef = AcDbAttributeDefinition::cast(pEnt);
    if (pAttDef != NULL)
    {
    AcDbAttribute *pAtt = new AcDbAttribute();
    pAtt->setPropertiesFrom(pAttDef);
    pAtt->setInvisible(pAttDef->isInvisible());
    pAtt->setHorizontalMode(pAttDef->horizontalMode());
    pAtt->setVerticalMode(pAttDef->verticalMode());

    AcGePoint3d basePt = pAttDef->position();
    basePt.x += ptInsert.x;
    basePt.y += ptInsert.y;
    basePt.z += ptInsert.z;
    pAtt->setPosition(basePt);

    pAtt->setHeight(pAttDef->height());
    pAtt->setTextStyle(pAttDef->textStyle());

    //设置属性的Tag、TextString

    pAtt->setTag(pAttDef->tag());
    pAtt->setTextString(pAttDef->textString());
    pAtt->setFieldLength(pAttDef->fieldLength());

    //将属性对象追加到块参照中

    pBlkRef->appendAttribute(pAtt);
    pAtt->close();
    pEnt->close();
    }
    }
    delete pItr;
    }
    pBlkDefRcd->close();
    pBlockTabRcd->close();
    pBlkRef->close();

    }

  • 相关阅读:
    两步验证杀手锏:Java 接入 Google 身份验证器实战
    涨姿势:Spring Boot 2.x 启动全过程源码分析
    Spring Cloud 升级最新 Finchley 版本,踩了所有的坑!
    Spring Boot 2.x 启动全过程源码分析(上)入口类剖析
    推荐:7 月份值得一看的 Java 技术干货!
    屌炸天,Oracle 发布了一个全栈虚拟机 GraalVM,支持 Python!
    Spring Boot 核心配置文件 bootstrap & application 详解。
    出场率比较高的一道多线程安全面试题
    凉凉了,Eureka 2.x 停止维护,Spring Cloud 何去何从?
    读写Excel
  • 原文地址:https://www.cnblogs.com/pengjun-shanghai/p/4800846.html
Copyright © 2020-2023  润新知