Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Inventor Addin BeforeOrAfter == EventTimingEnum.kAfter how to use ?

1 REPLY 1
Reply
Message 1 of 2
ppauleSHGR6
114 Views, 1 Reply

Inventor Addin BeforeOrAfter == EventTimingEnum.kAfter how to use ?

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!

 

1 REPLY 1
Message 2 of 2
WCrihfield
in reply to: ppauleSHGR6

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

EESignature

(Not an Autodesk Employee)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report