Execute method when sheet size is changed in addin C#

Execute method when sheet size is changed in addin C#

gert-leonvanlier
Collaborator Collaborator
482 Views
4 Replies
Message 1 of 5

Execute method when sheet size is changed in addin C#

gert-leonvanlier
Collaborator
Collaborator

I am trying to figure out how to execute a method in my addin when the sheet size in a drawing is changed. I tried it using the on change event, however that leads to some kind of loop (stack overflow) that I kind get out of.

Currently using Inventor 2019. Addin in C#.

This is the code that is stuck in a loop:

private void OnChange(_Document DocumentObject, EventTimingEnum BeforeOrAfter, CommandTypesEnum ReasonsForChange, NameValueMap Context, out HandlingCodeEnum HandlingCode)
{
	UserInterfaceManager userInterfaceManager = Globals.invApp.UserInterfaceManager;
	Document document = Globals.invApp.ActiveDocument;
	DocumentTypeEnum documentType = document.DocumentType;
	DrawingDocument drawingDocument;
	Sheet sheet;
	DrawingSheetSizeEnum sheetSizeEnumOld = DrawingSheetSizeEnum.kADrawingSheetSize;

	if (BeforeOrAfter == EventTimingEnum.kBefore)
	{
		switch (documentType)
		{
			case DocumentTypeEnum.kDrawingDocumentObject:
				drawingDocument = (DrawingDocument)document;
				sheet = drawingDocument.ActiveSheet;
				sheetSizeEnumOld = sheet.Size;
				break;
		}
	}

	if (BeforeOrAfter == EventTimingEnum.kAfter)
	{
		if (Properties.Settings.Default.ShowFullyConstraints == true)
		{
			showFullyConstraint.startMarking(DocumentObject, isSaving);
		}

		switch (documentType)
		{
			case DocumentTypeEnum.kDrawingDocumentObject:
				Ribbon drawingRibbon = userInterfaceManager.Ribbons["Drawing"];
				RibbonTab ribbonTab = drawingRibbon.RibbonTabs["tab_dwg"];
				RibbonPanel ribbonPanel = ribbonTab.RibbonPanels["Drawing_panel_dwg"];
				drawingDocument = (DrawingDocument)document;
				sheet = drawingDocument.ActiveSheet;
				DrawingSheetSizeEnum sheetSizeEnum = sheet.Size;

				//This should later be placed in a method
				if (sheetSizeEnumOld != sheetSizeEnum)
				{
					RevisionTable revisionTable = null;

					foreach (Sheet sheetA in drawingDocument.Sheets)
					{
						try { revisionTable = sheetA.RevisionTables[1]; }
						catch { }

						if (revisionTable != null)
						{
							Point2d revPt;
							//Point2d borderPt;
							TransientGeometry transientGeometry;

							//borderPt = sheet.Border.RangeBox.MaxPoint;
							transientGeometry = Globals.invApp.TransientGeometry;
							revPt = transientGeometry.CreatePoint2d(sheetA.Border.RangeBox.MaxPoint.X - 1.085, 1 + (revisionTable.RangeBox.MaxPoint.Y - revisionTable.RangeBox.MinPoint.Y));
							revisionTable.Position = revPt;
						}
						//}
					}
				break;
		}

	}
	HandlingCode = HandlingCodeEnum.kEventNotHandled;
}

 

What am I doing wrong here? How can I check if the sheet size has changed so I can execute some code?

0 Likes
483 Views
4 Replies
Replies (4)
Message 2 of 5

Michael.Navara
Advisor
Advisor

Use this event is not recommended. But if you have no another choice, you need to be careful in handling of this event.

If you create any document modification, the event is fired again and again. Up to StackOverflowException.

 

In this case i necessary to check if you are currently handling this event. This asample is not complete, but demonstrates how to achieve this.

private void OnChange (...){
   if(currentlyHandling) return;
   currentlyHandling = true;

   //DO SOMETHING

   currentlyHandling = false;
}

 

 

Message 3 of 5

gert-leonvanlier
Collaborator
Collaborator

I tried adding the HandlingCode, but I get two errors on the if statement:

  • Error CS0269 Use of unassigned out parameter
  • Error CS0177 The out parameter 'HandlingCode' must be assigned to before control leaves the current method
private void OnChange(_Document DocumentObject, EventTimingEnum BeforeOrAfter, CommandTypesEnum ReasonsForChange, NameValueMap Context, out HandlingCodeEnum HandlingCode)
{
	if (HandlingCode == HandlingCodeEnum.kEventHandled) { return; } //Error happens on this line
	HandlingCode = HandlingCodeEnum.kEventNotHandled;
	
	//DO SOMETHING
	
	HandlingCode = HandlingCodeEnum.kEventNotHandled;
}

 

 Settings EventNotHandled before the if statement is not a solution I guess. In that case the if statement will always fail.

0 Likes
Message 4 of 5

gert-leonvanlier
Collaborator
Collaborator

After some trying I found a (workable) solution:

I check which change is being made using NameValueMap Context. In this case it is "ChangeSheetProperties".

private void OnChange(_Document DocumentObject, EventTimingEnum BeforeOrAfter, CommandTypesEnum ReasonsForChange, NameValueMap Context, out HandlingCodeEnum HandlingCode)
{
	UserInterfaceManager userInterfaceManager = Globals.invApp.UserInterfaceManager;
	Document document = Globals.invApp.ActiveDocument;
	DocumentTypeEnum documentType = document.DocumentType;
	DrawingDocument drawingDocument;
	Sheet sheet;

	if (BeforeOrAfter == EventTimingEnum.kBefore)
	{
		if (documentType == DocumentTypeEnum.kDrawingDocumentObject && Context.Item["InternalName"].ToString() == "ChangeSheetProperties")
		{
			drawingDocument = (DrawingDocument)document;
			sheet = drawingDocument.ActiveSheet;
			sheetSizeEnumOld = sheet.Size;
		}
	}

	if (BeforeOrAfter == EventTimingEnum.kAfter)
	{
		switch (documentType)
		{
			case DocumentTypeEnum.kDrawingDocumentObject:
				DrawingSheetSizeEnum sheetSizeEnum = sheet.Size;

				if (sheetSizeEnumOld != sheetSizeEnum && Context.Item["InternalName"].ToString() == "ChangeSheetProperties")
				{
					//DO SOMETHING
				}
				break;
		}

	}
	HandlingCode = HandlingCodeEnum.kEventNotHandled;
}

 

sheetSizeOld is declared at the constructor (it must have a value, cannot be null):

private DrawingSheetSizeEnum sheetSizeEnumOld = DrawingSheetSizeEnum.kADrawingSheetSize;

 

This solution will fire the OnDocumentChange twice: first the sheet sizes are different and my code is executed. That triggers again the OnDocumentChange event, but this time the sheet sizes are the same, so nothing further happens.

 

Still, I am interested how this could work using the HandlingCode. If anyone could me give an example (somewhere) on how to use this, would be great.

0 Likes
Message 5 of 5

Michael.Navara
Advisor
Advisor

The argument HandlingCode is defined as out. Set its value is required. If you don't want to cancel this change, set its value to kEventNotHandled.
For more information see the API specification of OnChange event

https://help.autodesk.com/view/INVNTOR/2020/ENU/?guid=GUID-45C2F0C0-6B5D-413A-9359-9DFD86E3A764

 

0 Likes