天气与日历 切换到窄版

 找回密码
 立即注册
中国膜结构网
十大进口膜材评选 十大国产膜材评选 十大膜结构设计评选 十大膜结构公司评选
查看: 78|回复: 3

图层操作

[复制链接]
  • TA的每日心情
    开心
    前天 08:09
  • 签到天数: 85 天

    [LV.6]常住居民II

    1640

    主题

    206

    回帖

    214748万

    积分

    管理员

    积分
    2147483647
    发表于 2024-3-6 11:44:44 | 显示全部楼层 |阅读模式
    1. //从CAD的图层表中获取指定图层的ID(图层名称)
    2. AcDbObjectId CLayerUtil::GetLayerId(const TCHAR * layerName)
    3. {
    4.         assert(layerName != NULL);
    5.         AcDbLayerTable *lTable = NULL;//AcDbLayerTable是AutoCAD中存储图层信息的数据结构
    6.         if (acdbHostApplicationServices()->workingDatabase()->getSymbolTable(lTable, AcDb::OpenMode::kForRead) != ErrorStatus::eOk)
    7.                 return AcDbObjectId::kNull;//打开工作数据库并将lTable指向它,以读取模式打开
    8.         AcDbObjectId lId;//存储图层的ID
    9.         //检查指定的图层名是否存在于图层表中
    10.         if (lTable->has(layerName)) {
    11.                 lTable->getAt(layerName, lId);
    12.     //如果指定的图层名存在于图层表中,这行代码将从图层表中获取该图层的ID,并将其存储在变量lId中
    13.         }
    14.         lTable->close();//关闭图层表,释放相关资源
    15.         return lId;//返回存储在变量lId中的图层ID
    16. }
    17. //处理AutoCAD的图层(图层名称,颜色索引)
    18. AcDbObjectId CLayerUtil::Add(const ACHAR* layerName, const int colorIndex)
    19. {
    20.         assert(layerName != NULL);//图层名称是否为空
    21.         bool colorRight = (colorIndex >= 1 && colorIndex <= 255);
    22.         assert(colorRight);//颜色索引是否在1到255的范围内
    23.         if (!colorRight)
    24.         {
    25.                 acutPrintf(_T("\n颜色索引值无效!"));
    26.         }
    27.         AcDbLayerTable* lTable = NULL;//AcDbLayerTable是AutoCAD中存储图层信息的数据结构。

    28.         if (acdbHostApplicationServices()->workingDatabase()->getSymbolTable(lTable, AcDb::OpenMode::kForWrite) != ErrorStatus::eOk)
    29.                 return AcDbObjectId::kNull;
    30.         if (lTable->has(layerName)) {
    31.                 //检查指定的图层名是否已经存在于图层表中
    32.                 lTable->close();
    33.                 acutPrintf(TEXT("已存在该层名\n"));
    34.                 return AcDbObjectId::kNull;
    35.         }
    36.         else {
    37.                 //新建图层
    38.                 AcDbLayerTableRecord* lRec = new AcDbLayerTableRecord();
    39.                 lRec->setName(layerName);
    40.                 //创建一个新的AcCmColor对象,并将其指针赋值给c。
    41.                 AcCmColor* c = new AcCmColor();
    42.                 //然后设置这个颜色的颜色索引为输入的colorIndex
    43.                 c->setColorIndex(colorIndex);
    44.                 lRec->setColor(*c);//将新图层的颜色设置为上一步创建的颜色的副本。
    45.                 AcDbObjectId  lId;//存储新图层的ID。
    46.                 lTable->add(lId, lRec);//将新图层添加到图层表中,并将新图层的ID存储在lId中
    47.                 lRec->close(); //关闭新创建的图层记录,释放其内存
    48.                 delete c;//删除创建的AcCmColor对象,释放其内存
    49.                 lTable->close();
    50.                 acutPrintf(TEXT("添加成功\n"));
    51.                 return lId;
    52.         }
    53. }
    54. //设置指定图层的颜色(图层的名称,颜色索引)
    55. bool CLayerUtil::SetColor(const TCHAR * layerName, const int colorIndex)
    56. {   //将图层名称转换为图层ID,并将其赋值给lId
    57.         AcDbObjectId lId = GetLayerId(layerName);
    58.         assert(colorIndex >= 1 && colorIndex <= 255);
    59.         if (lId.isNull()) {
    60.                 return false;
    61.         }
    62.         else
    63.         {
    64.                 AcDbLayerTableRecord *lRec = NULL; // AutoCAD的图层表记录。
    65.                 if (acdbOpenObject(lRec, lId, AcDb::OpenMode::kForWrite) != ErrorStatus::eOk)
    66.                         return false;
    67.                 AcCmColor *color = new AcCmColor();
    68.                 color->setColorIndex(colorIndex);//设置新创建的颜色的颜色索引为输入的颜色索引
    69.                 lRec->setColor(*color);//将新创建的颜色应用到打开的图层记录
    70.                 delete color;
    71.                 lRec->close();
    72.                 return true;
    73.         }
    74. }
    75. //获取AutoCAD图层表中所有图层的ID
    76. void CLayerUtil::GetLayerList(AcDbObjectIdArray & lIds)
    77. {
    78.         AcDbLayerTable *lTable = NULL; //存储图层信息的数据
    79.         acdbHostApplicationServices()->workingDatabase()->getSymbolTable(lTable, AcDb::OpenMode::kForRead);
    80.         AcDbLayerTableIterator * lIter = NULL;//这个迭代器用于遍历图层表
    81.         lTable->newIterator(lIter);//创建一个新的图层表迭代器,并将其赋值给lIter
    82.         for (lIter->start(); !lIter->done();lIter->step())
    83.                 /*这是一个循环,开始于图层表的起始位置,当迭代器没有完成时
    84.                 (即还有更多的图层),循环会继续。每次迭代后,
    85.                 都会调用lIter->step()来前进到下一个图层。*/
    86.         {   //使用迭代器的getRecordId方法获取当前图层的ID,并将其赋值给lId
    87.                 AcDbObjectId lId;
    88.                 lIter->getRecordId(lId);
    89.                 lIds.append(lId);//将当前图层的ID添加到输入参数lIds
    90.         }
    91.         delete lIter;//删除图层表迭代器,释放其占用的内存
    92. }
    复制代码

     

     

     

     

    图层操作
    中国膜结构网打造全中国最好的膜结构综合平台 ,统一协调膜结构设计,膜结构施工,膜材采购,膜材定制,膜结构预算全方位服务。 中国空间膜结构协会合作单位。
  • TA的每日心情
    开心
    前天 08:09
  • 签到天数: 85 天

    [LV.6]常住居民II

    1640

    主题

    206

    回帖

    214748万

    积分

    管理员

    积分
    2147483647
     楼主| 发表于 2024-3-6 11:51:04 | 显示全部楼层
    1.         // 新建一个图层
    2.         void CLayerOperator::NewLayer(ACHAR* layerName)
    3.         {
    4.             //获得当前图形的层表
    5.             AcDbLayerTable *pLayerTbl;
    6.             acdbHostApplicationServices()
    7.                 ->workingDatabase()
    8.                 ->getLayerTable(pLayerTbl,AcDb::kForWrite);

    9.     //是否已经包含指定的层表记录
    10.     if(pLayerTbl->has(layerName)){
    11.         pLayerTbl->close();
    12.         acedPrompt(_T("对应名称的图层已经存在!"));
    13.         return;
    14.     }

    15.     //创建层表记录
    16.     AcDbLayerTableRecord *pLayerTblRcd;
    17.     pLayerTblRcd=new AcDbLayerTableRecord();
    18.     pLayerTblRcd->setName(layerName);        //这里的layerName需要用(_T("?"))这种格式来命名

    19.     //将新建的层表记录添加到层表中
    20.     AcDbObjectId layerTblRcdId;
    21.     pLayerTbl->add(layerTblRcdId,pLayerTblRcd);

    22.     //设置图形的当前图层
    23.     acdbHostApplicationServices()
    24.         ->workingDatabase()
    25.         ->setClayer(layerTblRcdId);
    26.         //关闭层表
    27.     pLayerTblRcd->close();
    28.     pLayerTbl->close();
    29.         }
    复制代码

     

     

     

     

    图层操作
    中国膜结构网打造全中国最好的膜结构综合平台 ,统一协调膜结构设计,膜结构施工,膜材采购,膜材定制,膜结构预算全方位服务。 中国空间膜结构协会合作单位。
  • TA的每日心情
    开心
    前天 08:09
  • 签到天数: 85 天

    [LV.6]常住居民II

    1640

    主题

    206

    回帖

    214748万

    积分

    管理员

    积分
    2147483647
     楼主| 发表于 2024-3-6 11:51:21 | 显示全部楼层
    使用图形对话框设置指定图层的颜色
    1. // 使用图形对话框设置指定图层的颜色
    2. void CLayerOperator::SetLayerColor(ACHAR* layerName)
    3. {
    4.     //获得当前图形的层表
    5.     AcDbLayerTable *pLayerTbl;
    6.     acdbHostApplicationServices()
    7.         ->workingDatabase()
    8.         ->getLayerTable(pLayerTbl,AcDb::kForRead);
    9.     //判断是否包含指定名称的层表记录
    10.     if(!pLayerTbl->has(layerName)){
    11.         pLayerTbl->close();
    12.         return;
    13.     }
    14.     //获得指定层表记录的指针
    15.     AcDbLayerTableRecord *pLayerTblRcd;
    16.     pLayerTbl->getAt(layerName,pLayerTblRcd,AcDb::kForWrite);
    17.     //弹出“颜色”对话框
    18.     AcCmColor oldColor=pLayerTblRcd->color();
    19.     //图层修改前的颜色
    20.     int nCurColor=oldColor.colorIndex();
    21.     //用户选择的颜色
    22.     int nNewColor=oldColor.colorIndex();
    23.     if(acedSetColorDialog(nNewColor,Adesk::kFalse,nCurColor)){
    24.         AcCmColor color;
    25.         color.setColorIndex(nNewColor);
    26.         pLayerTblRcd->setColor(color);
    27.     }
    28.     //关闭层表
    29.     pLayerTblRcd->close();
    30.     pLayerTbl->close();
    31. }
    复制代码

     

     

     

     

    图层操作
    中国膜结构网打造全中国最好的膜结构综合平台 ,统一协调膜结构设计,膜结构施工,膜材采购,膜材定制,膜结构预算全方位服务。 中国空间膜结构协会合作单位。
  • TA的每日心情
    开心
    前天 08:09
  • 签到天数: 85 天

    [LV.6]常住居民II

    1640

    主题

    206

    回帖

    214748万

    积分

    管理员

    积分
    2147483647
     楼主| 发表于 2024-3-6 11:51:34 | 显示全部楼层
    删除一个图层
    1. // 删除一个图层
    2. void CLayerOperator::DelLayer(ACHAR* layerName)
    3. {
    4.     //获得当前图形的层表
    5.     AcDbLayerTable *pLayerTbl;
    6.     acdbHostApplicationServices()
    7.         ->workingDatabase()
    8.         ->getLayerTable(pLayerTbl,AcDb::kForRead);
    9.     //判断是否包含指定名称的层表记录
    10.     if(!pLayerTbl->has(layerName)){
    11.         pLayerTbl->close();
    12.         return;
    13.     }
    14.     //获得指定层表记录的指针
    15.     AcDbLayerTableRecord *pLayerTblRcd;
    16.     pLayerTbl->getAt(layerName,pLayerTblRcd,AcDb::kForWrite);
    17.     //为其设置“删除”标记
    18.     pLayerTblRcd->erase();
    19.     pLayerTblRcd->close();                //关闭层表
    20.     pLayerTbl->close();
    21. }

    复制代码

     

     

     

     

    图层操作
    中国膜结构网打造全中国最好的膜结构综合平台 ,统一协调膜结构设计,膜结构施工,膜材采购,膜材定制,膜结构预算全方位服务。 中国空间膜结构协会合作单位。
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|中国膜结构网_中国空间膜结构协会

    GMT+8, 2024-6-1 08:25 , Processed in 0.064041 second(s), 22 queries .

    Powered by Discuz! X3.5

    © 2001-2024 Discuz! Team.

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