Hi metamere,
That's not the way that I'd typically do it, but I did just take minute to set this up as you describe.
I added a Save and Close line to the "sheet metal scrap calculator" rule as such:
'.....rest of the rule above
oDoc.Save
oDoc.Close
End Sub
And I added another component to the assembly, and then updated Rule0 in the assembly to read:
iLogicVb.RunRule("sheet metal exp:1", "sheet metal scrap calculator")
iLogicVb.RunRule("sheet metal exp2:1", "sheet metal scrap calculator")
Now when running the rule, I can replicate the original issue of the Flat Pattern tab being active in the assembly!
But only when one or both of the files has an existing Flat Pattern ( I think, it might be only when both have an existing flat pattern).
In any case, I think is this is an issue with mixing the ilogic methods of calling the external rules with the API code, in a way that does not set the par files active first before running the lines to edit the flat pattern.
Below is the update to the rule that I used to fix this issue.
But maybe we can get a few moments of @MjDeck's time to look at this and see if there is another solution besides opening the sheet metal parts visibly.
Also attached are my 2017 test files in case it helps for testing. MjDeck, you just need to use the rule below with the red line commented out, as the external rule with the attached files to see the issue.
I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com
I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com
'calculates flat pattern cut area and scrap percent and saves to custom iProperties
'version 0.10
'BTS
'9-28-16
Sub Main
Dim oDoc As PartDocument = ThisDoc.Document 'ThisApplication.ActiveDocument
If oDoc.SubType <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then 'check to see if the document subtype = sheet metal
MessageBox.Show("This calculator only works on sheet metal part documents.", "Wrong document type")
Exit Sub
End If
ThisApplication.Documents.Open(oDoc.FullFilename,True)
try_count = 0
REDO:
'MessageBox.Show(oDoc.FullFilename)
'---------------------------------------------------------------------
'unfold to flat pattern ----------------------------------------------
Try
Dim oCompDef As SheetMetalComponentDefinition = oDoc.ComponentDefinition
If oCompDef.HasFlatPattern = False Then
oCompDef.Unfold
Else
oCompDef.FlatPattern.Edit
End If
'---------------------------------------------------------------------
'calculate flat pattern cut area and scrap percent
L = SheetMetal.FlatExtentsLength 'Gets the width (X dimension) of the flat pattern extents
W = SheetMetal.FlatExtentsWidth 'Gets the width (Y dimension) Of the flat pattern extents
'note that it will automatically find the minimum bounding box.
margin = .5 '.5" margin on each side as specified by the work instructions
A_cut = (L + 2*margin)*(W + 2*margin)
'get flat pattern
Dim oFlatPattern As FlatPattern = oCompDef.FlatPattern
'get top face
Dim oFace As Face = oFlatPattern.TopFace
'get face area
Dim A_flat As Double = oFace.Evaluator.Area 'returns area in units of cm^2
A_flat = A_flat/(2.54^2) 'convert to units of in^2
excess = A_cut - A_flat
scrap_percent = excess/A_flat*100
'---------------
'FOR DEBUG:
'MessageBox.Show("L = " & L & vbLf & "W = " & W & vbLf & "A_cut = " & A_cut & vbLf & "A_flat = " & A_flat & vbLf & "excess = " & excess & vbLf & "scrap_percent = " & scrap_percent, oDoc.FullFilename) 'to verify calculations
'--------------
iProperties.Value("Custom", "MATL_CONSUMPTION") = Microsoft.VisualBasic.Strings.Format(A_flat,"00.00")
iProperties.Value("Custom", "MATL_SCRAP") = Microsoft.VisualBasic.Strings.Format(scrap_percent,"00.00")
oCompDef.FlatPattern.ExitEdit
Catch
Try
ThisApplication.Documents.Open(oDoc.FullFilename,True)
oDoc.Close
try_count = try_count + 1
If try_count < 2 Then Goto REDO
Catch
MessageBox.Show("sheet metal scrap calculator failed for " & ThisDoc.FileName(False), "ERROR")
iProperties.Value("Custom", "MATL_CONSUMPTION") = ""
iProperties.Value("Custom", "MATL_SCRAP") = ""
End Try
End Try
oDoc.Save
oDoc.Close
End Sub