当前位置: 首页 > news >正文

Vba选择cad中不同类型图元(Select Case True语句和like用法)

Select Case True 是一个常见的VBA编程技巧,用于在多个条件之间进行选择。具体来说,Select Case True 语句的每个 Case 语句都包含一个布尔表达式,这些表达式会逐个与 True 进行比较。当其中一个表达式的结果为 True 时,对应的代码块将被执行。Select Case True 示例,每个 Case 语句中的表达式 ent.ObjectName Like "*Line" 和 ent.ObjectName Like "*Circle" 都会返回布尔值 (True 或 False)。Select Case True 将这些布尔值与 True 进行比较,并执行匹配的代码块。

我们选择图形空间中第一个图元:

Dim ent As AcadEntity

Set ent = ThisDrawing.ModelSpace.Item(0)

 

' 不区分大小写的匹配

Select Case LCase(ent.ObjectName)

    Case "acdbline"

        MsgBox "This is a Line"

    Case "acdbcircle"

        MsgBox "This is a Circle"

    Case Else

        MsgBox "This is some other type of object"

End Select

 

 

Select Case True

    Case ent.ObjectName Like "*line"

        MsgBox "This is a Line"

    Case ent.ObjectName Like "*circle"

        MsgBox "This is a Circle"

    Case Else

        MsgBox "This is some other type of object"

End Select

 

'(InStr 函数)

If InStr(1, ent.ObjectName, "Line", vbTextCompare) > 0 Then

    MsgBox "This is a Line"

ElseIf InStr(1, ent.ObjectName, "Circle", vbTextCompare) > 0 Then

    MsgBox "This is a Circle"

Else

    MsgBox "This is some other type of object"

End If

如果你不指定 compare 参数,InStr 默认区分大小写。使用 vbTextCompare 参数可以使 InStr 不区分大小写。以下是代码以及解释:Dim ent As AcadEntity

Set ent = ThisDrawing.ModelSpace.Item(0)

 

Select Case True

    Case ent.ObjectName Like "*Line"

        MsgBox "This is a Line"

    Case ent.ObjectName Like "*Circle"

        MsgBox "This is a Circle"

    Case Else

        MsgBox "This is some other type of object"

End SelectSelect 

Case True:启动一个 Select Case 结构,将每个 Case 表达式的结果与 True 进行比较。Case ent.ObjectName Like "*Line":如果 ent.ObjectName 包含 "Line",则此表达式为 True,将执行 MsgBox "This is a Line"。Case ent.ObjectName Like "*Circle":如果 ent.ObjectName 包含 "Circle",则此表达式为 True,将执行 MsgBox "This is a Circle"。Case Else:如果前面的条件都不满足,将执行 MsgBox "This is some other type of object"。

注意事项

通配符匹配:Like 运算符允许使用通配符进行匹配。星号 (*) 匹配零个或多个字符,问号 (?) 匹配单个字符。在你的代码中,*Line 和 *Circle 都是合法的用法。大小写敏感:Like 运算符在VBA中默认是不区分大小写的。所以即使 ObjectName 的值是 "acdbLINE" 或 "ACDbcircle",你的匹配依然会成功。

http://www.lryc.cn/news/416640.html

相关文章:

  • Kafka基本讲解
  • thinkphp6项目初始化配置方案二次修正版本
  • XXE靶机教学
  • 干货 | 2024步入数字化转型深水区,云原生业务稳定性如何保障(免费下载)
  • for(char c:s),std::vector<int> numbers 和std::int numbers[],.size()和.sizeof()区别
  • 桌面云备份可以删除吗?安不安全
  • 【爬虫实战】利用代理爬取电商数据
  • python如何统计列表中元素出现的次数
  • 【算法】山脉数组的峰顶索引
  • 牛客 JZ31.栈的压入,弹出序列 C++写法
  • PageHelper在Mybatis的一对多表关联时total数错误
  • (20240806)硫氧镁 / 碱式硫酸镁-混凝土
  • string类的模拟实现(C++)
  • C++_sizeof的相关知识点
  • Istio Proxy的Envoy代理架构中,Upstream提供的功能是:
  • LeetCode 热题 HOT 100 (015/100)【宇宙最简单版】
  • 【HarmonyOS】鸿蒙应用实现截屏
  • Conda包依赖侦探:conda inspect命令全解析
  • 数模——灰色关联分析算法
  • Python爬虫技术 第27节 API和RESTful服务
  • 音视频入门基础:WAV专题(4)——FFmpeg源码中获取WAV文件音频压缩编码格式、采样频率、声道数量、采样位数、码率的实现
  • 环境变量在Conda中的魔法:控制包安装的秘诀
  • VS Code C/C++ MSVC编译器
  • 【技巧】IDEA 个性化配置
  • `pytest` 中一些常用的选项
  • fme从json中提取位置到kml中
  • 在Ubuntu 18.04上安装和配置pgAdmin 4服务器模式的方法
  • NiFi :1 初识这把“十年一剑”的利器
  • Pyside6实战教程专栏目录
  • 【Dash】使用 Dash Design Kit (DDK) 创建图表