-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreateDrawingNotes.cs
More file actions
263 lines (211 loc) · 10.9 KB
/
Copy pathcreateDrawingNotes.cs
File metadata and controls
263 lines (211 loc) · 10.9 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* createDrawingNotes.cs
*
* Ein Journal welches assoziative Texthinweise auf einer Zeichnung hinzufügt. Die Hinweise werden dabei immer im jeweiligen
* Einzelteil als Teileattribut hinterlegt. Diese Attribute können z.B. über die Stücklistenfunktion oder eine Datenbankverbindung
* bearbeitet werden.
*
* Beim Verwenden der Funktion muss die Komponente, von welcher die Attribute verwendet werden sollen und die Ansicht, an die die
* Informationen geheftet werden sollen, ausgewählt werden.
*
*/
using System;
// using System.IO;
// using System.Text;
using NXOpen;
using NXOpen.UF;
public class CreateDrawingNotes {
private static NXOpen.Session theSession;
// private static NXOpen.ListingWindow theLW;
private static NXOpen.Part workPart;
// private static NXOpen.Part displayPart;
private static NXOpen.UI theUI;
private static NXOpen.UF.UFSession theUfSession;
//------------------------------------------------------------------------------
// Hauptprogramm
//------------------------------------------------------------------------------
public static void Main(string[] args) {
try {
theSession = NXOpen.Session.GetSession();
workPart = theSession.Parts.Work;
// displayPart = theSession.Parts.Display;
// theLW = theSession.ListingWindow;
theUI = UI.GetUI();
theUfSession = UFSession.GetUFSession();
} catch (NXOpen.NXException ex) {
UI.GetUI().NXMessageBox.Show("Error / Fehler", NXMessageBox.DialogType.Error, ex.Message);
return;
}
// ----------------------------------------------
// UnDo-Marke setzen
// ----------------------------------------------
NXOpen.Session.UndoMarkId markId1;
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "CreateDrawingNotes");
// ----------------------------------------------
// Eine Komponente & eine Ansicht auswählen
// ----------------------------------------------
NXObject component;
NXOpen.Drawings.DraftingView view;
try {
component = (NXObject) selectComponent();
view = (NXOpen.Drawings.DraftingView) selectView();
} catch (Exception ex) {
UI.GetUI().NXMessageBox.Show("Error / Fehler", NXMessageBox.DialogType.Error, ex.Message);
return;
}
// ----------------------------------------------
// Den Texthinweis erstellen und ablegen
// ----------------------------------------------
try {
createNote(component, view);
} catch (NXOpen.NXException ex) {
UI.GetUI().NXMessageBox.Show("Error / Fehler", NXMessageBox.DialogType.Error, ex.Message);
theSession.UndoToMark(markId1,"CreateDrawingNotes");
return;
}
}
//------------------------------------------------------------------------------
// Eine Komponente auswählen
//------------------------------------------------------------------------------
public static TaggedObject selectComponent() {
theUfSession.Ui.SetCursorView(0); // Für das auswählen innerhalb einer Zeichnung notwendig
TaggedObject tagobj;
Point3d cursor;
Selection.MaskTriple[] maskArray = new Selection.MaskTriple[1];
maskArray[0].Type = NXOpen.UF.UFConstants.UF_component_type; // 63
maskArray[0].Subtype = NXOpen.UF.UFConstants.UF_component_subtype;
Selection.Response resp = theUI.SelectionManager.SelectTaggedObject(
"Komponente auswählen",
"Komponente auswählen",
Selection.SelectionScope.AnyInAssembly,
Selection.SelectionAction.ClearAndEnableSpecific,
false,
false,
maskArray,
out tagobj,
out cursor);
if (resp == Selection.Response.ObjectSelected || resp == Selection.Response.ObjectSelectedByName) {
return tagobj;
}
throw new Exception("Keine Komponente ausgewählt oder Auswahl fehlerhaft.");
}
//------------------------------------------------------------------------------
// Eine Zeichnungsansicht auswählen
//------------------------------------------------------------------------------
public static TaggedObject selectView() {
theUfSession.Ui.SetCursorView(0); // Für das auswählen innerhalb einer Zeichnung notwendig
TaggedObject tagobj;
Point3d cursor;
Selection.MaskTriple[] maskArray = new Selection.MaskTriple[1];
maskArray[0].Type = NXOpen.UF.UFConstants.UF_view_type; // 60
maskArray[0].Subtype = NXOpen.UF.UFConstants.UF_all_subtype;
Selection.Response resp = theUI.SelectionManager.SelectTaggedObject(
"Ansicht auswählen",
"Ansicht auswählen",
Selection.SelectionScope.AnyInAssembly,
Selection.SelectionAction.ClearAndEnableSpecific,
false,
false,
maskArray,
out tagobj,
out cursor);
if (resp == Selection.Response.ObjectSelected || resp == Selection.Response.ObjectSelectedByName) {
return tagobj;
}
throw new Exception("Keine Ansicht ausgewählt oder Auswahl fehlerhaft.");
}
//------------------------------------------------------------------------------
// Den Texthinweis erstellen
//------------------------------------------------------------------------------
public static void createNote(NXObject component, NXOpen.Drawings.DraftingView view) {
// ----------------------------------------------
// Note-Builder erstellen
// ----------------------------------------------
NXOpen.Annotations.DraftingNoteBuilder draftingNoteBuilder1;
draftingNoteBuilder1 = workPart.Annotations.CreateDraftingNoteBuilder(null);
draftingNoteBuilder1.Origin.Anchor = NXOpen.Annotations.OriginBuilder.AlignmentPosition.BottomCenter;
// ----------------------------------------------
// Text erstellen
// ----------------------------------------------
string[] text1 = new string[5];
text1[0] = "<C1.25><D1><I20>Teil - <I0><D><C>";
text1[1] = "<C0.75>()<C>";
text1[2] = "Material: ";
text1[3] = "Abmessungen: ";
text1[4] = "Maßstab: ";
draftingNoteBuilder1.Text.TextBlock.SetText(text1);
// ----------------------------------------------
// Attribute dem Text hinzufügen
// ----------------------------------------------
try {
AddPartAttributeToText(draftingNoteBuilder1, component, "PART_NAME", 1, 25);
AddPartAttributeToText(draftingNoteBuilder1, component, "NUMBER", 1, 22);
AddPartAttributeToText(draftingNoteBuilder1, component, "$PART_NAME", 2, 9);
AddPartAttributeToText(draftingNoteBuilder1, component, "MATERIAL_TOOLING", 3, 11);
AddPartAttributeToText(draftingNoteBuilder1, component, "MW_STOCK_SIZE", 4, 14);
AddPartAttributeToText(draftingNoteBuilder1, view, "VWSCALE", 5, 10);
} catch (NXOpen.NXException ex) {
throw;
}
// ----------------------------------------------
// Drafting-Note mit Ansicht verknüpfen und platzieren
// ----------------------------------------------
NXOpen.Annotations.Annotation.AssociativeOriginData assDate = new NXOpen.Annotations.Annotation.AssociativeOriginData();
assDate.View = view;
assDate.OriginType = NXOpen.Annotations.AssociativeOriginType.RelativeToView;
draftingNoteBuilder1.Origin.SetAssociativeOrigin(assDate);
Double[] viewBorder = new Double[4];
theUfSession.Draw.AskViewBorders(view.Tag, viewBorder);
/* viewBorder[]
* [0] - X min
* [1] - Y min
* [2] - X max
* [3] - Y max
*/
NXOpen.Point3d point1 = new NXOpen.Point3d((viewBorder[0]+viewBorder[2])/2, viewBorder[3]+10, 0.0);
draftingNoteBuilder1.Origin.Origin.SetValue(null, view, point1);
// ----------------------------------------------
// Drafting-Note absetzen
// ----------------------------------------------
NXOpen.Annotations.Note note1 = (NXOpen.Annotations.Note) draftingNoteBuilder1.Commit();
draftingNoteBuilder1.Destroy();
// ----------------------------------------------
// Text zentrieren
// ----------------------------------------------
NXOpen.DisplayableObject[] dispOjects1 = new NXOpen.DisplayableObject[1];
dispOjects1[0] = note1;
NXOpen.Annotations.EditSettingsBuilder editSettingsBuilder1;
editSettingsBuilder1 = workPart.SettingsManager.CreateAnnotationEditSettingsBuilder(dispOjects1);
editSettingsBuilder1.AnnotationStyle.LetteringStyle.HorizontalTextJustification = NXOpen.Annotations.TextJustification.Center;
editSettingsBuilder1.Commit();
editSettingsBuilder1.Destroy();
}
//------------------------------------------------------------------------------
// Attribute prüfen, ggf. erstellen und dem Text hinzufügen...
//------------------------------------------------------------------------------
public static void AddPartAttributeToText(NXOpen.Annotations.DraftingNoteBuilder draftingNoteBuilder1, NXObject component, string attributename, int line, int cursorPos) {
try {
component.GetUserAttributeAsString(attributename, NXObject.AttributeType.Any, -1);
} catch {
try {
NXOpen.NXObject[] attributeObj = new NXOpen.NXObject[1];
attributeObj[0] = (NXObject)component.Prototype;
NXOpen.AttributePropertiesBuilder attributePropertiesBuilder;
attributePropertiesBuilder = theSession.AttributeManager.CreateAttributePropertiesBuilder(workPart, attributeObj, AttributePropertiesBuilder.OperationType.None);
attributePropertiesBuilder.Title = attributename;
attributePropertiesBuilder.StringValue = "???";
attributePropertiesBuilder.Commit();
attributePropertiesBuilder.Destroy();
} catch (NXOpen.NXException ex) {
UI.GetUI().NXMessageBox.Show("Error / Fehler", NXMessageBox.DialogType.Error, ex.Message);
throw;
}
}
draftingNoteBuilder1.Text.TextBlock.AddAttributeReference(component, attributename, false, line, cursorPos);
}
//------------------------------------------------------------------------------
// Unload-Fuktion, wird von NX automatisch aufgerufen
//------------------------------------------------------------------------------
public static int GetUnloadOption(string dummy) {
return (int)NXOpen.Session.LibraryUnloadOption.Immediately;
}
}