2010年4月26日月曜日

完成したジャーナル

Importsの内容とかMainの位置とかいろいろ変えてみて、以下のコードで動きました。パート属性を割り当てるところはNXのOpen for .NETヘルプを見ました。

Option Strict Off
Imports System
Imports NXOpen
Imports system.IO
Imports VB = Microsoft.VisualBasic
Imports System.Windows.Forms
Imports system.text

Module NXJournal
Sub Main
    Dim theSession As Session = Session.GetSession()
    Dim workPart As Part = theSession.Parts.Work
    Dim displayPart As Part = theSession.Parts.Display

    '---------------------------------------
    'ここからVB
        ' --------------------------------------
        Dim pathNXGate As String = "C:\nxgate\temp\"
        Dim tmpFile As String
        Dim tmpFileExp As String
        Dim sTmp(5) As String
        Dim sAry() As String
        Dim sTitle() As String
        Dim sAttId As String
        Dim sAttNam(30) As String
        Dim sAttVal(30) As String
        Dim iAttChk(100) As Integer
        Dim iCtAtt As Integer = 0
        Dim iCt As Integer = 0
        Dim i As Integer
        Dim num As Integer
        Dim sExpLn As String = ""
        Dim sLTtl As String = ""
        Dim aLn() As String
        Dim sjisEnc As Encoding = Encoding.GetEncoding("Shift_JIS")
        ' --------------------------------------File開くDialog
        Dim ofd As New OpenFileDialog()
        ofd.FileName = "csvUCF.csv"
        ofd.InitialDirectory = pathNXGate
        ofd.Title = "寸法リストファイルを選択"
        If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
            tmpFile = ofd.FileName
        Else
            Exit Sub
        End If
        ' --------------------------------------DataFile有
        ' 1行目はタイトル行
        ' パート属性にする列タイトルは先頭にatt_をつける
        ' FILE名するタイトルは、att_CL_ID固定&このタイトルは必須
        If File.Exists(tmpFile) Then
            FileOpen(1, tmpFile, OpenMode.Input, OpenAccess.Read)       ' 読込
            ' ----------------------------------------------1行目(Title行)処理
            sTmp(0) = LineInput(1)                          ' d,att_CL_ID,L...
            sTmp(0) = Replace(sTmp(0), Chr(9), "")          ' Tab除去
            sTitle = Split(sTmp(0), ",")
            num = sTitle.Length                             ' title総数
            For i = 0 To num - 1
                If sTitle(i).IndexOf("att_") = 0 Then       ' 先頭がatt_
                    sTitle(i) = Replace(sTitle(i), "att_", "")
                    iAttChk(i) = iCtAtt
                    iCtAtt = iCtAtt + 1                     ' 属性数
                Else                                        ' att以外
                    iAttChk(i) = -1                         ' 属性Check
                End If
            Next
            ' ----------------------------------------------2行目以降(Title行以外)処理
            Do While Not EOF(1)
                sTmp(0) = LineInput(1)
                sTmp(0) = Replace(sTmp(0), Chr(9), "")      ' Tab除去
                sAry = Split(sTmp(0), ",")
        sAttId=""
        sExpLn=""
                For i = 0 To num - 1
                    If sTitle(i) <> "" Then
                        If iAttChk(i) > -1 Then             ' 属性の場合
                            sAttNam(iAttChk(i)) = sTitle(i) ' 属性名
                            sAttVal(iAttChk(i)) = sAry(i)   ' 属性値
                            If sTitle(i) = "CL_ID" Then     ' IDの場合
                                sAttId = sAry(i)            ' ID
                            End If
                        Else
                            sExpLn = sExpLn & sTitle(i) & "=" & sAry(i) & vbCrLf
                        End If
                    End If
                Next
                ' ------------------------------------------式インポート用ファイル作成
                tmpFileExp = pathNXGate & sAttId & ".exp" 
                Dim writer As New StreamWriter(tmpFileExp, False, sjisEnc)
                writer.WriteLine(sExpLn)
                writer.Close()
        ' ここからJournal
        ' ----------------------------------------------
        ' メニュー: ツール->Expression...
        ' ----------------------------------------------
        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Expression")

        Dim expModified1 As Boolean
        Dim errorMessages1() As String
        workPart.Expressions.ImportFromFile(tmpFileExp, ExpressionCollection.ImportMode.Replace, expModified1, errorMessages1)
        Dim markId2 As Session.UndoMarkId
        markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Update Expression Data")
        Dim nErrs1 As Integer
        nErrs1 = theSession.UpdateManager.DoUpdate(markId2)

        theSession.DeleteUndoMark(markId2, "Update Expression Data")

        Dim markId3 As Session.UndoMarkId
        markId3 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Update Expression Data")

        Dim nErrs2 As Integer
        nErrs2 = theSession.UpdateManager.DoUpdate(markId3)

        theSession.DeleteUndoMark(markId3, "Update Expression Data")
        ' ----------------------------------------------
        ' 属性割当
        ' ----------------------------------------------
                For i = 0 To iCtAtt - 1 
            workPart.SetAttribute(sAttNam(i), sAttVal(i))
        NEXT
        ' ----------------------------------------------
        ' メニュー: ファイル->名前を変えて保存...
        ' ----------------------------------------------
        Dim partSaveStatus1 As PartSaveStatus
        partSaveStatus1 = workPart.SaveAs(pathNXGate & sAttId)
        partSaveStatus1.Dispose()
            Loop
        End If
' ----------------------------------------------
' メニュー: ツール->ジャーナル->停止
' ----------------------------------------------

End Sub
End Module

完成したコードはこれです。拡張子がvbだとアップロードできなかったので、ダウンロード後、拡張子をtxt→vbに変更してください。
http://sites.google.com/site/nxgatentn/nxGateCreatePart_csv.txt?attredirects=0&d=1

操作手順などへ続きます

0 件のコメント:

コメントを投稿