*cough* it's quite messy code at the moment, but as I say, it works when manually run so not sure why it doesn't when autorun. The function of the script is to delete certain fetaures and rebuild them through extrusions and combines each time it updates with new parameters. When autorun the code runs fully and gets to the end of the main sub completing the transaction and crashes out.
Sub Main extrude_and_mill
Dim oTrans As Transaction
oTrans = ThisApplication.TransactionManager.StartTransaction( ThisApplication.ActiveDocument, "extrude all")
Dim part_doc As PartDocument
part_doc = ThisApplication.ActiveDocument
delete_del(part_doc)
Dim sketches As PlanarSketches = ThisApplication.ActiveEditDocument.ComponentDefinition.Sketches
Dim n As Double = 0
For Each oSketch In sketches
'make visible
oSketch.Visible = True
'MsgBox(oSketch.Visible())
'make invisible if not extruding
If col < 2 Then
If oSketch.Name = "virt_bot"
oSketch.Visible = False
Else
oSketch.Visible = True
End If
End If
'extrude if still turned on
If oSketch.Name = "base" Then
'do nothing
Else
If oSketch.Visible() = True Then
If n = 0 Then
'MsgBox("newsolid")
extrude(oSketch, part_doc, True, 1.2, True)
rename_solids("tool")
n = n + 1
Else
'MsgBox("joining")
extrude(oSketch, part_doc, True, 1.2, True)
rename_solids("tool")
End If
End If
End If
Next
lastSketch = sketches("base")
extrude(lastSketch, part_doc, True, 1.8 + off/10, False)
rename_solids("base")
combine_features()
oTrans.End
'iLogicVb.UpdateWhenDone = True
InventorVb.DocumentUpdate()
End Sub
Sub delete_del(part_doc)
Dim comp_def As ComponentDefinition
comp_def = part_doc.ComponentDefinition
For Each feat In comp_def.Features
If feat.Name.contains("del")
feat.delete(True, True, True)
End If
Next
End Sub
Sub combine_features()
Dim bodies As SurfaceBodies = ThisApplication.ActiveEditDocument.ComponentDefinition.SurfaceBodies
Dim toolbodies As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection()
Dim basebody As SurfaceBody
For Each body In bodies
If body.Name.contains("tool")
'MsgBox("defined tool")
toolbodies.add(body)
Else If body.Name.contains("base")
'MsgBox("defined base")
basebody = body
End If
Next
'CombineFeatures.Add( BaseBody As SurfaceBody, ToolBodies As ObjectCollection, Operation As PartFeatureOperationEnum, [KeepToolBodies] As Boolean ) As CombineFeature
Dim CombineFeatures As CombineFeatures = ThisApplication.ActiveEditDocument.ComponentDefinition.Features.CombineFeatures
'MsgBox(CombineFeatures.count())
Dim booloperation As PartFeatureOperationEnum
booloperation = kCutOperation
Dim mill As CombineFeature
mill = CombineFeatures.Add(basebody, toolbodies, booloperation)
mill.Name = "MILL_del"
End Sub
Sub rename_solids(toolorbody)
Dim bodies As SurfaceBodies = ThisApplication.ActiveEditDocument.ComponentDefinition.SurfaceBodies
For Each body In bodies
If body.Name.contains("tool") Or body.Name.contains("base")
'do nothing
Else
body.Name = body.Name & "-" & toolorbody
End If
Next
End Sub
Sub extrude(oSketch, oPartDoc, newSolid, dist, positive)
' Create a profile that can be used for extrusion
'Dim oProfile As Profile
oProfile = oSketch.Profiles.AddForSolid
Dim n As Integer = 0
'MsgBox("profiles " & oSketch.Profiles.count())
For Each oProfile In oSketch.Profiles
n = n + 1
Dim oCompdef As ComponentDefinition
oCompdef = oPartDoc.ComponentDefinition
'Determine the extrusion direction from the radiobuttons
Dim extrusiondirection As DirectionEnum
If positive = True Then
extrusiondirection = kPositiveExtentDirection
Else
extrusiondirection = kNegativeExtentDirection
End If
'Determine the boolean operation from the radiobuttons
Dim booloperation As PartFeatureOperationEnum
'Read the extrusion height from the text box and convert to appropriate units
Dim extrusionheight As Double
extrusionheight = dist
' Create an extrusion without draft angle and based on an extrusion distance
Dim oExtrude1 As ExtrudeFeature
If newSolid = True Then
booloperation = kNewBodyOperation
Else
'oExtrude1 = oCompdef.Features.ExtrudeFeatures.AddByDistanceExtent( oProfile, extrusionheight, extrusiondirection, kJoinOperation)
booloperation = kJoinOperation
End If
oExtrude1 = oCompdef.Features.ExtrudeFeatures.AddByDistanceExtent( oProfile, extrusionheight, extrusiondirection, booloperation)
oExtrude1.Name = "FEATURES_del_" & oSketch.Name & n
Next
End Sub