Message 1 of 8
Rule crashes when run twice - shrinkwrap
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello everyone,
I'm making a rule that creates a shrinkwrap of a part in an assembly, save that shrinkwrap part and then place it in the original assembly. My rule works perfectly fine when I run it the first time, however, it always crashes when I run it a second time. Then if I run it a third time it works fine, but the fourth time it crashes again and so on. The problem seems to be in the line that makes the .saveas command.
Any ideas of what is happening and how to fix it?
Here is my code:
Sub Main()
Dim oAssy As AssemblyDocument = ThisApplication.ActiveDocument
Dim oAssyDef As AssemblyComponentDefinition = oAssy.ComponentDefinition
'Select the occurrences
Dim oOcc1 As ComponentOccurrence = ThisApplication.CommandManager.Pick(kAssemblyLeafOccurrenceFilter,"Select the first occurrence")
Dim oOcc2 As ComponentOccurrence = ThisApplication.CommandManager.Pick(kAssemblyLeafOccurrenceFilter,"Select the second occurrence")
Dim oOcc1Def As PartComponentDefinition = oOcc1.Definition
Dim oSWPath As String
oSWPath = SWrap(oAssy, oAssyDef, oOcc2, oSWPath)
Call AddToAssy(oAssyDef, oSWPath)
End Sub
Function SWrap(oAssy As AssemblyDocument, oAssyDef As AssemblyComponentDefinition, oOcc2 As ComponentOccurrence, oSWPath As String)
Dim oSWDoc As PartDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject)
Dim oSWD As ShrinkwrapDefinition = oSWDoc.ComponentDefinition.ReferenceComponents.ShrinkwrapComponents.CreateDefinition(oAssy.FullDocumentName)
Dim oExclude As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
Dim oOcc As ComponentOccurrence
'Add only the second occurrence
For Each oOcc In oAssyDef.Occurrences
If Not oOcc Is oOcc2 Then
Call oExclude.Add(oOcc)
End If
Next oOcc
oSWD.AdditionalExcludedOccurrences = oExclude
oSWD.RemoveHolesStyle = ShrinkwrapRemoveStyleEnum.kShrinkwrapRemoveAll
oSWD.BreakLink = True
Dim oSWComp As ShrinkwrapComponent
oSWComp = oSWDoc.ComponentDefinition.ReferenceComponents.ShrinkwrapComponents.Add(oSWD)
oSWPath = ThisDoc.PathAndFileName(True) & "_SW.ipt"
'//// Delete previous file if it already exists ////
If System.IO.File.Exists(oSWPath) = True Then
System.IO.File.Delete(oSWPath)
End If
oSWDoc.SaveAs(oSWPath, False) '/ THE CODE CRASHES EXACTLY AT THIS LINE EVERY SECOND TIME I RUN IT /
oSWDoc.Close
Return(oSWPath)
End Function
Sub AddToAssy(oAssyDef As AssemblyComponentDefinition, oSWPath As String)
Dim oMat As Matrix = ThisApplication.TransientGeometry.CreateMatrix()
oMat.SetTranslation(ThisApplication.TransientGeometry.CreateVector(0, 0, 0))
Dim oNewOcc As ComponentOccurrence = oAssyDef.Occurrences.Add(oSWPath, oMat)
End Sub