Get, validate and set a new sheet metal style from the assembly in all the SheetMetal components

Get, validate and set a new sheet metal style from the assembly in all the SheetMetal components

antonio.silvaluna
Explorer Explorer
1,478 Views
10 Replies
Message 1 of 11

Get, validate and set a new sheet metal style from the assembly in all the SheetMetal components

antonio.silvaluna
Explorer
Explorer

Hello everyone,

has anyone changed sheet metal styles from the assembly?

I'm trying to run the ChangeStyleToNewOne rule on each of the sheetmetal parts in an assembly, but it's not working.

Does anyone have any ideas how I can fix it?

Sub Main()

Dim thisAssyDoc As AssemblyDocument = CType(ThisDoc.Document, AssemblyDocument)

AddCusProp(thisAssyDoc)
 
End Sub

Sub AddCusProp(assemblyDoc As AssemblyDocument)
For Each subCompOcc As ComponentOccurrence In assemblyDoc.ComponentDefinition.Occurrences Dim oPartDoc As PartDocument= subCompOcc.ActiveEditDocument If oPartDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Dim oscCompDef As SheetMetalComponentDefinition = oPartDoc.ComponentDefinition Dim ActiveStyle As SheetMetalStyle = oscCompDef.ActiveSheetMetalStyle ChangeStyleToNewOne(ActiveStyle) ElseIf oPartDoc.SubType = DocumentTypeEnum.kAssemblyDocumentObject Then AddCusProp(oPartDoc.Definition.Document) End If Next End Sub Sub ChangeStyleToNewOne(ActiveStyle) If ActiveStyle = "10ga GA Brt" Then SheetMetal.SetActiveStyle("10ga GA Brt (Test)") ElseIf ActiveStyle = "10ga GA Jkt" Then SheetMetal.SetActiveStyle("10ga GA Jkt (Test)") ElseIf ActiveStyle = "10ga SS 304L Solid" Then SheetMetal.SetActiveStyle("10ga SS 304L Solid (Test)") ElseIf ActiveStyle = "12ga GA Brt" Then SheetMetal.SetActiveStyle("12ga GA Brt (Test)") ElseIf ActiveStyle = "12ga GA Jkt" Then SheetMetal.SetActiveStyle("12ga GA Jkt (Test)") ElseIf ActiveStyle = "14ga GA Brt" Then SheetMetal.SetActiveStyle("14ga GA Brt (Test)") ElseIf ActiveStyle = "14ga GA Jkt" Then SheetMetal.SetActiveStyle("14ga GA Jkt (Test)") ElseIf ActiveStyle = "14ga SS 304L Solid" Then SheetMetal.SetActiveStyle("14ga SS 304L Solid (Test)") ElseIf ActiveStyle = "16ga GA Brt" Then SheetMetal.SetActiveStyle("16ga GA Brt (Test)") ElseIf ActiveStyle = "16ga GA Jkt" Then SheetMetal.SetActiveStyle("16ga GA Jkt (Test)") ElseIf ActiveStyle = "16ga SS 304L Solid" Then SheetMetal.SetActiveStyle("16ga SS 304L Solid (Test)") ElseIf ActiveStyle = "18ga GA Brt" Then SheetMetal.SetActiveStyle("18ga GA Brt (Test)") ElseIf ActiveStyle = "18ga GA Jkt" Then SheetMetal.SetActiveStyle("18ga GA Jkt (Test)") ElseIf ActiveStyle = "18ga SS 304L Solid" Then SheetMetal.SetActiveStyle("18ga SS 304L Solid (Test)") ElseIf ActiveStyle = "20ga GA Brt" Then SheetMetal.SetActiveStyle("20ga GA Brt (Test)") ElseIf ActiveStyle = "20ga GA Jkt" Then SheetMetal.SetActiveStyle("20ga GA Jkt (Test)") Else SheetMetal.SetActiveStyle("Default") iProperties.Value("Project", "Stock Number") = "Material no disponible para tolerancias" iProperties.Value("Project", "Description") = "Material no disponible para tolerancias" End If End Sub
0 Likes
Accepted solutions (1)
1,479 Views
10 Replies
Replies (10)
Message 2 of 11

dalton98
Collaborator
Collaborator

This worked

Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument

Dim oCompDef As SheetMetalComponentDefinition
oCompDef = oDoc.ComponentDefinition

oCompDef.SheetMetalStyles.Item("18GA GA Jkt (Test)").Activate
Message 3 of 11

WCrihfield
Mentor
Mentor

What version of Inventor are you using?  Try this version of your code.  I changed a few thing around, and I am using a fairly new tool to direct all those vague iLogic snippets to the proper document reference.  If you have any questions about any of this, feel free to ask them.

Sub Main()
	Dim thisAssyDoc As AssemblyDocument = CType(ThisDoc.Document, AssemblyDocument)
	AddCusProp(thisAssyDoc)
End Sub

Sub AddCusProp(assemblyDoc As AssemblyDocument)
	For Each subCompOcc As ComponentOccurrence In assemblyDoc.ComponentDefinition.Occurrences
		If subCompOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			AddCusProp(subCompOcc.Definition.Document)
		End If
		If Not TypeOf subCompOcc.Definition Is SheetMetalComponentDefinition Then Continue For
		Dim oscCompDef As SheetMetalComponentDefinition = subCompOcc.Definition
		Dim ActiveStyle As SheetMetalStyle = oscCompDef.ActiveSheetMetalStyle
		ChangeStyleToNewOne(oscCompDef.Document, ActiveStyle)
	Next
End Sub

Sub ChangeStyleToNewOne(oSMDoc As PartDocument, ActiveStyle As SheetMetalStyle)
	Dim SOP = StandardObjectFactory.Create(oSMDoc)
	If ActiveStyle.Name = "10ga GA Brt" Then
		SOP.SheetMetal.SetActiveStyle("10ga GA Brt (Test)")
	ElseIf ActiveStyle.Name = "10ga GA Jkt" Then
		SOP.SheetMetal.SetActiveStyle("10ga GA Jkt (Test)")
	ElseIf ActiveStyle.Name = "10ga SS 304L Solid" Then
		SOP.SheetMetal.SetActiveStyle("10ga SS 304L Solid (Test)")
	ElseIf ActiveStyle.Name = "12ga GA Brt" Then
		SOP.SheetMetal.SetActiveStyle("12ga GA Brt (Test)")
	ElseIf ActiveStyle.Name = "12ga GA Jkt" Then
		SOP.SheetMetal.SetActiveStyle("12ga GA Jkt (Test)")
	ElseIf ActiveStyle.Name = "14ga GA Brt" Then
		SOP.SheetMetal.SetActiveStyle("14ga GA Brt (Test)")
	ElseIf ActiveStyle.Name = "14ga GA Jkt" Then
		SOP.SheetMetal.SetActiveStyle("14ga GA Jkt (Test)")
	ElseIf ActiveStyle.Name = "14ga SS 304L Solid" Then
		SOP.SheetMetal.SetActiveStyle("14ga SS 304L Solid (Test)")
	ElseIf ActiveStyle.Name = "16ga GA Brt" Then
		SOP.SheetMetal.SetActiveStyle("16ga GA Brt (Test)")
	ElseIf ActiveStyle.Name = "16ga GA Jkt" Then
		SOP.SheetMetal.SetActiveStyle("16ga GA Jkt (Test)")
	ElseIf ActiveStyle.Name = "16ga SS 304L Solid" Then
		SOP.SheetMetal.SetActiveStyle("16ga SS 304L Solid (Test)")
	ElseIf ActiveStyle.Name = "18ga GA Brt" Then
		SOP.SheetMetal.SetActiveStyle("18ga GA Brt (Test)")
	ElseIf ActiveStyle.Name = "18ga GA Jkt" Then
		SOP.SheetMetal.SetActiveStyle("18ga GA Jkt (Test)")
	ElseIf ActiveStyle.Name = "18ga SS 304L Solid" Then
		SOP.SheetMetal.SetActiveStyle("18ga SS 304L Solid (Test)")	
	ElseIf ActiveStyle.Name = "20ga GA Brt" Then
		SOP.SheetMetal.SetActiveStyle("20ga GA Brt (Test)")
	ElseIf ActiveStyle.Name = "20ga GA Jkt" Then
		SOP.SheetMetal.SetActiveStyle("20ga GA Jkt (Test)")
	Else
		SOP.SheetMetal.SetActiveStyle("Default")
		SOP.iProperties.Value("Project", "Stock Number") = "Material no disponible para tolerancias"
		SOP.iProperties.Value("Project", "Description") = "Material no disponible para tolerancias"	
	End If	
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS :bulb: or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 11

antonio.silvaluna
Explorer
Explorer
I have inventor 2020
This code show this error
Error on Line 19 : 'StandardObjectFactory' is not declared. It may be inaccessible due to its protection level.
0 Likes
Message 5 of 11

antonio.silvaluna
Explorer
Explorer
Let me try thanks
0 Likes
Message 6 of 11

WCrihfield
Mentor
Mentor

OK.  I think that StandardObjectFactory.Create() tool just became available in Inventor 2022, so that will not be an option to use in this situation.  The idea of using it though, was because that "SheetMetal" iLogic snippet does not have a way for you to specify which document you want it to act upon (target), and this would have been one way of making it target a specific document.    The "iProperties.Value()" iLogic snippet does allow you to specify either a component name or document name, as the first (of 3) input variables, but that can be confusing to use too, when you are wanting it to target another document.  In situations like this, I usually prefer to eliminate the use of that iProperties.Value snippet, in favor of the regular Inventor API route:

Document.PropertySets.Item("PropertySetName").Item("PropertyName").Value

That way you can start from an actual Document object, so you know for sure which document you are dealing with.  The SheetMetal snippet can also be avoided, in favor of similar (but longer) API route if needed, similar to the code that @dalton98 posted.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 11

antonio.silvaluna
Explorer
Explorer
I'm trying to use this code but I don't know how I can add the For Each loop, Could you help with more information about this
0 Likes
Message 8 of 11

antonio.silvaluna
Explorer
Explorer
I try this
Sub Main()
Dim thisAssyDoc As AssemblyDocument = CType(ThisDoc.Document, AssemblyDocument)
ChangeToTest(thisAssyDoc)
End Sub

Sub ChangeToTest(assemblyDoc As AssemblyDocument)

For Each subCompOcc As ComponentOccurrence In assemblyDoc.ComponentDefinition.Occurrences

Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument
Dim oCompDef As SheetMetalComponentDefinition

If oCompDef is oDoc.ComponentDefinition Then

oCompDef.SheetMetalStyles.Item("18ga GA JKT (Test)").Activate
Else
MessageBox.Show("Fail", "Title")

End If

Next

End Sub

0 Likes
Message 9 of 11

A.Acheson
Mentor
Mentor

Here is a working code to change the style of level one occurrences in an assembly. If the sheetmetal parts are in a subassembly you will need additional code to detect an assembly and iterate through those as well. 

 

Sub Main()
Dim thisAssyDoc As AssemblyDocument = CType(ThisDoc.Document, AssemblyDocument)
ChangeToTest(thisAssyDoc)
End Sub

Sub ChangeToTest(assemblyDoc As AssemblyDocument)

For Each subCompOcc As ComponentOccurrence In assemblyDoc.ComponentDefinition.Occurrences
	Dim oCompDef As ComponentDefinition = subCompOcc.Definition
	If TypeOf oCompDef Is SheetMetalComponentDefinition Then
		oCompDef = subCompOcc.Definition
		Try
		oCompDef.SheetMetalStyles.Item("18ga GA JKT (Test)").Activate
		Catch
			MessageBox.Show("Fail to find Style", "Title")
		End Try
	Else
		MessageBox.Show("Fail", "Title")
	End If
Next

End Sub

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 10 of 11

WCrihfield
Mentor
Mentor
Accepted solution

Hi @antonio.silvaluna.  Here is another version you can try that is similar to the last one I posted.  But this version does not use the StandardObjectFactory route.  Instead it uses the API route.  I also combined the two Sub routines into one.  That Sub routine might even be simplified even further since the 'test' version of SheetMetalStyle is always named exactly the same as the original, but with the " (Test)" at the end if its name.  It might just be able to capture its current SheetMetalStyle name, check if it 'EndsWith' " (Test)", and if not, try to change it to the same name with that at the end of it.  But I don't know if that would fit your more practical needs later.  But since the other 'Test' version SheetMetalStyle might not exist, in order to avoid that potential error without making the code exponentially longer, I incorporated a Try...Catch statement, and put the main task inside of that.  And since I don't know how many components you may be cycling through, in the Catch side I left the MsgBox commented out, and left the Logger line un-commented, to help it run more smoothly.  You would then want to check your iLogicLog tab to see if it encountered any problems afterwards.

Sub Main()
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	ChangeToTest(oADoc)
End Sub

Sub ChangeToTest(oADoc As AssemblyDocument)
	For Each oOcc As ComponentOccurrence In oADoc.ComponentDefinition.Occurrences
		If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			ChangeToTest(oOcc.Definition.Document)
		End If
		If Not TypeOf oOcc.Definition Is SheetMetalComponentDefinition Then Continue For
		Dim oSMDef As SheetMetalComponentDefinition = oOcc.Definition
		Dim ActiveStyle As SheetMetalStyle = oSMDef.ActiveSheetMetalStyle
		'since the other named SheetMetalStyle may not exist, we will use a Try...Catch statement
		Try
			If ActiveStyle.Name = "10ga GA Brt" Then
				oSMDef.SheetMetalStyles.Item("10ga GA Brt (Test)").Activate
			ElseIf ActiveStyle.Name = "10ga GA Jkt" Then
				oSMDef.SheetMetalStyles.Item("10ga GA Jkt (Test)").Activate
			ElseIf ActiveStyle.Name = "10ga SS 304L Solid" Then
				oSMDef.SheetMetalStyles.Item("10ga SS 304L Solid (Test)").Activate
			ElseIf ActiveStyle.Name = "12ga GA Brt" Then
				oSMDef.SheetMetalStyles.Item("12ga GA Brt (Test)").Activate
			ElseIf ActiveStyle.Name = "12ga GA Jkt" Then
				oSMDef.SheetMetalStyles.Item("12ga GA Jkt (Test)").Activate
			ElseIf ActiveStyle.Name = "14ga GA Brt" Then
				oSMDef.SheetMetalStyles.Item("14ga GA Brt (Test)").Activate
			ElseIf ActiveStyle.Name = "14ga GA Jkt" Then
				oSMDef.SheetMetalStyles.Item("14ga GA Jkt (Test)").Activate
			ElseIf ActiveStyle.Name = "14ga SS 304L Solid" Then
				oSMDef.SheetMetalStyles.Item("14ga SS 304L Solid (Test)").Activate
			ElseIf ActiveStyle.Name = "16ga GA Brt" Then
				oSMDef.SheetMetalStyles.Item("16ga GA Brt (Test)").Activate
			ElseIf ActiveStyle.Name = "16ga GA Jkt" Then
				oSMDef.SheetMetalStyles.Item("16ga GA Jkt (Test)").Activate
			ElseIf ActiveStyle.Name = "16ga SS 304L Solid" Then
				oSMDef.SheetMetalStyles.Item("16ga SS 304L Solid (Test)").Activate
			ElseIf ActiveStyle.Name = "18ga GA Brt" Then
				oSMDef.SheetMetalStyles.Item("18ga GA Brt (Test)").Activate
			ElseIf ActiveStyle.Name = "18ga GA Jkt" Then
				oSMDef.SheetMetalStyles.Item("18ga GA Jkt (Test)").Activate
			ElseIf ActiveStyle.Name = "18ga SS 304L Solid" Then
				oSMDef.SheetMetalStyles.Item("18ga SS 304L Solid (Test)").Activate	
			ElseIf ActiveStyle.Name = "20ga GA Brt" Then
				oSMDef.SheetMetalStyles.Item("20ga GA Brt (Test)").Activate
			ElseIf ActiveStyle.Name = "20ga GA Jkt" Then
				oSMDef.SheetMetalStyles.Item("20ga GA Jkt (Test)").Activate
			Else
				oSMDef.SheetMetalStyles.Item("Default").Activate
				Dim oSMDoc As PartDocument = oSMDef.Document
				oSMDoc.PropertySets.Item("Design Tracking Properties").Item("Stock Number").Value = "Material no disponible para tolerancias"
				oSMDoc.PropertySets.Item("Design Tracking Properties").Item("Description").Value = "Material no disponible para tolerancias"
			End If
		Catch oEx As Exception
			'MsgBox(oEx.Message & vbCrLf & oEx.StackTrace, vbExclamation, "")
			Logger.Error(oEx.Message & vbCrLf & oEx.StackTrace)
		End Try
	Next
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 11 of 11

antonio.silvaluna
Explorer
Explorer
Thanks everything it's working well
0 Likes