C#ATIA

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

sirenを利用し、点を作成してみる

最近 "siren" と言うすばらしいソフトを見つけました。
siren | Official web site


本当は数年前に、OpenCASCADEを簡単に利用できるものが
ないか? と探していた際に見つけたのですが、その頃は
ビュアーのような感じのものでした。
(現在はsiren.NETと言う名称に変更されています)
GitHub - dyama/siren: siren - simple cad system with Ruby


現行の "siren" は
GUI無し
・バッチ処理が可能
・Igesでのエクスポートが可能
CADシステムと言うより、"三次元幾何演算スプリクト" と言う位置付け
のようです。 特にGUI無しなのがすばらしいです。

"何か出来そう" と思うので早速、こちらの物で試してみました。
VBA
スピードテスト1 - C#ATIA

C#
スピードテスト3 - C#ATIA

VBA-Ariawase版
Ariawaseを利用してみたい3 - C#ATIA


ソースコードはこちら

'VBA - using_Siren
Private Const SirenPath$ = "C:\siren\siren_0.11_mingw32\siren"
Private Const TempPath$ = "C:\temp\"
Private Const TempPathRb$ = "/temp/"
Private Const PntCount& = 500 '製作する点の数

Private FSO As Object
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub CATMain()
    '準備
    Dim T&: T = timeGetTime
    Dim ScriptName$: ScriptName = "pts.rb"
    Dim IgesName$: IgesName = "pts.igs"
    Dim FgName$: FgName = "fg.txt"
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    'SirenScriptファイル作成
    Call WriteFile(TempPath + FgName, "削除しても結構です")
    Call WriteFile(TempPath + ScriptName, _
        Join(GetSirenScript(TempPathRb + IgesName, TempPathRb + FgName), vbNewLine))
    
    'SirenScript実行-Iges作成
    Call Execute(TempPath + ScriptName)
    
    'Iges読み込み
    Call OpenIges(TempPath + IgesName, TempPath + FgName)
    
    '掃除
    Call DeleteFile(TempPath + ScriptName)
    Call DeleteFile(TempPath + IgesName)
    Set FSO = Nothing
    
    MsgBox CStr(PntCount) + "点" + vbNewLine + CStr(CDbl(timeGetTime - T) / 1000) + "秒"
End Sub

'Shell
Private Sub Execute(ByVal ScriptPath$)
    Dim WshShell As Object: Set WshShell = CreateObject("WScript.Shell")
    Call WshShell.Run(SirenPath + " " + ScriptPath, 1, False)
    Set WshShell = Nothing
End Sub

'ファイルの書き込み
Private Sub WriteFile(ByVal FilePath$, ByVal txts$)
    Dim Ts As Object: Set Ts = FSO.OpenTextFile(FilePath, 2, True)
    Ts.Write txts
    Set Ts = Nothing
End Sub

'ファイルの削除
Private Sub DeleteFile(ByVal FilePath$)
    If Not FSO.FileExists(FilePath) Then Exit Sub
    Call FSO.DeleteFile(FilePath)
End Sub

'Igesオープン
Private Sub OpenIges(ByVal Path$, ByVal Fg$)
    Dim i&
    For i = 0 To CLng(PntCount / 10&)
        If Not FSO.FileExists(Fg) Then
            Dim doc As Document: Set doc = CATIA.Documents.Open(Path)
            Set doc = Nothing
            Exit Sub
        End If
        Call Sleep(100)
    Next
End Sub

'SirenScriptベースコード
Private Function GetSirenScript(ByVal IgesPath$, ByVal ScriptPath$) As Variant
    Dim count&: count = 7
    Dim txts() As String: ReDim txts(count)
    txts(0) = "pts=[]"
    txts(1) = "for num in 1.." + CStr(PntCount) + " do"
    txts(2) = " pts.push(Build.vertex [10 * num , 0 , 0])"
    txts(3) = "end"
    txts(4) = "com = Build::compound pts"
    txts(5) = "IGES.save [com]," + Chr(34) + IgesPath + Chr(34)
    txts(6) = "File.delete " + Chr(34) + ScriptPath + Chr(34)
    txts(7) = "puts" + Chr(34) + "OK" + Chr(34)
    GetSirenScript = txts
End Function

VBAからSirenScriptのバッチファイルを作成し、Shellで実行。
出来上がったIgesファイルをインポートする。 と言う方法です。
全く異なるアプローチなのですが、結果は同じになります。
まぁこちらの方法と同様です。
外部ファイルから点を3Dに取り込む3 - C#ATIA


500点ではこんな感じです。
f:id:kandennti:20160303161144p:plain
点が少な過ぎると遅いかも知れませんが、500点であればかなり速いです。
任意のファイルに取り込みたい場合は、コピペする必要がありますが・・・。

5000点ではこんな感じ。
f:id:kandennti:20160303161153p:plain

VBAでは試したくない、50000点であればこんな感じ。
f:id:kandennti:20160303161203p:plain

1分程であれば十分我慢できそうです。
(処理時間の大半はIgesファイルをインポートしている時間です)


リファレンスマニュアルを見ると、未実装な部分が結構あるのですが
今後も開発は続きそうなので、期待してます。