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:
When i press OK i get the following:
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
Solved! Go to Solution.
Solved by dalton98. Go to Solution.
This is a common issue. Use "ThisDoc.Document" instead of "ThisApplication.ActiveDocument".
Thank you for your quick answer, i am not sure how to change my code to this. Can you help me?
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.
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.
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
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
(Not an Autodesk Employee)
@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:
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
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:
Wesley Crihfield
(Not an Autodesk Employee)
Can't find what you're looking for? Ask the community or share your knowledge.