- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am writing some iLogic to open up parts in an assembly and replace the old part number with the new one. The part number is a sketch that contains a text box, and then is extrude cut into the part, which is in the folded model of the part. The following code runs, but it just cant find the sketch.
Sub Main() Dim oDoc As AssemblyDocument oDoc = ThisDoc.Document Dim newPartNumber As String newPartNumber = InputBox("Enter the new part number:") Dim isSketchFound As Boolean isSketchFound = False MsgBox("Searching for sketch 'Part_Number' in all parts and sub-assemblies...") For Each oOccurrence In oDoc.ComponentDefinition.Occurrences isSketchFound = isSketchFound Or SearchForSketch(oOccurrence, newPartNumber) Next If isSketchFound Then MsgBox("Sketch 'Part_Number' was found and updated with new part number.") Else MsgBox("Sketch 'Part_Number' was not found in any parts or sub-assemblies.") End If MsgBox("Search finished.") End Sub Private Function SearchForSketch(ByVal oCompDef As Object, ByVal newPartNumber As String) As Boolean If TypeOf oCompDef Is PartComponentDefinition Then For Each oExtrudeFeature In oCompDef.Features.ExtrudeFeatures If oExtrudeFeature.Profile.Name = "Part_Number" Then For Each oTextBox In oExtrudeFeature.Profile.Sketch.TextBoxes oTextBox.Text = newPartNumber Return True Next End If Next ElseIf TypeOf oCompDef Is AssemblyComponentDefinition Then For Each oSubOccurrence In oCompDef.Occurrences If SearchForSketch(oSubOccurrence.Definition, newPartNumber) Then Return True End If Next End If Return False End Function
This is the code i am using. I know it is possible as an earlier version worked, but only twice out of abut 20 times i ran it. below is the code that worked very temporarily.
Private Sub Main()
Dim oDoc As AssemblyDocument
oDoc = ThisDoc.Document
For Each oOccurrence In oDoc.ComponentDefinition.Occurrences
SearchForSketch(oOccurrence)
Next
End Sub
Private Sub SearchForSketch(ByVal oCompDef As Object)
If TypeOf oCompDef Is PartComponentDefinition Then
For Each oSketch In oCompDef.Sketches
If oSketch.Name = "Part_Number" Then
For Each oTextBox In oSketch.TextBoxes
oTextBox.Text = "New Part Number"
Next
End If
Next
ElseIf TypeOf oCompDef Is AssemblyComponentDefinition Then
For Each oSubOccurrence In oCompDef.Occurrences
SearchForSketch(oSubOccurrence.Definition)
Next
End If
End Sub
Solved! Go to Solution.