This repository has been archived by the owner on Aug 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Automation.cs
147 lines (123 loc) · 5.95 KB
/
Automation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/////////////////////////////////////////////////////////////////////
// Copyright (c) Autodesk, Inc. All rights reserved
// Written by Forge Partner Development
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
/////////////////////////////////////////////////////////////////////
using System.Diagnostics;
using System.Runtime.InteropServices;
using Inventor;
using Autodesk.Forge.DesignAutomation.Inventor.Utils;
using Shared;
namespace ExportDrawingAsPdfPlugin
{
[ComVisible(true)]
public class Automation : AutomationBase
{
public Automation(InventorServer inventorApp) : base(inventorApp)
{
}
public override void ExecWithArguments(Document doc, NameValueMap map)
{
using (new HeartBeat())
{
var dir = System.IO.Directory.GetCurrentDirectory();
var drawingToGenerate = map.Item["_2"];
if (drawingToGenerate == null)
{
LogTrace("Drawing not specified !");
}
LogTrace("Drawing to generate PDF: {0}", drawingToGenerate);
NameValueMap openOptions = _inventorApplication.TransientObjects.CreateNameValueMap();
string drawing = System.IO.Path.Combine(dir, /* ??? */"unzippedIam", drawingToGenerate);
LogTrace("Exporting : " + drawing);
var drawingDocument = _inventorApplication.Documents.OpenWithOptions(drawing, openOptions, true);
LogTrace("Drawing opened");
drawingDocument.Update2(true);
LogTrace("Drawing updated");
drawingDocument.Save2(true);
LogTrace("Drawing saved");
var pdfPath = System.IO.Path.Combine(dir, "Drawing.pdf");
LogTrace("Exporting drawing to: " + pdfPath);
ExportIDWToPDF(drawingDocument, pdfPath);
LogTrace("Drawing exported");
}
}
private void ActivateDefaultProject(string dir)
{
var defaultProjectName = "FDADefault";
var projectFullFileName = System.IO.Path.Combine(dir, defaultProjectName + ".ipj");
DesignProject project = null;
if (System.IO.File.Exists(projectFullFileName))
{
project = _inventorApplication.DesignProjectManager.DesignProjects.AddExisting(projectFullFileName);
Trace.TraceInformation("Adding existing default project file: {0}", projectFullFileName);
} else
{
project = _inventorApplication.DesignProjectManager.DesignProjects.Add(MultiUserModeEnum.kSingleUserMode, defaultProjectName, dir);
Trace.TraceInformation("Creating default project file with name: {0} at {1}", defaultProjectName, dir);
}
Trace.TraceInformation("Activating default project {0}", project.FullFileName);
project.Activate(true);
}
// Export Drawing file to PDF format
// In the case that the Drawing has more sheets -> it will export PDF with pages
// Each PDF page represents one Drawing sheet
public void ExportIDWToPDF(Document doc, string exportFileName)
{
if (doc == null)
{
LogError("Document is null!");
return;
}
LogTrace("PDF file full path : " + exportFileName);
LogTrace("Create PDF Translator Addin");
TranslatorAddIn PDFAddIn = (TranslatorAddIn)_inventorApplication.ApplicationAddIns.ItemById["{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}"];
if (PDFAddIn == null)
{
LogError("Error: PDF Translator Addin is null!");
return;
}
TranslationContext context = _inventorApplication.TransientObjects.CreateTranslationContext();
NameValueMap options = _inventorApplication.TransientObjects.CreateNameValueMap();
if (PDFAddIn.HasSaveCopyAsOptions[doc, context, options])
{
context.Type = IOMechanismEnum.kFileBrowseIOMechanism;
DataMedium dataMedium = _inventorApplication.TransientObjects.CreateDataMedium();
options.Value["Sheet_Range"] = PrintRangeEnum.kPrintAllSheets;
options.Value["Vector_Resolution"] = 300;
options.Value["All_Color_AS_Black"] = false;
options.Value["Sheets"] = GetSheetOptions(doc);
dataMedium.FileName = exportFileName;
LogTrace("Processing PDF export ...");
PDFAddIn.SaveCopyAs(doc, context, options, dataMedium);
LogTrace("Finish processing PDF export ...");
}
}
// Check if the Drawing file has more sheets
private NameValueMap GetSheetOptions(Document doc)
{
DrawingDocument drawingDocument = doc as DrawingDocument;
NameValueMap sheets = _inventorApplication.TransientObjects.CreateNameValueMap();
foreach (Sheet sheet in drawingDocument.Sheets)
{
NameValueMap option = _inventorApplication.TransientObjects.CreateNameValueMap();
option.Add("Name", sheet.Name);
option.Add("3DModel", false);
sheets.Add("Sheet" + sheets.Count + 1, option);
}
return sheets;
}
}
}