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: 

Laser Cut Length

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
alex_kalin
368 Views, 3 Replies

Laser Cut Length

Hi,

 

l have found this iLogic code to calculate the Laser cut length for our sheetmetal parts. The code works perfectly at part level, l have the event trigger set to "before save document". But when l have the part in an assembly and click save for an updated part, it doesnt update saying the part must be 'active' for this rule to work.

Is there any line of code that l can add to be able to run this rule when l click "save" in the assembly.

 

If Not TypeOf ThisApplication.ActiveDocument Is PartDocument Then

        MsgBox("A Part document must be 'active' for this rule to work.  Exiting rule.",,"")

        Exit Sub

End If

Dim oPDoc As PartDocument = ThisApplication.ActiveDocument

If Not TypeOf oPDoc.ComponentDefinition Is SheetMetalComponentDefinition Then

        MsgBox("This Part is not a Sheet Metal Part.  Exiting rule.",,"")

        Exit Sub

End If

Dim oSMDef As SheetMetalComponentDefinition = oPDoc.ComponentDefinition

If Not oSMDef.HasFlatPattern Then

        oAns = MsgBox("This Sheet Metal Part does not have a Flat Pattern." & vbCrLf & _

        "This is needed to calculate 'LaserCutLength'." & vbCrLf & _

        "Do you want this rule to automatically create the Flat Pattern, then continue?", vbYesNo + vbQuestion, "")

        If oAns = vbNo Then Exit Sub

               Try

                       oSMDef.Unfold

               Catch oEx As Exception

                       MsgBox("Failed to 'Unfold' this Sheet Metal Part, to create a Flat Pattern." & vbCrLf & _

                       oEx.Message & vbCrLf & oEx.StackTrace, , "")

                       Exit Sub

               End Try

End If

Dim oFP As FlatPattern = oSMDef.FlatPattern

Dim oLengths As New List(Of Double)

For Each oEL As EdgeLoop In oFP.BottomFace.EdgeLoops

    oLengths.Add(ThisApplication.MeasureTools.GetLoopLength(oEL))

Next

'a = InputListBox("", oLengths, 0.0, "Loop Lengths", "Individual Loop Lengths")

Dim oParam As UserParameter

Try

        oParam = oSMDef.Parameters.UserParameters.Item("Cutting_Distance")

        oParam.Value = oLengths.Sum

Catch

        oParam = oSMDef.Parameters.UserParameters.AddByValue("Cutting_Distance", oLengths.Sum, "mm")

End Try

iLogicVb.UpdateWhenDone = True

 

Thanks,

Alex

 

3 REPLIES 3
Message 2 of 4
matt_jlt
in reply to: alex_kalin

If you are using iLogic, this should fix your issue. You need to change it from ThisApplication.ActiveDocument to ThisDoc.Document.

 

The reasoning is that when you use ThisApplication.ActiveDocument. It is getting the current document that inventor has open / activated which is your assembly document. If you want to run it  on the document that the rule is stored in, you need to use ThisDoc.Document. As it doesnt matter what is calling up the rule, it will always get the document that is containing the rule.

 

Example

 

 

If Not TypeOf ThisDoc.Document Is PartDocument Then
        MsgBox("A Part document must be 'active' for this rule to work.  Exiting rule.",,"")
        Exit Sub
End If
Dim oPDoc As PartDocument = ThisDoc.Document
If Not TypeOf oPDoc.ComponentDefinition Is SheetMetalComponentDefinition Then
        MsgBox("This Part is not a Sheet Metal Part.  Exiting rule.",,"")
        Exit Sub

End If

 

 

Message 3 of 4
alex_kalin
in reply to: matt_jlt

Hi,

Thanks for your response. lm not sure if it solves my problem though, because when l run click save and run in the ilogic rule it instantly pops up with a messag "a part document must be active for this rule to work. exiting rule"

 

So its actually got worse. This is how l have updated the rule

 

If Not TypeOf ThisDoc Is PartDocument Then
	MsgBox("A Part document must be 'active' for this rule to work.  Exiting rule.",,"")
	Exit Sub
End If
Dim oPDoc As PartDocument = ThisDoc
If Not TypeOf oPDoc.ComponentDefinition Is SheetMetalComponentDefinition Then
	MsgBox("This Part is not a Sheet Metal Part.  Exiting rule.",,"")
	Exit Sub
End If
Dim oSMDef As SheetMetalComponentDefinition = oPDoc.ComponentDefinition
If Not oSMDef.HasFlatPattern Then
	oAns = MsgBox("This Sheet Metal Part does not have a Flat Pattern." & vbCrLf & _
	"This is needed to calculate 'LaserCutLength'." & vbCrLf & _
	"Do you want this rule to automatically create the Flat Pattern, then continue?", vbYesNo + vbQuestion, "")
	If oAns = vbNo Then Exit Sub
		Try
			oSMDef.Unfold
		Catch oEx As Exception
			MsgBox("Failed to 'Unfold' this Sheet Metal Part, to create a Flat Pattern." & vbCrLf & _
			oEx.Message & vbCrLf & oEx.StackTrace, , "")
			Exit Sub
		End Try
End If
Dim oFP As FlatPattern = oSMDef.FlatPattern
Dim oLengths As New List(Of Double)
For Each oEL As EdgeLoop In oFP.BottomFace.EdgeLoops
    oLengths.Add(ThisApplication.MeasureTools.GetLoopLength(oEL))
Next
'a = InputListBox("", oLengths, 0.0, "Loop Lengths", "Individual Loop Lengths")
Dim oParam As UserParameter
Try
	oParam = oSMDef.Parameters.UserParameters.Item("Cutting_Distance")
	oParam.Value = oLengths.Sum
Catch
	oParam = oSMDef.Parameters.UserParameters.AddByValue("Cutting_Distance", oLengths.Sum, "mm")
End Try
iLogicVb.UpdateWhenDone = True

Message 4 of 4
alex_kalin
in reply to: alex_kalin

Removed the first portion of the code and the portion "This.Doc.Document.

Seems to be working okay now

 

'If Not TypeOf ThisApplication.ActiveDocument Is PartDocument Then
	'MsgBox("A Part document must be 'active' for this rule to work.  Exiting rule.",,"")
	'Exit Sub
'End If
Dim oDoc As PartDocument = ThisDoc.Document
If Not TypeOf oDoc.ComponentDefinition Is SheetMetalComponentDefinition Then
	MsgBox("This Part is not a Sheet Metal Part.  Exiting rule.",,"")
	Exit Sub
End If
Dim oSMDef As SheetMetalComponentDefinition = oDoc.ComponentDefinition
If Not oSMDef.HasFlatPattern Then
	oAns = MsgBox("This Sheet Metal Part does not have a Flat Pattern." & vbCrLf & _
	"This is needed to calculate 'LaserCutLength'." & vbCrLf & _
	"Do you want this rule to automatically create the Flat Pattern, then continue?", vbYesNo + vbQuestion, "")
	If oAns = vbNo Then Exit Sub
		Try
			oSMDef.Unfold
		Catch oEx As Exception
			MsgBox("Failed to 'Unfold' this Sheet Metal Part, to create a Flat Pattern." & vbCrLf & _
			oEx.Message & vbCrLf & oEx.StackTrace, , "")
			Exit Sub
		End Try
End If
Dim oFP As FlatPattern = oSMDef.FlatPattern
Dim oLengths As New List(Of Double)
For Each oEL As EdgeLoop In oFP.BottomFace.EdgeLoops
    oLengths.Add(ThisApplication.MeasureTools.GetLoopLength(oEL))
Next
'a = InputListBox("", oLengths, 0.0, "Loop Lengths", "Individual Loop Lengths")
Dim oParam As UserParameter
Try
	oParam = oSMDef.Parameters.UserParameters.Item("Cutting_Distance")
	oParam.Value = oLengths.Sum
Catch
	oParam = oSMDef.Parameters.UserParameters.AddByValue("Cutting_Distance", oLengths.Sum, "mm")
End Try
iLogicVb.UpdateWhenDone = True

 

 

 

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

Post to forums  

Technology Administrators


Autodesk Design & Make Report