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:Ā 

Delete the last 2 letters in Part Number

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
haphanthanhtam.work
386 Views, 9 Replies

Delete the last 2 letters in Part Number

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

 

Tags (1)
Labels (1)
9 REPLIES 9
Message 2 of 10

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"

@LinkedIn     @JohnCockerill

Message 3 of 10

it doesn't work for me
You can fix it.
screenshot_1687793224.png

Message 4 of 10

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

 

Message 5 of 10

it is true that i have a subassembly in your assembly
it works with subassemblies
how does it work with subassemblies in assemblies

Message 6 of 10

Hi @haphanthanhtam.work 

 

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 

 

Message 7 of 10

Thank you so much for the code !!

Message 8 of 10

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?

Message 9 of 10

Hi @haphanthanhtam.work 

 

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

 

Message 10 of 10

Thank you!!

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

Post to forums  

Autodesk Design & Make Report