NX Journal 図面を開いてPDF変換

Share on:

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

Pythonコード

 1import NXOpen
 2import NXOpen.Layer
 3import NXOpen.UF
 4import csv
 5import os
 6
 7def main():
 8    theSession  = NXOpen.Session.GetSession()
 9    
10    # csvを開く
11    dicts = open_csv()
12
13    # 開けなければ終了
14    if dicts is None:
15        return
16    
17    # NXoutputフォルダが存在するか
18    if not os.path.exists("D:\\NXoutput"):
19        os.mkdir("D:\\NXoutput")
20    
21    for d in dicts:
22
23        # 図面のファイル名を作成
24        dwg_filename = "@DB/" + d["ID"] + "/" + d["リビジョン"] + "/specification/"  + d["図面名"]
25        
26        # ファイルを開く
27        workPart = openPart(theSession, dwg_filename)
28        
29        # アプリケーションの切り替え
30        theSession.ApplicationSwitchImmediate("UG_APP_DRAFTING")
31
32        # PDF変換
33        exportPDF(theSession, workPart, d)
34
35def openPart(theSession, id):
36    try:
37        # 既にファイルを開いていれば表示パートを変える
38        part1 = theSession.Parts.FindObject(id)
39        status1, partLoadStatus1 = theSession.Parts.SetActiveDisplay(
40            part1, 
41            NXOpen.DisplayPartOption.AllowAdditional, 
42            NXOpen.PartDisplayPartWorkPartOption.UseLast
43        )
44        partLoadStatus1.Dispose()
45    except Exception as e:
46        # 見つからなければファイルを開く
47        status1 = theSession.Parts.OpenBaseDisplay(id)
48    return theSession.Parts.Work
49
50def open_csv():
51    try:
52        # ファイル選択ダイアログ表示
53        theUfSession  = NXOpen.UF.UFSession.GetUFSession()
54        theUI = NXOpen.UI.GetUI()
55        ret = theUfSession.Ui.CreateFilebox("", "CSVファイルを選択", "csv", "*.csv")
56
57        # CSVを辞書のリストに変換
58        with open(ret[1]) as f:
59            reader = csv.DictReader(f)
60            dicts = [row for row in reader]
61        
62        return dicts
63    except:
64        return None
65
66def exportPDF(theSession, workPart, d):
67    printPDFBuilder1 = workPart.PlotManager.CreatePrintPdfbuilder()
68    printPDFBuilder1.Relation = NXOpen.PrintPDFBuilder.RelationOption.Manifestation
69    printPDFBuilder1.DatasetType = "PDF"
70    printPDFBuilder1.NamedReferenceType = "PDF_Reference"
71    printPDFBuilder1.Scale = 1.0
72    printPDFBuilder1.Action = NXOpen.PrintPDFBuilder.ActionOption.Native
73    printPDFBuilder1.DatasetName = d["ID"] + "_" + d["リビジョン"] + "_PDF_1"
74    printPDFBuilder1.Colors = NXOpen.PrintPDFBuilder.Color.BlackOnWhite
75    printPDFBuilder1.Widths = NXOpen.PrintPDFBuilder.Width.CustomThreeWidths
76    printPDFBuilder1.Size = NXOpen.PrintPDFBuilder.SizeOption.ScaleFactor
77    printPDFBuilder1.Units = NXOpen.PrintPDFBuilder.UnitsOption.English
78    printPDFBuilder1.XDimension = 8.5
79    printPDFBuilder1.YDimension = 11.0
80    printPDFBuilder1.OutputText = NXOpen.PrintPDFBuilder.OutputTextOption.Polylines
81    printPDFBuilder1.RasterImages = True
82    printPDFBuilder1.Assign()
83    printPDFBuilder1.Watermark = ""
84
85    # 作業パートのシート数分繰り返し
86    for sheet1 in workPart.DrawingSheets:
87        printPDFBuilder1.SourceBuilder.SetSheets([sheet1])
88        printPDFBuilder1.Filename = "D:\\NXoutput\\" + d["ID"] + "_" + d["リビジョン"] + "_" + sheet1.Name + ".pdf"
89        printPDFBuilder1.Commit()
90    
91    printPDFBuilder1.Destroy()
92    theSession.CleanUpFacetedFacesAndEdges()
93
94if __name__ == '__main__':
95    main()

関連記事