C#ATIA

↑タイトル詐欺 主にFusion360API 偶にCATIA V5 VBA(絶賛ネタ切れ中)

RGBAのLong型

こちらの続きのようなものです。
GetParameterOnSubStringメソッド - C#ATIA

こちらのDrawingText.TextPropertiesのcolorがRGBAのLong型を返してきます。
r1 DrawingTextProperties (Object)
この数値がRGB(例えば255, 255, 0)をどうやって計算すれば求められる値なのか
分かりません。

色々と調べたのですがやっぱりわかりませんでした。
きっと計算で出来るのでしょうけど。

仕方ないのでSelectionで色を変更し、GetParameterOnSubStringで拾い出すマクロを
作りました。

'vba
Sub test()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    Dim sel As Object 'selection
    Set sel = doc.selection
    
    Dim msg$: msg = "選択して下さい : ESCキー 終了"
    Dim txt As Variant 'DrawingText
    
    Dim vis As Variant 'As VisPropertySet
    Set vis = sel.VisProperties
    Dim colorMap As Variant
    
    colorMap = initColorMap()
        
    Dim rgbAry As Variant
    Select Case sel.SelectElement2(Array("DrawingText"), msg, False)
        Case "Cancel", "Undo", "Redo"
            Exit Sub
        End Select
    Set txt = sel.Item(1).value
    
    For i = 0 To UBound(colorMap)
        rgbAry = Split(colorMap(i), ",")
        Call vis.SetRealColor( _
            CLng(rgbAry(0)), _
            CLng(rgbAry(1)), _
            CLng(rgbAry(2)), _
            1)
        iParam = CatTextProperty.catColor
        res = txt.GetParameterOnSubString(iParam, 0, 0)
        
        Debug.Print colorMap(i) & " -> " & res
    Next
    
End Sub

Private Function initColorMap() _
    As Variant 'array(str)
    
    initColorMap = Array( _
        "255, 255, 0", _
        "128, 0, 255", _
        "0, 0, 255", _
        "0, 128, 255", _
        "0, 255, 255", _
        "0, 255, 0", _
        "0, 128, 0", _
        "211, 178, 125", _
        "255, 128, 0", _
        "255, 0, 0", _
        "255, 0, 255", _
        "128, 64, 64")

End Function

で結果はこちら

255, 255, 0 -> -65281
128, 0, 255 -> -2147418113
0, 0, 255 -> 65535
0, 128, 255 -> 8454143
0, 255, 255 -> 16777215
0, 255, 0 -> 16711935
0, 128, 0 -> 8388863
211, 178, 125 -> -743277057
255, 128, 0 -> -8388353
255, 0, 0 -> -16776961
255, 0, 255 -> -16711681
128, 64, 64 -> -2143272705

フムフム・・・やっぱりわかりません。 でもこの方法で行けそう。

GetParameterOnSubStringメソッド

CATIAのDrawingTxtオブジェクトのGetParameterOnSubStringメソッドを
使ったことが無いので、戸惑ってます。
御存知の方いらっしゃいましたらアドバイス頂けると助かります。

GetParameterOnSubStringメソッドはDrawingTxtの部分的な文字の情報を
取得するメソッドだと思ってます。
r1 DrawingText (Object)


こんな雑なコードを作りました。

'vba
Sub test()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    Dim sel As Object 'selection
    Set sel = doc.selection
    
    Dim msg$: msg = "選択して下さい : ESCキー 終了"
    Dim txt As Variant 'DrawingText
    Do
        Select Case sel.SelectElement2(Array("DrawingText"), msg, False)
            Case "Cancel", "Undo", "Redo"
                Exit Sub
            End Select
        Set txt = sel.Item(1).value
        
        iParam = CatTextProperty.catFontSize
        iFirst = 0
        inbCharacter = 0
        oval = txt.GetParameterOnSubString(iParam, iFirst, inbCharacter)
        
        Debug.Print "oval: " & oval
        Stop
    Loop
End Sub

DrawingTextを選択し、GetParameterOnSubStringの結果をイミディエイトウィンドウに
垂れ流してます。
第一パラメータはこちらです。
r1 Enumeration CatTextProperty
フォントのサイズを取得したいのです。

こんなデータで試しました。
f:id:kandennti:20200615184153p:plain
例えば「a11」は全てフォントサイズ3です。これを選択すると

oval: 3000

ん?単位mmで返すってHelpに書いてあるのに。

続いて「a8」を選択。こちらは全てサイズ10です。

oval: 10000

ん~単位の間違いだけなのだろう。

次は「a12」です。「a」がサイズ5で「12」はサイズ3です。

oval: 0

なるほど、0は文字列のフォントサイズが統一されていないって解釈で
良いんですかね?


分からなかったのが 、上記の「 iFirst 」「 inbCharacter 」。
試すと、「 iFirst 」で指定する文字列の先頭は1の様で、全ての場合は0かな?
文字数の「 inbCharacter 」は「 iFirst 」からの文字数で、「 iFirst 」以降全ての
場合は0で良いような・・・。

解釈合ってますかね?

Draw内のTextをサイズ毎に色を付ける2

こちらの続きです。
Draw内のTextをサイズ毎に色を付ける - C#ATIA

リンク先のトピが削除されている。
とりあえずコードが残っていたので再投稿。

'catvba  by kantoku
'Sample that changes color with text size

Option Explicit

Private Const OTHERCOLOR = "0, 0, 0"
    
Sub CATMain()
    
    Dim txts As Collection
    Set txts = getShowTxts()
    If txts Is Nothing Then Exit Sub
    
    Dim colorMap As Variant
    colorMap = initColorMap()
    
    Dim sizeDic As Object
    Set sizeDic = groupBySize(txts)
    
    Call execChangeColor(sizeDic, colorMap)
    
    MsgBox "Done"
End Sub

Private Sub execChangeColor( _
    ByVal group As Object, _
    ByVal colorMap As Variant)
    
    Dim sel As selection
    Set sel = CATIA.ActiveDocument.selection
    
    Dim vis As VisPropertySet
    Set vis = sel.VisProperties
    
    CATIA.HSOSynchronized = False
    
    Dim key As Variant ' Long
    Dim rgbAry As Variant
    Dim rgbTxt As String
    Dim dt As DrawingText
    For Each key In group.keys

        If UBound(colorMap) > key Then
            'In colormap
            rgbTxt = colorMap(key)
        Else
            'other
            rgbTxt = OTHERCOLOR
        End If

        rgbAry = Split(rgbTxt, ",")
        
        sel.Clear
        For Each dt In group(key)
            sel.Add dt
        Next
        
        Call vis.SetRealColor( _
            CLng(rgbAry(0)), _
            CLng(rgbAry(1)), _
            CLng(rgbAry(2)), _
            1)

    Next
    
    sel.Clear
    
    CATIA.HSOSynchronized = True
    
End Sub

'return dic(txtsize,lst(drawtxt))
Private Function groupBySize( _
    ByVal txts As Collection) _
    As Object

    Dim dic As Object
    Set dic = initDic()
    
    Dim dt As DrawingText
    Dim prop As DrawingTextProperties
    Dim key As Long
    Dim lst As Collection
    
    For Each dt In txts
        Set prop = dt.TextProperties
        key = CLng(prop.FONTSIZE)
        
        If dic.Exists(key) Then
            Call dic(key).Add(dt)
        Else
            Set lst = New Collection
            lst.Add dt
            Call dic.Add(key, lst)
        End If
    Next
    
    Set groupBySize = dic
    
End Function

Private Function initDic() _
    As Object
    
    Set initDic = CreateObject("Scripting.Dictionary")
    
End Function

Private Function getShowTxts() _
    As Collection
    
    Set getShowTxts = Nothing
    
    Dim sel As selection
    Set sel = CATIA.ActiveDocument.selection
    
    CATIA.HSOSynchronized = False
    
    sel.Clear
    sel.Search "CATDrwSearch.DrwText,scr"

    CATIA.HSOSynchronized = True
    
    If sel.Count < 1 Then
        MsgBox "Text not found"
        Exit Function
    End If
    
    Dim lst As Collection
    Set lst = New Collection
    
    Dim i As Long
    For i = 1 To sel.Count
        lst.Add sel.Item2(i).value
    Next
    
    sel.Clear
    Set getShowTxts = lst
    
End Function

Private Function initColorMap() _
    As Variant 'array(str)
    
    initColorMap = Array( _
        "255, 255, 0", _
        "128, 0, 255", _
        "0, 0, 255", _
        "0, 128, 255", _
        "0, 255, 255", _
        "0, 255, 0", _
        "0, 128, 0", _
        "211, 178, 125", _
        "255, 128, 0", _
        "255, 0, 0", _
        "255, 0, 255", _
        "128, 64, 64")

End Function

閉鎖的だな。スタンスにがっかりした。
こんなのお金にはならないのに。

Draw内のTextをサイズ毎に色を付ける

面白そうなテーマだったので、挑戦してみました。
catia font size identification - DASSAULT: CATIA products - Eng-Tips

思ったより短時間に幾つものレスが付いて、ビックリ。
本当は、ズバリの物を投げちゃうことを快く思わない人も
いるのは知っているのですが、こっちも腕試しでやってみました。
(色々と細かなチェックは省きました)

通常であれば、文字を探しつつ、文字サイズを調べ、色を変更・・・
みたいな処理をするのだろうと思います。
実際に動くコードではないけれど、正攻法的にはこんな感じかな?

for each view in views
  for each txt in view.texts
    size = clng(txt.getfontsize())
    sel.Clear
    select case size
      case < 1
        sel.add txt
        vis.vis.SetRealColor(r,g,b,1)
      case < 2
        sel.add txt
        ・・・

これだと、恐らく遅いんですよね。それ以上にNestが深い・・・。


だから僕は、
・全部のテキストをかき集める
・文字サイズ毎にコレクションに投入
・同じ色になるものを選択状態にして色を変更
の方法にしました。
要は同じ色になるものをまとめて選択しプロパティを
開いて色を変更している感じです。

恐らく、色の種類分しか変更処理しないので正攻法より
速いはず。特に文字の数が多い場合は。


今回の個人的な収穫としては
・Scripting.DictionaryはkeyがLong型でもイケる。
 (Collectionはkeyはstring型のみだったはず)
・Scripting.DictionaryはvalueがCollection型でもイケる。
・DrawingTextPropertiesのColorを変更しても色が変わらない。
 (何の為のプロパティなのだろう・・・)

但し、質問者さんのお望みが僕の想定を超えている・・・。

Drawのプリントアウトをマクロで行いたい1

手元に大量のDrawファイルがあるのですが、イチイチ開くのが面倒なので
PDFとしてエクスポートしておきたいんです。

主な手法としては、以前色々と教わったこちらの方法です。
DrawをPDFでエクスポート 3 - C#ATIA
SaveAsでは不要なシートも出力されるし、背景ブランクが無効だしで
他の選択肢はありませぬ。


今後も含め、大量なのでマクロで行いたいのですが、今までプリンター関連の
マクロを作った事が無いので、手探り状態です。
・・・客先から送られて来る時にPDF付きの時もあり、きっと
世の中にはあるはずなので、サンプル探してます。

サラッと探したところ
r1 DrawingSheet (Object)
こちらのprintoutメソッドにたどり着くとは思っていますが、
軽く試したところ、無反応。

目標的には、CATIAをバッチ起動させるアイコンに複数のDrawファイルをD&D
一度に変換させちゃいたいです。

印刷だけわからないので、何方かサンプル御存知であれば教えて頂けると
助かります。

JSONフォーマットを見易く

JSONフォーマットを見易くするサービスを提供してくれているサイトは、
恐らく幾つもあると思いますが、毎回検索するのも面倒なので、、、
覚書です。

The Fastest JSON visualizer - 最速のJSON可視化・解析ツール

思いのほか見易い。(その為のサイトですからね・・・)

こちらのコードを実行すると確実にFusion360がクラッシュしてしまうので
テキストコマンド2 - C#ATIA

テキストコマンドで実行し、あのサイトへコピペで済ませたい。