toolbar switching to sheet metal part when iLogic rule is run in assembly

metamere
Contributor
Contributor

toolbar switching to sheet metal part when iLogic rule is run in assembly

metamere
Contributor
Contributor

I have found a weird issue/bug where the main toolbar switches from the standard assembly toolbar to the sheet metal toolbar whenever I run an iLogic rule in an assembly which changes the suppression state of a feature in a subcomponent that is a sheet metal part (see attached images).  Has anyone else found this bug, or do you know of a way to avoid this?  I found that if I switch to a different window in inventor such as My Home or another assembly and then back, it will revert to the assembly toolbar, so it's not a huge issue, but it is somewhat annoying.  Thanks in advance for any help.

0 Likes
Reply
Accepted solutions (1)
2,178 Views
15 Replies
Replies (15)

CCarreiras
Mentor
Mentor

HI!

 

Not the Sheet Metal Ribbon, but the Flat Pattern Ribbon, which is odder...

Your rule it's something about create flat pattern, or flat pattern's dxf's creation?

CCarreiras

EESignature

0 Likes

mcgyvr
Consultant
Consultant

post your ilogic code so we don't have to guess..

 

 



-------------------------------------------------------------------------------------------
Inventor 2023 - Dell Precision 5570

Did you find this reply helpful ? If so please use the Accept Solution button below.
Maybe buy me a beer through Venmo @mcgyvr1269
0 Likes

metamere
Contributor
Contributor

After working to isolate things as much as possible, I found that the root of the problem came from the sheet metal part having an event trigger on geometry changes to run an external rule which calculates an estimate of the area of sheet metal material required by finding the extents of the flat pattern.  I tested, and whether that external rule is called from the event trigger or from a rule in the part that is triggered by a rule in the assembly, it will still cause the toolbar issue.  I tried calling that rule from a new part and assembly, and it had the same issue.  I guess because it is going into the flat pattern for the part, it thinks it should display that toolbar.  I attached the code so you can test to see if you get the same results if you like.

0 Likes

Curtis_Waguespack
Consultant
Consultant

Hi metamere,

 

I didn't run the rule or test, but just a quick look at the code makes me think that your TRY/CATCH statement is hitting an error before it can get to this line:

oCompDef.FlatPattern.ExitEdit

 so that leaves the browser in some state of confusion.

 

You might try something like the version below.

 

note I didn't test this

 

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

try_count = 0

REDO:

'MessageBox.Show(oDoc.FullFilename)
'---------------------------------------------------------------------
'unfold to flat pattern ----------------------------------------------
Dim oCompDef As SheetMetalComponentDefinition = oDoc.ComponentDefinition

Try
	
	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")
		
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

	oCompDef.FlatPattern.ExitEdit

End Sub

 

0 Likes

metamere
Contributor
Contributor

Curtis,

Thanks for looking into this.  It turns out that the exitedit command was running as it should in my code, so that's not the cause.  But that got me wondering if I had the syntax right on it, so I tried commenting out that line of code.  It still ran just fine, and it no longer caused the toolbar glitch.  I still don't know why that line of code would cause that issue, and now I need to figure out how to exit the flat pattern editing mode in a different way.  Any thoughts on that?

 

Thanks,

Ben

0 Likes

Curtis_Waguespack
Consultant
Consultant

Hi @metamere

 I'm all but sure that I've seen this issue in the past, but I don't recall the specifics. I'll try to look at this again tomorrow but in the meantime you can search for the 'exit edit' line on the customization forum and you might find some examples that could shed light on this.

 

Inventor Customization forum too:
http://forums.autodesk.com/t5/Autodesk-Inventor-Customization/bd-p/120

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

0 Likes

metamere
Contributor
Contributor

Curtis,

I searched through the customization forum and didn't find any hints, although it will be a useful resource.  I did manage to come up with a workaround that should suffice.  I can automate the switching to another document and back to refresh the toolbar, by just opening one of the subcomponents and then closing it at the end of the rule in the assembly that updates all of the subcomponents.  It doesn't address the root issue, but I've given up on doing that with a lot of things in Inventor, so it's business as usual.

 

Thanks,

Ben

0 Likes

Curtis_Waguespack
Consultant
Consultant

Hi @metamere,

 

In thinking about this some more, I think another thing to look into would be is how the rule is defining oDoc.

 

If you use:

 

               Dim oDoc As PartDocument = ThisDoc.Document

 

or 

 

               Dim oDoc As PartDocument = ThisApplication.ActiveDocument

 

 

you might see issues when running the rule from an assembly. 

 

But if you use this:

 

               Dim oDoc As PartDocument = ThisApplication.ActiveEditDocument

 

then a rule that cycles through components from an assembly, will look at the correct file. I still think the Try/Catch is hiding an error in your rule, but it likely has to do with the ilogic rule trying to do some sheet metal tasks to the assembly document, etc.

 

Again, I know I've seen this exact issue when working with a sheet metal rule in the past, but I just don't recall specifically what the solution was.

 

Just in case it helps, here is a quick example rule that  can be run from an assembly file, to cycle through all of the sheet metal files and flatten them. 

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Dim oDoc As Document = ThisApplication.ActiveDocument

For Each oSubDoc In oDoc.AllReferencedDocuments	
	'only run for sheet metal
	If oSubDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then  
	
		'open file
		invDoc = ThisApplication.Documents.Open(oSubDoc.FullFileName, True) 

		Dim oCompDef As SheetMetalComponentDefinition = invDoc.ComponentDefinition
		
		If oCompDef.HasFlatPattern = False Then
			oCompDef.Unfold
		Else
			oCompDef.FlatPattern.Edit
		End If
	
		oCompDef.FlatPattern.ExitEdit	
		invDoc.Close

	End If

Next

 

0 Likes

metamere
Contributor
Contributor

Curtis,

Thanks again for the further guidance.  I tested using the ActiveEditDocument method, and it appears that it does just the opposite of what you described.  When that is used in a subcomponent's rule, it points to the parent assembly document.  I tested it by looking at ActiveEditDocument.FullFilename.  ThisDoc.Document points to the document that triggered the rule.  

 

I also tried cycling through all of the component occurrences like in the rule you included, and that did not work either.  I was able to make it work most of the way, but for some reason, the SheetMetal functions such as SheetMetal.FlatExtentsLength do not work properly, and return a value of zero, so I can't make the rule do what I intended.  Presumably, it is looking at the assembly document for those values in this case.  It appears that I'm bumping up against the unstated limitations of iLogic yet again.  So close, yet so far.  I guess my workaround will have to suffice.

 

Regards,

Ben

0 Likes

Curtis_Waguespack
Consultant
Consultant

@metamere wrote:

...It appears that I'm bumping up against the unstated limitations of iLogic yet again. 


Hi metamere,

 

In reviewing this topic I don't see where you have posted the rule that triggers the sheet metal rule? So we don't yet have a complete picture of the issue.

 

Since you report that ActiveEditDocument gives you the opposite result of what I suggested it should, it stands to reason that you're calling components and the rules to be run on those components, differently than I would expect. 

 

I don't think you're seeing limitations of ilogic here, as I have rules and combinations of rules that do as much as you're doing here and more. It's more likely you just have a fundamental "zig" in the routine, where you need a "zag".

 

If you can post a more complete picture of all the rules involved, I think that might shed some light on this.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

0 Likes

metamere
Contributor
Contributor

Curtis,

I am pretty sure I am calling things in the standard way.  For the test, I created a sheet metal part with a flange and a flat pattern, and called it "sheet metal exp.ipt".  I then created a rule in it called "sheet metal scrap calculator", which contains the line: 

SyntaxEditor Code Snippet

iLogicVb.RunExternalRule("scrap test")

Then I placed that part into the assembly "sheet metal exp.iam", and created a rule in the assembly, Rule0:

SyntaxEditor Code Snippet

iLogicVb.RunRule("sheet metal exp:1", "sheet metal scrap calculator")

The external rule is where I have been testing the various changes to the original rule I posted.  In my original assembly, I have many sheet metal parts, and because the sheet metal functions only work properly when called from the parts themselves, I found it to be more efficient to have the code in a single external rule so it is easier to edit.  If there is a better way to do things like this, please let me know.

 

Regards,

Ben

Curtis_Waguespack
Consultant
Consultant

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

metamere
Contributor
Contributor

Curtis,

Thanks for testing and confirming, and for calling for assistance from one of the developers.  I guess it bears mentioning that there is another issue there which I worked around earlier.  That's what the error catching and part opening operation is for.  Basically, when you first open the assembly and try to do the sheet metal operations on the subcomponent via the top level rule calling a subcomponent rule calling an external rule, it will throw an error and fail.  The only way to get it to work is to open the part before performing those operations.  It only needs to be opened once, and then you can run the rule as much as you want.  Does this seem like the way it should be, or could it be possible there is something I could do differently there to not require it to be opened initially (and at the end too)?  It seems like there are certain things that only work if you "jump start" things like that.  I have another rule where I am suppressing and unsuppressing weld beads in a subcomponent weldment and it fails unless you first go and manually manipulate one of the weld features at the beginning of the session.  It seems like there must be some other method for pointing to things that I am missing, but maybe it just doesn't exist.

 

Regards,

Ben

0 Likes

MjDeck
Autodesk
Autodesk
Accepted solution

Hi Curtis and Ben,

 Sorry I took so long to reply. I think the attached version of the external rule solves a few problems in a streamlined way. It avoids explicitly opening the part documents. They're already open as components of the assembly, so it just creates new views of them. Also, I don't think you have to put the flat pattern into edit mode in order to get the calculations to work. You only have to exit from flat pattern mode if you create it. And you can exit immediately after creating it.
 Here's the code as described. (Also see the attached full rule.)

	If oCompDef.HasFlatPattern = False Then
		Dim openedView As Inventor.View = Nothing
		If oDoc IsNot ThisApplication.ActiveDocument Then
			openedView = oDoc.Views.Add()
			oDoc.Activate()
		End If
		oCompDef.Unfold
		oCompDef.FlatPattern.ExitEdit
		If openedView IsNot Nothing Then
			openedView.Close()
		End If 
	End If

 


Mike Deck
Software Developer
Autodesk, Inc.

metamere
Contributor
Contributor

Mike,

Thank you so much for your help on this.  You have given me the missing piece of the puzzle on how to make all of my rules work consistently regardless of how they are called.  Thank you for taking the time to edit the full code so that I would be sure to understand how this way of doing things should work.  I have been able to integrate that technique into other rules where I was having issues with things not working as I had expected, and it seems to have sorted things out nicely. 

 

Thanks again,

Ben

0 Likes