NX Journal 図面を開いてPDF変換

2020/01/19 categories:NX Journal| tags:NX Journal|Python|

CSVからID、リビジョン、図面名を取得して図面を開く、PDF変換を繰り返すプログラムを作成しました。

Pythonコード

import NXOpen
import NXOpen.Layer
import NXOpen.UF
import csv
import os

def main():
    theSession  = NXOpen.Session.GetSession()
    
    # csvを開く
    dicts = open_csv()

    # 開けなければ終了
    if dicts is None:
        return
    
    # NXoutputフォルダが存在するか
    if not os.path.exists("D:\\NXoutput"):
        os.mkdir("D:\\NXoutput")
    
    for d in dicts:

        # 図面のファイル名を作成
        dwg_filename = "@DB/" + d["ID"] + "/" + d["リビジョン"] + "/specification/"  + d["図面名"]
        
        # ファイルを開く
        workPart = openPart(theSession, dwg_filename)
        
        # アプリケーションの切り替え
        theSession.ApplicationSwitchImmediate("UG_APP_DRAFTING")

        # PDF変換
        exportPDF(theSession, workPart, d)

def openPart(theSession, id):
    try:
        # 既にファイルを開いていれば表示パートを変える
        part1 = theSession.Parts.FindObject(id)
        status1, partLoadStatus1 = theSession.Parts.SetActiveDisplay(
            part1, 
            NXOpen.DisplayPartOption.AllowAdditional, 
            NXOpen.PartDisplayPartWorkPartOption.UseLast
        )
        partLoadStatus1.Dispose()
    except Exception as e:
        # 見つからなければファイルを開く
        status1 = theSession.Parts.OpenBaseDisplay(id)
    return theSession.Parts.Work

def open_csv():
    try:
        # ファイル選択ダイアログ表示
        theUfSession  = NXOpen.UF.UFSession.GetUFSession()
        theUI = NXOpen.UI.GetUI()
        ret = theUfSession.Ui.CreateFilebox("", "CSVファイルを選択", "csv", "*.csv")

        # CSVを辞書のリストに変換
        with open(ret[1]) as f:
            reader = csv.DictReader(f)
            dicts = [row for row in reader]
        
        return dicts
    except:
        return None

def exportPDF(theSession, workPart, d):
    printPDFBuilder1 = workPart.PlotManager.CreatePrintPdfbuilder()
    printPDFBuilder1.Relation = NXOpen.PrintPDFBuilder.RelationOption.Manifestation
    printPDFBuilder1.DatasetType = "PDF"
    printPDFBuilder1.NamedReferenceType = "PDF_Reference"
    printPDFBuilder1.Scale = 1.0
    printPDFBuilder1.Action = NXOpen.PrintPDFBuilder.ActionOption.Native
    printPDFBuilder1.DatasetName = d["ID"] + "_" + d["リビジョン"] + "_PDF_1"
    printPDFBuilder1.Colors = NXOpen.PrintPDFBuilder.Color.BlackOnWhite
    printPDFBuilder1.Widths = NXOpen.PrintPDFBuilder.Width.CustomThreeWidths
    printPDFBuilder1.Size = NXOpen.PrintPDFBuilder.SizeOption.ScaleFactor
    printPDFBuilder1.Units = NXOpen.PrintPDFBuilder.UnitsOption.English
    printPDFBuilder1.XDimension = 8.5
    printPDFBuilder1.YDimension = 11.0
    printPDFBuilder1.OutputText = NXOpen.PrintPDFBuilder.OutputTextOption.Polylines
    printPDFBuilder1.RasterImages = True
    printPDFBuilder1.Assign()
    printPDFBuilder1.Watermark = ""

    # 作業パートのシート数分繰り返し
    for sheet1 in workPart.DrawingSheets:
        printPDFBuilder1.SourceBuilder.SetSheets([sheet1])
        printPDFBuilder1.Filename = "D:\\NXoutput\\" + d["ID"] + "_" + d["リビジョン"] + "_" + sheet1.Name + ".pdf"
        printPDFBuilder1.Commit()
    
    printPDFBuilder1.Destroy()
    theSession.CleanUpFacetedFacesAndEdges()

if __name__ == '__main__':
    main()

Share post

Related Posts

コメント