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: 

Problem with DocumentEvents after update to Inventor 2021.1

5 REPLIES 5
Reply
Message 1 of 6
thomas.johansson
849 Views, 5 Replies

Problem with DocumentEvents after update to Inventor 2021.1

We have a product that uses DocumentEvents.OnChangeSelectSet. After the latest update from 2021 to 2021.1 it stopped working.

The code/event we use to subscribe the event is Application.ApplicationEvents.OnActivateDocument  and to unsubscribe the event in Application.ApplicationEvents.OnDeactivateDocument.

 

The DocumentEvents.OnChangeSelectSet do not always fire after updating Inventor to version 2021.1.

Is there enyone else who has or can reproduce this problem?

 

The best way to test this is you have an assembly open (not new) and click in the model browser pane several times (Folders, parts, plane etc.) and then move between open files and repeat clicking in the model browser pane or in the modelview.

 

Sending my example code below.

 

Example code:

Test class:

 

public class Test
{
private Inventor.Application inventorApplication;

public Test()
{
StandardAddInServer.Instance.Application.ApplicationEvents.OnActivateDocument += ApplicationEvents_OnActivateDocument;
StandardAddInServer.Instance.Application.ApplicationEvents.OnDeactivateDocument += ApplicationEvents_OnDeactivateDocument;
}

/// <summary>
/// We use this event for unsubscribe the DocumentEvents_OnChangeSelectSet.
/// </summary>
/// <param name="DocumentObject"></param>
/// <param name="BeforeOrAfter"></param>
/// <param name="Context"></param>
/// <param name="HandlingCode"></param>
private void ApplicationEvents_OnDeactivateDocument(_Document DocumentObject, EventTimingEnum BeforeOrAfter, NameValueMap Context, out HandlingCodeEnum HandlingCode)
{
try
{
if (BeforeOrAfter == EventTimingEnum.kBefore)
{
DocumentObject.DocumentEvents.OnChangeSelectSet -= DocumentEvents_OnChangeSelectSet;
}
}
catch
{
}
HandlingCode = HandlingCodeEnum.kEventNotHandled;
}

/// <summary>
/// We use this event for subscribe the DocumentEvents_OnChangeSelectSet.
/// </summary>
/// <param name="DocumentObject"></param>
/// <param name="BeforeOrAfter"></param>
/// <param name="Context"></param>
/// <param name="HandlingCode"></param>
private void ApplicationEvents_OnActivateDocument(_Document DocumentObject, EventTimingEnum BeforeOrAfter, NameValueMap Context, out HandlingCodeEnum HandlingCode)
{
try
{
if (BeforeOrAfter == EventTimingEnum.kAfter)
{

DocumentObject.DocumentEvents.OnChangeSelectSet += DocumentEvents_OnChangeSelectSet;
MessageBox.Show("Connected!");
}
}
catch
{
}
HandlingCode = HandlingCodeEnum.kEventNotHandled;
}


/// <summary>
/// Fires on selection change in view or browser. Very Important for our product.
/// </summary>
/// <param name="BeforeOrAfter"></param>
/// <param name="Context"></param>
/// <param name="HandlingCode"></param>
private void DocumentEvents_OnChangeSelectSet(EventTimingEnum BeforeOrAfter, NameValueMap Context, out HandlingCodeEnum HandlingCode)
{
MessageBox.Show("Working!");

HandlingCode = HandlingCodeEnum.kEventNotHandled;
}

}

 

Modified code in StandardAddinServer class:

public StandardAddInServer()
{
}

/// <summary>
/// Get this AddInServer instance
/// </summary>
public static StandardAddInServer Instance
{
get
{
return _instance;
}
}

/// <summary>
///
/// </summary>
public Inventor.Application Application
{
get
{
return m_inventorApplication;
}
}

public void Activate(Inventor.ApplicationAddInSite AddInSiteObject, bool FirstTime)
{
m_inventorApplication = AddInSiteObject.Application;
_instance = this;
Test testClass = new Test();
}

 

 

 

Tags (1)
Labels (1)
5 REPLIES 5
Message 2 of 6
tschramm
in reply to: thomas.johansson

I have exactly the same error. The event is not always called. In 2021.0 everything works fine.

Message 3 of 6
thomas.johansson
in reply to: tschramm

Have found a solution?
Message 4 of 6

Found a solution.

Use local instances.

 

public class Test
{
private Inventor.Application inventorApplication;
private ApplicationEvents _applicationEvents;

 

public ApplicationEvents AppEvents
{
get => _applicationEvents; set => _applicationEvents = value;
}

 

public Test()
{
 AppEvents = AddInServer.Instance.Application.ApplicationEvents;
AppEvents.OnActivateDocument += ApplicationEvents_OnActivateDocument;
AppEvents.OnDeactivateDocument += ApplicationEvents_OnDeactivateDocument;
}

Message 5 of 6
tschramm
in reply to: thomas.johansson

Yes, we solved the problem. The lifetime of the COM-Objects seem to end very short, if they are not a member of a class. In the earlier Versions, those objects had a longer lifetime.

Message 6 of 6
c_hoppen
in reply to: thomas.johansson

That's the solution!

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

Post to forums  

Autodesk Design & Make Report