Announcements

The Autodesk Community Forums has a new look. Read more about what's changed on the Community Announcements board.

How to get access on assembly level to part.sheetmetal

Damian_Apex
Enthusiast

How to get access on assembly level to part.sheetmetal

Damian_Apex
Enthusiast
Enthusiast

Hello,

I am looking for code that will run in assembly and it will loop through all parts and for bent plates will read SheetMetal.FlatExtentsLength and SheetMetal.FlatExtentsWidth. 

I have a problem to get from assembly level to Part sheetmetal.

 

Sub main
Dim oAssDoc As AssemblyDocument = ThisDoc.Document
Dim oDoc As Document
Dim BoxLength, BoxWidth As Double	

For Each oDoc In oAssDoc.AllReferencedDocuments
	If oDoc.DocumentType = kPartDocumentObject And oDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then 'sheet metal part'

		Dim dir1 As Double = SheetMetal.FlatExtentsLength 'oDoc.SheetMetal.FlatExtentsLength ?
		Dim dir2 As Double = SheetMetal.FlatExtentsWidth 'oDoc.SheetMetal.FlatExtentsWidth ? 	
		
		Dim lengths As New List(Of Double) From {dir1, dir2}
		lengths.Sort

		BoxWidth = lengths(0)
		BoxLength = lengths(1)
		
		Trace.Write("oDoc: " & oDoc.DisplayName)	
		Trace.Write("BoxWidth: " & BoxWidth)
		Trace.Write("BoxLength: " & BoxLength)
	End If 
Next
End Sub

 

 

0 Likes
Reply
Accepted solutions (1)
494 Views
4 Replies
Replies (4)

Damian_Apex
Enthusiast
Enthusiast

I discovered this line of code, this will do the job ? 

Sub main
Dim oAssDoc As AssemblyDocument = ThisDoc.Document
Dim oDoc As Document
Dim BoxLength, BoxWidth As Double	

For Each oDoc In oAssDoc.AllReferencedDocuments
	If oDoc.DocumentType = kPartDocumentObject And oDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then 'sheet metal part'

                Dim oSMDef As SheetMetalComponentDefinition 
oSMDef = PartDoc.ComponentDefinition
Dim dir1 As Double = oSMDef.FlatPattern.Length
Dim dir2 As Double = oSMDef.FlatPattern.Width 
Dim lengths As New List(Of Double) From {dir1, dir2} lengths.Sort BoxWidth = lengths(0) BoxLength = lengths(1) Trace.Write("oDoc: " & oDoc.DisplayName) Trace.Write("BoxWidth: " & BoxWidth) Trace.Write("BoxLength: " & BoxLength) End If Next End Sub

 

0 Likes

WCrihfield
Mentor
Mentor
Accepted solution

I believe that those two iLogic snippets for getting flat pattern size target the 'active' document, which in this case would be the main assembly, and they don't appear to have an option that would allow you to specify a different component name or document name for them to target.  So, the next path to get those numbers is by stepping down through the normal Inventor API objects until we get to them.  They are two of the properties of the actual FlatPattern object, which is under the special SheetMetalComponentDefinition object, and acts like its own special type of ComponentDefinition.

Here is an alternate iLogic rule you can use.  You may, or may not have to convert the units of the resulting values returned, depending on your needs.

 

Sub main
	Dim oAssDoc As AssemblyDocument = ThisDoc.Document
	Dim oDoc As Document
	Dim BoxLength, BoxWidth As Double	

	For Each oDoc In oAssDoc.AllReferencedDocuments
		If oDoc.DocumentType = kPartDocumentObject And oDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then 'sheet metal part'
			Dim oPDoc As PartDocument = oDoc
			Dim oSMDef As SheetMetalComponentDefinition = oPDoc.ComponentDefinition
			Dim oFP As FlatPattern
			If Not oSMDef.HasFlatPattern Then
				Try
					'tries to generate a FlatPattern
					oSMDef.Unfold
				Catch
				End Try
			End If
			If Not oSMDef.HasFlatPattern Then Continue For 'skip to next oDoc
			oFP = oSMDef.FlatPattern
			Dim dir1 As Double = oFP.Length
			Dim dir2 As Double = oFP.Width 	
			
			Dim lengths As New List(Of Double) From {dir1, dir2}
			lengths.Sort

			BoxWidth = lengths(0)
			BoxLength = lengths(1)
			
			Trace.Write("oDoc: " & oDoc.DisplayName)	
			Trace.Write("BoxWidth: " & BoxWidth)
			Trace.Write("BoxLength: " & BoxLength)
		End If 
	Next
End Sub

 

Oops...I didn't see that earlier response when posting this, that contains similar code.  Oh well.  My version contains a few extra checks in there to help avoid potential errors that you might like.

 

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 💡 or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

WCrihfield
Mentor
Mentor

Actually, now that I'm looking at the code in your second post here, I think I see a problem.

This line:

oSMDef = PartDoc.ComponentDefinition

most likely won't work, because that variable you are using there "PartDoc" has not been created/defined yet.

If might work like this though:

oSMDef = oDoc.ComponentDefinition

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

Damian_Apex
Enthusiast
Enthusiast

Thank you for your attention. This is part of bigger code and I am transferring it to sub procedure and rename it.

0 Likes