Bending Number of sheet metal part.

Bending Number of sheet metal part.

tamnn.designer
Contributor Contributor
961 Views
5 Replies
Message 1 of 6

Bending Number of sheet metal part.

tamnn.designer
Contributor
Contributor

Hi every one.

I would very appreciate if some one can help me small things.

 

I would like to get quantity of bend number in the sheet metal part. But that need to be automatically update from the total 3D assembly file.  I have many rule, after i finish 3D drawing i just need to click Regenerate all rule => The bending quantity of all part will come inside each part.

 

i have code work with only single part. If i Regenerate from asssembly, that will not work. MANY THANK

 

'If ThisApplication.ActiveDocumentType() <> kAssemblyDocumentObject
        'MsgBox("You would have better luck if you ran this from an assembly document.")
		'Dim oAsmCompDef As AssemblyComponentDefinition
		'oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
		

'If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kPartDocumentObject Then
	'MsgBox("A Part Document must be active for this rule to work. Exiting.",vbCritical, "WRONG DOCUMENT TYPE")
	'Exit Sub
'End If
Dim oPDoc As PartDocument = ThisApplication.ActiveDocument
'If Not TypeOf oPDoc.ComponentDefinition Is SheetMetalComponentDefinition Then
	'MsgBox("This Part is not a Sheet Metal Part.  Exiting rule.",,"")
	'Exit Sub
'End If
Dim oSMDef As SheetMetalComponentDefinition = oPDoc.ComponentDefinition
Dim oBends As Integer = oSMDef.Bends.Count
'MsgBox("This Sheet Metal Part has:  " & oBends & " 'Bends'.", vbInformation, "bend_number")
Dim oUParams As UserParameters = oSMDef.Parameters.UserParameters
Dim oUParam As UserParameter
'Try
	oUParam = oUParams.Item("bend_number")
	oUParam.Value = oBends
iLogicVb.UpdateWhenDone = True
InventorVb.DocumentUpdate()
MultiValue.UpdateAfterChange = True
Parameter.UpdateAfterChange = True
'Catch
	'oUParam = oUParams.AddByValue("bend_number", oBends, UnitsTypeEnum.kUnitlessUnits)

'End Try

 

0 Likes
Accepted solutions (1)
962 Views
5 Replies
Replies (5)
Message 2 of 6

Andrii_Humeniuk
Advisor
Advisor

Hi @tamnn.designer . This code can be used both in the assembly and in the detail.

 

 

Private Sub Main()
	Dim oDoc As Document = ThisApplication.ActiveDocument
	Dim oTM As Transaction = ThisApplication.TransactionManager.StartTransaction(oDoc, "CountBendInPart")
	If TypeOf oDoc Is AssemblyDocument Then
		Dim oAsmDoc As AssemblyDocument = oDoc
		Call GetAllRefDoc(oAsmDoc)
	Else If TypeOf oDoc Is PartDocument Then
		Dim oPartDoc As PartDocument = oDoc
		Call CountBendInPart(oPartDoc)
	End If
	oTM.End()
End Sub

Private Sub GetAllRefDoc(ByVal oAsmDoc As AssemblyDocument)
	Dim oOccs As ComponentOccurrences = oAsmDoc.ComponentDefinition.Occurrences
	For Each oRefDoc As Document In oAsmDoc.AllReferencedDocuments
		If oOccs.AllReferencedOccurrences(oRefDoc).Count > 0 Then
			Dim oOcc As ComponentOccurrence = oOccs.AllReferencedOccurrences(oRefDoc)
			If oOcc.Suppressed Then Continue For
			If oOcc.DefinitionDocumentType <> DocumentTypeEnum.kPartDocumentObject Then Continue For
			Call CountBendInPart(oOcc.Definition.Document)
		End If
	Next
End Sub

Private Function CountBendInPart(oDoc As PartDocument)
	If oDoc.ComponentDefinition.Type = ObjectTypeEnum.kSheetMetalComponentDefinitionObject Then
		Dim oSMDef As SheetMetalComponentDefinition = oDoc.ComponentDefinition
		Dim oUParams As UserParameters = oSMDef.Parameters.UserParameters
		Dim oUParam As UserParameter
		Try
			oUParam = oUParams("bend_number")
			oUParam.Value = oSMDef.Bends.Count
		Catch
			oUParam = oUParams.AddByValue("bend_number", oSMDef.Bends.Count, UnitsTypeEnum.kUnitlessUnits)
		End Try
		oDoc.Update()
	End If
End Function

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 3 of 6

tamnn.designer
Contributor
Contributor

Hi Sir. It worked well with single part but not work with assembly . The error is coming.

Error in rule: Rule0, in document: 224026a0_R00.iam

Unable to cast COM object of type 'System.__ComObject' to interface type 'Inventor.ComponentOccurrence'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{5DF86020-6B16-11D3-B794-0060B0F159EF}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

 

Can you help me the reason. I don't want to update for every single part. I would want that automatically regenerate from 3D assembly file. mean all children part created automatically. 

 

Thanks sir

0 Likes
Message 4 of 6

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

Sorry, but it looks like I didn't give you the latest version of the code. It had one small error in line 18 (Dim oOcc As ComponentOccurrence = oOccs.AllReferencedOccurrences(oRefDoc)(1))

 

 

Private Sub Main()
	Dim oDoc As Document = ThisApplication.ActiveDocument
	Dim oTM As Transaction = ThisApplication.TransactionManager.StartTransaction(oDoc, "CountBendInPart")
	If TypeOf oDoc Is AssemblyDocument Then
		Dim oAsmDoc As AssemblyDocument = oDoc
		Call GetAllRefDoc(oAsmDoc)
	Else If TypeOf oDoc Is PartDocument Then
		Dim oPartDoc As PartDocument = oDoc
		Call CountBendInPart(oPartDoc)
	End If
	oTM.End()
End Sub

Private Sub GetAllRefDoc(ByVal oAsmDoc As AssemblyDocument)
	Dim oOccs As ComponentOccurrences = oAsmDoc.ComponentDefinition.Occurrences
	For Each oRefDoc As Document In oAsmDoc.AllReferencedDocuments
		If oOccs.AllReferencedOccurrences(oRefDoc).Count > 0 Then
			Dim oOcc As ComponentOccurrence = oOccs.AllReferencedOccurrences(oRefDoc)(1)
			If oOcc.Suppressed Then Continue For
			If oOcc.DefinitionDocumentType <> DocumentTypeEnum.kPartDocumentObject Then Continue For
			Call CountBendInPart(oOcc.Definition.Document)
		End If
	Next
End Sub

Private Function CountBendInPart(oDoc As PartDocument)
	If Not oDoc.IsModifiable Then Exit Function
	If oDoc.ComponentDefinition.Type = ObjectTypeEnum.kSheetMetalComponentDefinitionObject Then
		Dim oSMDef As SheetMetalComponentDefinition = oDoc.ComponentDefinition
		Dim oUParams As UserParameters = oSMDef.Parameters.UserParameters
		Dim oUParam As UserParameter
		Try
			oUParam = oUParams("bend_number")
			oUParam.Value = oSMDef.Bends.Count
		Catch
			oUParam = oUParams.AddByValue("bend_number", oSMDef.Bends.Count, UnitsTypeEnum.kUnitlessUnits)
		End Try
		oDoc.Update()
	End If
End Function

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 5 of 6

tamnn.designer
Contributor
Contributor
It worked perfectly, thank you very much., Sir
0 Likes
Message 6 of 6

Ahmed.shawkyXTZHN
Enthusiast
Enthusiast

Hi @tamnn.designer  ,

I have question for above , how you will get the number of bends like the output will shown in table or in custom property?

0 Likes