0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AutoCAD VBA 選択フィルタ (2) - FilterTypeを作るFunction -

Last updated at Posted at 2025-04-14

はじめに

SelectionSet.Selectメソッドや .SelectOnScreenメソッドでは、選択フィルタを設定することが可能です。公式ヘルプのサンプルコードの作り方では毎回書くのが面倒なので、それならFunctionプロシージャ化してしまおうというのが今回の目的です。長くなりますので、今回はFilterTypeを、次回FilterDataについて解説します。

FilterType(Variant型配列)を作るFunction

概要

  • 配列の要素は、コンマ区切りテキストを入力し、Split関数を使って配列化します
  • 配列の各要素が数値と見做せるか判定後、Integer型に型変換して別の配列に格納します
  • 戻り値となる配列は Selectメソッド等の引数なので、Variant型とします
  • 数値と見做せない要素があった場合は、戻り値に False を入れて終了します

定義する配列は3つ

少しややこしいのですが、このプロシージャで定義する配列は 3つあります。

  1. 入力したコンマ区切りテキストをSplit関数で配列化したもの
  2. 1の配列の要素を Integer型に型変換して格納するInteger型配列
  3. 2の配列を格納する、戻り値となるVariant型配列 ※プロシージャとして宣言
AutoCAD VBA
  ' 3の配列
  Public Function uMakeFilterType(ByVal aTextByComma As String) As Variant
  End Function

  ' 1,2の配列
  Dim uFilterTypeArrayStr   As Variant
  Dim uFilterTypeArrayInt() As Integer

コード

AutoCAD VBA - FilterTypeを作成するファンクション
'[図形|選択|フィルタ] オブジェクト選択フィルター(FilterType)の配列を作成する
Public Function uMakeFilterType(ByVal aTextByComma As String) As Variant
  
  Dim uFilterTypeArrayStr   As Variant ' Split関数の結果を格納する配列
  Dim uFilterTypeArrayInt() As Integer ' Integer型の要素を格納する動的配列
  
  ' Splitで配列化(要素はすべてString値) ※添え字の最小値は 0
  uFilterTypeArrayStr = Split(Expression:=aTextByComma, Delimiter:=",")
  
  ' 動的配列の要素数確定
  ReDim uFilterTypeArrayInt(UBound(uFilterTypeArrayStr))
  
  ' 配列の要素をString型からInteger型に変換して動的配列に移す
  For n = LBound(uFilterTypeArrayStr) To UBound(uFilterTypeArrayStr)
    ' 要素が数値と見做せるかチェック
    If IsNumeric(uFilterTypeArrayStr(n)) = True Then
      ' 数値と見做せる場合、型変換して格納
      uFilterTypeArrayInt(n) = CInt(uFilterTypeArrayStr(n))
    Else
      ' 数値と見做せない場合、戻り値を False として終了する
      uMakeFilterType = False
      Exit Function
    End If
  Next
  
  ' 戻り値
  uMakeFilterType = uFilterTypeArrayInt

End Function

おわりに

次回は、FilterDataを作るFunctionです。基本的には今回のコードとほぼ同じですが、細かいところが違ってきます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?