Create Flat Pattern On Save

Create Flat Pattern On Save

SometimesInventorMakesMeAngry
Advocate Advocate
559 Views
3 Replies
Message 1 of 4

Create Flat Pattern On Save

SometimesInventorMakesMeAngry
Advocate
Advocate

So I made an add-in that creates a flat pattern when a part is saved (if it needs one). It works as expected when a single part is open. However, it does not work well when an assembly with sheet metal part children is saved. When the code is executed, Inventor opens one of the children, unfolds it, and exits flat pattern, which is great. However, I get exceptions for each of the remaining children.

public void CreateFlatPattern()
{
    if (CompDefinition.SurfaceBodies.Count != 1) return;
    //Above handles multi-body or no body
    //Sheet metal part can't be unfolded if multi-body
    if (!SheetDefinition.HasFlatPattern)
    {
        try
        {
            SheetDefinition.Unfold();
            SheetDefinition.FlatPattern.ExitEdit();
        }
        catch
        { throw new System.Exception("Unable to unfold model"); }
    }
}

 Does anyone understand this behavior?

0 Likes
560 Views
3 Replies
Replies (3)
Message 2 of 4

SometimesInventorMakesMeAngry
Advocate
Advocate

I found this. So I added some lines to the method to open the part before attempting to unfold as follows:

 

 

public void CreateFlatPattern()
{
	if (CompDefinition.SurfaceBodies.Count != 1) return;
	//Above handles multi-body or no body
	//Sheet metal part can't be unfolded if multi-body
	if (!SheetDefinition.HasFlatPattern)
	{
		Application inventor = App.GetApplication();
		bool docWasOpen = 
			inventor.ActiveDocument == 
			(_Document)this.DocumentObject;
		if (!docWasOpen)
		{
			inventor.Documents.Open(
				this.DocumentObject.FullFileName, OpenVisible: true);
		}

		try
		{
			SheetDefinition.Unfold();
			SheetDefinition.FlatPattern.ExitEdit();
		}
		catch
		{ throw new System.Exception("Unable to unfold model"); }

		if(!docWasOpen)this.DocumentObject.Close();
	}
}

 

 

But I get this warning for each part:

SometimesInventorMakesMeAngry_0-1637763455701.png

If I click "Yes", I get the following errors:

SometimesInventorMakesMeAngry_1-1637763499932.png

SometimesInventorMakesMeAngry_3-1637763848452.png

I assume this is related to some infinite loop situation because my code executes on save. If I click "No", it works fine with no errors, but that means I have to remember to click the correct button each time and I have to actually click something every time a flat pattern is created. I'd rather have no user interaction. Does anyone have any suggestion on how to fix this?

 

0 Likes
Message 3 of 4

maybe a bit counter-intuitive but at the end of the rule, you could save the document in silent mode. That way you can suppress any dialogues while saving. 

ThisApplication.SilentOperation = True
doc.Save()
ThisApplication.SilentOperation = False

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 4 of 4

SometimesInventorMakesMeAngry
Advocate
Advocate
I will give that a shot after the holidays. I'm hoping that by saving
inside the method, I don't run into another infinite loop situation because
this method executes on save.
0 Likes