C#ATIA

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

ライセンス無しでSTLをCATIAに取り込む2

sirenを利用した、点を作成するマクロをちょっとだけ変更しました。
こちらの続きでもあります。
ライセンス無しでSTLをCATIAに取り込む - C#ATIA


sirenではSTLのインポートも対応していたので、それを利用しCATIAに
サーフェスをして取り込みます。
(念の為ですが、リファレンスマニュアルを見ると、サイズの大きいSTLファイルを
インポートする事は推奨しておりません・・・)

'VBA - using_Siren
Private Const SirenPath$ = "C:\siren\siren_0.11_mingw32\siren"
Private Const TempPath$ = "C:\temp\"

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 FilePath As String
    FilePath = CATIA.FileSelectionBox("インポートファイルを選択", "*.stl", CatFileSelectionModeOpen)
    If Len(FilePath) < 1 Then Exit Sub

    '準備
    Dim T&: T = timeGetTime
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Dim ScriptName$: ScriptName = "stl2igs.rb"
    Dim IgesName$: IgesName = FSO.GetBaseName(FilePath) + ".igs"
    Dim FgName$: FgName = "fg.txt"
    
    'SirenScriptファイル作成
    Call WriteFile(TempPath + FgName, "削除しても結構です")
    Call WriteFile(TempPath + ScriptName, _
        Join(GetSirenScript(Replace(FilePath, "\", "/") _
            , Replace(TempPath, "\", "/") + IgesName _
            , Replace(TempPath, "\", "/") + FgName), vbNewLine))
    
    'SirenScript実行-Iges作成
    Call Execute(TempPath + ScriptName)
    
    'Iges読み込み
    Call OpenIges(TempPath + IgesName, TempPath + FgName, FSO.GetFile(FilePath).Size)
    
    '掃除
    Call DeleteFile(TempPath + ScriptName)
    Call DeleteFile(TempPath + IgesName)
    Set FSO = Nothing
    
    MsgBox 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$, ByVal Count&)
    Dim i&
    For i = 0 To Count
        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 InPath$, ByVal ExPath$, ByVal FgPath$) As Variant
    Dim Count&: Count = 4
    Dim txts() As String: ReDim txts(Count)
    txts(0) = "puts" + Chr(34) + "Stl to Iges ...." + Chr(34)
    txts(1) = "stl = STL.load " + Chr(34) + InPath + Chr(34)
    txts(2) = "IGES.save [stl]," + Chr(34) + ExPath + Chr(34)
    txts(3) = "File.delete " + Chr(34) + FgPath + Chr(34)
    txts(4) = "puts" + Chr(34) + "OK" + Chr(34)
    GetSirenScript = txts
End Function

結果はこちら。
f:id:kandennti:20160303185248p:plain
八割ぐらいの時間短縮なので、効果は絶大なのですが16分ぐらい・・。
230MBぐらいのIges読み込むから、仕方ないのかな・・・。
やっぱり、 STLD&D → Iges化 → Part化
までをバッチ処理するVBSを作った方が、オペレーションを奪われなくて
良い気がします。 (それにしてもSirenは、すばらしい)