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: 

Ilogic flat pattern issues

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
koenroovers
382 Views, 8 Replies

Ilogic flat pattern issues

Hello Everyone,

 

I have been in the process of implementing several Ilogic codes to make the workflow in our company better. All of them are working, but one is giving me problems. When in a sheet metal, i have a trigger set on save of the document. This trigger executes my flat pattern check ilogic. If there is no flat pattern it will be created, if there is a flat pattern it will be updated. 

 

But when i'm in a assembly and i change my part inside an assembly (So double click the part, make the changes, and return to assembly) upon saving i get multiple errors. For example the following:

koenroovers_0-1660219394664.png

When i press OK i get the following:

koenroovers_1-1660219419034.png

After which it saves without a problem.

 

I am sure that my fellow engineers will find this a nuisance so I would like to resolve this issue. Is there a way to do this? If not possible is there a code that i can run when i'm inside an assembly that checks if all flat patterns are made? 


So for example i've created a weldment which consists of sheet metal parts, in the weldment i add my Ilogic to my forms. Upon clicking it the check will be done and the missing flat patterns will be created.

 

I hope somebody can help me. 

 

Below is the rule i'm using in my sheet metal parts:

Dim partDoc As PartDocument
If ThisApplication.ActiveDocument.DocumentType <> kPartDocumentObject Then
MessageBox.Show ("Please open a part document", "iLogic")
End If

'Check for flat pattern >> create one if needed
Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument
Dim oCompDef As SheetMetalComponentDefinition
oCompDef = oDoc.ComponentDefinition
If oCompDef.HasFlatPattern = False Then
oCompDef.Unfold

Else
oCompDef.FlatPattern.Edit
End If

Dim oSMDef As SheetMetalComponentDefinition
oSMDef = oDoc.ComponentDefinition
oSMDef.FlatPattern.ExitEdit

 

With kind regards,

 

Koen Roovers

8 REPLIES 8
Message 2 of 9
dalton98
in reply to: koenroovers

This is a common issue. Use "ThisDoc.Document" instead of "ThisApplication.ActiveDocument".

https://knowledge.autodesk.com/support/inventor/learn-explore/caas/simplecontent/content/thisapplica...

Message 3 of 9
koenroovers
in reply to: koenroovers

Thank you for your quick answer, i am not sure how to change my code to this. Can you help me?

Message 4 of 9
dalton98
in reply to: koenroovers

You'll just have to manually change "ThisApplication.ActiveDocument" to "ThisDoc.Document". The article explains the difference. The rule of thumb is to always use "ThisDoc.Document" in rules for iTriggers.

Message 5 of 9
koenroovers
in reply to: dalton98

Thank you! this did the trick.

Message 6 of 9
koenroovers
in reply to: koenroovers

Hello,

This Ilogic worked perfectly but now seems to start giving problems. I'm not sure what has changed.

When I edit the BOM in assembly and change properties, I get a error on line 15. 

When I edit iproperties in the BOM only the flat pattern does not have to be updated. 

 

koenroovers_0-1713162047535.png

 

Can somebody help me with this?

 

Below my Ilogic i'm using:

Dim partDoc As PartDocument
If ThisDoc.Document.DocumentType <> kPartDocumentObject Then
MessageBox.Show ("Please open a part document", "iLogic")
End If

'Check for flat pattern >> create one if needed
Dim oDoc As PartDocument
oDoc = ThisDoc.Document
Dim oCompDef As SheetMetalComponentDefinition
oCompDef = oDoc.ComponentDefinition
If oCompDef.HasFlatPattern = False Then
oCompDef.Unfold

Else
oCompDef.FlatPattern.Edit
End If

Dim oSMDef As SheetMetalComponentDefinition
oSMDef = oDoc.ComponentDefinition
oSMDef.FlatPattern.ExitEdit
Message 7 of 9
WCrihfield
in reply to: koenroovers

Hi @koenroovers.  One process may automatically cause referenced documents to get updated, while the other does not.  So, you may just need to include some code in your rule that checks if the document needs to be updated before it tries to do stuff to that document.  Below is a partial starter example, similar to the one you are showing, which includes a line of code for checking that status, and updating the document, if needed.  Not 100% sure this will fix it, but quite possible it might.

Sub Main
	Dim oPDoc As PartDocument = TryCast(ThisDoc.Document, Inventor.PartDocument)
	If oPDoc Is Nothing Then
		Logger.Debug("This rule '" & iLogicVb.RuleName & "' requires a PartDocument.  Exiting rule.")
		Return
	End If
	If oPDoc.SubType <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
		Logger.Debug("This rule '" & iLogicVb.RuleName & "' requires a Sheet Metal Part.  Exiting rule.")
		Return
	End If
	'check if part needs to be updated.  If so, update it before moving forward.
	If oPDoc.RequiresUpdate Then oPDoc.Update2(True)
	Dim oSMDef As SheetMetalComponentDefinition = oPDoc.ComponentDefinition
	'Dim oSMStyles As SheetMetalStyles = oSMDef.SheetMetalStyles
	'Dim oActiveSMStyle As SheetMetalStyle = oSMDef.ActiveSheetMetalStyle
	'Dim oSMFeats As SheetMetalFeatures = oSMDef.Features
	'Dim oSMParams As Inventor.Parameters = oSMDef.Parameters
	Dim oFP As FlatPattern = Nothing
	If oSMDef.HasFlatPattern Then
		oFP = oSMDef.FlatPattern
	Else
		Try 'unfold might fail
			oSMDef.Unfold
			oSMDef.FlatPattern.ExitEdit
			oFP = oSMDef.FlatPattern
		Catch
			Logger.Error("Error unfolding model to get FlatPattern.  Exiting routine.")
		End Try
	End If
	If oFP Is Nothing Then
		Logger.Debug("FlatPattern not obtained.  Exiting routine.")
	End If
'	Dim oFPFeats As FlatPatternFeatures = oFP.Features
'	Dim oFPParams As Inventor.Parameters = oFP.Parameters
'	Dim oFPPlanarSketches As PlanarSketches = oFP.Sketches
'	'export FlatPattern object directly to DXF
'	oFP.DataIO.WriteDataToFile(sFormat, sFileName)

End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 9
koenroovers
in reply to: WCrihfield

@WCrihfield 

I have changed the Ilogic to this, and it works but when i save in assembly mode the top toolbar changes to Flat pattern (inside assembly environment). Can you spot what's wrong with my Ilogic? See picture below:

koenroovers_0-1713261037668.png

Dim partDoc As PartDocument
If ThisDoc.Document.DocumentType <> kPartDocumentObject Then
MessageBox.Show ("Please open a part document", "iLogic")
End If

'check if part needs to be updated.  If so, update it before moving forward.
Dim oPDoc As PartDocument = TryCast(ThisDoc.Document, Inventor.PartDocument)
	If oPDoc.RequiresUpdate Then oPDoc.Update2(True)
	Dim oSMDef As SheetMetalComponentDefinition = oPDoc.ComponentDefinition
	'Dim oSMStyles As SheetMetalStyles = oSMDef.SheetMetalStyles
	'Dim oActiveSMStyle As SheetMetalStyle = oSMDef.ActiveSheetMetalStyle
	'Dim oSMFeats As SheetMetalFeatures = oSMDef.Features
	'Dim oSMParams As Inventor.Parameters = oSMDef.Parameters
	Dim oFP As FlatPattern = Nothing
	If oSMDef.HasFlatPattern Then
		oFP = oSMDef.FlatPattern
	Else
		Try 'unfold might fail
			oSMDef.Unfold
			oSMDef.FlatPattern.ExitEdit
			oFP = oSMDef.FlatPattern
		Catch
			Logger.Error("Error unfolding model to get FlatPattern.  Exiting routine.")
		End Try
	End If
	If oFP Is Nothing Then
		Logger.Debug("FlatPattern not obtained.  Exiting routine.")
	End If
	
'Check for flat pattern >> create one if needed
Dim oDoc As PartDocument
oDoc = ThisDoc.Document
Dim oCompDef As SheetMetalComponentDefinition
oCompDef = oDoc.ComponentDefinition
If oCompDef.HasFlatPattern = False Then
oCompDef.Unfold

Else
oCompDef.FlatPattern.Edit
End If

oSMDef = oDoc.ComponentDefinition
oSMDef.FlatPattern.ExitEdit

  

Message 9 of 9
WCrihfield
in reply to: koenroovers

Hi @koenroovers.  I do not understand why you are using this line of code:

oCompDef.FlatPattern.Edit

near the end.  You have this line of code to 'exit' the flat pattern edit mode just 2-3 lines after this point:

oSMDef.FlatPattern.ExitEdit

That first line I just mentioned is likely the cause of this problem.  You almost never need to use the FlatPattern.Edit mode, unless you are planning on making changes directly to the FlatPattern (not to the folded model).

 

Also, there are several other things going on here that I feel like I should mention.

First of all, you are declaring 3 different variables for a PartDocument throughout your code, and are using two of them, when there is only one document.  This is always a bad idea, and often leads to problems.  You should always just get the document you want to be working with to a variable once, then use that throughout the rest of the code.  The 'ThisDoc.Document' phrase is the one I use most often, and is also the one you are using, but you are just using it too many times in this rule.  I will explain the following line of code, so you understand how to use it.

Dim oPDoc As PartDocument = TryCast(ThisDoc.Document, Inventor.PartDocument)

That line of code starts out be 'declaring' a variable, setting its Type to PartDocument, then setting its value using the 'ThisDoc.Document' phrase.  However, it is attempting to 'Cast' the value it is obtaining from the 'ThisDoc.Document' phrase to a PartDocument Type.  So, if the value that 'ThisDoc.Document' returns is not a PartDocument (like an assembly or drawing), it will not set a value to that 'oPDoc' variable (it will be Nothing).  Then, to check this, simply check if 'oPDoc Is Nothing' in a following line of code.  If that is True, then we were not able to get a reference to a PartDocument, and should exit the rule.  That is why I had it formatted that way in the code I posted above.  When using this technique, there is no need for those first 4-5 lines of code in your last code, above that point.

 

Next, that last code you posted already included an area of code that got the FlatPattern object (ending around Line 29).  But then you included a whole extra block of code below that which completely goes through the whole process again of getting the main document, getting its ComponentDefinition, and getting the FlatPattern, unnecessarily.  It seems to me like you could get rid of all the code past Line 29.

 

However, I may have lost sight of the main purpose of this code.  It sounds like all you want this code to do is:

  • If there is no FlatPattern present in the sheet metal part, then create it
    • This code will do that.
  • If  there is already a FlatPattern present in the sheet metal part, then make sure it is updated.
    • The one line of code I suggested in my last post should take care of that need:  (where it checks if the document needs to be updated, then if it does, then update the document)
    • If that line of code is not taking care of this 'update' need, then you may need to try the Document.Rebuild2(True) method instead.  That will 'force' an update, even if it may not think it needs to be updated.
    • However, if you are editing this part from an assembly, then the whole assembly will need to be updated after this process runs on this one part, to see the results of those changes in the assembly.  So, if you are using code to process all sheet metal parts in an entire assembly, you will want to use a line of code to update the main assembly after making changes to any of the components within that assembly, to then see the results of those changes in the main assembly.
    • You should not need to enter into the 'edit mode' of the FlatPattern to accomplish this update.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

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

Post to forums  

Technology Administrators


Autodesk Design & Make Report