Hello,
I'm trying to implement a functionality in Inventor to display a WPF window when saving a document or part, and add some custom properties. My approach works well, but I encounter an issue when trying to get the part number of my document. The document needs to be saved first before I can retrieve the part number, so I need to use the After Save event. However, using the After Save event prevents the document from being saved. I can get the part number, but the document does not save due to the After Save event. When I remove the After Save event, the document saves, but I don't get the part number.
Here is my code:
private void OnSaving(_Document DocumentObject, EventTimingEnum BeforeOrAfter, NameValueMap Context, out HandlingCodeEnum HandlingCode)
{
HandlingCode = HandlingCodeEnum.kEventNotHandled;
BedienerBoxKrampe wpf = new BedienerBoxKrampe();
var activeDocumnet = m_inventorApplication.ActiveDocument;
Inventor.DocumentTypeEnum docType = activeDocumnet.DocumentType;
string currentUser = m_inventorApplication.UserName;
if (BeforeOrAfter == EventTimingEnum.kAbort)
{
HandlingCode = HandlingCodeEnum.kEventCanceled;
MessageBox.Show("Speicher Vorgang abgebrochen");
}
//MessageBox.Show("inventor");
if (BeforeOrAfter == EventTimingEnum.kBefore)
{
var partDocument = (PartDocument)activeDocumnet; // convert to partx to access ComponentDefinition
var materialName = partDocument.ComponentDefinition.Material.Name;
if (!wpf._isMaterialGenerisch)
{
wpf.ShowDialog();
}
}
}
}
if (BeforeOrAfter == EventTimingEnum.kAfter)
{
wpf.AddPartNumberAsCusumProp();
}
}
I need assistance with correctly using the After Save event to ensure the document is saved and I can retrieve the part number. Did I miss something in my implementation?
Thanks for your help!
Hi @ppauleSHGR6. I am not that familiar with C#.net, but I am with vb.net, and familiar with handling events, so I may be able to help some. It sounds like you want to show a dialog to help you add some custom iProperties to the document when you save the document. It would normally be best to do that before saving, but the part number is not available before save, so you need to do it after save, right?
You will need to avoid setting the HandlingCodeEnum to the kEventCanceled variation, and may also want to avoid setting it to the 'kEventHandled' variation also, to avoid canceling or overriding the Save event. If you only want to react to this event during its 'after' timing, then use a simple 'If' statement, to check if 'BeforeOrAfter' variable equals 'EventTimingEnum.kAfter', then put your code within that If statement. You do not need to change the value of the HandlingCodeEnum unless you specifically want to cancel the event, or otherwise do not want it to Save normally. If you do not change its value, it will remain 'kEventNotHandled', and will continue to save as normal. We can only cancel the event if reacting within its 'kBefore' timing. If you do cancel the event during 'kBefore' it will still react again, but then the timing will be kAbort instead of kAfter. Any code you do not enclose within an If statement that immediately checks timing, will run during every timing.
Of course making edits to the document after a save leaves the document 'Dirty', and therefore needing to be saved again. But we certainly do not want to include a call for it to save the document again within the save event handler, because that can lead to an endless loop. However, if you do call for a standard save, it will check the status of the documents Dirty property first, and if not dirty, it will not actually save, because it does not believe it to be necessary.
ApplicationEvents.OnSaveDocument
Wesley Crihfield
(Not an Autodesk Employee)
Can't find what you're looking for? Ask the community or share your knowledge.