I want to delete the last 2 letters of many Parts in Part Number in Assembly.
And get that value attached to Stock Number
This is the code. But it errors š
Dim oPartDoc As PartDocument
Dim oPropSets As PropertySets
Dim oPartNumber As String
Dim oStockNumber As String
For Each oComp In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences
Set oPartDoc = oComp.Definition.Document
Set oPropSets = oPartDoc.PropertySets
oPartNumber = oPropSets.Item("Design Tracking Properties").Item("Part Number").Value
oStockNumber = Left(oPartNumber, Len(oPartNumber) - 2)
oPropSets.Item("Design Tracking Properties").Item("Stock Number").Value = oStockNumber
Next
Solved! Go to Solution.
Solved by Curtis_Waguespack. Go to Solution.
Solved by Curtis_Waguespack. Go to Solution.
Hi,
I tested this on my computer and it worked as intended..
I just had to remove the "set" since it is not applicable in iLogic editor.
Dim oPartDoc As PartDocument Dim oPropSets As PropertySets Dim oPartNumber As String Dim oStockNumber As String For Each oComp In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences oPartDoc = oComp.Definition.Document oPropSets = oPartDoc.PropertySets oPartNumber = oPropSets.Item("Design Tracking Properties").Item("Part Number").Value oStockNumber = Left(oPartNumber, Len(oPartNumber) -2) oPropSets.Item("Design Tracking Properties").Item("Stock Number").Value = oStockNumber Next
Maybe what causes an issue is the assembly that contains the rule contains sub assemblies, and not only parts.
Then you would need to change this line to this line, to loop through each individual parts :
For Each oComp In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences.AllLeafOccurrences
Regards,
FINET L.
If this post solved your question, please kindly mark it as "Solution"
If this post helped out in any way to solve your question, please drop a "Like"Hi @haphanthanhtam.work ,
I suspect the issue is that you have a subassembly in your assembly, and the code was trying to set it to be a part document.
Try this version.
I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com
Dim oDoc As Document Dim oPropSets As PropertySets Dim oPartNumber As String Dim oStockNumber As String For Each oComp In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences oDoc = oComp.Definition.Document oPropSets = oDoc.PropertySets oPartNumber = oPropSets.Item("Design Tracking Properties").Item("Part Number").Value oStockNumber = Left(oPartNumber, Len(oPartNumber) - 2) oPropSets.Item("Design Tracking Properties").Item("Stock Number").Value = oStockNumber Next
it is true that i have a subassembly in your assembly
it works with subassemblies
how does it work with subassemblies in assemblies
The example I posted will work with subassemblies, but in order to set all subassemblies and parts you would want to traverse the entire assembly tree, as shown in this example.
I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com
Sub Main If Not TypeOf ThisApplication.ActiveDocument Is AssemblyDocument Then Exit Sub Call TraverseAssembly(ThisApplication.ActiveDocument.ComponentDefinition.Occurrences) End Sub Sub TraverseAssembly(oOccs As ComponentOccurrences) For Each oOcc As ComponentOccurrence In oOccs Dim oDoc As Document = oOcc.Definition.Document Dim oPropSets As PropertySets = oDoc.PropertySets Dim oPartNumber As String = oPropSets.Item("Design Tracking Properties").Item("Part Number").Value Dim oStockNumber As String = Left(oPartNumber, Len(oPartNumber) -2) oPropSets.Item("Design Tracking Properties").Item("Stock Number").Value = oStockNumber If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then Call TraverseAssembly(oOcc.SubOccurrences) End If Next End Sub
I can ask you the same question
I want to remove the first 3 letters of the last 5 letters of many Parts in Part Number in Assembly
And get that value attached to Stock Number
Ex: Solid67812
after running the code becomes: "Solid12"
how to achieve that?
To get the string, use something like this.
oPartNumber = " Solid67812" sLast5 = Right(oPartNumber, 5) sPrefix = oPartNumber.Replace(sLast5, "") 'replace the last 5 with nothing sLast2 = Right(oPartNumber, 2) Dim oStockNumber As String = sPrefix & sLast2 MsgBox(oStockNumber,"iLogic")
if you're asking about how to set the stock number in that way, for only part files, then something like this would work:
Sub Main If Not TypeOf ThisApplication.ActiveDocument Is AssemblyDocument Then Exit Sub Call TraverseAssembly(ThisApplication.ActiveDocument.ComponentDefinition.Occurrences) End Sub Sub TraverseAssembly(oOccs As ComponentOccurrences) For Each oOcc As ComponentOccurrence In oOccs Dim oDoc As Document = oOcc.Definition.Document Dim oPropSets As PropertySets = oDoc.PropertySets pStockNumber = oPropSets.Item("Design Tracking Properties").Item("Stock Number") Dim oPartNumber As String = oPropSets.Item("Design Tracking Properties").Item("Part Number").Value If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then pStockNumber.Value = Left(oPartNumber, Len(oPartNumber) -2) Call TraverseAssembly(oOcc.SubOccurrences) Else sLast5 = Right(oPartNumber, 5) sPrefix = oPartNumber.Replace(sLast5, "") 'replace the last 5 with nothing sLast2 = Right(oPartNumber, 2) pStockNumber.Value = sPrefix & sLast2 End If Next End Sub