找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 42|回复: 0

arx DynamicBlock 有一个 “Visibility” 属性

[复制链接]

0

主题

0

回帖

26

积分

管理员

积分
26
发表于 2024-9-28 12:53:35 | 显示全部楼层 |阅读模式
  1. void SetDynamicBlkProperty()
  2. {
  3. ads_name ename;
  4. ads_point pt;
  5. if(acedEntSel(L"\nSelect a dynamic block reference: ", ename, pt) != RTNORM)
  6. {
  7.   acutPrintf(L"\nError selecting entity.");
  8.   return;
  9. }
  10. AcDbObjectId eId;
  11. acdbGetObjectId(eId, ename);
  12. AcDbEntity* pEnt = NULL;
  13. if (acdbOpenObject(pEnt, eId , AcDb::kForRead) != Acad::eOk)
  14. {
  15.   acutPrintf(L"\nError opening entity.");
  16.   if(pEnt)
  17.     pEnt->close();
  18.   return;
  19. }
  20. if(pEnt->isA() != AcDbBlockReference::desc())
  21. {
  22.   acutPrintf(L"\nMust select a block insert.");
  23.   pEnt->close();
  24.   return;
  25. }
  26. AcDbBlockReference *pBlkRef = AcDbBlockReference::cast(pEnt);
  27. // initialise a AcDbDynBlockReference from the object id of the blockreference
  28. AcDbDynBlockReference* pDynBlkRef = new AcDbDynBlockReference(pBlkRef->objectId());
  29. //Don't forget to close the blockreference here,
  30. //otherwise you wont be able to modify properties
  31. pEnt->close();
  32. if (pDynBlkRef)
  33. {
  34.   AcDbDynBlockReferencePropertyArray blkPropAry;
  35.   pDynBlkRef->getBlockProperties(blkPropAry);
  36.   Acad::ErrorStatus err;
  37.   AcDbDynBlockReferenceProperty blkProp;
  38.   for(long lIndex1=0L ; lIndex1<blkPropAry.length() ; ++lIndex1)
  39.   {
  40.    blkProp = blkPropAry[lIndex1];
  41.    //look for the relevant property
  42.    if (wcscmp(blkProp.propertyName().kACharPtr(), L"Visibility") != 0) continue;
  43.    //Get allowed values for property
  44.    AcDbEvalVariantArray evalAry;
  45.    if ((err = blkProp.getAllowedValues(evalAry)) == Acad::eOk )
  46.    {
  47.     if( evalAry.length() >= 1)
  48.     {
  49.      AcDbEvalVariant eval = evalAry[1];
  50.      if(!blkProp.readOnly())
  51.      {
  52.       if((err = blkProp.setValue(eval)) != Acad::eOk)
  53.       {
  54.        acutPrintf(L"\nError setting property value...");
  55.       }
  56.      }
  57.     }
  58.    }
  59.   }
  60.   //Don't forget to delete this reference, otherwise you will have problems.
  61.   delete pDynBlkRef;
  62. }
  63. }
  64. Here is the C# version:
  65. [CommandMethod("SetDynamicBlkProperty")]
  66. static public void SetDynamicBlkProperty()
  67. {
  68.     Document doc = Application.DocumentManager.MdiActiveDocument;
  69.     Database db = doc.Database;
  70.     Editor ed = doc.Editor;
  71.     PromptEntityOptions prEntOptions = new PromptEntityOptions(
  72.         "Select a dynamic block reference...");
  73.     PromptEntityResult prEntResult = ed.GetEntity(prEntOptions);
  74.     if (prEntResult.Status != PromptStatus.OK)
  75.     {
  76.         ed.WriteMessage("Error...");
  77.         return;
  78.     }
  79.     using(Transaction Tx = db.TransactionManager.StartTransaction())
  80.     {
  81.         BlockReference bref = Tx.GetObject(
  82.             prEntResult.ObjectId,
  83.             OpenMode.ForWrite)
  84.                 as BlockReference;
  85.         if (bref.IsDynamicBlock)
  86.         {
  87.             DynamicBlockReferencePropertyCollection props =
  88.                 bref.DynamicBlockReferencePropertyCollection;
  89.             foreach (DynamicBlockReferenceProperty prop in props)
  90.             {
  91.                 object[] values = prop.GetAllowedValues();
  92.                 //Switch Property
  93.                 if (prop.PropertyName == "Visibility" && !prop.ReadOnly)
  94.                 {
  95.                     if (prop.Value.ToString() == values[0].ToString())
  96.                         prop.Value = values[1];
  97.                     else
  98.                         prop.Value = values[0];
  99.                 }
  100.             }
  101.         }
  102.         Tx.Commit();
  103.     }
  104. }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|膜结构网

GMT+8, 2024-12-27 22:08 , Processed in 0.120392 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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